Skip to content

Commit 375f9b8

Browse files
committed
fix: Moving the get cloud formation export to after the stack was updated.
1 parent 05be98f commit 375f9b8

File tree

2 files changed

+61
-72
lines changed

2 files changed

+61
-72
lines changed

src/Plugin.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ class Plugin implements ServerlessPlugin {
1515
private serverless: Serverless<Custom>;
1616
private cli: CLI;
1717
private config: Config;
18-
19-
endpoint: string;
20-
2118
hooks: Hooks;
2219

2320
constructor(serverless: Serverless<Custom>, context: any) {
@@ -36,36 +33,37 @@ class Plugin implements ServerlessPlugin {
3633
private async create() {
3734
const custom = this.serverless.service.custom || {};
3835
this.config = custom.elasticsearch || {};
39-
this.endpoint = this.config.endpoint;
36+
37+
if (!this.config.endpoint && !this.config["cf-endpoint"]) {
38+
throw new Error("Elasticsearch endpoint not specified.");
39+
}
40+
}
41+
42+
/**
43+
* Sends the mapping information to elasticsearch.
44+
*/
45+
private async setupElasticCache() {
46+
let endpoint = this.config.endpoint;
4047
if (this.config["cf-endpoint"]) {
4148
const cloudFormation = new CloudFormation({
4249
region: ServerlessUtils.getRegion(this.serverless),
4350
credentials: new SharedIniFileCredentials({
4451
profile: ServerlessUtils.getProfile(this.serverless)
4552
})
4653
});
47-
this.endpoint = await AwsUtils.findCloudformationExport(cloudFormation, this.config["cf-endpoint"]);
48-
if (!this.endpoint) {
54+
endpoint = await AwsUtils.findCloudformationExport(cloudFormation, this.config["cf-endpoint"]);
55+
if (!endpoint) {
4956
throw new Error("Endpoint not found at cloudformation export.");
5057
}
5158
}
52-
if (!this.endpoint) {
53-
throw new Error("Elasticsearch endpoint not specified.");
54-
} else {
55-
if (!this.endpoint.startsWith("http")) {
56-
this.endpoint = `https://${this.endpoint}`;
57-
}
59+
if (!endpoint.startsWith("http")) {
60+
endpoint = `https://${endpoint}`;
5861
}
59-
}
6062

61-
/**
62-
* Sends the mapping information to elasticsearch.
63-
*/
64-
private async setupElasticCache() {
6563
this.cli.log("Setting up templates...");
66-
await setupTemplates(this.endpoint, this.config.templates);
64+
await setupTemplates(endpoint, this.config.templates);
6765
this.cli.log("Setting up indices...");
68-
await setupIndices(this.endpoint, this.config.indices);
66+
await setupIndices(endpoint, this.config.indices);
6967
this.cli.log("Elasticsearch setup complete.");
7068
}
7169
}

test/Plugin.test.ts

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -55,102 +55,93 @@ describe("Plugin", () => {
5555
"Elasticsearch endpoint not specified."
5656
);
5757
});
58+
});
5859

59-
it("Tests that an error is through if there is a cf-endpoint and it does not have an endpoint", async () => {
60-
const serverless = {
60+
describe("Setup indices", () => {
61+
function createServerless(indices: Index[]): Serverless<Custom> {
62+
return {
6163
...fakeServerless,
6264
service: {
6365
custom: {
6466
elasticsearch: {
65-
"cf-endpoint": "TestCfEndpoint"
67+
...endpointConfig,
68+
indices
6669
}
6770
}
6871
}
6972
};
73+
}
7074

71-
findCloudformationExportStub.returns(Promise.resolve(undefined));
72-
const plugin: ServerlessPlugin = new Plugin(serverless, {});
73-
74-
await checkAndCatchError(
75-
() => plugin.hooks["before:aws:deploy:deploy:updateStack"](),
76-
"Endpoint not found at cloudformation export."
77-
);
78-
});
79-
80-
it("Tests that https is pre-pended to the url if it does not exist.", async () => {
75+
it("Tests that an error is through if there is a cf-endpoint and it does not have an endpoint", async () => {
8176
const serverless = {
8277
...fakeServerless,
8378
service: {
8479
custom: {
8580
elasticsearch: {
86-
endpoint: "TestCfEndpoint"
81+
"cf-endpoint": "TestCfEndpoint"
8782
}
8883
}
8984
}
9085
};
9186

9287
findCloudformationExportStub.returns(Promise.resolve(undefined));
88+
const plugin: ServerlessPlugin = new Plugin(serverless, {});
89+
90+
plugin.hooks["before:aws:deploy:deploy:updateStack"]();
91+
await checkAndCatchError(() => plugin.hooks["after:aws:deploy:deploy:updateStack"](), "Endpoint not found at cloudformation export.");
92+
});
93+
94+
it("Tests that https is pre-pended to the url if it does not exist.", async () => {
95+
const serverless = createServerless([
96+
{
97+
name: "Index1",
98+
file: "./test/testFiles/TestIndices1.json"
99+
}
100+
]);
101+
serverless.service.custom.elasticsearch.endpoint = "TestCfEndpoint";
102+
93103
const plugin: Plugin = new Plugin(serverless, {});
94104

95105
await plugin.hooks["before:aws:deploy:deploy:updateStack"]();
106+
await plugin.hooks["after:aws:deploy:deploy:updateStack"]();
96107

97-
expect(plugin.endpoint).to.equal("https://TestCfEndpoint");
108+
expect(putStub).to.have.been.calledWith("https://TestCfEndpoint/Index1");
98109
});
99110

100111
it("Tests that https is pre-pended to the url if it does not exist from a cloudformation domain.", async () => {
101-
const serverless = {
102-
...fakeServerless,
103-
service: {
104-
custom: {
105-
elasticsearch: {
106-
"cf-endpoint": "TestCfEndpoint"
107-
}
108-
}
112+
const serverless = createServerless([
113+
{
114+
name: "Index1",
115+
file: "./test/testFiles/TestIndices1.json"
109116
}
110-
};
117+
]);
118+
serverless.service.custom.elasticsearch.endpoint = undefined;
119+
serverless.service.custom.elasticsearch["cf-endpoint"] = "ABCD123";
111120

121+
findCloudformationExportStub.returns(Promise.resolve("TestCfEndpoint"));
112122
const plugin: Plugin = new Plugin(serverless, {});
113123

114124
await plugin.hooks["before:aws:deploy:deploy:updateStack"]();
125+
await plugin.hooks["after:aws:deploy:deploy:updateStack"]();
115126

116-
expect(plugin.endpoint).to.equal("https://ABCD123");
127+
expect(putStub).to.have.been.calledWith("https://TestCfEndpoint/Index1");
117128
});
118129

119130
it("Tests that the url is not touched if it already has https.", async () => {
120-
const serverless = {
121-
...fakeServerless,
122-
service: {
123-
custom: {
124-
elasticsearch: {
125-
endpoint: "https://TestCfEndpoint"
126-
}
127-
}
131+
const serverless = createServerless([
132+
{
133+
name: "Index1",
134+
file: "./test/testFiles/TestIndices1.json"
128135
}
129-
};
130-
131-
findCloudformationExportStub.returns(Promise.resolve(undefined));
136+
]);
137+
serverless.service.custom.elasticsearch.endpoint = "https://TestCfEndpoint";
132138
const plugin: Plugin = new Plugin(serverless, {});
133139

134140
await plugin.hooks["before:aws:deploy:deploy:updateStack"]();
141+
await plugin.hooks["after:aws:deploy:deploy:updateStack"]();
135142

136-
expect(plugin.endpoint).to.equal("https://TestCfEndpoint");
143+
expect(putStub).to.have.been.calledWith("https://TestCfEndpoint/Index1");
137144
});
138-
});
139-
140-
describe("Setup indices", () => {
141-
function createServerless(indices: Index[]): Serverless<Custom> {
142-
return {
143-
...fakeServerless,
144-
service: {
145-
custom: {
146-
elasticsearch: {
147-
...endpointConfig,
148-
indices
149-
}
150-
}
151-
}
152-
};
153-
}
154145

155146
it("Tests that an error is thrown if a name is not provided for index.", async () => {
156147
const indices: Index[] = [

0 commit comments

Comments
 (0)