Skip to content

Commit

Permalink
Merge branch 'release/1.7.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Diana Ionita committed Jun 12, 2021
2 parents 25e3417 + 3e43d44 commit 76e19b6
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 32 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-api-gateway-caching",
"version": "1.7.2",
"version": "1.7.3",
"description": "A plugin for the serverless framework which helps with configuring caching for API Gateway endpoints.",
"main": "src/apiGatewayCachingPlugin.js",
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions src/apiGatewayCachingPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class ApiGatewayCachingPlugin {
this.settings = new ApiGatewayCachingSettings(this.serverless, this.options);
}

updateCloudFormationTemplate() {
this.thereIsARestApi = restApiExists(this.serverless, this.settings);
async updateCloudFormationTemplate() {
this.thereIsARestApi = await restApiExists(this.serverless, this.settings);
if (!this.thereIsARestApi) {
this.serverless.cli.log(`[serverless-api-gateway-caching] No REST API found. Caching settings will not be updated.`);
return;
Expand All @@ -40,8 +40,8 @@ class ApiGatewayCachingPlugin {
return pathParametersCache.addPathParametersCacheConfig(this.settings, this.serverless);
}

updateStage() {
this.thereIsARestApi = restApiExists(this.serverless, this.settings);
async updateStage() {
this.thereIsARestApi = await restApiExists(this.serverless, this.settings);
if (!this.thereIsARestApi) {
this.serverless.cli.log(`[serverless-api-gateway-caching] No REST API found. Caching settings will not be updated.`);
return;
Expand Down
44 changes: 33 additions & 11 deletions src/restApiId.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const getConfiguredRestApiId = (serverless) => {
return get(serverless, 'service.provider.apiGateway.restApiId')
}

const restApiExists = (serverless, settings) => {
const restApiExists = async (serverless, settings) => {
if (get(settings, 'restApiId')) {
return true;
}
Expand All @@ -19,6 +19,14 @@ const restApiExists = (serverless, settings) => {
if (resource) {
return true;
}

const stack = await getAlreadyDeployedStack(serverless, settings);
if (stack) {
const restApiIdFromAlreadyDeployedStack = await retrieveRestApiId(serverless, settings);
if (restApiIdFromAlreadyDeployedStack) {
return true;
}
}
return false;
}

Expand All @@ -32,21 +40,35 @@ const outputRestApiIdTo = (serverless) => {
};
};

const getAlreadyDeployedStack = async (serverless, settings) => {
const stackName = serverless.providers.aws.naming.getStackName(settings.stage);
try {
const stack = await serverless.providers.aws.request('CloudFormation', 'describeStacks', { StackName: stackName },
settings.stage,
settings.region
);
return stack;
}
catch (error) {
serverless.cli.log(`[serverless-api-gateway-caching] Could not retrieve stack because: ${error.message}.`);
return;
}
}

const retrieveRestApiId = async (serverless, settings) => {
if (settings.restApiId) {
return settings.restApiId;
}

const stackName = serverless.providers.aws.naming.getStackName(settings.stage);

const cloudFormation = await serverless.providers.aws.request('CloudFormation', 'describeStacks', { StackName: stackName },
settings.stage,
settings.region
);
const outputs = cloudFormation.Stacks[0].Outputs;
const restApiKey = outputs.find(({ OutputKey }) => OutputKey === REST_API_ID_KEY).OutputValue;

return restApiKey;
const stack = await getAlreadyDeployedStack(serverless, settings);
const outputs = stack.Stacks[0].Outputs;
const restApiKey = outputs.find(({ OutputKey }) => OutputKey === REST_API_ID_KEY)
if (restApiKey) {
return restApiKey.OutputValue;
}
else {
serverless.cli.log(`[serverless-api-gateway-caching] Could not find stack output variable named ${REST_API_ID_KEY}.`);
}
};

module.exports = {
Expand Down
44 changes: 35 additions & 9 deletions test/configuring-rest-api-id.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const APP_ROOT = '..';
const given = require(`${APP_ROOT}/test/steps/given`);
const expect = require('chai').expect;
const { restApiExists } = require(`${APP_ROOT}/src/restApiId`);
const { restApiExists, retrieveRestApiId } = require(`${APP_ROOT}/src/restApiId`);
const ApiGatewayCachingSettings = require(`${APP_ROOT}/src/ApiGatewayCachingSettings`);

describe('Finding the REST API', () => {
let result;

describe('when the REST API ID has been specified in the settings', () => {
before(() => {
before(async () => {
let serverless = given
.a_serverless_instance()
.withApiGatewayCachingConfig({ restApiId: given.a_rest_api_id() });

let settings = new ApiGatewayCachingSettings(serverless);

result = restApiExists(serverless, settings);
result = await restApiExists(serverless, settings);
});

it('should return that the REST API exists', () => {
Expand All @@ -24,30 +24,54 @@ describe('Finding the REST API', () => {
});

describe('when the REST API ID has already been defined in serverless configuration', () => {
before(() => {
before(async () => {
let serverless = given
.a_serverless_instance()
.withProviderRestApiId(given.a_rest_api_id());
settings = new ApiGatewayCachingSettings(serverless);

result = restApiExists(serverless);
result = await restApiExists(serverless, settings);
});

it('should return that the REST API exists', () => {
expect(result).to.be.true;
});
});

describe('when the CloudFormation stack has already been deployed and it output a RestApiIdForApigCaching', () => {
let restApiId, serverless, settings;
before(async () => {
serverless = given
.a_serverless_instance();

settings = new ApiGatewayCachingSettings(serverless);
restApiId = given.a_rest_api_id_for_deployment(serverless, settings);

result = await restApiExists(serverless, settings);
});

it('should return that the REST API exists', () => {
expect(result).to.be.true;
});

it('should return the value of the REST API id', async () => {
const retrievedRestApiId = await retrieveRestApiId(serverless, settings);
expect(retrievedRestApiId).to.equal(restApiId);
});
});

describe('when the REST API has not been defined in serverless configuration', () => {
describe('and there are HTTP handler functions', () => {
before(() => {
before(async () => {
let functionWithHttpEndpoint = given
.a_serverless_function('get-cat-by-paw-id')
.withHttpEndpoint('get', '/cat/{pawId}');
serverless = given
.a_serverless_instance()
.withFunction(functionWithHttpEndpoint);
settings = new ApiGatewayCachingSettings(serverless);

result = restApiExists(serverless);
result = await restApiExists(serverless, settings);
});

it('should return that the REST API does exist', () => {
Expand All @@ -56,10 +80,12 @@ describe('Finding the REST API', () => {
});

describe('and there are no HTTP handler functions', () => {
before(() => {
before(async () => {
serverless = given.a_serverless_instance();
settings = new ApiGatewayCachingSettings(serverless);
given.the_rest_api_id_is_not_set_for_deployment(serverless, settings);

result = restApiExists(serverless);
result = await restApiExists(serverless, settings);
});

it('should return that the REST API does not exist', () => {
Expand Down
2 changes: 0 additions & 2 deletions test/model/Serverless.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const split = require('lodash.split');

class Serverless {
constructor(serviceName) {
this._logMessages = [];
Expand Down
7 changes: 6 additions & 1 deletion test/steps/given.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ const a_rest_api_id = () => {
return chance.guid();
}

const a_rest_api_id_for_deployment = async (serverless, settings) => {
const a_rest_api_id_for_deployment = (serverless, settings) => {
let restApiId = a_rest_api_id();
serverless.setRestApiId(restApiId, settings);

return restApiId;
}

const the_rest_api_id_is_not_set_for_deployment = (serverless, settings) => {
serverless.setRestApiId(undefined, settings);
}

const endpoints_with_caching_enabled = (endpointCount) => {
let result = [];
for (let i = 0; i < endpointCount; i++) {
Expand All @@ -45,6 +49,7 @@ module.exports = {
a_serverless_function,
a_rest_api_id,
a_rest_api_id_for_deployment,
the_rest_api_id_is_not_set_for_deployment,
endpoints_with_caching_enabled,
an_additional_endpoint
}

0 comments on commit 76e19b6

Please sign in to comment.