1- import { ActionContext , FunctionDomain , ServerlessIac } from '../../types' ;
1+ import { ActionContext , FunctionDomain , NasStorageClassEnum , ServerlessIac } from '../../types' ;
22import {
33 CODE_ZIP_SIZE_LIMIT ,
4+ encodeBase64ForRosId ,
45 getFileSource ,
56 readCodeSize ,
67 replaceReference ,
@@ -11,8 +12,43 @@ import { isEmpty } from 'lodash';
1112import * as ossDeployment from '@alicloud/ros-cdk-ossdeployment' ;
1213import * as ros from '@alicloud/ros-cdk-core' ;
1314import * as sls from '@alicloud/ros-cdk-sls' ;
15+ import * as nas from '@alicloud/ros-cdk-nas' ;
16+ import * as ecs from '@alicloud/ros-cdk-ecs' ;
1417import { RosFunction } from '@alicloud/ros-cdk-fc3/lib/fc3.generated' ;
1518
19+ const storageClassMap = {
20+ [ NasStorageClassEnum . STANDARD_CAPACITY ] : { fileSystemType : 'standard' , storageType : 'Capacity' } ,
21+ [ NasStorageClassEnum . STANDARD_PERFORMANCE ] : {
22+ fileSystemType : 'standard' ,
23+ storageType : 'Performance' ,
24+ } ,
25+ [ NasStorageClassEnum . EXTREME_STANDARD ] : { fileSystemType : 'extreme' , storageType : 'standard' } ,
26+ [ NasStorageClassEnum . EXTREME_ADVANCE ] : { fileSystemType : 'extreme' , storageType : 'advance' } ,
27+ } ;
28+ const securityGroupRangeMap : { [ key : string ] : string } = {
29+ TCP : '1/65535' ,
30+ UDP : '1/65535' ,
31+ ICMP : '-1/-1' ,
32+ GRE : '-1/-1' ,
33+ ALL : '-1/-1' ,
34+ } ;
35+ const transformSecurityRules = ( rules : Array < string > , ruleType : 'INGRESS' | 'EGRESS' ) => {
36+ return rules . map ( ( rule ) => {
37+ const [ protocol , cidrIp , portRange ] = rule . split ( ':' ) ;
38+
39+ return {
40+ ipProtocol : protocol . toLowerCase ( ) ,
41+ portRange :
42+ portRange . toUpperCase ( ) === 'ALL'
43+ ? securityGroupRangeMap [ protocol . toUpperCase ( ) ]
44+ : portRange . includes ( '/' )
45+ ? portRange
46+ : `${ portRange } /${ portRange } ` ,
47+ [ ruleType === 'INGRESS' ? 'sourceCidrIp' : 'destCidrIp' ] : cidrIp ,
48+ } ;
49+ } ) ;
50+ } ;
51+
1652export const resolveFunctions = (
1753 scope : ros . Construct ,
1854 functions : Array < FunctionDomain > | undefined ,
@@ -102,6 +138,79 @@ export const resolveFunctions = (
102138 ) ?. objectKey ,
103139 } ;
104140 }
141+
142+ let vpcConfig : fc . RosFunction . VpcConfigProperty | undefined = undefined ;
143+ if ( fnc . network ) {
144+ const securityGroup = new ecs . SecurityGroup (
145+ scope ,
146+ `${ fnc . key } _security_group` ,
147+ {
148+ securityGroupName : fnc . network . security_group . name ,
149+ vpcId : replaceReference ( fnc . network . vpc_id , context ) ,
150+ tags : replaceReference ( tags , context ) ,
151+ securityGroupIngress : transformSecurityRules (
152+ fnc . network . security_group . ingress ,
153+ 'INGRESS' ,
154+ ) ,
155+ securityGroupEgress : transformSecurityRules ( fnc . network . security_group . egress , 'EGRESS' ) ,
156+ } ,
157+ true ,
158+ ) ;
159+
160+ vpcConfig = {
161+ vpcId : replaceReference ( fnc . network . vpc_id , context ) ,
162+ vSwitchIds : replaceReference ( fnc . network . subnet_ids , context ) ,
163+ securityGroupId : securityGroup . attrSecurityGroupId ,
164+ } ;
165+ }
166+
167+ let fcNas :
168+ | Array < { nas : nas . FileSystem ; nasMount : nas . MountTarget ; mountDir : string } >
169+ | undefined ;
170+ if ( fnc . storage ?. nas ) {
171+ fcNas = fnc . storage . nas . map ( ( nasItem ) => {
172+ const { fileSystemType, storageType } = storageClassMap [ nasItem . storage_class ] ;
173+ const accessGroup = new nas . AccessGroup (
174+ scope ,
175+ `${ fnc . key } _nas_access_${ encodeBase64ForRosId ( nasItem . mount_path ) } ` ,
176+ {
177+ accessGroupName : `${ fnc . name } -nas-access-${ encodeBase64ForRosId ( nasItem . mount_path ) } ` ,
178+ accessGroupType : 'Vpc' ,
179+ } ,
180+ true ,
181+ ) ;
182+
183+ const nasResource = new nas . FileSystem (
184+ scope ,
185+ `${ fnc . key } _nas_${ encodeBase64ForRosId ( nasItem . mount_path ) } ` ,
186+ {
187+ fileSystemType,
188+ storageType,
189+ protocolType : 'NFS' ,
190+ tags : [
191+ ...( replaceReference ( tags , context ) ?? [ ] ) ,
192+ { key : 'function-name' , value : fnc . name } ,
193+ ] ,
194+ } ,
195+ true ,
196+ ) ;
197+ const nasMountTarget = new nas . MountTarget (
198+ scope ,
199+ `${ fnc . key } _nas_mount_${ encodeBase64ForRosId ( nasItem . mount_path ) } ` ,
200+ {
201+ fileSystemId : nasResource . attrFileSystemId ,
202+ networkType : 'Vpc' ,
203+ accessGroupName : accessGroup . attrAccessGroupName ,
204+ vpcId : fnc . network ! . vpc_id ,
205+ vSwitchId : fnc . network ! . subnet_ids [ 0 ] ,
206+ } ,
207+ true ,
208+ ) ;
209+
210+ return { nas : nasResource , nasMount : nasMountTarget , mountDir : nasItem . mount_path } ;
211+ } ) ;
212+ }
213+
105214 const fcn = new fc . RosFunction (
106215 scope ,
107216 fnc . key ,
@@ -111,9 +220,19 @@ export const resolveFunctions = (
111220 runtime : replaceReference ( fnc . runtime , context ) ,
112221 memorySize : replaceReference ( fnc . memory , context ) ,
113222 timeout : replaceReference ( fnc . timeout , context ) ,
223+ diskSize : fnc . storage ?. disk ,
114224 environmentVariables : replaceReference ( fnc . environment , context ) ,
115225 code,
116226 logConfig,
227+ vpcConfig,
228+ nasConfig : fcNas ?. length
229+ ? {
230+ mountPoints : fcNas ?. map ( ( { nasMount, mountDir } ) => ( {
231+ mountDir,
232+ serverAddr : `${ nasMount . attrMountTargetDomain } :/` ,
233+ } ) ) ,
234+ }
235+ : undefined ,
117236 } ,
118237 true ,
119238 ) ;
@@ -126,5 +245,10 @@ export const resolveFunctions = (
126245 if ( storeInBucket ) {
127246 fcn . addRosDependency ( `${ service } _artifacts_code_deployment` ) ;
128247 }
248+ if ( fcNas ?. length ) {
249+ fcNas . forEach ( ( nasItem ) => {
250+ fcn . addRosDependency ( `${ fnc . key } _nas_mount_${ encodeBase64ForRosId ( nasItem . mountDir ) } ` ) ;
251+ } ) ;
252+ }
129253 } ) ;
130254} ;
0 commit comments