Skip to content

Commit

Permalink
fix: Moving the get cloud formation export to after the stack was upd…
Browse files Browse the repository at this point in the history
…ated.
  • Loading branch information
chrsdietz committed Jan 10, 2019
1 parent 05be98f commit 375f9b8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 72 deletions.
36 changes: 17 additions & 19 deletions src/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ class Plugin implements ServerlessPlugin {
private serverless: Serverless<Custom>;
private cli: CLI;
private config: Config;

endpoint: string;

hooks: Hooks;

constructor(serverless: Serverless<Custom>, context: any) {
Expand All @@ -36,36 +33,37 @@ class Plugin implements ServerlessPlugin {
private async create() {
const custom = this.serverless.service.custom || {};
this.config = custom.elasticsearch || {};
this.endpoint = this.config.endpoint;

if (!this.config.endpoint && !this.config["cf-endpoint"]) {
throw new Error("Elasticsearch endpoint not specified.");
}
}

/**
* Sends the mapping information to elasticsearch.
*/
private async setupElasticCache() {
let endpoint = this.config.endpoint;
if (this.config["cf-endpoint"]) {
const cloudFormation = new CloudFormation({
region: ServerlessUtils.getRegion(this.serverless),
credentials: new SharedIniFileCredentials({
profile: ServerlessUtils.getProfile(this.serverless)
})
});
this.endpoint = await AwsUtils.findCloudformationExport(cloudFormation, this.config["cf-endpoint"]);
if (!this.endpoint) {
endpoint = await AwsUtils.findCloudformationExport(cloudFormation, this.config["cf-endpoint"]);
if (!endpoint) {
throw new Error("Endpoint not found at cloudformation export.");
}
}
if (!this.endpoint) {
throw new Error("Elasticsearch endpoint not specified.");
} else {
if (!this.endpoint.startsWith("http")) {
this.endpoint = `https://${this.endpoint}`;
}
if (!endpoint.startsWith("http")) {
endpoint = `https://${endpoint}`;
}
}

/**
* Sends the mapping information to elasticsearch.
*/
private async setupElasticCache() {
this.cli.log("Setting up templates...");
await setupTemplates(this.endpoint, this.config.templates);
await setupTemplates(endpoint, this.config.templates);
this.cli.log("Setting up indices...");
await setupIndices(this.endpoint, this.config.indices);
await setupIndices(endpoint, this.config.indices);
this.cli.log("Elasticsearch setup complete.");
}
}
Expand Down
97 changes: 44 additions & 53 deletions test/Plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,102 +55,93 @@ describe("Plugin", () => {
"Elasticsearch endpoint not specified."
);
});
});

it("Tests that an error is through if there is a cf-endpoint and it does not have an endpoint", async () => {
const serverless = {
describe("Setup indices", () => {
function createServerless(indices: Index[]): Serverless<Custom> {
return {
...fakeServerless,
service: {
custom: {
elasticsearch: {
"cf-endpoint": "TestCfEndpoint"
...endpointConfig,
indices
}
}
}
};
}

findCloudformationExportStub.returns(Promise.resolve(undefined));
const plugin: ServerlessPlugin = new Plugin(serverless, {});

await checkAndCatchError(
() => plugin.hooks["before:aws:deploy:deploy:updateStack"](),
"Endpoint not found at cloudformation export."
);
});

it("Tests that https is pre-pended to the url if it does not exist.", async () => {
it("Tests that an error is through if there is a cf-endpoint and it does not have an endpoint", async () => {
const serverless = {
...fakeServerless,
service: {
custom: {
elasticsearch: {
endpoint: "TestCfEndpoint"
"cf-endpoint": "TestCfEndpoint"
}
}
}
};

findCloudformationExportStub.returns(Promise.resolve(undefined));
const plugin: ServerlessPlugin = new Plugin(serverless, {});

plugin.hooks["before:aws:deploy:deploy:updateStack"]();
await checkAndCatchError(() => plugin.hooks["after:aws:deploy:deploy:updateStack"](), "Endpoint not found at cloudformation export.");
});

it("Tests that https is pre-pended to the url if it does not exist.", async () => {
const serverless = createServerless([
{
name: "Index1",
file: "./test/testFiles/TestIndices1.json"
}
]);
serverless.service.custom.elasticsearch.endpoint = "TestCfEndpoint";

const plugin: Plugin = new Plugin(serverless, {});

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

expect(plugin.endpoint).to.equal("https://TestCfEndpoint");
expect(putStub).to.have.been.calledWith("https://TestCfEndpoint/Index1");
});

it("Tests that https is pre-pended to the url if it does not exist from a cloudformation domain.", async () => {
const serverless = {
...fakeServerless,
service: {
custom: {
elasticsearch: {
"cf-endpoint": "TestCfEndpoint"
}
}
const serverless = createServerless([
{
name: "Index1",
file: "./test/testFiles/TestIndices1.json"
}
};
]);
serverless.service.custom.elasticsearch.endpoint = undefined;
serverless.service.custom.elasticsearch["cf-endpoint"] = "ABCD123";

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

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

expect(plugin.endpoint).to.equal("https://ABCD123");
expect(putStub).to.have.been.calledWith("https://TestCfEndpoint/Index1");
});

it("Tests that the url is not touched if it already has https.", async () => {
const serverless = {
...fakeServerless,
service: {
custom: {
elasticsearch: {
endpoint: "https://TestCfEndpoint"
}
}
const serverless = createServerless([
{
name: "Index1",
file: "./test/testFiles/TestIndices1.json"
}
};

findCloudformationExportStub.returns(Promise.resolve(undefined));
]);
serverless.service.custom.elasticsearch.endpoint = "https://TestCfEndpoint";
const plugin: Plugin = new Plugin(serverless, {});

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

expect(plugin.endpoint).to.equal("https://TestCfEndpoint");
expect(putStub).to.have.been.calledWith("https://TestCfEndpoint/Index1");
});
});

describe("Setup indices", () => {
function createServerless(indices: Index[]): Serverless<Custom> {
return {
...fakeServerless,
service: {
custom: {
elasticsearch: {
...endpointConfig,
indices
}
}
}
};
}

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

0 comments on commit 375f9b8

Please sign in to comment.