11import * as ros from '@alicloud/ros-cdk-core' ;
22import { ActionContext , EventDomain , EventTypes , ServerlessIac } from '../../types' ;
33import * as ram from '@alicloud/ros-cdk-ram' ;
4- import { encodeBase64ForRosId , replaceReference } from '../../common' ;
4+ import { encodeBase64ForRosId , replaceReference , splitDomain } from '../../common' ;
55import * as agw from '@alicloud/ros-cdk-apigateway' ;
66import { isEmpty } from 'lodash' ;
7+ import * as dns from '@alicloud/ros-cdk-dns' ;
78
89export const resolveEvents = (
910 scope : ros . Construct ,
@@ -16,12 +17,14 @@ export const resolveEvents = (
1617 return undefined ;
1718 }
1819 const apiGateway = events ! . filter ( ( event ) => event . type === EventTypes . API_GATEWAY ) ;
19- if ( apiGateway ?. length ) {
20+ if ( ! apiGateway ?. length ) return ;
21+
22+ apiGateway . forEach ( ( event ) => {
2023 const gatewayAccessRole = new ram . RosRole (
2124 scope ,
22- replaceReference ( `${ service } _role` , context ) ,
25+ replaceReference ( `${ event . key } _role` , context ) ,
2326 {
24- roleName : replaceReference ( `${ service } -gateway -access-role` , context ) ,
27+ roleName : replaceReference ( `${ service } -${ event . name } -agw -access-role` , context ) ,
2528 description : replaceReference ( `${ service } role` , context ) ,
2629 assumeRolePolicyDocument : {
2730 version : '1' ,
@@ -37,7 +40,7 @@ export const resolveEvents = (
3740 } ,
3841 policies : [
3942 {
40- policyName : replaceReference ( `${ service } -policy` , context ) ,
43+ policyName : replaceReference ( `${ service } -${ event . name } - policy` , context ) ,
4144 policyDocument : {
4245 version : '1' ,
4346 statement : [
@@ -66,64 +69,75 @@ export const resolveEvents = (
6669 true ,
6770 ) ;
6871
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 ) ;
8175
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+ } ) ;
8782
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 ) ,
111125 } ,
112- resultSample : 'ServerlessInsight resultSample' ,
113- resultType : 'PASSTHROUGH' ,
114- tags : replaceReference ( tags , context ) ,
115126 } ,
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 ) ;
119134
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` ,
126140 } ) ;
127141 } ) ;
128- }
142+ } ) ;
129143} ;
0 commit comments