Skip to content

Commit

Permalink
fix(provider-generator): use module name that does not collide commonly
Browse files Browse the repository at this point in the history
Closes #3026
  • Loading branch information
DanielMSchmidt committed Dec 6, 2023
1 parent 6bfb057 commit 767f5c9
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 11 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.20.0 (Unreleased)

**Breaking changes**

### Module names / import paths changing

To resolve [\#3026](https://github.com/hashicorp/terraform-cdk/issues/3026) we improved the logic for generating names for [Terraform Modules](https://developer.hashicorp.com/terraform/cdktf/concepts/modules). This makes the default module names nicer, but also leads to breaking changes for Python, C#, and Java. When updating CDKTF, please generate the new bindings with `cdktf get` after updating the CLI and update your module imports.

## 0.19.1

### feat
Expand Down
12 changes: 2 additions & 10 deletions examples/python/aws-eks/cdktf.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@
}
],
"terraformModules": [
{
"name": "vpc",
"source": "terraform-aws-modules/vpc/aws",
"version": "2.77.0"
},
{
"name": "eks",
"source": "terraform-aws-modules/eks/aws",
"version": "~> 15.0"
}
"terraform-aws-modules/vpc/[email protected]",
"terraform-aws-modules/rds/[email protected]"
],
"codeMakerOutput": "imports"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@cdktf/commons/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class TerraformModuleConstraint
toProcess = prefixMatch[2];
}

// strip off any protocl
// strip off any protocol
const protocolMatch = toProcess.match(/^([a-zA-Z]*):\/\/(.*)/);
if (protocolMatch) {
toProcess = protocolMatch[2];
Expand Down
80 changes: 80 additions & 0 deletions packages/@cdktf/commons/src/construct-maker-target.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/

import { Language, TerraformModuleConstraint } from "./config";
import { ConstructsMakerModuleTarget } from "./construct-maker-target";

describe("ConstructsMakerModuleTarget", () => {
describe.each([
{
fqn: "cloudposse/label/null",
names: {
[Language.TYPESCRIPT]: "cloudposse.null",
[Language.PYTHON]: "cloudposse.label.null",
[Language.JAVA]: "cloudposse.label.null",
[Language.CSHARP]: "cloudposse.label.null",
[Language.GO]: "label",
},
},
{
fqn: "terraform-google-modules/project-factory/google",
names: {
[Language.TYPESCRIPT]: "terraform_google_modules.google",
[Language.PYTHON]: "terraform_google_modules.project_factory.google",
[Language.JAVA]: "terraform_google_modules.project_factory.google",
[Language.CSHARP]: "terraform_google_modules.project_factory.google",
[Language.GO]: "project_factory",
},
},
{
fqn: "terraform-aws-modules/vpc/[email protected]",
names: {
[Language.TYPESCRIPT]: "terraform_aws_modules.aws",
[Language.PYTHON]: "terraform_aws_modules.vpc.aws",
[Language.JAVA]: "terraform_aws_modules.vpc.aws",
[Language.CSHARP]: "terraform_aws_modules.vpc.aws",
[Language.GO]: "vpc",
},
},
{
fqn: "terraform-aws-modules/eks/aws//modules/self-managed-node-group",
names: {
[Language.TYPESCRIPT]: "terraform_aws_modules.aws.eks.modules",
[Language.PYTHON]:
"terraform_aws_modules.eks.aws.modules.self_managed_node_group",
[Language.JAVA]:
"terraform_aws_modules.eks.aws.modules.self_managed_node_group",
[Language.CSHARP]:
"terraform_aws_modules.eks.aws.modules.self_managed_node_group",
[Language.GO]: "self_managed_node_group",
},
},
{
fqn: {
name: "my-local-module",
source: "./path/to/local/terraform/module",
},
names: {
[Language.TYPESCRIPT]: "my-local-module",
[Language.PYTHON]: "my_local_module",
[Language.JAVA]: "my_local_module",
[Language.CSHARP]: "my_local_module",
[Language.GO]: "my_local_module",
},
},
])("#srcMakName %#", ({ fqn, names }) => {
it.each(Object.entries(names))(
`expect name for ${JSON.stringify(fqn)} in %s to be %s`,
(language, name) => {
const constraint = new TerraformModuleConstraint(fqn as any);
const target = new ConstructsMakerModuleTarget(
constraint,
language as Language
);
expect(target.srcMakName).toBe(name);
}
);
});
});
5 changes: 5 additions & 0 deletions packages/@cdktf/commons/src/construct-maker-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export class ConstructsMakerModuleTarget extends ConstructsMakerTarget {
case Language.JAVA:
case Language.CSHARP:
case Language.PYTHON:
return this.simplifiedFqn;
default:
return this.simplifiedName;
}
Expand All @@ -110,6 +111,10 @@ export class ConstructsMakerModuleTarget extends ConstructsMakerTarget {
this.namespace?.replace(/\//gi, ".").replace(/-/gi, "_") ?? this.name
);
}

protected get simplifiedFqn(): string {
return this.fqn.replace(/\//gi, ".").replace(/-/gi, "_");
}
}

export class ConstructsMakerProviderTarget extends ConstructsMakerTarget {
Expand Down

0 comments on commit 767f5c9

Please sign in to comment.