Skip to content

Commit 76e19b6

Browse files
author
Diana Ionita
committed
Merge branch 'release/1.7.3'
2 parents 25e3417 + 3e43d44 commit 76e19b6

File tree

7 files changed

+83
-32
lines changed

7 files changed

+83
-32
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-api-gateway-caching",
3-
"version": "1.7.2",
3+
"version": "1.7.3",
44
"description": "A plugin for the serverless framework which helps with configuring caching for API Gateway endpoints.",
55
"main": "src/apiGatewayCachingPlugin.js",
66
"scripts": {

src/apiGatewayCachingPlugin.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class ApiGatewayCachingPlugin {
2323
this.settings = new ApiGatewayCachingSettings(this.serverless, this.options);
2424
}
2525

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

43-
updateStage() {
44-
this.thereIsARestApi = restApiExists(this.serverless, this.settings);
43+
async updateStage() {
44+
this.thereIsARestApi = await restApiExists(this.serverless, this.settings);
4545
if (!this.thereIsARestApi) {
4646
this.serverless.cli.log(`[serverless-api-gateway-caching] No REST API found. Caching settings will not be updated.`);
4747
return;

src/restApiId.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const getConfiguredRestApiId = (serverless) => {
77
return get(serverless, 'service.provider.apiGateway.restApiId')
88
}
99

10-
const restApiExists = (serverless, settings) => {
10+
const restApiExists = async (serverless, settings) => {
1111
if (get(settings, 'restApiId')) {
1212
return true;
1313
}
@@ -19,6 +19,14 @@ const restApiExists = (serverless, settings) => {
1919
if (resource) {
2020
return true;
2121
}
22+
23+
const stack = await getAlreadyDeployedStack(serverless, settings);
24+
if (stack) {
25+
const restApiIdFromAlreadyDeployedStack = await retrieveRestApiId(serverless, settings);
26+
if (restApiIdFromAlreadyDeployedStack) {
27+
return true;
28+
}
29+
}
2230
return false;
2331
}
2432

@@ -32,21 +40,35 @@ const outputRestApiIdTo = (serverless) => {
3240
};
3341
};
3442

43+
const getAlreadyDeployedStack = async (serverless, settings) => {
44+
const stackName = serverless.providers.aws.naming.getStackName(settings.stage);
45+
try {
46+
const stack = await serverless.providers.aws.request('CloudFormation', 'describeStacks', { StackName: stackName },
47+
settings.stage,
48+
settings.region
49+
);
50+
return stack;
51+
}
52+
catch (error) {
53+
serverless.cli.log(`[serverless-api-gateway-caching] Could not retrieve stack because: ${error.message}.`);
54+
return;
55+
}
56+
}
57+
3558
const retrieveRestApiId = async (serverless, settings) => {
3659
if (settings.restApiId) {
3760
return settings.restApiId;
3861
}
39-
40-
const stackName = serverless.providers.aws.naming.getStackName(settings.stage);
41-
42-
const cloudFormation = await serverless.providers.aws.request('CloudFormation', 'describeStacks', { StackName: stackName },
43-
settings.stage,
44-
settings.region
45-
);
46-
const outputs = cloudFormation.Stacks[0].Outputs;
47-
const restApiKey = outputs.find(({ OutputKey }) => OutputKey === REST_API_ID_KEY).OutputValue;
4862

49-
return restApiKey;
63+
const stack = await getAlreadyDeployedStack(serverless, settings);
64+
const outputs = stack.Stacks[0].Outputs;
65+
const restApiKey = outputs.find(({ OutputKey }) => OutputKey === REST_API_ID_KEY)
66+
if (restApiKey) {
67+
return restApiKey.OutputValue;
68+
}
69+
else {
70+
serverless.cli.log(`[serverless-api-gateway-caching] Could not find stack output variable named ${REST_API_ID_KEY}.`);
71+
}
5072
};
5173

5274
module.exports = {

test/configuring-rest-api-id.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
const APP_ROOT = '..';
22
const given = require(`${APP_ROOT}/test/steps/given`);
33
const expect = require('chai').expect;
4-
const { restApiExists } = require(`${APP_ROOT}/src/restApiId`);
4+
const { restApiExists, retrieveRestApiId } = require(`${APP_ROOT}/src/restApiId`);
55
const ApiGatewayCachingSettings = require(`${APP_ROOT}/src/ApiGatewayCachingSettings`);
66

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

1010
describe('when the REST API ID has been specified in the settings', () => {
11-
before(() => {
11+
before(async () => {
1212
let serverless = given
1313
.a_serverless_instance()
1414
.withApiGatewayCachingConfig({ restApiId: given.a_rest_api_id() });
1515

1616
let settings = new ApiGatewayCachingSettings(serverless);
1717

18-
result = restApiExists(serverless, settings);
18+
result = await restApiExists(serverless, settings);
1919
});
2020

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

2626
describe('when the REST API ID has already been defined in serverless configuration', () => {
27-
before(() => {
27+
before(async () => {
2828
let serverless = given
2929
.a_serverless_instance()
3030
.withProviderRestApiId(given.a_rest_api_id());
31+
settings = new ApiGatewayCachingSettings(serverless);
3132

32-
result = restApiExists(serverless);
33+
result = await restApiExists(serverless, settings);
3334
});
3435

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

41+
describe('when the CloudFormation stack has already been deployed and it output a RestApiIdForApigCaching', () => {
42+
let restApiId, serverless, settings;
43+
before(async () => {
44+
serverless = given
45+
.a_serverless_instance();
46+
47+
settings = new ApiGatewayCachingSettings(serverless);
48+
restApiId = given.a_rest_api_id_for_deployment(serverless, settings);
49+
50+
result = await restApiExists(serverless, settings);
51+
});
52+
53+
it('should return that the REST API exists', () => {
54+
expect(result).to.be.true;
55+
});
56+
57+
it('should return the value of the REST API id', async () => {
58+
const retrievedRestApiId = await retrieveRestApiId(serverless, settings);
59+
expect(retrievedRestApiId).to.equal(restApiId);
60+
});
61+
});
62+
4063
describe('when the REST API has not been defined in serverless configuration', () => {
4164
describe('and there are HTTP handler functions', () => {
42-
before(() => {
65+
before(async () => {
4366
let functionWithHttpEndpoint = given
4467
.a_serverless_function('get-cat-by-paw-id')
4568
.withHttpEndpoint('get', '/cat/{pawId}');
4669
serverless = given
4770
.a_serverless_instance()
4871
.withFunction(functionWithHttpEndpoint);
72+
settings = new ApiGatewayCachingSettings(serverless);
4973

50-
result = restApiExists(serverless);
74+
result = await restApiExists(serverless, settings);
5175
});
5276

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

5882
describe('and there are no HTTP handler functions', () => {
59-
before(() => {
83+
before(async () => {
6084
serverless = given.a_serverless_instance();
85+
settings = new ApiGatewayCachingSettings(serverless);
86+
given.the_rest_api_id_is_not_set_for_deployment(serverless, settings);
6187

62-
result = restApiExists(serverless);
88+
result = await restApiExists(serverless, settings);
6389
});
6490

6591
it('should return that the REST API does not exist', () => {

test/model/Serverless.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const split = require('lodash.split');
2-
31
class Serverless {
42
constructor(serviceName) {
53
this._logMessages = [];

test/steps/given.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ const a_rest_api_id = () => {
1515
return chance.guid();
1616
}
1717

18-
const a_rest_api_id_for_deployment = async (serverless, settings) => {
18+
const a_rest_api_id_for_deployment = (serverless, settings) => {
1919
let restApiId = a_rest_api_id();
2020
serverless.setRestApiId(restApiId, settings);
2121

2222
return restApiId;
2323
}
2424

25+
const the_rest_api_id_is_not_set_for_deployment = (serverless, settings) => {
26+
serverless.setRestApiId(undefined, settings);
27+
}
28+
2529
const endpoints_with_caching_enabled = (endpointCount) => {
2630
let result = [];
2731
for (let i = 0; i < endpointCount; i++) {
@@ -45,6 +49,7 @@ module.exports = {
4549
a_serverless_function,
4650
a_rest_api_id,
4751
a_rest_api_id_for_deployment,
52+
the_rest_api_id_is_not_set_for_deployment,
4853
endpoints_with_caching_enabled,
4954
an_additional_endpoint
5055
}

0 commit comments

Comments
 (0)