Skip to content

Commit 733aeb9

Browse files
committed
feat: passing parameters to cloud #5
Signed-off-by: seven <zilisheng1996@gmail.com>
1 parent 93d178e commit 733aeb9

File tree

6 files changed

+46
-23
lines changed

6 files changed

+46
-23
lines changed

src/commands/deploy.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { deployStack } from '../stack';
2-
import { printer } from '../common';
2+
import { printer, constructActionContext } from '../common';
33
import { parseYaml } from '../iac';
4-
import { constructActionContext } from '../common/actionContext';
54

6-
export const deploy = async (stackName: string, options: { location: string }) => {
7-
const context = constructActionContext({ location: options.location });
8-
printer.info(`Deploying stack context: ${JSON.stringify(context)}...`);
5+
export const deploy = async (
6+
stackName: string,
7+
options: { location: string; parameters: { [key: string]: string } },
8+
) => {
9+
const context = constructActionContext(options);
910
printer.info('Validating yaml...');
1011
const iac = parseYaml(context.iacLocation);
1112
printer.success('Yaml is valid! 🎉');

src/commands/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,19 @@ program
3535
.command('deploy <stackName>')
3636
.description('deploy serverless Iac yaml')
3737
.option('-f, --file <path>', 'specify the yaml file')
38+
.option(
39+
'-p, --parameter <key=value>',
40+
'override parameters',
41+
(value, previous: { [key: string]: string }) => {
42+
const [key, val] = value.split('=');
43+
previous[key] = val;
44+
return previous;
45+
},
46+
{},
47+
)
3848
.action(async (stackName, options) => {
3949
logger.debug('log command info');
40-
await deploy(stackName, { location: options.file });
50+
await deploy(stackName, { location: options.file, parameters: options.parameter });
4151
});
4252

4353
program.parse();

src/common/actionContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const constructActionContext = (config?: {
88
accessKeySecret?: string;
99
securityToken?: string;
1010
location?: string;
11+
parameters?: { [key: string]: string };
1112
}): ActionContext => {
1213
return {
1314
region:
@@ -21,5 +22,6 @@ export const constructActionContext = (config?: {
2122
? path.resolve(projectRoot, config?.location)
2223
: path.resolve(projectRoot, 'serverless-insight.yml');
2324
})(),
25+
parameters: Object.entries(config?.parameters ?? {}).map(([key, value]) => ({ key, value })),
2426
};
2527
};

src/common/rosClient.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ROS20190910, {
55
CreateStackRequestTags,
66
ListStacksRequest,
77
UpdateStackRequest,
8+
UpdateStackRequestParameters,
89
} from '@alicloud/ros20190910';
910
import { Config } from '@alicloud/openapi-client';
1011
import { ActionContext } from '../types';
@@ -46,12 +47,18 @@ const createStack = async (stackName: string, templateBody: unknown, context: Ac
4647
const updateStack = async (stackId: string, templateBody: unknown, context: ActionContext) => {
4748
const parameters = context.parameters?.map(
4849
(parameter) =>
49-
new CreateStackRequestParameters({
50+
new UpdateStackRequestParameters({
5051
parameterKey: Util.assertAsString(parameter.key),
51-
parameterValue: Util.assertAsString(parameter.value),
52+
parameterValue: Util.assertAsString(parameter.key),
5253
}),
5354
);
54-
55+
console.log(
56+
'parameters:',
57+
JSON.stringify({
58+
parameters,
59+
contextParam: context.parameters,
60+
}),
61+
);
5562
const createStackRequest = new UpdateStackRequest({
5663
regionId: context.region,
5764
stackId,

src/stack/deploy.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as ros from '@alicloud/ros-cdk-core';
2+
import { RosParameterType } from '@alicloud/ros-cdk-core';
23
import * as fc from '@alicloud/ros-cdk-fc';
34
import * as agw from '@alicloud/ros-cdk-apigateway';
45
import * as ram from '@alicloud/ros-cdk-ram';
@@ -23,7 +24,15 @@ export class IacStack extends ros.Stack {
2324
return acc;
2425
}, {}),
2526
});
26-
console.log('tags', iac.tags);
27+
28+
Object.entries(iac.vars).map(
29+
([key, value]) =>
30+
new ros.RosParameter(this, key, {
31+
type: RosParameterType.STRING,
32+
defaultValue: value,
33+
}),
34+
);
35+
2736
new ros.RosInfo(this, ros.RosInfo.description, `${iac.service} stack`);
2837

2938
const service = new fc.RosService(
@@ -153,23 +162,17 @@ const generateStackTemplate = (stackName: string, iac: ServerlessIac, context: A
153162

154163
const assembly = app.synth();
155164
const stackArtifact = assembly.getStackByName(stackName);
156-
const parameters = Object.entries(stackArtifact.parameters).map(([key, value]) => ({
157-
key,
158-
value,
159-
}));
160165

161-
return { template: stackArtifact.template, parameters };
166+
return { template: stackArtifact.template };
162167
};
163168

164169
export const deployStack = async (
165170
stackName: string,
166171
iac: ServerlessIac,
167172
context: ActionContext,
168173
) => {
169-
printer.info(`Deploying stack... ${JSON.stringify(iac)}`);
170-
171-
const { template, parameters } = generateStackTemplate(stackName, iac, context);
172-
console.log('Generated ROS YAML:', JSON.stringify({ template, parameters }));
173-
await rosStackDeploy(stackName, template, { ...context, parameters });
174+
const { template } = generateStackTemplate(stackName, iac, context);
175+
console.log('Generated ROS YAML:', JSON.stringify({ template }));
176+
await rosStackDeploy(stackName, template, context);
174177
printer.info(`Stack deployed! 🎉`);
175178
};

tests/fixtures/serverless-insignt.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ provider: aliyun
33

44
vars:
55
region: cn-hangzhou
6-
account_id: 1234567890
6+
testv: testVarValue
77

88
stages:
99
dev:
10-
region: ${vars:region}
11-
account_id: ${vars:account_id}
10+
region: ${vars.region}
1211

1312
service: insight-poc
1413

@@ -25,6 +24,7 @@ functions:
2524
timeout: 10
2625
environment:
2726
NODE_ENV: production
27+
TEST_VAR: ${vars.testv}
2828

2929

3030
events:

0 commit comments

Comments
 (0)