Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hcl2cdk): Fix duplicate modules #1025

Merged
merged 3 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions packages/@cdktf/hcl2cdk/lib/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,23 @@ export const providerImports = (providers: string[]) =>
)() as t.Statement;
});

export const moduleImports = (modules: Record<string, Module> | undefined) =>
Object.values(modules || {}).map(([module]) => {
const moduleConstraint = new TerraformModuleConstraint(module.source);
return template.ast(
`import * as ${moduleConstraint.className} from "./.gen/modules/${moduleConstraint.fileName}"`
) as t.Statement;
export const moduleImports = (modules: Record<string, Module> | undefined) => {
const uniqueModules = new Set<string>();
Object.values(modules || {}).map(([module]) =>
uniqueModules.add(module.source)
);

const imports: t.Statement[] = [];
uniqueModules.forEach((m) => {
const moduleConstraint = new TerraformModuleConstraint(m);
imports.push(
template.ast(
`import * as ${moduleConstraint.className} from "./.gen/modules/${moduleConstraint.fileName}"`
) as t.Statement
);
});
return imports;
};

export function gen(statements: t.Statement[]) {
return prettier.format(generate(t.program(statements) as any).code, {
Expand Down
31 changes: 17 additions & 14 deletions packages/@cdktf/hcl2cdk/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,23 @@ ${JSON.stringify((err as z.ZodError).errors)}`);
);

// We collect all module sources
const moduleRequirements =
Object.values(plan.module || {}).reduce(
(carry, moduleBlock) => [
...carry,
...moduleBlock.reduce(
(arr, { source, version }) => [
...arr,
version ? `${source}@${version}` : source,
],
[] as string[]
),
],
[] as string[]
) || [];
const moduleRequirements = [
...new Set(
Object.values(plan.module || {}).reduce(
(carry, moduleBlock) => [
...carry,
...moduleBlock.reduce(
(arr, { source, version }) => [
...arr,
version ? `${source}@${version}` : source,
],
[] as string[]
),
],
[] as string[]
) || []
),
];

// Variables, Outputs, and Backends are defined in the CDKTF project so we need to import from it
// If none are used we don't want to leave a stray import
Expand Down
11 changes: 11 additions & 0 deletions packages/@cdktf/hcl2cdk/test/__snapshots__/hcl2cdk.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,17 @@ new aws.S3.S3Bucket(this, \\"examplebucket\\", {
"
`;

exports[`convert duplicate modules configuration 1`] = `
"import * as Vpc from \\"./.gen/modules/terraform-aws-modules/aws/vpc\\";
new Vpc.Vpc(this, \\"vpca\\", {
name: \\"my-vpc-a\\",
});
new Vpc.Vpc(this, \\"vpcb\\", {
name: \\"my-vpc-b\\",
});
"
`;

exports[`convert dynamic blocks configuration 1`] = `
"import * as cdktf from \\"cdktf\\";

Expand Down
15 changes: 15 additions & 0 deletions packages/@cdktf/hcl2cdk/test/hcl2cdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,21 @@ describe("convert", () => {
}
}`,
],
[
"duplicate modules",
`
module "vpca" {
source = "terraform-aws-modules/vpc/aws"

name = "my-vpc-a"
}

module "vpcb" {
source = "terraform-aws-modules/vpc/aws"

name = "my-vpc-b"
}`,
],
[
"referenced modules",
`
Expand Down