Skip to content

Commit 6b96451

Browse files
committed
feat: transform stages to mapping #5
Signed-off-by: seven <[email protected]>
1 parent 45ba9dd commit 6b96451

File tree

3 files changed

+51
-27
lines changed

3 files changed

+51
-27
lines changed

src/common/actionContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ export const constructActionContext = (config?: {
99
securityToken?: string;
1010
location?: string;
1111
parameters?: { [key: string]: string };
12+
stage?: string;
1213
}): ActionContext => {
1314
return {
15+
stage: config?.stage ?? 'default',
1416
region:
1517
config?.region ?? process.env.ROS_REGION_ID ?? process.env.ALIYUN_REGION ?? 'cn-hangzhou',
1618
accessKeyId: config?.accessKeyId ?? (process.env.ALIYUN_ACCESS_KEY_ID as string),

src/stack/iacStack.ts

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,39 @@ const resolveCode = (location: string): string => {
1414
return fileContent.toString('base64');
1515
};
1616

17-
const replaceVars = <T>(value: T): T => {
17+
const replaceVars = <T>(value: T, stage: string): T => {
1818
if (typeof value === 'string') {
1919
const matchVar = value.match(/^\$\{vars\.(\w+)}$/);
2020
const containsVar = value.match(/\$\{vars\.(\w+)}/);
21+
const matchMap = value.match(/^\$\{stages\.(\w+)}$/);
22+
const containsMap = value.match(/\$\{stages\.(\w+)}/);
2123
if (matchVar?.length) {
2224
return ros.Fn.ref(matchVar[1]) as T;
2325
}
26+
if (matchMap?.length) {
27+
return ros.Fn.findInMap('stages', '', matchMap[1]) as T;
28+
}
29+
if (containsMap?.length && containsVar?.length) {
30+
return ros.Fn.sub(
31+
value.replace(/\$\{stages\.(\w+)}/g, '${$1}').replace(/\$\{vars\.(\w+)}/g, '${$1}'),
32+
) as T;
33+
}
2434
if (containsVar?.length) {
2535
return ros.Fn.sub(value.replace(/\$\{vars\.(\w+)}/g, '${$1}')) as T;
2636
}
37+
if (containsMap?.length) {
38+
return ros.Fn.sub(value.replace(/\$\{stages\.(\w+)}/g, '${$1}')) as T;
39+
}
2740
return value;
2841
}
2942

3043
if (Array.isArray(value)) {
31-
return value.map(replaceVars) as T;
44+
return value.map((item) => replaceVars(item, stage)) as T;
3245
}
3346

3447
if (typeof value === 'object' && value !== null) {
3548
return Object.fromEntries(
36-
Object.entries(value).map(([key, val]) => [key, replaceVars(val)]),
49+
Object.entries(value).map(([key, val]) => [key, replaceVars(val, stage)]),
3750
) as T;
3851
}
3952

@@ -44,27 +57,35 @@ export class IacStack extends ros.Stack {
4457
constructor(scope: ros.Construct, iac: ServerlessIac, context: ActionContext) {
4558
super(scope, iac.service, {
4659
tags: iac.tags.reduce((acc: { [key: string]: string }, tag) => {
47-
acc[tag.key] = replaceVars(tag.value);
60+
acc[tag.key] = replaceVars(tag.value, context.stage);
4861
return acc;
4962
}, {}),
5063
});
5164

65+
// Define Parameters
5266
Object.entries(iac.vars).map(
5367
([key, value]) =>
5468
new ros.RosParameter(this, key, {
5569
type: RosParameterType.STRING,
5670
defaultValue: value,
5771
}),
5872
);
73+
console.log('stages:', iac.stages);
74+
// Define Mappings
75+
new ros.RosMapping(this, 'stages', { mapping: replaceVars(iac.stages, context.stage) });
5976

60-
new ros.RosInfo(this, ros.RosInfo.description, replaceVars(`${iac.service} stack`));
77+
new ros.RosInfo(
78+
this,
79+
ros.RosInfo.description,
80+
replaceVars(`${iac.service} stack`, context.stage),
81+
);
6182

6283
const service = new fc.RosService(
6384
this,
64-
replaceVars(`${iac.service}-service`),
85+
replaceVars(`${iac.service}-service`, context.stage),
6586
{
66-
serviceName: replaceVars(`${iac.service}-service`),
67-
tags: replaceVars(iac.tags),
87+
serviceName: replaceVars(`${iac.service}-service`, context.stage),
88+
tags: replaceVars(iac.tags, context.stage),
6889
},
6990
true,
7091
);
@@ -74,13 +95,13 @@ export class IacStack extends ros.Stack {
7495
this,
7596
fnc.key,
7697
{
77-
functionName: replaceVars(fnc.name),
78-
serviceName: replaceVars(service.serviceName),
79-
handler: replaceVars(fnc.handler),
80-
runtime: replaceVars(fnc.runtime),
81-
memorySize: replaceVars(fnc.memory),
82-
timeout: replaceVars(fnc.timeout),
83-
environmentVariables: replaceVars(fnc.environment),
98+
functionName: replaceVars(fnc.name, context.stage),
99+
serviceName: replaceVars(service.serviceName, context.stage),
100+
handler: replaceVars(fnc.handler, context.stage),
101+
runtime: replaceVars(fnc.runtime, context.stage),
102+
memorySize: replaceVars(fnc.memory, context.stage),
103+
timeout: replaceVars(fnc.timeout, context.stage),
104+
environmentVariables: replaceVars(fnc.environment, context.stage),
84105
code: {
85106
zipFile: resolveCode(fnc.code),
86107
},
@@ -94,10 +115,10 @@ export class IacStack extends ros.Stack {
94115
if (apiGateway) {
95116
const gatewayAccessRole = new ram.RosRole(
96117
this,
97-
replaceVars(`${iac.service}_role`),
118+
replaceVars(`${iac.service}_role`, context.stage),
98119
{
99-
roleName: replaceVars(`${iac.service}-gateway-access-role`),
100-
description: replaceVars(`${iac.service} role`),
120+
roleName: replaceVars(`${iac.service}-gateway-access-role`, context.stage),
121+
description: replaceVars(`${iac.service} role`, context.stage),
101122
assumeRolePolicyDocument: {
102123
version: '1',
103124
statement: [
@@ -112,7 +133,7 @@ export class IacStack extends ros.Stack {
112133
},
113134
policies: [
114135
{
115-
policyName: replaceVars(`${iac.service}-policy`),
136+
policyName: replaceVars(`${iac.service}-policy`, context.stage),
116137
policyDocument: {
117138
version: '1',
118139
statement: [
@@ -132,10 +153,10 @@ export class IacStack extends ros.Stack {
132153

133154
const apiGatewayGroup = new agw.RosGroup(
134155
this,
135-
replaceVars(`${iac.service}_apigroup`),
156+
replaceVars(`${iac.service}_apigroup`, context.stage),
136157
{
137-
groupName: replaceVars(`${iac.service}_apigroup`),
138-
tags: replaceVars(iac.tags),
158+
groupName: replaceVars(`${iac.service}_apigroup`, context.stage),
159+
tags: replaceVars(iac.tags, context.stage),
139160
},
140161
true,
141162
);
@@ -148,15 +169,15 @@ export class IacStack extends ros.Stack {
148169

149170
const api = new agw.RosApi(
150171
this,
151-
replaceVars(`${event.key}_api_${key}`),
172+
replaceVars(`${event.key}_api_${key}`, context.stage),
152173
{
153-
apiName: replaceVars(`${event.name}_api_${key}`),
174+
apiName: replaceVars(`${event.name}_api_${key}`, context.stage),
154175
groupId: apiGatewayGroup.attrGroupId,
155176
visibility: 'PRIVATE',
156177
requestConfig: {
157178
requestProtocol: 'HTTP',
158-
requestHttpMethod: replaceVars(trigger.method),
159-
requestPath: replaceVars(trigger.path),
179+
requestHttpMethod: replaceVars(trigger.method, context.stage),
180+
requestPath: replaceVars(trigger.path, context.stage),
160181
requestMode: 'PASSTHROUGH',
161182
},
162183
serviceConfig: {
@@ -170,7 +191,7 @@ export class IacStack extends ros.Stack {
170191
},
171192
resultSample: 'ServerlessInsight resultSample',
172193
resultType: 'JSON',
173-
tags: replaceVars(iac.tags),
194+
tags: replaceVars(iac.tags, context.stage),
174195
},
175196
true,
176197
);

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export type ServerlessIac = {
6969
};
7070

7171
export type ActionContext = {
72+
stage: string;
7273
region: string;
7374
accessKeyId: string;
7475
accessKeySecret: string;

0 commit comments

Comments
 (0)