From 93d178efe8943d80c61e20dcb6a885819fb383b9 Mon Sep 17 00:00:00 2001 From: seven Date: Sun, 22 Sep 2024 14:56:11 +0800 Subject: [PATCH] feat: add tags to defined resources Signed-off-by: seven --- src/iac/parse.ts | 9 ++++++++- src/stack/deploy.ts | 11 ++++++++++- src/types.ts | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/iac/parse.ts b/src/iac/parse.ts index 06eb401..bd4925e 100644 --- a/src/iac/parse.ts +++ b/src/iac/parse.ts @@ -9,6 +9,10 @@ const mapToArr = (obj: Record | string>) => { ); }; +const mapToKvArr = (obj: Record) => { + return Object.entries(obj).map(([key, value]) => ({ key, value })); +}; + const validateExistence = (path: string) => { if (!existsSync(path)) { throw new Error(`File does not exist at path: ${path}`); @@ -24,7 +28,10 @@ const transformYaml = (iacJson: RawServerlessIac): ServerlessIac => { stages: iacJson.stages, functions: mapToArr(iacJson.functions) as unknown as Array, events: mapToArr(iacJson.events) as unknown as Array, - tags: mapToArr(iacJson.tags) as unknown as Array, + tags: [ + { key: 'iac-provider', value: 'ServerlessInsight' }, + ...mapToKvArr(iacJson.tags), + ] as unknown as Array<{ key: string; value: string }>, }; }; diff --git a/src/stack/deploy.ts b/src/stack/deploy.ts index 76c94e2..0f35e7c 100644 --- a/src/stack/deploy.ts +++ b/src/stack/deploy.ts @@ -17,7 +17,13 @@ const resolveCode = (location: string): string => { export class IacStack extends ros.Stack { constructor(scope: ros.Construct, iac: ServerlessIac, context: ActionContext) { - super(scope, iac.service); + super(scope, iac.service, { + tags: iac.tags.reduce((acc: { [key: string]: string }, tag) => { + acc[tag.key] = tag.value; + return acc; + }, {}), + }); + console.log('tags', iac.tags); new ros.RosInfo(this, ros.RosInfo.description, `${iac.service} stack`); const service = new fc.RosService( @@ -25,6 +31,7 @@ export class IacStack extends ros.Stack { `${iac.service}-service`, { serviceName: `${iac.service}-service`, + tags: iac.tags, }, true, ); @@ -95,6 +102,7 @@ export class IacStack extends ros.Stack { `${iac.service}_apigroup`, { groupName: `${iac.service}_apigroup`, + tags: iac.tags, }, true, ); @@ -128,6 +136,7 @@ export class IacStack extends ros.Stack { }, resultSample: 'ServerlessInsight resultSample', resultType: 'JSON', + tags: iac.tags, }, true, ); diff --git a/src/types.ts b/src/types.ts index c35ec13..fed566a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -63,7 +63,7 @@ export type ServerlessIac = { vars: Vars; stages: Stages; service: string; - tags: Array; + tags: Array<{ key: string; value: string }>; functions: Array; events: Array; };