From d4cf33e4b8b99a3bf8389b623722c4a2fa950b63 Mon Sep 17 00:00:00 2001 From: seven Date: Sun, 15 Dec 2024 00:19:14 +0800 Subject: [PATCH] feat: enable new provider huawei (#20) feat: enable new provider huawei #20 - serverlessInsight is able to specify new provider `huawei` - basic terraform template are generate Refs: #19 --------- Signed-off-by: seven --- package-lock.json | 172 +++++++++++++++++-- src/commands/template.ts | 17 +- src/common/provider.ts | 6 +- src/parser/index.ts | 3 +- src/stack/deploy.ts | 36 +++- src/stack/rfsStack/function.ts | 38 ++++ src/stack/rfsStack/index.ts | 42 +++++ src/stack/rosStack/index.ts | 34 ++++ src/stack/rosStack/rosStack.ts | 34 ---- src/types/index.ts | 19 +- tests/fixtures/deployFixture.ts | 11 +- tests/fixtures/serverless-insight-huawei.yml | 61 +++++++ 12 files changed, 397 insertions(+), 76 deletions(-) create mode 100644 src/stack/rfsStack/function.ts create mode 100644 src/stack/rfsStack/index.ts create mode 100644 src/stack/rosStack/index.ts create mode 100644 tests/fixtures/serverless-insight-huawei.yml diff --git a/package-lock.json b/package-lock.json index 65fad0d..2ea4a27 100644 --- a/package-lock.json +++ b/package-lock.json @@ -124,6 +124,14 @@ "constructs": "^3.0.4" } }, + "node_modules/@alicloud/ros-cdk-apigateway/node_modules/constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==", + "engines": { + "node": ">= 16.14.0" + } + }, "node_modules/@alicloud/ros-cdk-assembly-schema": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/@alicloud/ros-cdk-assembly-schema/-/ros-cdk-assembly-schema-1.4.0.tgz", @@ -213,6 +221,14 @@ "inBundle": true, "license": "MIT" }, + "node_modules/@alicloud/ros-cdk-core/node_modules/constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==", + "engines": { + "node": ">= 16.14.0" + } + }, "node_modules/@alicloud/ros-cdk-core/node_modules/fs-extra": { "version": "9.1.0", "inBundle": true, @@ -309,6 +325,14 @@ "constructs": "^3.0.4" } }, + "node_modules/@alicloud/ros-cdk-elasticsearchserverless/node_modules/constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==", + "engines": { + "node": ">= 16.14.0" + } + }, "node_modules/@alicloud/ros-cdk-fc": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/@alicloud/ros-cdk-fc/-/ros-cdk-fc-1.4.0.tgz", @@ -326,6 +350,14 @@ "constructs": "^3.0.4" } }, + "node_modules/@alicloud/ros-cdk-fc/node_modules/constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==", + "engines": { + "node": ">= 16.14.0" + } + }, "node_modules/@alicloud/ros-cdk-fc3": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/@alicloud/ros-cdk-fc3/-/ros-cdk-fc3-1.4.0.tgz", @@ -339,6 +371,14 @@ "constructs": "^3.0.4" } }, + "node_modules/@alicloud/ros-cdk-fc3/node_modules/constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==", + "engines": { + "node": ">= 16.14.0" + } + }, "node_modules/@alicloud/ros-cdk-oss": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/@alicloud/ros-cdk-oss/-/ros-cdk-oss-1.4.0.tgz", @@ -352,6 +392,14 @@ "constructs": "^3.0.4" } }, + "node_modules/@alicloud/ros-cdk-oss/node_modules/constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==", + "engines": { + "node": ">= 16.14.0" + } + }, "node_modules/@alicloud/ros-cdk-ossassets": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/@alicloud/ros-cdk-ossassets/-/ros-cdk-ossassets-1.4.0.tgz", @@ -457,6 +505,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/@alicloud/ros-cdk-ossassets/node_modules/constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==", + "engines": { + "node": ">= 16.14.0" + } + }, "node_modules/@alicloud/ros-cdk-ossassets/node_modules/content-type": { "version": "1.0.5", "inBundle": true, @@ -1287,6 +1343,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/@alicloud/ros-cdk-ossdeployment/node_modules/constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==", + "engines": { + "node": ">= 16.14.0" + } + }, "node_modules/@alicloud/ros-cdk-ossdeployment/node_modules/content-type": { "version": "1.0.5", "inBundle": true, @@ -2020,6 +2084,14 @@ "constructs": "^3.0.4" } }, + "node_modules/@alicloud/ros-cdk-ram/node_modules/constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==", + "engines": { + "node": ">= 16.14.0" + } + }, "node_modules/@alicloud/ros-cdk-ros": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/@alicloud/ros-cdk-ros/-/ros-cdk-ros-1.4.0.tgz", @@ -2033,6 +2105,14 @@ "constructs": "^3.0.4" } }, + "node_modules/@alicloud/ros-cdk-ros/node_modules/constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==", + "engines": { + "node": ">= 16.14.0" + } + }, "node_modules/@alicloud/ros-cdk-sls": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/@alicloud/ros-cdk-sls/-/ros-cdk-sls-1.4.0.tgz", @@ -2046,6 +2126,14 @@ "constructs": "^3.0.4" } }, + "node_modules/@alicloud/ros-cdk-sls/node_modules/constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==", + "engines": { + "node": ">= 16.14.0" + } + }, "node_modules/@alicloud/ros20190910": { "version": "3.5.2", "resolved": "https://registry.npmjs.org/@alicloud/ros20190910/-/ros20190910-3.5.2.tgz", @@ -4517,14 +4605,6 @@ "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", "dev": true }, - "node_modules/constructs": { - "version": "3.4.344", - "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", - "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==", - "engines": { - "node": ">= 16.14.0" - } - }, "node_modules/convert-source-map": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz", @@ -8103,6 +8183,13 @@ "requires": { "@alicloud/ros-cdk-core": "^1.0.27", "constructs": "^3.0.4" + }, + "dependencies": { + "constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==" + } } }, "@alicloud/ros-cdk-assembly-schema": { @@ -8162,6 +8249,11 @@ "version": "0.0.1", "bundled": true }, + "constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==" + }, "fs-extra": { "version": "9.1.0", "bundled": true, @@ -8223,6 +8315,13 @@ "requires": { "@alicloud/ros-cdk-core": "^1.0.27", "constructs": "^3.0.4" + }, + "dependencies": { + "constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==" + } } }, "@alicloud/ros-cdk-fc": { @@ -8234,6 +8333,13 @@ "@alicloud/ros-cdk-oss": "^1.0.25", "@alicloud/ros-cdk-ossassets": "^1.0.25", "constructs": "^3.0.4" + }, + "dependencies": { + "constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==" + } } }, "@alicloud/ros-cdk-fc3": { @@ -8243,6 +8349,13 @@ "requires": { "@alicloud/ros-cdk-core": "^1.0.27", "constructs": "^3.0.4" + }, + "dependencies": { + "constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==" + } } }, "@alicloud/ros-cdk-oss": { @@ -8252,6 +8365,13 @@ "requires": { "@alicloud/ros-cdk-core": "^1.0.27", "constructs": "^3.0.4" + }, + "dependencies": { + "constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==" + } } }, "@alicloud/ros-cdk-ossassets": { @@ -8330,6 +8450,11 @@ "set-function-length": "^1.2.1" } }, + "constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==" + }, "content-type": { "version": "1.0.5", "bundled": true @@ -8879,6 +9004,11 @@ "set-function-length": "^1.2.1" } }, + "constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==" + }, "content-type": { "version": "1.0.5", "bundled": true @@ -9354,6 +9484,13 @@ "requires": { "@alicloud/ros-cdk-core": "^1.0.27", "constructs": "^3.0.4" + }, + "dependencies": { + "constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==" + } } }, "@alicloud/ros-cdk-ros": { @@ -9363,6 +9500,13 @@ "requires": { "@alicloud/ros-cdk-core": "^1.0.27", "constructs": "^3.0.4" + }, + "dependencies": { + "constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==" + } } }, "@alicloud/ros-cdk-sls": { @@ -9372,6 +9516,13 @@ "requires": { "@alicloud/ros-cdk-core": "^1.0.27", "constructs": "^3.0.4" + }, + "dependencies": { + "constructs": { + "version": "3.4.344", + "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", + "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==" + } } }, "@alicloud/ros20190910": { @@ -11251,11 +11402,6 @@ "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", "dev": true }, - "constructs": { - "version": "3.4.344", - "resolved": "https://registry.npmjs.org/constructs/-/constructs-3.4.344.tgz", - "integrity": "sha512-Qq3upn44oGdvgasHUKWVFsrynyYrtVRd9fd8ko9cJOrFzx9eCm3iI4bhBryQqaISdausbTYUOXmoEe/YSJ16Nw==" - }, "convert-source-map": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz", diff --git a/src/commands/template.ts b/src/commands/template.ts index 24d4a25..50f2673 100644 --- a/src/commands/template.ts +++ b/src/commands/template.ts @@ -10,12 +10,15 @@ export const template = ( ) => { const context = constructActionContext({ ...options, stackName }); const iac = parseYaml(context); - const { template } = generateStackTemplate(stackName, iac, context); - - const output = - options.format === TemplateFormat.JSON - ? JSON.stringify(template, null, 2) - : yaml.stringify(template); - logger.info(`\n${output}`); + const { template } = generateStackTemplate(stackName, iac, context); + if (typeof template === 'string') { + logger.info(`\n${template}`); + } else { + const output = + options.format === TemplateFormat.JSON + ? JSON.stringify(template, null, 2) + : yaml.stringify(template); + logger.info(`\n${output}`); + } }; diff --git a/src/common/provider.ts b/src/common/provider.ts index 366cb0a..67af4b2 100644 --- a/src/common/provider.ts +++ b/src/common/provider.ts @@ -1,6 +1,6 @@ -export enum PROVIDER { - HUAWEI = 'HUAWEI', - ALIBABA = 'ALIBABA', +export enum Provider { + HUAWEI = 'huawei', + ALIYUN = 'aliyun', // TENCENT = 'TENCENT', // AWS = 'AZURE', // AZURE = 'AZURE', diff --git a/src/parser/index.ts b/src/parser/index.ts index 5dcd13e..9e9cd65 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -6,6 +6,7 @@ import { parseDatabase } from './databaseParser'; import { parseTag } from './tagParser'; import { parse } from 'yaml'; import { validateYaml } from '../validator'; +import { Provider } from '../common'; const validateExistence = (path: string) => { if (!existsSync(path)) { @@ -17,7 +18,7 @@ const transformYaml = (iacJson: ServerlessIacRaw): ServerlessIac => { return { service: iacJson.service, version: iacJson.version, - provider: iacJson.provider, + provider: iacJson.provider as Provider, vars: iacJson.vars, stages: iacJson.stages, functions: parseFunction(iacJson.functions), diff --git a/src/stack/deploy.ts b/src/stack/deploy.ts index 359c374..93f8d3c 100644 --- a/src/stack/deploy.ts +++ b/src/stack/deploy.ts @@ -1,9 +1,11 @@ import * as ros from '@alicloud/ros-cdk-core'; + import { ActionContext, ServerlessIac } from '../types'; -import { logger, rosStackDeploy } from '../common'; -import { RosStack } from './rosStack/rosStack'; +import { logger, Provider, rosStackDeploy } from '../common'; +import { RosStack } from './rosStack'; +import { RfsStack } from './rfsStack'; -export const generateStackTemplate = ( +export const generateRosStackTemplate = ( stackName: string, iac: ServerlessIac, context: ActionContext, @@ -17,13 +19,39 @@ export const generateStackTemplate = ( return { template: stackArtifact.template }; }; +export const generateRfsStackTemplate = ( + stackName: string, + iac: ServerlessIac, + context: ActionContext, +) => { + const stack = new RfsStack(iac, context); + + const hcl = stack.toHclTerraform(); + console.log('HCL:', hcl); + + return { template: hcl }; +}; + export const deployStack = async ( stackName: string, iac: ServerlessIac, context: ActionContext, ) => { - const { template } = generateStackTemplate(stackName, iac, context); + const { template } = generateRosStackTemplate(stackName, iac, context); await rosStackDeploy(stackName, template, context); logger.info(`Stack deployed! 🎉`); }; + +export const generateStackTemplate = ( + stackName: string, + iac: ServerlessIac, + context: ActionContext, +): { template: unknown } => { + if (iac.provider === Provider.ALIYUN) { + return generateRosStackTemplate(stackName, iac, context); + } else if (iac.provider === Provider.HUAWEI) { + return generateRfsStackTemplate(stackName, iac, context); + } + return { template: '' }; +}; diff --git a/src/stack/rfsStack/function.ts b/src/stack/rfsStack/function.ts new file mode 100644 index 0000000..44aa246 --- /dev/null +++ b/src/stack/rfsStack/function.ts @@ -0,0 +1,38 @@ +import { ActionContext, FunctionDomain } from '../../types'; +import { resolveCode } from '../../common'; +import { RfsStack } from './index'; + +const fgsApplication = (context: ActionContext, service: string) => ` +resource "huaweicloud_fgs_application" "${service}_app" { + name = "${service}-app" + description = "${service} application" + package_type = "event" +} +`; + +const fgsFunction = (fn: FunctionDomain, context: ActionContext, service: string) => ` +resource "huaweicloud_fgs_function" "${fn.key}" { + name = "${fn.name}" + handler = "${fn.handler}" + runtime = "${fn.runtime}" + memory_size = ${fn.memory} + timeout = ${fn.timeout} + environment = ${JSON.stringify(fn.environment)} + code_type = "inline" + func_code = "${resolveCode(fn.code)}" + app = "huaweicloud_fgs_application.${service}_app.id" +} +`; + +export const resolveFunction = ( + stack: RfsStack, + functions: Array | undefined, + context: ActionContext, + service: string, +) => { + if (!functions) { + return undefined; + } + const app = fgsApplication(context, service); + stack.appendHcl(app + '\n' + functions.map((fn) => fgsFunction(fn, context, service)).join('\n')); +}; diff --git a/src/stack/rfsStack/index.ts b/src/stack/rfsStack/index.ts new file mode 100644 index 0000000..5b637fe --- /dev/null +++ b/src/stack/rfsStack/index.ts @@ -0,0 +1,42 @@ +import { ActionContext, ServerlessIac } from '../../types'; +import { resolveFunction } from './function'; + +const provider = (stack: RfsStack, context: ActionContext) => { + const hcl = ` +terraform { + required_providers { + huaweicloud = { + source = "huaweicloud/huaweicloud" + version = ">= 1.67.1" + } + } +} + +provider "huaweicloud" { + region = "${context.region}" + access_key = "${context.accessKeyId}" + secret_key = "${context.accessKeySecret}" +} + `; + stack.appendHcl(hcl); +}; + +export class RfsStack { + private hcl: string = ''; + + constructor( + private readonly iac: ServerlessIac, + private readonly context: ActionContext, + ) { + provider(this, context); + resolveFunction(this, iac.functions, context, iac.service); + } + + public toHclTerraform() { + return this.hcl; + } + + public appendHcl(hcl: string) { + this.hcl += hcl; + } +} diff --git a/src/stack/rosStack/index.ts b/src/stack/rosStack/index.ts new file mode 100644 index 0000000..3d5de16 --- /dev/null +++ b/src/stack/rosStack/index.ts @@ -0,0 +1,34 @@ +import * as ros from '@alicloud/ros-cdk-core'; +import { ActionContext, ServerlessIac } from '../../types'; +import { replaceReference } from '../../common'; +import { resolveTags } from './tag'; +import { resolveFunctions } from './function'; +import { resolveStages } from './stage'; +import { resloveVars } from './vars'; +import { resolveDatabases } from './database'; +import { resolveEvents } from './event'; + +export class RosStack extends ros.Stack { + private readonly service: string; + + constructor(scope: ros.Construct, iac: ServerlessIac, context: ActionContext) { + super(scope, replaceReference(iac.service, context), { + stackName: context.stackName, + tags: resolveTags(iac.tags, context), + }); + + this.service = replaceReference(iac.service, context); + new ros.RosInfo(this, ros.RosInfo.description, `${this.service} stack`); + + // Define Parameters + resloveVars(this, iac.vars); + // Define Mappings + resolveStages(this, iac.stages, context); + // Define functions + resolveFunctions(this, iac.functions, context, this.service); + // Define Events + resolveEvents(this, iac.events, iac.tags, context, this.service); + // Define Databases + resolveDatabases(this, iac.databases, context); + } +} diff --git a/src/stack/rosStack/rosStack.ts b/src/stack/rosStack/rosStack.ts index 3d5de16..e69de29 100644 --- a/src/stack/rosStack/rosStack.ts +++ b/src/stack/rosStack/rosStack.ts @@ -1,34 +0,0 @@ -import * as ros from '@alicloud/ros-cdk-core'; -import { ActionContext, ServerlessIac } from '../../types'; -import { replaceReference } from '../../common'; -import { resolveTags } from './tag'; -import { resolveFunctions } from './function'; -import { resolveStages } from './stage'; -import { resloveVars } from './vars'; -import { resolveDatabases } from './database'; -import { resolveEvents } from './event'; - -export class RosStack extends ros.Stack { - private readonly service: string; - - constructor(scope: ros.Construct, iac: ServerlessIac, context: ActionContext) { - super(scope, replaceReference(iac.service, context), { - stackName: context.stackName, - tags: resolveTags(iac.tags, context), - }); - - this.service = replaceReference(iac.service, context); - new ros.RosInfo(this, ros.RosInfo.description, `${this.service} stack`); - - // Define Parameters - resloveVars(this, iac.vars); - // Define Mappings - resolveStages(this, iac.stages, context); - // Define functions - resolveFunctions(this, iac.functions, context, this.service); - // Define Events - resolveEvents(this, iac.events, iac.tags, context, this.service); - // Define Databases - resolveDatabases(this, iac.databases, context); - } -} diff --git a/src/types/index.ts b/src/types/index.ts index dd5d222..504c7e7 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -3,10 +3,18 @@ import { Tags } from './domains/tag'; import { EventDomain, EventRaw } from './domains/event'; import { DatabaseDomain, DatabaseRaw } from './domains/database'; import { FunctionDomain, FunctionRaw } from './domains/function'; +import { Provider } from '../common'; + +export * from './domains/database'; +export * from './domains/event'; +export * from './domains/function'; +export * from './domains/tag'; +export * from './domains/vars'; +export * from './domains/context'; export type ServerlessIacRaw = { version: string; - provider: string; + provider: Provider; vars: Vars; stages: Stages; service: string; @@ -18,7 +26,7 @@ export type ServerlessIacRaw = { export type ServerlessIac = { version: string; - provider: string; + provider: Provider; service: string; vars?: Vars; stages?: Stages; @@ -27,10 +35,3 @@ export type ServerlessIac = { events?: Array; databases?: Array; }; - -export * from './domains/database'; -export * from './domains/event'; -export * from './domains/function'; -export * from './domains/tag'; -export * from './domains/vars'; -export * from './domains/context'; diff --git a/tests/fixtures/deployFixture.ts b/tests/fixtures/deployFixture.ts index 544b7ec..7cfdbeb 100644 --- a/tests/fixtures/deployFixture.ts +++ b/tests/fixtures/deployFixture.ts @@ -1,10 +1,11 @@ import { DatabaseEnum, ServerlessIac } from '../../src/types'; import { cloneDeep, set } from 'lodash'; +import { Provider } from '../../src/common'; export const oneFcOneGatewayIac = { service: 'my-demo-service', version: '0.0.1', - provider: 'aliyun', + provider: 'aliyun' as Provider, vars: { region: 'cn-hangzhou', account_id: 1234567890, @@ -239,7 +240,7 @@ export const referredServiceRos = { export const minimumIac = { service: 'my-demo-minimum-service', version: '0.0.1', - provider: 'aliyun', + provider: 'aliyun' as Provider, functions: [ { @@ -272,7 +273,7 @@ export const minimumRos = { export const oneFcIac = { service: 'my-demo-service', version: '0.0.1', - provider: 'aliyun', + provider: 'aliyun' as Provider, vars: { region: 'cn-hangzhou', account_id: 1234567890, @@ -340,7 +341,7 @@ export const oneFcRos = { export const oneFcIacWithStage = { service: 'my-demo-service', version: '0.0.1', - provider: 'aliyun', + provider: 'aliyun' as Provider, vars: { region: 'cn-hangzhou', account_id: 1234567890, @@ -751,7 +752,7 @@ export const defaultContext = { export const esServerlessMinimumIac: ServerlessIac = { service: 'my-demo-es-serverless-service', version: '0.0.1', - provider: 'aliyun', + provider: 'aliyun' as Provider as Provider, databases: [ { key: 'insight_es_db_test', diff --git a/tests/fixtures/serverless-insight-huawei.yml b/tests/fixtures/serverless-insight-huawei.yml new file mode 100644 index 0000000..7dc6d08 --- /dev/null +++ b/tests/fixtures/serverless-insight-huawei.yml @@ -0,0 +1,61 @@ +version: 0.0.1 +provider: huawei + +vars: + region: cn-hangzhou + testv: testVarValue + handler: index.handler + +stages: + default: + region: ${vars.region} + node_env: default + dev: + region: ${vars.region} + node_env: development + prod: + region: cn-shanghai + +service: insight-poc + +tags: + owner: geek-fun + +functions: + insight_poc_fn: + name: insight-poc-fn + runtime: nodejs18 + handler: ${vars.handler} + code: tests/fixtures/artifacts/artifact.zip + memory: 512 + timeout: 10 + environment: + NODE_ENV: ${stages.node_env} + TEST_VAR: ${vars.testv} + TEST_VAR_EXTRA: abcds-${vars.testv}-andyou + +#databases: +# insight_es_db: +# name: insight-poc-es +# type: ELASTICSEARCH_SERVERLESS +# version: 7.10 +# engine_mode: SEARCH +# security: +# basic_auth: +# password: 123456 +# cu: 1 +# storage_size: 20 + +events: + gateway_event: + type: API_GATEWAY + name: insight-poc-gateway + triggers: + - method: GET + path: /api/hello + backend: ${functions.insight_poc_fn} +# custom_domain: +# domain_name: test.com +# certificate_name: test +# certificate_private_key: test +# certificate_body: test