1
1
import * as ros from '@alicloud/ros-cdk-core' ;
2
2
import { ActionContext , EventDomain , EventTypes , ServerlessIac } from '../../types' ;
3
3
import * as ram from '@alicloud/ros-cdk-ram' ;
4
- import { encodeBase64ForRosId , replaceReference } from '../../common' ;
4
+ import { encodeBase64ForRosId , replaceReference , splitDomain } from '../../common' ;
5
5
import * as agw from '@alicloud/ros-cdk-apigateway' ;
6
6
import { isEmpty } from 'lodash' ;
7
+ import * as dns from '@alicloud/ros-cdk-dns' ;
7
8
8
9
export const resolveEvents = (
9
10
scope : ros . Construct ,
@@ -16,12 +17,14 @@ export const resolveEvents = (
16
17
return undefined ;
17
18
}
18
19
const apiGateway = events ! . filter ( ( event ) => event . type === EventTypes . API_GATEWAY ) ;
19
- if ( apiGateway ?. length ) {
20
+ if ( ! apiGateway ?. length ) return ;
21
+
22
+ apiGateway . forEach ( ( event ) => {
20
23
const gatewayAccessRole = new ram . RosRole (
21
24
scope ,
22
- replaceReference ( `${ service } _role` , context ) ,
25
+ replaceReference ( `${ event . key } _role` , context ) ,
23
26
{
24
- roleName : replaceReference ( `${ service } -gateway -access-role` , context ) ,
27
+ roleName : replaceReference ( `${ service } -${ event . name } -agw -access-role` , context ) ,
25
28
description : replaceReference ( `${ service } role` , context ) ,
26
29
assumeRolePolicyDocument : {
27
30
version : '1' ,
@@ -37,7 +40,7 @@ export const resolveEvents = (
37
40
} ,
38
41
policies : [
39
42
{
40
- policyName : replaceReference ( `${ service } -policy` , context ) ,
43
+ policyName : replaceReference ( `${ service } -${ event . name } - policy` , context ) ,
41
44
policyDocument : {
42
45
version : '1' ,
43
46
statement : [
@@ -66,64 +69,75 @@ export const resolveEvents = (
66
69
true ,
67
70
) ;
68
71
69
- // new agw.RosCustomDomain(
70
- // this,
71
- // 'customDomain',
72
- // {
73
- // domainName: 'example.com',
74
- // certificateName: 'example.com',
75
- // certificateBody: 'example.com',
76
- // certificatePrivateKey: 'example.com',
77
- // groupId: apiGatewayGroup.attrGroupId,
78
- // },
79
- // true,
80
- // );
72
+ if ( event . domain ) {
73
+ const dnsRecordRosId = `${ event . key } _custom_domain_record_${ encodeBase64ForRosId ( event . domain . domain_name ) } ` ;
74
+ const { domainName, rr } = splitDomain ( event . domain ?. domain_name ) ;
81
75
82
- apiGateway . forEach ( ( event ) => {
83
- event . triggers . forEach ( ( trigger ) => {
84
- const key = encodeBase64ForRosId (
85
- replaceReference ( `${ trigger . method } _${ trigger . path } ` , context ) ,
86
- ) ;
76
+ new dns . DomainRecord ( scope , dnsRecordRosId , {
77
+ domainName,
78
+ rr,
79
+ type : 'CNAME' ,
80
+ value : apiGatewayGroup . attrSubDomain ,
81
+ } ) ;
87
82
88
- const api = new agw . RosApi (
89
- scope ,
90
- `${ event . key } _api_${ key } ` ,
91
- {
92
- apiName : replaceReference ( `${ event . name } _api_${ key } ` , context ) ,
93
- groupId : apiGatewayGroup . attrGroupId ,
94
- visibility : 'PRIVATE' ,
95
- authType : 'ANONYMOUS' ,
96
- requestConfig : {
97
- requestProtocol : 'HTTP' ,
98
- requestHttpMethod : replaceReference ( trigger . method , context ) ,
99
- requestPath : replaceReference ( trigger . path , context ) ,
100
- requestMode : 'PASSTHROUGH' ,
101
- } ,
102
- serviceConfig : {
103
- serviceProtocol : 'FunctionCompute' ,
104
- functionComputeConfig : {
105
- fcRegionId : context . region ,
106
- functionName : replaceReference ( trigger . backend , context ) ,
107
- roleArn : gatewayAccessRole . attrArn ,
108
- fcVersion : '3.0' ,
109
- method : replaceReference ( trigger . method , context ) ,
110
- } ,
83
+ const agwCustomDomain = new agw . RosCustomDomain (
84
+ scope ,
85
+ `${ event . key } _custom_domain_${ encodeBase64ForRosId ( event . domain . domain_name ) } ` ,
86
+ {
87
+ groupId : apiGatewayGroup . attrGroupId ,
88
+ domainName : event . domain . domain_name ,
89
+ certificateName : event . domain . certificate_name ,
90
+ certificateBody : event . domain . certificate_body ,
91
+ certificatePrivateKey : event . domain . certificate_private_key ,
92
+ } ,
93
+ true ,
94
+ ) ;
95
+ agwCustomDomain . addRosDependency ( dnsRecordRosId ) ;
96
+ }
97
+
98
+ event . triggers . forEach ( ( trigger ) => {
99
+ const key = encodeBase64ForRosId (
100
+ replaceReference ( `${ trigger . method } _${ trigger . path } ` , context ) ,
101
+ ) ;
102
+
103
+ const api = new agw . RosApi (
104
+ scope ,
105
+ `${ event . key } _api_${ key } ` ,
106
+ {
107
+ apiName : replaceReference ( `${ event . name } _api_${ key } ` , context ) ,
108
+ groupId : apiGatewayGroup . attrGroupId ,
109
+ visibility : 'PRIVATE' ,
110
+ authType : 'ANONYMOUS' ,
111
+ requestConfig : {
112
+ requestProtocol : 'HTTP' ,
113
+ requestHttpMethod : replaceReference ( trigger . method , context ) ,
114
+ requestPath : replaceReference ( trigger . path , context ) ,
115
+ requestMode : 'PASSTHROUGH' ,
116
+ } ,
117
+ serviceConfig : {
118
+ serviceProtocol : 'FunctionCompute' ,
119
+ functionComputeConfig : {
120
+ fcRegionId : context . region ,
121
+ functionName : replaceReference ( trigger . backend , context ) ,
122
+ roleArn : gatewayAccessRole . attrArn ,
123
+ fcVersion : '3.0' ,
124
+ method : replaceReference ( trigger . method , context ) ,
111
125
} ,
112
- resultSample : 'ServerlessInsight resultSample' ,
113
- resultType : 'PASSTHROUGH' ,
114
- tags : replaceReference ( tags , context ) ,
115
126
} ,
116
- true ,
117
- ) ;
118
- api . addDependsOn ( apiGatewayGroup ) ;
127
+ resultSample : 'ServerlessInsight resultSample' ,
128
+ resultType : 'PASSTHROUGH' ,
129
+ tags : replaceReference ( tags , context ) ,
130
+ } ,
131
+ true ,
132
+ ) ;
133
+ api . addDependsOn ( apiGatewayGroup ) ;
119
134
120
- new agw . Deployment ( scope , `${ service } _deployment` , {
121
- apiId : api . attrApiId ,
122
- groupId : apiGatewayGroup . attrGroupId ,
123
- stageName : 'RELEASE' ,
124
- description : `${ service } Api Gateway deployment` ,
125
- } ) ;
135
+ new agw . Deployment ( scope , `${ service } _deployment` , {
136
+ apiId : api . attrApiId ,
137
+ groupId : apiGatewayGroup . attrGroupId ,
138
+ stageName : 'RELEASE' ,
139
+ description : `${ service } Api Gateway deployment` ,
126
140
} ) ;
127
141
} ) ;
128
- }
142
+ } ) ;
129
143
} ;
0 commit comments