From 456f742f9ba8779dd903c142b468808000355912 Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Mon, 4 Oct 2021 19:41:11 +0200 Subject: [PATCH] fix(hcl2cdk): adjust module import name and path --- packages/@cdktf/hcl2cdk/lib/expressions.ts | 6 ++- packages/@cdktf/hcl2cdk/lib/generation.ts | 20 ++++--- .../test/__snapshots__/hcl2cdk.test.ts.snap | 53 ++++++++----------- .../hcl2cdk/test/convertProject.test.ts | 2 + .../@cdktf/provider-generator/lib/index.ts | 1 + 5 files changed, 42 insertions(+), 40 deletions(-) diff --git a/packages/@cdktf/hcl2cdk/lib/expressions.ts b/packages/@cdktf/hcl2cdk/lib/expressions.ts index cb8b6d4b8d..f7050fb631 100644 --- a/packages/@cdktf/hcl2cdk/lib/expressions.ts +++ b/packages/@cdktf/hcl2cdk/lib/expressions.ts @@ -3,6 +3,7 @@ import reservedWords from "reserved-words"; import { camelCase, pascalCase } from "./utils"; import { TerraformResourceBlock, Scope } from "./types"; import { getResourceNamespace } from "@cdktf/provider-generator"; +import { TerraformModuleConstraint } from "@cdktf/provider-generator"; export type Reference = { start: number; @@ -224,9 +225,10 @@ export function variableName( export function constructAst(type: string, isModuleImport: boolean) { if (isModuleImport) { + const tfMod = new TerraformModuleConstraint(type); return t.memberExpression( - t.identifier(pascalCase(type)), - t.identifier(pascalCase(type)) + t.identifier(pascalCase(tfMod.name)), + t.identifier(pascalCase(tfMod.name)) ); } diff --git a/packages/@cdktf/hcl2cdk/lib/generation.ts b/packages/@cdktf/hcl2cdk/lib/generation.ts index 482b1a113b..3c55d71452 100644 --- a/packages/@cdktf/hcl2cdk/lib/generation.ts +++ b/packages/@cdktf/hcl2cdk/lib/generation.ts @@ -3,7 +3,10 @@ import template from "@babel/template"; import * as t from "@babel/types"; import { DirectedGraph } from "graphology"; import prettier from "prettier"; -import { isRegistryModule } from "@cdktf/provider-generator"; +import { + isRegistryModule, + TerraformModuleConstraint, +} from "@cdktf/provider-generator"; import { TerraformResourceBlock, Scope } from "./types"; import { camelCase, pascalCase, uniqueId } from "./utils"; @@ -517,12 +520,15 @@ export const providerImports = (providers: string[]) => export const moduleImports = (modules: Record | undefined) => Object.values(modules || {}) .filter(([{ source }]) => isRegistryModule(source)) - .map( - ([{ source }]) => - template( - `import * as ${pascalCase(source)} from "./.gen/modules/${source}"` - )() as t.Statement - ); + .map(([{ source }]) => { + const mod = new TerraformModuleConstraint(source); + const fullName = mod.namespace + ? `${mod.namespace}/${mod.name}` + : mod.name; + return template( + `import * as %%importName%% from "./.gen/modules/${fullName}"` + )({ importName: t.identifier(pascalCase(mod.name)) }) as t.Statement; + }); export function gen(statements: t.Statement[]) { return prettier.format(generate(t.program(statements) as any).code, { diff --git a/packages/@cdktf/hcl2cdk/test/__snapshots__/hcl2cdk.test.ts.snap b/packages/@cdktf/hcl2cdk/test/__snapshots__/hcl2cdk.test.ts.snap index 1689479bff..7bbc95eef9 100644 --- a/packages/@cdktf/hcl2cdk/test/__snapshots__/hcl2cdk.test.ts.snap +++ b/packages/@cdktf/hcl2cdk/test/__snapshots__/hcl2cdk.test.ts.snap @@ -33,8 +33,8 @@ new auth0.Auth0Provider(this, \\"auth0_2\\", { exports[`convert all module types configuration 1`] = ` "import * as cdktf from \\"cdktf\\"; -import * as AppTerraformIoExampleCorpK8SClusterAzurerm from \\"./.gen/modules/app.terraform.io/example-corp/k8s-cluster/azurerm\\"; -import * as HashicorpConsulAws from \\"./.gen/modules/hashicorp/consul/aws\\"; +import * as K8SCluster from \\"./.gen/modules/example-corp/azurerm/k8s-cluster\\"; +import * as Consul from \\"./.gen/modules/hashicorp/aws/consul\\"; new cdktf.TerraformHclModule(this, \\"consul\\", { source: \\"./consul\\", }); @@ -47,12 +47,8 @@ new cdktf.TerraformHclModule(this, \\"consul-git\\", { new cdktf.TerraformHclModule(this, \\"consul-github\\", { source: \\"github.com/hashicorp/example\\", }); -new AppTerraformIoExampleCorpK8SClusterAzurerm.AppTerraformIoExampleCorpK8SClusterAzurerm( - this, - \\"consul-hosted-registry\\", - {} -); -new HashicorpConsulAws.HashicorpConsulAws(this, \\"consul-registry\\", {}); +new K8SCluster.K8SCluster(this, \\"consul-hosted-registry\\", {}); +new Consul.Consul(this, \\"consul-registry\\", {}); new cdktf.TerraformHclModule(this, \\"storage\\", { source: \\"git::ssh://username@example.com/storage.git\\", }); @@ -674,8 +670,8 @@ new aws.S3.S3Bucket(this, \\"examplebucket\\", { `; exports[`convert modules configuration 1`] = ` -"import * as TerraformAwsModulesVpcAws from \\"./.gen/modules/terraform-aws-modules/vpc/aws\\"; -new TerraformAwsModulesVpcAws.TerraformAwsModulesVpcAws(this, \\"vpc\\", { +"import * as Vpc from \\"./.gen/modules/terraform-aws-modules/aws/vpc\\"; +new Vpc.Vpc(this, \\"vpc\\", { azs: [\\"eu-west-1a\\", \\"eu-west-1b\\", \\"eu-west-1c\\"], cidr: \\"10.0.0.0/16\\", enableNatGateway: true, @@ -830,7 +826,7 @@ exports[`convert provider alias configuration 1`] = ` "/*Provider bindings are generated by running cdktf get. See https://github.com/hashicorp/terraform-cdk/blob/main/docs/working-with-cdk-for-terraform/using-providers.md#importing-providers-and-modules for more details.*/ import * as aws from \\"./.gen/providers/aws\\"; -import * as TerraformAwsModulesVpcAws from \\"./.gen/modules/terraform-aws-modules/vpc/aws\\"; +import * as Vpc from \\"./.gen/modules/terraform-aws-modules/aws/vpc\\"; new aws.AwsProvider(this, \\"aws\\", { region: \\"us-east-1\\", }); @@ -838,8 +834,7 @@ new aws.AwsProvider(this, \\"aws_1\\", { alias: \\"west\\", region: \\"us-west-2\\", }); -const terraformAwsModulesVpcAwsVpc = - new TerraformAwsModulesVpcAws.TerraformAwsModulesVpcAws(this, \\"vpc\\", {}); +const terraformAwsModulesVpcAwsVpc = new Vpc.Vpc(this, \\"vpc\\", {}); terraformAwsModulesVpcAwsVpc.addOverride(\\"providers\\", { aws: \`\\\\\${\${awsWest.fqn}}\`, }); @@ -890,24 +885,20 @@ new auth0.Auth0Provider(this, \\"auth0\\", { exports[`convert referenced modules configuration 1`] = ` "import * as cdktf from \\"cdktf\\"; -import * as TerraformAwsModulesVpcAws from \\"./.gen/modules/terraform-aws-modules/vpc/aws\\"; -const vpc = new TerraformAwsModulesVpcAws.TerraformAwsModulesVpcAws( - this, - \\"vpc\\", - { - azs: [\\"eu-west-1a\\", \\"eu-west-1b\\", \\"eu-west-1c\\"], - cidr: \\"10.0.0.0/16\\", - enableNatGateway: true, - enableVpnGateway: true, - name: \\"my-vpc\\", - privateSubnets: [\\"10.0.1.0/24\\", \\"10.0.2.0/24\\", \\"10.0.3.0/24\\"], - publicSubnets: [\\"10.0.101.0/24\\", \\"10.0.102.0/24\\", \\"10.0.103.0/24\\"], - tags: { - environment: \\"dev\\", - terraform: \\"true\\", - }, - } -); +import * as Vpc from \\"./.gen/modules/terraform-aws-modules/aws/vpc\\"; +const vpc = new Vpc.Vpc(this, \\"vpc\\", { + azs: [\\"eu-west-1a\\", \\"eu-west-1b\\", \\"eu-west-1c\\"], + cidr: \\"10.0.0.0/16\\", + enableNatGateway: true, + enableVpnGateway: true, + name: \\"my-vpc\\", + privateSubnets: [\\"10.0.1.0/24\\", \\"10.0.2.0/24\\", \\"10.0.3.0/24\\"], + publicSubnets: [\\"10.0.101.0/24\\", \\"10.0.102.0/24\\", \\"10.0.103.0/24\\"], + tags: { + environment: \\"dev\\", + terraform: \\"true\\", + }, +}); new cdktf.TerraformOutput(this, \\"subnet_ids\\", { value: vpc.publicSubnetsOutput, }); diff --git a/packages/@cdktf/hcl2cdk/test/convertProject.test.ts b/packages/@cdktf/hcl2cdk/test/convertProject.test.ts index 16dcc3ae68..81099ca0de 100644 --- a/packages/@cdktf/hcl2cdk/test/convertProject.test.ts +++ b/packages/@cdktf/hcl2cdk/test/convertProject.test.ts @@ -192,6 +192,8 @@ describe("convertProject", () => { ], ]); + console.log({ importPath, targetPath }); + const previousPlan = getTerraformPlan(importPath); createTSCdkProject(targetPath); const mainTs = fs.readFileSync(path.resolve(targetPath, "main.ts"), "utf8"); diff --git a/packages/@cdktf/provider-generator/lib/index.ts b/packages/@cdktf/provider-generator/lib/index.ts index cb16137cb7..331da53bd3 100644 --- a/packages/@cdktf/provider-generator/lib/index.ts +++ b/packages/@cdktf/provider-generator/lib/index.ts @@ -2,3 +2,4 @@ export * from "./get/constructs-maker"; export * as config from "./config"; export { getResourceNamespace } from "./get/generator/constants/provider-namespaces"; export { isRegistryModule } from "./get/module"; +export { TerraformModuleConstraint } from "./config";