Skip to content

Commit

Permalink
fix(hcl2cdk): adjust module import name and path
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Oct 4, 2021
1 parent 1037d9c commit 456f742
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 40 deletions.
6 changes: 4 additions & 2 deletions packages/@cdktf/hcl2cdk/lib/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
);
}

Expand Down
20 changes: 13 additions & 7 deletions packages/@cdktf/hcl2cdk/lib/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -517,12 +520,15 @@ export const providerImports = (providers: string[]) =>
export const moduleImports = (modules: Record<string, Module> | 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, {
Expand Down
53 changes: 22 additions & 31 deletions packages/@cdktf/hcl2cdk/test/__snapshots__/hcl2cdk.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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\\",
});
Expand All @@ -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\\",
});
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -830,16 +826,15 @@ 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\\",
});
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}}\`,
});
Expand Down Expand Up @@ -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,
});
Expand Down
2 changes: 2 additions & 0 deletions packages/@cdktf/hcl2cdk/test/convertProject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions packages/@cdktf/provider-generator/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

0 comments on commit 456f742

Please sign in to comment.