Skip to content

Commit 3e4ca95

Browse files
committed
feat: reference function
Signed-off-by: seven <zilisheng1996@gmail.com>
1 parent be209c4 commit 3e4ca95

File tree

6 files changed

+57
-39
lines changed

6 files changed

+57
-39
lines changed

package-lock.json

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@geek-fun/serverlessinsight",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "Full life cycle cross providers serverless application management for your fast-growing business.",
55
"homepage": "https://serverlessinsight.geekfun.club",
66
"main": "dist/src/index.js",

src/common/iacHelper.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@ export const resolveCode = (location: string): string => {
99
return fileContent.toString('base64');
1010
};
1111

12-
export const replaceVars = <T>(value: T, stage: string): T => {
12+
export const replaceReference = <T>(value: T, stage: string): T => {
1313
if (typeof value === 'string') {
1414
const matchVar = value.match(/^\$\{vars\.(\w+)}$/);
1515
const containsVar = value.match(/\$\{vars\.(\w+)}/);
1616
const matchMap = value.match(/^\$\{stages\.(\w+)}$/);
1717
const containsMap = value.match(/\$\{stages\.(\w+)}/);
18+
const matchFn = value.match(/^\$\{functions\.(\w+(\.\w+)?)}$/);
19+
1820
if (matchVar?.length) {
1921
return ros.Fn.ref(matchVar[1]) as T;
2022
}
2123
if (matchMap?.length) {
2224
return ros.Fn.findInMap('stages', '', matchMap[1]) as T;
2325
}
26+
27+
if (matchFn?.length) {
28+
return ros.Fn.getAtt(matchFn[1], 'FunctionName') as T;
29+
}
2430
if (containsMap?.length && containsVar?.length) {
2531
return ros.Fn.sub(
2632
value.replace(/\$\{stages\.(\w+)}/g, '${$1}').replace(/\$\{vars\.(\w+)}/g, '${$1}'),
@@ -36,12 +42,12 @@ export const replaceVars = <T>(value: T, stage: string): T => {
3642
}
3743

3844
if (Array.isArray(value)) {
39-
return value.map((item) => replaceVars(item, stage)) as T;
45+
return value.map((item) => replaceReference(item, stage)) as T;
4046
}
4147

4248
if (typeof value === 'object' && value !== null) {
4349
return Object.fromEntries(
44-
Object.entries(value).map(([key, val]) => [key, replaceVars(val, stage)]),
50+
Object.entries(value).map(([key, val]) => [key, replaceReference(val, stage)]),
4551
) as T;
4652
}
4753

src/stack/iacStack.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { ActionContext, EventTypes, ServerlessIac } from '../types';
44
import * as fc from '@alicloud/ros-cdk-fc3';
55
import * as ram from '@alicloud/ros-cdk-ram';
66
import * as agw from '@alicloud/ros-cdk-apigateway';
7-
import { replaceVars, resolveCode } from '../common';
7+
import { replaceReference, resolveCode } from '../common';
88

99
export class IacStack extends ros.Stack {
1010
constructor(scope: ros.Construct, iac: ServerlessIac, context: ActionContext) {
1111
super(scope, iac.service, {
1212
stackName: context.stackName,
1313
tags: iac.tags.reduce((acc: { [key: string]: string }, tag) => {
14-
acc[tag.key] = replaceVars(tag.value, context.stage);
14+
acc[tag.key] = replaceReference(tag.value, context.stage);
1515
return acc;
1616
}, {}),
1717
});
@@ -26,25 +26,25 @@ export class IacStack extends ros.Stack {
2626
);
2727

2828
// Define Mappings
29-
new ros.RosMapping(this, 'stages', { mapping: replaceVars(iac.stages, context.stage) });
29+
new ros.RosMapping(this, 'stages', { mapping: replaceReference(iac.stages, context.stage) });
3030

3131
new ros.RosInfo(
3232
this,
3333
ros.RosInfo.description,
34-
replaceVars(`${iac.service} stack`, context.stage),
34+
replaceReference(`${iac.service} stack`, context.stage),
3535
);
3636

3737
iac.functions.forEach((fnc) => {
3838
new fc.RosFunction(
3939
this,
4040
fnc.key,
4141
{
42-
functionName: replaceVars(fnc.name, context.stage),
43-
handler: replaceVars(fnc.handler, context.stage),
44-
runtime: replaceVars(fnc.runtime, context.stage),
45-
memorySize: replaceVars(fnc.memory, context.stage),
46-
timeout: replaceVars(fnc.timeout, context.stage),
47-
environmentVariables: replaceVars(fnc.environment, context.stage),
42+
functionName: replaceReference(fnc.name, context.stage),
43+
handler: replaceReference(fnc.handler, context.stage),
44+
runtime: replaceReference(fnc.runtime, context.stage),
45+
memorySize: replaceReference(fnc.memory, context.stage),
46+
timeout: replaceReference(fnc.timeout, context.stage),
47+
environmentVariables: replaceReference(fnc.environment, context.stage),
4848
code: {
4949
zipFile: resolveCode(fnc.code),
5050
},
@@ -57,10 +57,10 @@ export class IacStack extends ros.Stack {
5757
if (apiGateway?.length) {
5858
const gatewayAccessRole = new ram.RosRole(
5959
this,
60-
replaceVars(`${iac.service}_role`, context.stage),
60+
replaceReference(`${iac.service}_role`, context.stage),
6161
{
62-
roleName: replaceVars(`${iac.service}-gateway-access-role`, context.stage),
63-
description: replaceVars(`${iac.service} role`, context.stage),
62+
roleName: replaceReference(`${iac.service}-gateway-access-role`, context.stage),
63+
description: replaceReference(`${iac.service} role`, context.stage),
6464
assumeRolePolicyDocument: {
6565
version: '1',
6666
statement: [
@@ -75,7 +75,7 @@ export class IacStack extends ros.Stack {
7575
},
7676
policies: [
7777
{
78-
policyName: replaceVars(`${iac.service}-policy`, context.stage),
78+
policyName: replaceReference(`${iac.service}-policy`, context.stage),
7979
policyDocument: {
8080
version: '1',
8181
statement: [
@@ -95,10 +95,10 @@ export class IacStack extends ros.Stack {
9595

9696
const apiGatewayGroup = new agw.RosGroup(
9797
this,
98-
replaceVars(`${iac.service}_apigroup`, context.stage),
98+
replaceReference(`${iac.service}_apigroup`, context.stage),
9999
{
100-
groupName: replaceVars(`${iac.service}_apigroup`, context.stage),
101-
tags: replaceVars(iac.tags, context.stage),
100+
groupName: replaceReference(`${iac.service}_apigroup`, context.stage),
101+
tags: replaceReference(iac.tags, context.stage),
102102
},
103103
true,
104104
);
@@ -122,29 +122,29 @@ export class IacStack extends ros.Stack {
122122

123123
const api = new agw.RosApi(
124124
this,
125-
replaceVars(`${event.key}_api_${key}`, context.stage),
125+
replaceReference(`${event.key}_api_${key}`, context.stage),
126126
{
127-
apiName: replaceVars(`${event.name}_api_${key}`, context.stage),
127+
apiName: replaceReference(`${event.name}_api_${key}`, context.stage),
128128
groupId: apiGatewayGroup.attrGroupId,
129129
visibility: 'PRIVATE',
130130
requestConfig: {
131131
requestProtocol: 'HTTP',
132-
requestHttpMethod: replaceVars(trigger.method, context.stage),
133-
requestPath: replaceVars(trigger.path, context.stage),
132+
requestHttpMethod: replaceReference(trigger.method, context.stage),
133+
requestPath: replaceReference(trigger.path, context.stage),
134134
requestMode: 'PASSTHROUGH',
135135
},
136136
serviceConfig: {
137137
serviceProtocol: 'FunctionCompute',
138138
functionComputeConfig: {
139139
fcRegionId: context.region,
140-
functionName: trigger.backend,
140+
functionName: replaceReference(trigger.backend, trigger.backend),
141141
roleArn: gatewayAccessRole.attrArn,
142142
fcVersion: '3.0',
143143
},
144144
},
145145
resultSample: 'ServerlessInsight resultSample',
146146
resultType: 'JSON',
147-
tags: replaceVars(iac.tags, context.stage),
147+
tags: replaceReference(iac.tags, context.stage),
148148
},
149149
true,
150150
);

tests/fixtures/deployFixture.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export const oneFcOneGatewayIac = {
1010
},
1111
stages: {
1212
dev: {
13-
region: '${vars:region}',
14-
account_id: '${vars:account_id}',
13+
region: '${vars.region}',
14+
account_id: '${vars.account_id}',
1515
},
1616
},
1717
functions: [
@@ -37,7 +37,7 @@ export const oneFcOneGatewayIac = {
3737
{
3838
method: 'GET',
3939
path: '/api/hello',
40-
backend: 'demo_fn_gateway',
40+
backend: '${functions.hello_fn}',
4141
},
4242
],
4343
},
@@ -53,7 +53,12 @@ export const oneFcOneGatewayIac = {
5353
export const oneFcOneGatewayRos = {
5454
Description: 'my-demo-service stack',
5555
Mappings: {
56-
stages: { dev: { account_id: '${vars:account_id}', region: '${vars:region}' } },
56+
stages: {
57+
dev: {
58+
account_id: { Ref: 'account_id' },
59+
region: { Ref: 'region' },
60+
},
61+
},
5762
},
5863
Metadata: { 'ALIYUN::ROS::Interface': { TemplateTags: ['Create by ROS CDK'] } },
5964
Parameters: {
@@ -76,7 +81,7 @@ export const oneFcOneGatewayRos = {
7681
ResultType: 'JSON',
7782
ServiceConfig: {
7883
FunctionComputeConfig: {
79-
FunctionName: 'demo_fn_gateway',
84+
FunctionName: { 'Fn::GetAtt': ['hello_fn', 'FunctionName'] },
8085
RoleArn: { 'Fn::GetAtt': ['my-demo-service_role', 'Arn'] },
8186
FcVersion: '3.0',
8287
},
@@ -145,8 +150,8 @@ export const oneFcIac = {
145150
},
146151
stages: {
147152
dev: {
148-
region: '${vars:region}',
149-
account_id: '${vars:account_id}',
153+
region: '${vars.region}',
154+
account_id: '${vars.account_id}',
150155
},
151156
},
152157
functions: [
@@ -174,7 +179,12 @@ export const oneFcIac = {
174179
export const oneFcRos = {
175180
Description: 'my-demo-service stack',
176181
Mappings: {
177-
stages: { dev: { account_id: '${vars:account_id}', region: '${vars:region}' } },
182+
stages: {
183+
dev: {
184+
account_id: { Ref: 'account_id' },
185+
region: { Ref: 'region' },
186+
},
187+
},
178188
},
179189
Metadata: { 'ALIYUN::ROS::Interface': { TemplateTags: ['Create by ROS CDK'] } },
180190
Parameters: {

tests/fixtures/serverless-insight.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ events:
4848
triggers:
4949
- method: GET
5050
path: /api/hello
51-
backend: insight-poc-fn
51+
backend: ${functions.insight_poc_fn}
5252
# custom_domain:
5353
# domain_name: test.com
5454
# certificate_name: test

0 commit comments

Comments
 (0)