Skip to content

Commit

Permalink
fix(lib): fix source path generated for local modules
Browse files Browse the repository at this point in the history
Closes #946
  • Loading branch information
DanielMSchmidt committed Sep 2, 2021
1 parent 337acff commit a81df11
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
40 changes: 13 additions & 27 deletions packages/cdktf/lib/terraform-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { deepMerge } from "./util";
import { ITerraformDependable } from "./terraform-dependable";
import { Token } from "./tokens";
import * as path from "path";
import { TerraformAsset } from "./terraform-asset";

export interface TerraformModuleOptions {
readonly source: string;
Expand All @@ -31,13 +32,20 @@ export abstract class TerraformModule
super(scope, id);

if (options.source.startsWith("./") || options.source.startsWith("../")) {
this.source = path.join("..", options.source);
throw new Error(
"Please use absolute paths as source for TerraformModules"
);
}

if (options.source.startsWith("/")) {
this.source = new TerraformAsset(scope, `local-module-${id}`, {
path: path.resolve(options.source),
}).path;
} else {
this.source = options.source;
}
this.version = options.version;
this._providers = options.providers;
this.validateIfProvidersHaveUniqueKeys();
if (Array.isArray(options.dependsOn)) {
this.dependsOn = options.dependsOn.map((dependency) => dependency.fqn);
}
Expand Down Expand Up @@ -65,7 +73,6 @@ export abstract class TerraformModule
this._providers = [];
}
this._providers.push(provider);
this.validateIfProvidersHaveUniqueKeys();
}

public toTerraform(): any {
Expand All @@ -74,17 +81,16 @@ export abstract class TerraformModule
...this.synthesizeAttributes(),
source: this.source,
version: this.version,
providers: this._providers?.reduce((a, p) => {
providers: this.providers?.map((p) => {
if (p instanceof TerraformProvider) {
return { ...a, [p.terraformResourceType]: p.fqn };
return { [p.terraformResourceType]: p.fqn };
} else {
return {
...a,
[`${p.provider.terraformResourceType}.${p.moduleAlias}`]:
p.provider.fqn,
};
}
}, {}),
}),
depends_on: this.dependsOn,
},
this.rawOverrides
Expand All @@ -110,24 +116,4 @@ export abstract class TerraformModule
},
};
}

private validateIfProvidersHaveUniqueKeys(): void {
const moduleAliases = this._providers?.map((p) => {
if (p instanceof TerraformProvider) {
return p.terraformResourceType;
} else {
return `${p.provider.terraformResourceType}.${p.moduleAlias}`;
}
});

const uniqueModuleAliases = new Set();
moduleAliases?.forEach((alias) => {
if (uniqueModuleAliases.has(alias)) {
throw new Error(
`Error: Multiple providers have the same alias: "${alias}"`
);
}
uniqueModuleAliases.add(alias);
});
}
}
3 changes: 3 additions & 0 deletions packages/cdktf/test/fixtures/hcl-module/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_vpc" "example" {
cidr_block = "10.1.0.0/16"
}
27 changes: 14 additions & 13 deletions packages/cdktf/test/terraform-hcl-module.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Testing, TerraformStack, TerraformHclModule } from "../lib";
import { TestProvider, TestResource } from "./helper";
import * as path from "path";

test("minimal configuration", () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");

new TerraformHclModule(stack, "test", {
source: "./foo",
source: path.resolve(__dirname, "./fixtures/hcl-module/"),
});
expect(Testing.synth(stack)).toMatchSnapshot();
});
Expand All @@ -16,7 +17,7 @@ test("pass variables", () => {
const stack = new TerraformStack(app, "test");

new TerraformHclModule(stack, "test", {
source: "./foo",
source: path.resolve(__dirname, "./fixtures/hcl-module/"),
variables: {
param1: "name",
param2: 1,
Expand All @@ -36,7 +37,7 @@ test("simple provider", () => {
});

new TerraformHclModule(stack, "test", {
source: "./foo",
source: path.resolve(__dirname, "./fixtures/hcl-module/"),
providers: [provider],
});
expect(Testing.synth(stack)).toMatchSnapshot();
Expand All @@ -56,7 +57,7 @@ test("multiple providers", () => {
});

new TerraformHclModule(stack, "test", {
source: "./foo",
source: path.resolve(__dirname, "./fixtures/hcl-module/"),
providers: [provider1, provider2],
});
expect(Testing.synth(stack)).toMatchSnapshot();
Expand All @@ -78,7 +79,7 @@ test("multiple providers can't have the same module alias", () => {

try {
new TerraformHclModule(stack, "test", {
source: "./foo",
source: path.resolve(__dirname, "./fixtures/hcl-module/"),
providers: [provider1, provider2],
});
} catch (e) {
Expand All @@ -103,7 +104,7 @@ test("complex providers", () => {
});

new TerraformHclModule(stack, "test", {
source: "./foo",
source: path.resolve(__dirname, "./fixtures/hcl-module/"),
providers: [
{ provider: provider1, moduleAlias: "src" },
{ provider: provider2, moduleAlias: "dst" },
Expand All @@ -123,7 +124,7 @@ test("reference module", () => {
new TestProvider(stack, "provider", {});

const module = new TerraformHclModule(stack, "test", {
source: "./foo",
source: path.resolve(__dirname, "./fixtures/hcl-module/"),
});

new TestResource(stack, "resource", {
Expand All @@ -138,7 +139,7 @@ test("reference module list", () => {
new TestProvider(stack, "provider", {});

const module = new TerraformHclModule(stack, "test", {
source: "./foo",
source: path.resolve(__dirname, "./fixtures/hcl-module/"),
});

const resource = new TestResource(stack, "resource", {
Expand All @@ -154,7 +155,7 @@ test("set variable", () => {
const stack = new TerraformStack(app, "test");

const module = new TerraformHclModule(stack, "test", {
source: "./foo",
source: path.resolve(__dirname, "./fixtures/hcl-module/"),
});

module.set("param1", "value1");
Expand All @@ -166,7 +167,7 @@ test("add provider", () => {
const stack = new TerraformStack(app, "test");

const module = new TerraformHclModule(stack, "test", {
source: "./foo",
source: path.resolve(__dirname, "./fixtures/hcl-module/"),
});

const provider = new TestProvider(stack, "provider", {
Expand All @@ -183,7 +184,7 @@ test("depend on module", () => {
new TestProvider(stack, "provider", {});

const module = new TerraformHclModule(stack, "test", {
source: "./foo",
source: path.resolve(__dirname, "./fixtures/hcl-module/"),
});

new TestResource(stack, "resource", {
Expand All @@ -198,11 +199,11 @@ test("depend on other module", () => {
const stack = new TerraformStack(app, "test");

const module1 = new TerraformHclModule(stack, "test_1", {
source: "./foo",
source: path.resolve(__dirname, "./fixtures/hcl-module/"),
});

new TerraformHclModule(stack, "test_2", {
source: "./foo",
source: path.resolve(__dirname, "./fixtures/hcl-module/"),
dependsOn: [module1],
});

Expand Down

0 comments on commit a81df11

Please sign in to comment.