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(amplify): cannot map branch to domain root #7621

Merged
merged 4 commits into from
May 3, 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
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-amplify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ amplifyApp.addCustomRule({
Add a domain and map sub domains to branches:
```ts
const domain = amplifyApp.addDomain('example.com');
domain.mapRoot(master); // map master branch to domain root
domain.mapSubDomain(master, 'www');
domain.mapSubDomain(dev); // sub domain prefix defaults to branch name
```
Expand Down
14 changes: 12 additions & 2 deletions packages/@aws-cdk/aws-amplify/lib/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,22 @@ export class Domain extends Resource {

/**
* Maps a branch to a sub domain
*
* @param branch The branch
* @param prefix The prefix. Use '' to map to the root of the domain. Defaults to branch name.
*/
public mapSubDomain(branch: IBranch, prefix?: string) {
this.subDomains.push({ branch, prefix });
return this;
}

/**
* Maps a branch to the domain root
*/
public mapRoot(branch: IBranch) {
return this.mapSubDomain(branch, '');
}

protected validate() {
if (this.subDomains.length === 0) {
return ['The domain doesn\'t contain any subdomains'];
Expand All @@ -112,7 +122,7 @@ export class Domain extends Resource {
private renderSubDomainSettings() {
return this.subDomains.map(s => ({
branchName: s.branch.branchName,
prefix: s.prefix || s.branch.branchName,
prefix: s.prefix === undefined ? s.branch.branchName : s.prefix,
}));
}
}
Expand All @@ -127,7 +137,7 @@ export interface SubDomain {
readonly branch: IBranch;

/**
* The prefix
* The prefix. Use '' to map to the root of the domain
*
* @default - the branch name
*/
Expand Down
39 changes: 39 additions & 0 deletions packages/@aws-cdk/aws-amplify/test/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,45 @@ test('create a domain', () => {
});
});

test('map a branch to the domain root', () => {
// GIVEN
const stack = new Stack();
const app = new amplify.App(stack, 'App', {
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
owner: 'aws',
repository: 'aws-cdk',
oauthToken: SecretValue.plainText('secret'),
}),
});
const prodBranch = app.addBranch('master');

// WHEN
const domain = app.addDomain('amazon.com');
domain.mapRoot(prodBranch);

// THEN
expect(stack).toHaveResource('AWS::Amplify::Domain', {
AppId: {
'Fn::GetAtt': [
'AppF1B96344',
'AppId',
],
},
DomainName: 'amazon.com',
SubDomainSettings: [
{
BranchName: {
'Fn::GetAtt': [
'Appmaster71597E87',
'BranchName',
],
},
Prefix: '',
},
],
});
});

test('throws at synthesis without subdomains', () => {
// GIVEN
const app = new App();
Expand Down