Skip to content

Commit c505391

Browse files
committed
feat: serverlessInsight generate function terraform hcl
Signed-off-by: seven <[email protected]>
1 parent c022f26 commit c505391

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/stack/rfsStack/function.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { ActionContext, FunctionDomain } from '../../types';
2+
3+
const fgsApplication = (context: ActionContext, service: string) => `
4+
resource "huaweicloud_fgs_application" "${service}_app" {
5+
name = "${service}-app"
6+
description = "${service} application"
7+
package_type = "event"
8+
}
9+
`;
10+
11+
const fgsFunction = (fn: FunctionDomain, context: ActionContext, service: string) => `
12+
resource "huaweicloud_fgs_function" "${fn.key}" {
13+
function_name = "${fn.name}"
14+
handler = "${fn.handler}"
15+
runtime = "${fn.runtime}"
16+
memory_size = ${fn.memory}
17+
timeout = ${fn.timeout}
18+
environment = ${JSON.stringify(fn.environment)}
19+
code = {
20+
package_type = "inline"
21+
code = <<EOF
22+
${fn.code}
23+
EOF
24+
}
25+
application = huaweicloud_fgs_application.${service}_app.id
26+
27+
}
28+
`;
29+
30+
export const resolveFunction = (
31+
functions: Array<FunctionDomain> | undefined,
32+
context: ActionContext,
33+
service: string,
34+
) => {
35+
if (!functions) {
36+
return undefined;
37+
}
38+
const app = fgsApplication(context, service);
39+
40+
return app + '\n' + functions.map((fn) => fgsFunction(fn, context, service)).join('\n');
41+
};

src/stack/rfsStack/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { ActionContext, ServerlessIac } from '../../types';
22

33
const provider = (context: ActionContext) => `
4+
terraform {
5+
required_providers {
6+
huaweicloud = {
7+
source = "huaweicloud/huaweicloud"
8+
version = ">= 1.71.2"
9+
}
10+
}
11+
}
12+
413
provider "huaweicloud" {
5-
version = ">= 1.36.0"
614
region = "${context.region}"
715
access_key = "${context.accessKeyId}"
816
secret_key = "${context.accessKeySecret}"

0 commit comments

Comments
 (0)