Skip to content

Commit

Permalink
feat: passing parameters to cloud #5
Browse files Browse the repository at this point in the history
Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll committed Sep 22, 2024
1 parent 93d178e commit 733aeb9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 23 deletions.
11 changes: 6 additions & 5 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { deployStack } from '../stack';
import { printer } from '../common';
import { printer, constructActionContext } from '../common';
import { parseYaml } from '../iac';
import { constructActionContext } from '../common/actionContext';

export const deploy = async (stackName: string, options: { location: string }) => {
const context = constructActionContext({ location: options.location });
printer.info(`Deploying stack context: ${JSON.stringify(context)}...`);
export const deploy = async (
stackName: string,
options: { location: string; parameters: { [key: string]: string } },
) => {
const context = constructActionContext(options);
printer.info('Validating yaml...');
const iac = parseYaml(context.iacLocation);
printer.success('Yaml is valid! 🎉');
Expand Down
12 changes: 11 additions & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,19 @@ program
.command('deploy <stackName>')
.description('deploy serverless Iac yaml')
.option('-f, --file <path>', 'specify the yaml file')
.option(
'-p, --parameter <key=value>',
'override parameters',
(value, previous: { [key: string]: string }) => {
const [key, val] = value.split('=');
previous[key] = val;
return previous;
},
{},
)
.action(async (stackName, options) => {
logger.debug('log command info');
await deploy(stackName, { location: options.file });
await deploy(stackName, { location: options.file, parameters: options.parameter });
});

program.parse();
2 changes: 2 additions & 0 deletions src/common/actionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const constructActionContext = (config?: {
accessKeySecret?: string;
securityToken?: string;
location?: string;
parameters?: { [key: string]: string };
}): ActionContext => {
return {
region:
Expand All @@ -21,5 +22,6 @@ export const constructActionContext = (config?: {
? path.resolve(projectRoot, config?.location)
: path.resolve(projectRoot, 'serverless-insight.yml');
})(),
parameters: Object.entries(config?.parameters ?? {}).map(([key, value]) => ({ key, value })),
};
};
13 changes: 10 additions & 3 deletions src/common/rosClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ROS20190910, {
CreateStackRequestTags,
ListStacksRequest,
UpdateStackRequest,
UpdateStackRequestParameters,
} from '@alicloud/ros20190910';
import { Config } from '@alicloud/openapi-client';
import { ActionContext } from '../types';
Expand Down Expand Up @@ -46,12 +47,18 @@ const createStack = async (stackName: string, templateBody: unknown, context: Ac
const updateStack = async (stackId: string, templateBody: unknown, context: ActionContext) => {
const parameters = context.parameters?.map(
(parameter) =>
new CreateStackRequestParameters({
new UpdateStackRequestParameters({
parameterKey: Util.assertAsString(parameter.key),
parameterValue: Util.assertAsString(parameter.value),
parameterValue: Util.assertAsString(parameter.key),
}),
);

console.log(
'parameters:',
JSON.stringify({
parameters,
contextParam: context.parameters,
}),
);
const createStackRequest = new UpdateStackRequest({
regionId: context.region,
stackId,
Expand Down
25 changes: 14 additions & 11 deletions src/stack/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ros from '@alicloud/ros-cdk-core';
import { RosParameterType } from '@alicloud/ros-cdk-core';
import * as fc from '@alicloud/ros-cdk-fc';
import * as agw from '@alicloud/ros-cdk-apigateway';
import * as ram from '@alicloud/ros-cdk-ram';
Expand All @@ -23,7 +24,15 @@ export class IacStack extends ros.Stack {
return acc;
}, {}),
});
console.log('tags', iac.tags);

Object.entries(iac.vars).map(
([key, value]) =>
new ros.RosParameter(this, key, {
type: RosParameterType.STRING,
defaultValue: value,
}),
);

new ros.RosInfo(this, ros.RosInfo.description, `${iac.service} stack`);

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

const assembly = app.synth();
const stackArtifact = assembly.getStackByName(stackName);
const parameters = Object.entries(stackArtifact.parameters).map(([key, value]) => ({
key,
value,
}));

return { template: stackArtifact.template, parameters };
return { template: stackArtifact.template };
};

export const deployStack = async (
stackName: string,
iac: ServerlessIac,
context: ActionContext,
) => {
printer.info(`Deploying stack... ${JSON.stringify(iac)}`);

const { template, parameters } = generateStackTemplate(stackName, iac, context);
console.log('Generated ROS YAML:', JSON.stringify({ template, parameters }));
await rosStackDeploy(stackName, template, { ...context, parameters });
const { template } = generateStackTemplate(stackName, iac, context);
console.log('Generated ROS YAML:', JSON.stringify({ template }));
await rosStackDeploy(stackName, template, context);
printer.info(`Stack deployed! 🎉`);
};
6 changes: 3 additions & 3 deletions tests/fixtures/serverless-insignt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ provider: aliyun

vars:
region: cn-hangzhou
account_id: 1234567890
testv: testVarValue

stages:
dev:
region: ${vars:region}
account_id: ${vars:account_id}
region: ${vars.region}

service: insight-poc

Expand All @@ -25,6 +24,7 @@ functions:
timeout: 10
environment:
NODE_ENV: production
TEST_VAR: ${vars.testv}


events:
Expand Down

0 comments on commit 733aeb9

Please sign in to comment.