-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathdomain.ts
170 lines (146 loc) · 3.71 KB
/
domain.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { Lazy, Resource, IResolvable } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnDomain } from './amplify.generated';
import { IApp } from './app';
import { IBranch } from './branch';
/**
* Options to add a domain to an application
*/
export interface DomainOptions {
/**
* The name of the domain
*
* @default - the construct's id
*/
readonly domainName?: string;
/**
* Subdomains
*
* @default - use `addSubDomain()` to add subdomains
*/
readonly subDomains?: SubDomain[];
}
/**
* Properties for a Domain
*/
export interface DomainProps extends DomainOptions {
/**
* The application to which the domain must be connected
*/
readonly app: IApp;
}
/**
* An Amplify Console domain
*/
export class Domain extends Resource {
/**
* The ARN of the domain
*
* @attribute
*/
public readonly arn: string;
/**
* The DNS Record for certificate verification
*
* @attribute
*/
public readonly certificateRecord: string;
/**
* The name of the domain
*
* @attribute
*/
public readonly domainName: string;
/**
* The status of the domain association
*
* @attribute
*/
public readonly domainStatus: string;
/**
* The reason for the current status of the domain
*
* @attribute
*/
public readonly statusReason: string;
/**
* Branch patterns for the automatically created subdomain.
*
* @attribute
*/
public readonly domainAutoSubDomainCreationPatterns: string[];
/**
* The IAM service role for the subdomain.
*
* @attribute
*/
public readonly domainAutoSubDomainIamRole: string;
/**
* Specifies whether the automated creation of subdomains for branches is enabled.
*
* @attribute
*/
public readonly domainEnableAutoSubDomain: IResolvable;
private readonly subDomains: SubDomain[];
constructor(scope: Construct, id: string, props: DomainProps) {
super(scope, id);
this.subDomains = props.subDomains || [];
const domainName = props.domainName || id;
const domain = new CfnDomain(this, 'Resource', {
appId: props.app.appId,
domainName,
subDomainSettings: Lazy.any({ produce: () => this.renderSubDomainSettings() }, { omitEmptyArray: true }),
});
this.arn = domain.attrArn;
this.certificateRecord = domain.attrCertificateRecord;
this.domainName = domain.attrDomainName;
this.domainStatus = domain.attrDomainStatus;
this.statusReason = domain.attrStatusReason;
this.domainAutoSubDomainCreationPatterns = domain.attrAutoSubDomainCreationPatterns;
this.domainAutoSubDomainIamRole = domain.attrAutoSubDomainIamRole;
this.domainEnableAutoSubDomain = domain.attrEnableAutoSubDomain;
}
/**
* 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'];
}
return [];
}
private renderSubDomainSettings() {
return this.subDomains.map(s => ({
branchName: s.branch.branchName,
prefix: s.prefix === undefined ? s.branch.branchName : s.prefix,
}));
}
}
/**
* Sub domain settings
*/
export interface SubDomain {
/**
* The branch
*/
readonly branch: IBranch;
/**
* The prefix. Use '' to map to the root of the domain
*
* @default - the branch name
*/
readonly prefix?: string;
}