Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apigateway): unable to associate RestApi as a route53 target for late bound domains #5555

Merged
merged 3 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions packages/@aws-cdk/aws-apigateway/lib/restapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,8 @@ export class RestApi extends Resource implements IRestApi {
*/
public deploymentStage!: Stage;

/**
* The domain name mapped to this API, if defined through the `domainName`
* configuration prop.
*/
public readonly domainName?: DomainName;

private readonly methods = new Array<Method>();
private _domainName?: DomainName;
private _latestDeployment: Deployment | undefined;

constructor(scope: Construct, id: string, props: RestApiProps = { }) {
Expand Down Expand Up @@ -253,10 +248,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 +295,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();
}
};