Skip to content

Commit

Permalink
refactor: format terraform template
Browse files Browse the repository at this point in the history
Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll committed Dec 14, 2024
1 parent c505391 commit ebf1857
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
19 changes: 8 additions & 11 deletions src/stack/rfsStack/function.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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" {
Expand All @@ -10,24 +12,20 @@ resource "huaweicloud_fgs_application" "${service}_app" {

const fgsFunction = (fn: FunctionDomain, context: ActionContext, service: string) => `
resource "huaweicloud_fgs_function" "${fn.key}" {
function_name = "${fn.name}"
name = "${fn.name}"
handler = "${fn.handler}"
runtime = "${fn.runtime}"
memory_size = ${fn.memory}
timeout = ${fn.timeout}
environment = ${JSON.stringify(fn.environment)}
code = {
package_type = "inline"
code = <<EOF
${fn.code}
EOF
}
application = huaweicloud_fgs_application.${service}_app.id
code_type = "inline"
func_code = "${resolveCode(fn.code)}"
app = "huaweicloud_fgs_application.${service}_app.id"
}
`;

export const resolveFunction = (
stack: RfsStack,
functions: Array<FunctionDomain> | undefined,
context: ActionContext,
service: string,
Expand All @@ -36,6 +34,5 @@ export const resolveFunction = (
return undefined;

Check warning on line 34 in src/stack/rfsStack/function.ts

View check run for this annotation

Codecov / codecov/patch

src/stack/rfsStack/function.ts#L34

Added line #L34 was not covered by tests
}
const app = fgsApplication(context, service);

return app + '\n' + functions.map((fn) => fgsFunction(fn, context, service)).join('\n');
stack.appendHcl(app + '\n' + functions.map((fn) => fgsFunction(fn, context, service)).join('\n'));

Check warning on line 37 in src/stack/rfsStack/function.ts

View check run for this annotation

Codecov / codecov/patch

src/stack/rfsStack/function.ts#L36-L37

Added lines #L36 - L37 were not covered by tests
};
18 changes: 14 additions & 4 deletions src/stack/rfsStack/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ActionContext, ServerlessIac } from '../../types';
import { resolveFunction } from './function';

const provider = (context: ActionContext) => `
const provider = (stack: RfsStack, context: ActionContext) => {
const hcl = `

Check warning on line 5 in src/stack/rfsStack/index.ts

View check run for this annotation

Codecov / codecov/patch

src/stack/rfsStack/index.ts#L5

Added line #L5 was not covered by tests
terraform {
required_providers {
huaweicloud = {
source = "huaweicloud/huaweicloud"
version = ">= 1.71.2"
version = ">= 1.67.1"
}
}
}
Expand All @@ -15,7 +17,9 @@ provider "huaweicloud" {
access_key = "${context.accessKeyId}"
secret_key = "${context.accessKeySecret}"
}
`;
`;
stack.appendHcl(hcl);

Check warning on line 21 in src/stack/rfsStack/index.ts

View check run for this annotation

Codecov / codecov/patch

src/stack/rfsStack/index.ts#L21

Added line #L21 was not covered by tests
};

export class RfsStack {
private hcl: string = '';

Check warning on line 25 in src/stack/rfsStack/index.ts

View check run for this annotation

Codecov / codecov/patch

src/stack/rfsStack/index.ts#L25

Added line #L25 was not covered by tests
Expand All @@ -24,9 +28,15 @@ export class RfsStack {
private readonly iac: ServerlessIac,
private readonly context: ActionContext,

Check warning on line 29 in src/stack/rfsStack/index.ts

View check run for this annotation

Codecov / codecov/patch

src/stack/rfsStack/index.ts#L28-L29

Added lines #L28 - L29 were not covered by tests
) {
this.hcl = provider(context);
provider(this, context);
resolveFunction(this, iac.functions, context, iac.service);

Check warning on line 32 in src/stack/rfsStack/index.ts

View check run for this annotation

Codecov / codecov/patch

src/stack/rfsStack/index.ts#L31-L32

Added lines #L31 - L32 were not covered by tests
}

public toHclTerraform() {
return this.hcl;

Check warning on line 36 in src/stack/rfsStack/index.ts

View check run for this annotation

Codecov / codecov/patch

src/stack/rfsStack/index.ts#L36

Added line #L36 was not covered by tests
}

public appendHcl(hcl: string) {
this.hcl += hcl;

Check warning on line 40 in src/stack/rfsStack/index.ts

View check run for this annotation

Codecov / codecov/patch

src/stack/rfsStack/index.ts#L40

Added line #L40 was not covered by tests
}
}

0 comments on commit ebf1857

Please sign in to comment.