Skip to content

Commit 6de8c7c

Browse files
authored
Merge pull request #114 from aleksdikanski/domain-name-plugin-support
Add support for domain manager plugin
2 parents b65e258 + 73a0cd6 commit 6de8c7c

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

lib/stackInformation.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,15 @@ module.exports = {
171171
BbPromise.bind(this).then(this.aliasStackLoadAliasTemplates)
172172
)
173173
.spread((currentTemplate, aliasStackTemplates) => {
174-
const currentAliasStackTemplate =
175-
_.get(
176-
_.first(_.remove(aliasStackTemplates, ['stack', `${this._provider.naming.getStackName()}-${this._alias}`])),
177-
'template',
178-
{});
179-
const deployedAliasStackTemplates = _.map(aliasStackTemplates, template => template.template);
174+
const removed = _.filter(aliasStackTemplates, ['stack', `${this._provider.naming.getStackName()}-${this._alias}`]);
175+
const filteredAliasStackTemplates = _.without(aliasStackTemplates, ['stack', `${this._provider.naming.getStackName()}-${this._alias}`]);
180176

177+
const currentAliasStackTemplate = _.get(_.first(removed), 'template', {});
178+
const deployedAliasStackTemplates = _.map(filteredAliasStackTemplates, template => template.template);
179+
181180
this._serverless.service.provider.deployedCloudFormationTemplate = currentTemplate;
182181
this._serverless.service.provider.deployedCloudFormationAliasTemplate = currentAliasStackTemplate;
183-
this._serverless.service.provider.deployedAliasTemplates = aliasStackTemplates;
182+
this._serverless.service.provider.deployedAliasTemplates = filteredAliasStackTemplates;
184183
return BbPromise.resolve([ currentTemplate, deployedAliasStackTemplates, currentAliasStackTemplate ]);
185184
});
186185
},

lib/stackops/apiGateway.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,19 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
186186
this.options.verbose && this._serverless.cli.log('Configuring stage');
187187
const stageResource = internal.createStageResource.call(this, `${stackName}-ApiGatewayRestApi`, deploymentName);
188188
aliasResources.push({ ApiGatewayStage: stageResource });
189-
}
189+
190+
const baseMapping = _.assign({}, _.pickBy(stageStack.Resources, ['Type', 'AWS::ApiGateway::BasePathMapping']));
191+
if (!_.isEmpty(baseMapping)) {
192+
const baseMappingName = _.keys(baseMapping)[0];
193+
const obj = baseMapping[baseMappingName];
194+
195+
obj.Properties.Stage = { Ref: 'ApiGatewayStage' };
196+
obj.Properties.RestApiId = { 'Fn::ImportValue': `${stackName}-ApiGatewayRestApi`};
197+
198+
aliasResources.push(baseMapping);
199+
delete stageStack.Resources[baseMappingName];
200+
}
201+
}
190202

191203
// Fetch lambda permissions, methods and resources. These have to be updated later to allow the aliased functions.
192204
const apiLambdaPermissions =
@@ -253,7 +265,7 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
253265
if (_.isString(resource.DependsOn) && resource.DependsOn === name) {
254266
resource.DependsOn = aliasedName;
255267
} else if (_.isArray(resource.DependsOn) && _.includes(resource.DependsOn, name)) {
256-
_.pull(resource.DependsOn, name);
268+
resource.DependsOn = _.without(resource.DependsOn, name);
257269
resource.DependsOn.push(aliasedName);
258270
}
259271
});

test/data/auth-stack-2.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,15 @@
464464
}
465465
}
466466
},
467+
"pathmapping": {
468+
"Type": "AWS::ApiGateway::BasePathMapping",
469+
"Properties": {
470+
"BasePath": "(none)",
471+
"DomainName": "example.com",
472+
"RestApiId": { "Fn::ImportValue": "ApiGatewayRestApi" },
473+
"Stage": "test"
474+
}
475+
},
467476
"TestauthLambdaPermissionApiGateway": {
468477
"Type": "AWS::Lambda::Permission",
469478
"Properties": {

test/stackops/apiGateway.test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ describe('API Gateway', () => {
713713
.to.have.a.nested.property("Resources.ApiGatewayMethodFunc2Get.DependsOn")
714714
.that.equals("PseudoParamCustomAuthApiGatewayAuthorizermyAlias"),
715715
expect(template)
716-
.to.have.a.nested.property('Resources.PseudoParamCustomAuthApiGatewayAuthorizermyAlias.Properties.AuthorizerUri')
716+
.to.have.a.nested.property('Resources.PseudoParamCustomAuthApiGatewayAuthorizermyAlias.Properties.AuthorizerUri')
717717
.that.deep.equals({
718718
"Fn::Join": [
719719
"",
@@ -733,6 +733,29 @@ describe('API Gateway', () => {
733733

734734
});
735735

736+
it('should move base mappings to alias stack', () => {
737+
stackTemplate = _.cloneDeep(require('../data/auth-stack-2.json'));
738+
const template = serverless.service.provider.compiledCloudFormationTemplate = stackTemplate;
739+
const compiledAliasTemplate = serverless.service.provider.compiledCloudFormationAliasTemplate = aliasTemplate;
740+
return expect(awsAlias.aliasHandleApiGateway({}, [], {})).to.be.fulfilled
741+
.then(()=> BbPromise.all([
742+
expect(template)
743+
.to.not.have.a.nested.property('Resources.pathmapping'),
744+
expect(aliasTemplate)
745+
.to.have.a.nested.property('Resources.pathmapping')
746+
.that.deep.equals({
747+
Type: 'AWS::ApiGateway::BasePathMapping',
748+
Properties: {
749+
BasePath: '(none)',
750+
DomainName: 'example.com',
751+
RestApiId: { 'Fn::ImportValue': 'testService-myStage-ApiGatewayRestApi' },
752+
Stage: { Ref: 'ApiGatewayStage' }
753+
}
754+
})
755+
]));
756+
757+
});
758+
736759
it('should transform string dependencies and references to authorizers', () => {
737760
const template = serverless.service.provider.compiledCloudFormationTemplate = stackTemplate;
738761
serverless.service.provider.compiledCloudFormationAliasTemplate = aliasTemplate;

0 commit comments

Comments
 (0)