1
1
import ec2 = require( '@aws-cdk/aws-ec2' ) ;
2
2
import cdk = require( '@aws-cdk/cdk' ) ;
3
3
import { HostedZoneImportProps , IHostedZone } from './hosted-zone-ref' ;
4
- import { CfnHostedZone , HostedZoneNameServers } from './route53.generated' ;
4
+ import { CfnHostedZone } from './route53.generated' ;
5
5
import { validateZoneName } from './util' ;
6
6
7
- /**
8
- * Properties of a new hosted zone
9
- */
10
- export interface PublicHostedZoneProps {
7
+ export interface CommonHostedZoneProps {
11
8
/**
12
- * The fully qualified domain name for the hosted zone
9
+ * The name of the domain. For resource record types that include a domain
10
+ * name, specify a fully qualified domain name.
13
11
*/
14
12
zoneName : string ;
15
13
16
14
/**
17
15
* Any comments that you want to include about the hosted zone.
18
16
*
19
- * @default no comment
17
+ * @default none
20
18
*/
21
19
comment ?: string ;
22
20
23
21
/**
24
22
* The Amazon Resource Name (ARN) for the log group that you want Amazon Route 53 to send query logs to.
25
23
*
26
- * @default no DNS query logging
24
+ * @default disabled
27
25
*/
28
26
queryLogsLogGroupArn ?: string ;
29
27
}
30
28
31
- export abstract class HostedZone extends cdk . Construct implements IHostedZone {
32
- public static import ( scope : cdk . Construct , id : string , props : HostedZoneImportProps ) : IHostedZone {
33
- return new ImportedHostedZone ( scope , id , props ) ;
34
- }
35
-
36
- public abstract readonly hostedZoneId : string ;
37
- public abstract readonly zoneName : string ;
38
-
39
- public export ( ) : HostedZoneImportProps {
40
- return {
41
- hostedZoneId : new cdk . Output ( this , 'HostedZoneId' , { value : this . hostedZoneId } ) . makeImportValue ( ) . toString ( ) ,
42
- zoneName : this . zoneName ,
43
- } ;
44
- }
45
- }
46
-
47
29
/**
48
- * Create a Route53 public hosted zone.
30
+ * Properties of a new hosted zone
49
31
*/
50
- export class PublicHostedZone extends HostedZone {
32
+ export interface HostedZoneProps extends CommonHostedZoneProps {
51
33
/**
52
- * Identifier of this hosted zone
34
+ * A VPC that you want to associate with this hosted zone. When you specify
35
+ * this property, a private hosted zone will be created.
36
+ *
37
+ * You can associate additional VPCs to this private zone using `addVpc(vpc)`.
38
+ *
39
+ * @default public (no VPCs associated)
53
40
*/
54
- public readonly hostedZoneId : string ;
41
+ vpcs ?: ec2 . IVpcNetwork [ ] ;
42
+ }
55
43
44
+ export class HostedZone extends cdk . Construct implements IHostedZone {
56
45
/**
57
- * Fully qualified domain name for the hosted zone
46
+ * Imports a hosted zone from another stack.
58
47
*/
48
+ public static import ( scope : cdk . Construct , id : string , props : HostedZoneImportProps ) : IHostedZone {
49
+ return new ImportedHostedZone ( scope , id , props ) ;
50
+ }
51
+
52
+ public readonly hostedZoneId : string ;
59
53
public readonly zoneName : string ;
54
+ public readonly hostedZoneNameServers ?: string [ ] ;
60
55
61
56
/**
62
- * Nameservers for this public hosted zone
57
+ * VPCs to which this hosted zone will be added
63
58
*/
64
- public readonly nameServers : HostedZoneNameServers ;
59
+ protected readonly vpcs = new Array < CfnHostedZone . VPCProperty > ( ) ;
65
60
66
- constructor ( scope : cdk . Construct , id : string , props : PublicHostedZoneProps ) {
61
+ constructor ( scope : cdk . Construct , id : string , props : HostedZoneProps ) {
67
62
super ( scope , id ) ;
68
63
69
64
validateZoneName ( props . zoneName ) ;
70
65
71
66
const hostedZone = new CfnHostedZone ( this , 'Resource' , {
72
- ...determineHostedZoneProps ( props )
67
+ name : props . zoneName + '.' ,
68
+ hostedZoneConfig : props . comment ? { comment : props . comment } : undefined ,
69
+ queryLoggingConfig : props . queryLogsLogGroupArn ? { cloudWatchLogsLogGroupArn : props . queryLogsLogGroupArn } : undefined ,
70
+ vpcs : new cdk . Token ( ( ) => this . vpcs . length === 0 ? undefined : this . vpcs )
73
71
} ) ;
74
72
75
73
this . hostedZoneId = hostedZone . ref ;
76
- this . nameServers = hostedZone . hostedZoneNameServers ;
74
+ this . hostedZoneNameServers = hostedZone . hostedZoneNameServers . toList ( ) ;
77
75
this . zoneName = props . zoneName ;
76
+
77
+ for ( const vpc of props . vpcs || [ ] ) {
78
+ this . addVpc ( vpc ) ;
79
+ }
80
+ }
81
+
82
+ public export ( ) : HostedZoneImportProps {
83
+ return {
84
+ hostedZoneId : new cdk . Output ( this , 'HostedZoneId' , { value : this . hostedZoneId } ) . makeImportValue ( ) ,
85
+ zoneName : this . zoneName ,
86
+ } ;
87
+ }
88
+
89
+ /**
90
+ * Add another VPC to this private hosted zone.
91
+ *
92
+ * @param vpc the other VPC to add.
93
+ */
94
+ public addVpc ( vpc : ec2 . IVpcNetwork ) {
95
+ this . vpcs . push ( { vpcId : vpc . vpcId , vpcRegion : new cdk . AwsRegion ( ) } ) ;
78
96
}
79
97
}
80
98
99
+ // tslint:disable-next-line:no-empty-interface
100
+ export interface PublicHostedZoneProps extends CommonHostedZoneProps {
101
+
102
+ }
103
+
81
104
/**
82
- * Properties for a private hosted zone.
105
+ * Create a Route53 public hosted zone.
83
106
*/
84
- export interface PrivateHostedZoneProps extends PublicHostedZoneProps {
107
+ export class PublicHostedZone extends HostedZone {
108
+ constructor ( scope : cdk . Construct , id : string , props : PublicHostedZoneProps ) {
109
+ super ( scope , id , props ) ;
110
+ }
111
+
112
+ public addVpc ( _vpc : ec2 . IVpcNetwork ) {
113
+ throw new Error ( 'Cannot associate public hosted zones with a VPC' ) ;
114
+ }
115
+ }
116
+
117
+ export interface PrivateHostedZoneProps extends CommonHostedZoneProps {
85
118
/**
86
- * One VPC that you want to associate with this hosted zone.
119
+ * A VPC that you want to associate with this hosted zone.
120
+ *
121
+ * Private hosted zones must be associated with at least one VPC. You can
122
+ * associated additional VPCs using `addVpc(vpc)`.
87
123
*/
88
124
vpc : ec2 . IVpcNetwork ;
89
125
}
@@ -95,65 +131,18 @@ export interface PrivateHostedZoneProps extends PublicHostedZoneProps {
95
131
* for the VPC you're configuring for private hosted zones.
96
132
*/
97
133
export class PrivateHostedZone extends HostedZone {
98
- /**
99
- * Identifier of this hosted zone
100
- */
101
- public readonly hostedZoneId : string ;
102
-
103
- /**
104
- * Fully qualified domain name for the hosted zone
105
- */
106
- public readonly zoneName : string ;
107
-
108
- /**
109
- * VPCs to which this hosted zone will be added
110
- */
111
- private readonly vpcs : CfnHostedZone . VPCProperty [ ] = [ ] ;
112
-
113
134
constructor ( scope : cdk . Construct , id : string , props : PrivateHostedZoneProps ) {
114
- super ( scope , id ) ;
115
-
116
- validateZoneName ( props . zoneName ) ;
117
-
118
- const hostedZone = new CfnHostedZone ( this , 'Resource' , {
119
- vpcs : new cdk . Token ( ( ) => this . vpcs ? this . vpcs : undefined ) ,
120
- ...determineHostedZoneProps ( props )
121
- } ) ;
122
-
123
- this . hostedZoneId = hostedZone . ref ;
124
- this . zoneName = props . zoneName ;
135
+ super ( scope , id , props ) ;
125
136
126
137
this . addVpc ( props . vpc ) ;
127
138
}
128
-
129
- /**
130
- * Add another VPC to this private hosted zone.
131
- *
132
- * @param vpc the other VPC to add.
133
- */
134
- public addVpc ( vpc : ec2 . IVpcNetwork ) {
135
- this . vpcs . push ( toVpcProperty ( vpc ) ) ;
136
- }
137
- }
138
-
139
- function toVpcProperty ( vpc : ec2 . IVpcNetwork ) : CfnHostedZone . VPCProperty {
140
- return { vpcId : vpc . vpcId , vpcRegion : new cdk . AwsRegion ( ) } ;
141
- }
142
-
143
- function determineHostedZoneProps ( props : PublicHostedZoneProps ) {
144
- const name = props . zoneName + '.' ;
145
- const hostedZoneConfig = props . comment ? { comment : props . comment } : undefined ;
146
- const queryLoggingConfig = props . queryLogsLogGroupArn ? { cloudWatchLogsLogGroupArn : props . queryLogsLogGroupArn } : undefined ;
147
-
148
- return { name, hostedZoneConfig, queryLoggingConfig } ;
149
139
}
150
140
151
141
/**
152
142
* Imported hosted zone
153
143
*/
154
144
class ImportedHostedZone extends cdk . Construct implements IHostedZone {
155
145
public readonly hostedZoneId : string ;
156
-
157
146
public readonly zoneName : string ;
158
147
159
148
constructor ( scope : cdk . Construct , name : string , private readonly props : HostedZoneImportProps ) {
0 commit comments