Skip to content

Commit

Permalink
fix(apigateway): allow adding custom domain later
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Jan 7, 2020
1 parent 6108b91 commit 06b10e5
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 4 deletions.
20 changes: 16 additions & 4 deletions packages/@aws-cdk/aws-apigateway/lib/restapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ export class RestApi extends Resource implements IRestApi {

/**
* The domain name mapped to this API, if defined through the `domainName`
* configuration prop.
* configuration prop, or added later via `addDomainName`
*/
public readonly domainName?: DomainName;
private _domainName?: DomainName;

private readonly methods = new Array<Method>();
private _latestDeployment: Deployment | undefined;
Expand Down Expand Up @@ -253,10 +253,18 @@ export class RestApi extends Resource implements IRestApi {
this.restApiRootResourceId = resource.attrRootResourceId;

if (props.domainName) {
this.domainName = this.addDomainName('CustomDomain', props.domainName);
this.addDomainName('CustomDomain', props.domainName);
}
}

/**
* The first domain name mapped to this API, if defined through the `domainName`
* configuration prop, or added via `addDomainName`
*/
public get domainName() {
return this._domainName;
}

/**
* API Gateway deployment that represents the latest changes of the API.
* This resource will be automatically updated every time the REST API model changes.
Expand Down Expand Up @@ -292,10 +300,14 @@ export class RestApi extends Resource implements IRestApi {
* @param options custom domain options
*/
public addDomainName(id: string, options: DomainNameOptions): DomainName {
return new DomainName(this, id, {
const domainName = new DomainName(this, id, {
...options,
mapping: this
});
if (!this._domainName) {
this._domainName = domainName;
}
return domainName;
}

/**
Expand Down
106 changes: 106 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/test.domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,112 @@ export = {
}
}));

test.done();
},

'a domain name can be added later'(test: Test) {
// GIVEN
const domainName = 'my.domain.com';
const stack = new Stack();
const certificate = new acm.Certificate(stack, 'cert', { domainName: 'my.domain.com' });

// WHEN
const api = new apigw.RestApi(stack, 'api', {});

api.root.addMethod('GET');

api.addDomainName('domainId', { domainName, certificate });

// THEN
expect(stack).to(haveResource('AWS::ApiGateway::DomainName', {
"DomainName": domainName,
"EndpointConfiguration": {
"Types": [
"REGIONAL"
]
},
"RegionalCertificateArn": {
"Ref": "cert56CA94EB"
}
}));
expect(stack).to(haveResource('AWS::ApiGateway::BasePathMapping', {
"DomainName": {
"Ref": "apidomainId102F8DAA"
},
"RestApiId": {
"Ref": "apiC8550315"
},
"Stage": {
"Ref": "apiDeploymentStageprod896C8101"
}
}));

test.done();
},

'multiple domain names can be added'(test: Test) {
// GIVEN
const domainName = 'my.domain.com';
const stack = new Stack();
const certificate = new acm.Certificate(stack, 'cert', { domainName: 'my.domain.com' });

// WHEN
const api = new apigw.RestApi(stack, 'api', {});

api.root.addMethod('GET');

const domainName1 = api.addDomainName('domainId', { domainName, certificate });
api.addDomainName('domainId1', { domainName: 'your.domain.com', certificate });
api.addDomainName('domainId2', { domainName: 'our.domain.com', certificate });

test.deepEqual(api.domainName, domainName1);

// THEN
expect(stack).to(haveResource('AWS::ApiGateway::DomainName', {
"DomainName": 'my.domain.com',
"EndpointConfiguration": {
"Types": [
"REGIONAL"
]
},
"RegionalCertificateArn": {
"Ref": "cert56CA94EB"
}
}));
expect(stack).to(haveResource('AWS::ApiGateway::DomainName', {
"DomainName": 'your.domain.com',
"EndpointConfiguration": {
"Types": [
"REGIONAL"
]
},
"RegionalCertificateArn": {
"Ref": "cert56CA94EB"
}
}));
expect(stack).to(haveResource('AWS::ApiGateway::DomainName', {
"DomainName": 'our.domain.com',
"EndpointConfiguration": {
"Types": [
"REGIONAL"
]
},
"RegionalCertificateArn": {
"Ref": "cert56CA94EB"
}
}));
expect(stack).to(haveResource('AWS::ApiGateway::BasePathMapping', {
"DomainName": {
"Ref": "apidomainId102F8DAA"
},
"RestApiId": {
"Ref": "apiC8550315"
},
"Stage": {
"Ref": "apiDeploymentStageprod896C8101"
}
}));

test.done();
}
};

0 comments on commit 06b10e5

Please sign in to comment.