@cdktn/hcl2cdk 0.24.0-pre.45 → 0.24.0-pre.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/LICENSE +355 -0
  2. package/README.md +1 -1
  3. package/build/__tests__/expressions.test.js +10 -19
  4. package/build/__tests__/functions.test.js +8 -18
  5. package/build/__tests__/testHelpers.js +3 -2
  6. package/build/coerceType.js +11 -21
  7. package/build/dynamic-blocks.js +3 -3
  8. package/build/expressions.js +13 -22
  9. package/build/function-bindings/functions.generated.js +2 -2
  10. package/build/generation.js +24 -34
  11. package/build/index.js +15 -25
  12. package/build/iteration.js +7 -6
  13. package/build/jsii-rosetta-workarounds.js +6 -5
  14. package/build/partialCode.js +11 -20
  15. package/build/provider.js +4 -3
  16. package/build/references.js +6 -5
  17. package/build/schema.js +8 -18
  18. package/build/terraformSchema.js +4 -4
  19. package/build/utils.js +3 -3
  20. package/build/variables.js +12 -21
  21. package/package.json +20 -17
  22. package/package.sh +1 -1
  23. package/src/__tests__/coerceType.test.ts +207 -0
  24. package/src/__tests__/expressionToTs.test.ts +1167 -0
  25. package/src/__tests__/expressions.test.ts +541 -0
  26. package/src/__tests__/findExpressionType.test.ts +112 -0
  27. package/src/__tests__/functions.test.ts +768 -0
  28. package/src/__tests__/generation.test.ts +72 -0
  29. package/src/__tests__/jsii-rosetta-workarounds.test.ts +145 -0
  30. package/src/__tests__/partialCode.test.ts +432 -0
  31. package/src/__tests__/terraformSchema.test.ts +107 -0
  32. package/src/__tests__/testHelpers.ts +11 -0
  33. package/src/coerceType.ts +261 -0
  34. package/src/dynamic-blocks.ts +61 -0
  35. package/src/expressions.ts +968 -0
  36. package/src/function-bindings/functions.generated.ts +1139 -0
  37. package/src/function-bindings/functions.ts +104 -0
  38. package/src/generation.ts +1189 -0
  39. package/src/index.ts +584 -0
  40. package/src/iteration.ts +156 -0
  41. package/src/jsii-rosetta-workarounds.ts +145 -0
  42. package/src/partialCode.ts +132 -0
  43. package/src/provider.ts +60 -0
  44. package/src/references.ts +193 -0
  45. package/src/schema.ts +74 -0
  46. package/src/terraformSchema.ts +182 -0
  47. package/src/types.ts +58 -0
  48. package/src/utils.ts +19 -0
  49. package/src/variables.ts +214 -0
  50. package/test/__snapshots__/backends.test.ts.snap +70 -0
  51. package/test/__snapshots__/externals.test.ts.snap +37 -0
  52. package/test/__snapshots__/granular-imports.test.ts.snap +180 -0
  53. package/test/__snapshots__/imports.test.ts.snap +159 -0
  54. package/test/__snapshots__/iteration.test.ts.snap +532 -0
  55. package/test/__snapshots__/jsiiLanguage.test.ts.snap +347 -0
  56. package/test/__snapshots__/locals.test.ts.snap +55 -0
  57. package/test/__snapshots__/modules.test.ts.snap +127 -0
  58. package/test/__snapshots__/outputs.test.ts.snap +77 -0
  59. package/test/__snapshots__/partialCode.test.ts.snap +120 -0
  60. package/test/__snapshots__/provider.test.ts.snap +128 -0
  61. package/test/__snapshots__/references.test.ts.snap +376 -0
  62. package/test/__snapshots__/resource-meta-properties.test.ts.snap +342 -0
  63. package/test/__snapshots__/resources.test.ts.snap +613 -0
  64. package/test/__snapshots__/tfExpressions.test.ts.snap +537 -0
  65. package/test/__snapshots__/typeCoercion.test.ts.snap +253 -0
  66. package/test/__snapshots__/variables.test.ts.snap +150 -0
  67. package/test/backends.test.ts +75 -0
  68. package/test/convertProject.test.ts +257 -0
  69. package/test/externals.test.ts +35 -0
  70. package/test/globalSetup.ts +224 -0
  71. package/test/globalTeardown.ts +11 -0
  72. package/test/granular-imports.test.ts +161 -0
  73. package/test/hcl2cdk.test.ts +88 -0
  74. package/test/helpers/convert.ts +543 -0
  75. package/test/helpers/tmp.ts +25 -0
  76. package/test/imports.test.ts +141 -0
  77. package/test/iteration.test.ts +342 -0
  78. package/test/jsiiLanguage.test.ts +73 -0
  79. package/test/locals.test.ts +47 -0
  80. package/test/modules.test.ts +143 -0
  81. package/test/outputs.test.ts +69 -0
  82. package/test/partialCode.test.ts +25 -0
  83. package/test/provider.test.ts +106 -0
  84. package/test/references.test.ts +287 -0
  85. package/test/resource-meta-properties.test.ts +288 -0
  86. package/test/resources.test.ts +551 -0
  87. package/test/tfExpressions.test.ts +300 -0
  88. package/test/typeCoercion.test.ts +154 -0
  89. package/test/variables.test.ts +96 -0
@@ -0,0 +1,253 @@
1
+ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2
+
3
+ exports[`type coercion coerces string literal to number snapshot typescript 1`] = `
4
+ "import { Construct } from "constructs";
5
+ import { Op, Token, TerraformStack } from "cdktn";
6
+ /*
7
+ * Provider bindings are generated by running \`cdktn get\`.
8
+ * See https://cdktn.io/docs/concepts/providers#import-providers for more details.
9
+ */
10
+ import { AwsProvider } from "./.gen/providers/aws/provider";
11
+ import { SecurityGroupRule } from "./.gen/providers/aws/security-group-rule";
12
+ class MyConvertedCode extends TerraformStack {
13
+ constructor(scope: Construct, name: string) {
14
+ super(scope, name);
15
+ new AwsProvider(this, "aws", {
16
+ region: "us-east-1",
17
+ });
18
+ new SecurityGroupRule(this, "client_alb_allow_outbound", {
19
+ cidrBlocks: ["0.0.0.0/0"],
20
+ description: "Allow any outbound traffic.",
21
+ fromPort: 0,
22
+ ipv6CidrBlocks: ["::/0"],
23
+ protocol: Token.asString(Op.negate(1)),
24
+ securityGroupId: "sg-1234567890",
25
+ toPort: 0,
26
+ type: "egress",
27
+ });
28
+ }
29
+ }
30
+ "
31
+ `;
32
+
33
+ exports[`type coercion expressions need to be coerced to required type snapshot typescript 1`] = `
34
+ "import { Construct } from "constructs";
35
+ import {
36
+ VariableType,
37
+ TerraformVariable,
38
+ Fn,
39
+ Token,
40
+ TerraformStack,
41
+ } from "cdktn";
42
+ /*
43
+ * Provider bindings are generated by running \`cdktn get\`.
44
+ * See https://cdktn.io/docs/concepts/providers#import-providers for more details.
45
+ */
46
+ import { EksCluster } from "./.gen/providers/aws/eks-cluster";
47
+ import { AwsProvider } from "./.gen/providers/aws/provider";
48
+ class MyConvertedCode extends TerraformStack {
49
+ constructor(scope: Construct, name: string) {
50
+ super(scope, name);
51
+ /*Terraform Variables are not always the best fit for getting inputs in the context of Terraform CDK.
52
+ You can read more about this at https://cdktn.io/docs/concepts/variables-and-outputs#input-variables*/
53
+ new AwsProvider(this, "aws", {
54
+ region: "us-east-1",
55
+ });
56
+ const exampleSpotAndFargateName = new TerraformVariable(
57
+ this,
58
+ "example_spot_and_fargate_name",
59
+ {
60
+ type: VariableType.STRING,
61
+ }
62
+ );
63
+ const privateSubnets = new TerraformVariable(this, "private_subnets", {
64
+ type: VariableType.list(
65
+ VariableType.object({
66
+ id: VariableType.STRING,
67
+ })
68
+ ),
69
+ });
70
+ const publicSubnets = new TerraformVariable(this, "public_subnets", {
71
+ type: VariableType.list(
72
+ VariableType.object({
73
+ id: VariableType.STRING,
74
+ })
75
+ ),
76
+ });
77
+ const roleArn = new TerraformVariable(this, "role_arn", {
78
+ type: VariableType.STRING,
79
+ });
80
+ new EksCluster(this, "example_spot_and_fargate_eks_cluster", {
81
+ name: exampleSpotAndFargateName.stringValue,
82
+ roleArn: roleArn.stringValue,
83
+ vpcConfig: {
84
+ subnetIds: Token.asList(
85
+ Fn.concat([
86
+ Fn.lookupNested(privateSubnets.value, ["*", "id"]),
87
+ Fn.lookupNested(publicSubnets.value, ["*", "id"]),
88
+ ])
89
+ ),
90
+ },
91
+ });
92
+ }
93
+ }
94
+ "
95
+ `;
96
+
97
+ exports[`type coercion function arguments get coerced to the correct type snapshot typescript 1`] = `
98
+ "import { Construct } from "constructs";
99
+ import {
100
+ VariableType,
101
+ TerraformVariable,
102
+ Token,
103
+ Fn,
104
+ TerraformStack,
105
+ } from "cdktn";
106
+ /*
107
+ * Provider bindings are generated by running \`cdktn get\`.
108
+ * See https://cdktn.io/docs/concepts/providers#import-providers for more details.
109
+ */
110
+ import { AwsProvider } from "./.gen/providers/aws/provider";
111
+ import { Route53Record } from "./.gen/providers/aws/route53-record";
112
+ class MyConvertedCode extends TerraformStack {
113
+ constructor(scope: Construct, name: string) {
114
+ super(scope, name);
115
+ /*Terraform Variables are not always the best fit for getting inputs in the context of Terraform CDK.
116
+ You can read more about this at https://cdktn.io/docs/concepts/variables-and-outputs#input-variables*/
117
+ new AwsProvider(this, "aws", {
118
+ region: "us-east-1",
119
+ });
120
+ const zoneId = new TerraformVariable(this, "zone_id", {
121
+ type: VariableType.STRING,
122
+ });
123
+ new Route53Record(this, "example_aws_route53_simple_record", {
124
+ name: "example.com",
125
+ records: ["example"],
126
+ ttl: Token.asNumber("300"),
127
+ type: "TXT",
128
+ zoneId: Token.asString(
129
+ Fn.textencodebase64(zoneId.stringValue, "UTF-16LE")
130
+ ),
131
+ });
132
+ }
133
+ }
134
+ "
135
+ `;
136
+
137
+ exports[`type coercion references used in resource for_eachs need to understand how the value is used snapshot typescript 1`] = `
138
+ "import { Construct } from "constructs";
139
+ import {
140
+ VariableType,
141
+ TerraformVariable,
142
+ Token,
143
+ TerraformIterator,
144
+ Fn,
145
+ TerraformStack,
146
+ } from "cdktn";
147
+ /*
148
+ * Provider bindings are generated by running \`cdktn get\`.
149
+ * See https://cdktn.io/docs/concepts/providers#import-providers for more details.
150
+ */
151
+ import { IamUserGroupMembership } from "./.gen/providers/aws/iam-user-group-membership";
152
+ import { AwsProvider } from "./.gen/providers/aws/provider";
153
+ class MyConvertedCode extends TerraformStack {
154
+ constructor(scope: Construct, name: string) {
155
+ super(scope, name);
156
+ /*Terraform Variables are not always the best fit for getting inputs in the context of Terraform CDK.
157
+ You can read more about this at https://cdktn.io/docs/concepts/variables-and-outputs#input-variables*/
158
+ new AwsProvider(this, "aws", {
159
+ region: "us-east-1",
160
+ });
161
+ const iamGroupsUsers = new TerraformVariable(this, "iam_groups_users", {
162
+ type: VariableType.map(
163
+ VariableType.object({
164
+ groups: VariableType.ANY,
165
+ })
166
+ ),
167
+ });
168
+ /*In most cases loops should be handled in the programming language context and
169
+ not inside of the Terraform context. If you are looping over something external, e.g. a variable or a file input
170
+ you should consider using a for loop. If you are looping over something only known to Terraform, e.g. a result of a data source
171
+ you need to keep this like it is.*/
172
+ const iamUserGroupMembershipUserGroupsForEachIterator =
173
+ TerraformIterator.fromList(Token.asAny(iamGroupsUsers.value));
174
+ new IamUserGroupMembership(this, "iam_user_group_membership_user_groups", {
175
+ groups: Token.asList(
176
+ Fn.lookupNested(iamUserGroupMembershipUserGroupsForEachIterator.value, [
177
+ '["groups"]',
178
+ ])
179
+ ),
180
+ user: Token.asString(iamUserGroupMembershipUserGroupsForEachIterator.key),
181
+ forEach: iamUserGroupMembershipUserGroupsForEachIterator,
182
+ });
183
+ }
184
+ }
185
+ "
186
+ `;
187
+
188
+ exports[`type coercion references used in resources need to use Token.asString snapshot typescript 1`] = `
189
+ "import { Construct } from "constructs";
190
+ import { Token, TerraformStack } from "cdktn";
191
+ /*
192
+ * Provider bindings are generated by running \`cdktn get\`.
193
+ * See https://cdktn.io/docs/concepts/providers#import-providers for more details.
194
+ */
195
+ import { AwsProvider } from "./.gen/providers/aws/provider";
196
+ import { Route53Record } from "./.gen/providers/aws/route53-record";
197
+ import { Route53Zone } from "./.gen/providers/aws/route53-zone";
198
+ class MyConvertedCode extends TerraformStack {
199
+ constructor(scope: Construct, name: string) {
200
+ super(scope, name);
201
+ new AwsProvider(this, "aws", {
202
+ region: "us-east-1",
203
+ });
204
+ const exampleAwsRoute53SimpleZone = new Route53Zone(
205
+ this,
206
+ "example_aws_route53_simple_zone",
207
+ {
208
+ name: "example.com",
209
+ }
210
+ );
211
+ new Route53Record(this, "example_aws_route53_simple_record", {
212
+ name: "example.com",
213
+ records: ["example"],
214
+ ttl: Token.asNumber("300"),
215
+ type: "TXT",
216
+ zoneId: exampleAwsRoute53SimpleZone.zoneId,
217
+ });
218
+ }
219
+ }
220
+ "
221
+ `;
222
+
223
+ exports[`type coercion variables used in resources need to use stringValue snapshot typescript 1`] = `
224
+ "import { Construct } from "constructs";
225
+ import { VariableType, TerraformVariable, Token, TerraformStack } from "cdktn";
226
+ /*
227
+ * Provider bindings are generated by running \`cdktn get\`.
228
+ * See https://cdktn.io/docs/concepts/providers#import-providers for more details.
229
+ */
230
+ import { AwsProvider } from "./.gen/providers/aws/provider";
231
+ import { Route53Record } from "./.gen/providers/aws/route53-record";
232
+ class MyConvertedCode extends TerraformStack {
233
+ constructor(scope: Construct, name: string) {
234
+ super(scope, name);
235
+ /*Terraform Variables are not always the best fit for getting inputs in the context of Terraform CDK.
236
+ You can read more about this at https://cdktn.io/docs/concepts/variables-and-outputs#input-variables*/
237
+ new AwsProvider(this, "aws", {
238
+ region: "us-east-1",
239
+ });
240
+ const zoneId = new TerraformVariable(this, "zone_id", {
241
+ type: VariableType.STRING,
242
+ });
243
+ new Route53Record(this, "example_aws_route53_simple_record", {
244
+ name: "example.com",
245
+ records: ["example"],
246
+ ttl: Token.asNumber("300"),
247
+ type: "TXT",
248
+ zoneId: zoneId.stringValue,
249
+ });
250
+ }
251
+ }
252
+ "
253
+ `;
@@ -0,0 +1,150 @@
1
+ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2
+
3
+ exports[`variables basic snapshot typescript 1`] = `
4
+ "import { Construct } from "constructs";
5
+ import { VariableType, TerraformVariable, TerraformStack } from "cdktn";
6
+ /*
7
+ * Provider bindings are generated by running \`cdktn get\`.
8
+ * See https://cdktn.io/docs/concepts/providers#import-providers for more details.
9
+ */
10
+ import { DataLocalFile } from "./.gen/providers/local/data-local-file";
11
+ import { LocalProvider } from "./.gen/providers/local/provider";
12
+ class MyConvertedCode extends TerraformStack {
13
+ constructor(scope: Construct, name: string) {
14
+ super(scope, name);
15
+ /*Terraform Variables are not always the best fit for getting inputs in the context of Terraform CDK.
16
+ You can read more about this at https://cdktn.io/docs/concepts/variables-and-outputs#input-variables*/
17
+ new LocalProvider(this, "local", {});
18
+ const imageId = new TerraformVariable(this, "image_id", {
19
+ type: VariableType.STRING,
20
+ });
21
+ new DataLocalFile(this, "foo", {
22
+ filename: "./\${" + imageId.value + "}.img",
23
+ });
24
+ }
25
+ }
26
+ "
27
+ `;
28
+
29
+ exports[`variables complex type and default snapshot typescript 1`] = `
30
+ "import { Construct } from "constructs";
31
+ import {
32
+ VariableType,
33
+ TerraformVariable,
34
+ Fn,
35
+ Token,
36
+ TerraformStack,
37
+ } from "cdktn";
38
+ /*
39
+ * Provider bindings are generated by running \`cdktn get\`.
40
+ * See https://cdktn.io/docs/concepts/providers#import-providers for more details.
41
+ */
42
+ import { DataLocalFile } from "./.gen/providers/local/data-local-file";
43
+ import { LocalProvider } from "./.gen/providers/local/provider";
44
+ class MyConvertedCode extends TerraformStack {
45
+ constructor(scope: Construct, name: string) {
46
+ super(scope, name);
47
+ /*Terraform Variables are not always the best fit for getting inputs in the context of Terraform CDK.
48
+ You can read more about this at https://cdktn.io/docs/concepts/variables-and-outputs#input-variables*/
49
+ new LocalProvider(this, "local", {});
50
+ const dockerPorts = new TerraformVariable(this, "docker_ports", {
51
+ default: [
52
+ {
53
+ external: 8300,
54
+ internal: 8300,
55
+ protocol: "tcp",
56
+ },
57
+ ],
58
+ type: VariableType.list(
59
+ VariableType.object({
60
+ external: VariableType.NUMBER,
61
+ internal: VariableType.NUMBER,
62
+ protocol: VariableType.STRING,
63
+ })
64
+ ),
65
+ });
66
+ new DataLocalFile(this, "foo", {
67
+ filename:
68
+ "./" +
69
+ Token.asString(Fn.lookupNested(dockerPorts.value, ["0", "protocol"])) +
70
+ ".img",
71
+ });
72
+ }
73
+ }
74
+ "
75
+ `;
76
+
77
+ exports[`variables sensitive and required snapshot typescript 1`] = `
78
+ "import { Construct } from "constructs";
79
+ import {
80
+ VariableType,
81
+ TerraformVariable,
82
+ Fn,
83
+ Token,
84
+ TerraformStack,
85
+ } from "cdktn";
86
+ /*
87
+ * Provider bindings are generated by running \`cdktn get\`.
88
+ * See https://cdktn.io/docs/concepts/providers#import-providers for more details.
89
+ */
90
+ import { DataLocalFile } from "./.gen/providers/local/data-local-file";
91
+ import { LocalProvider } from "./.gen/providers/local/provider";
92
+ class MyConvertedCode extends TerraformStack {
93
+ constructor(scope: Construct, name: string) {
94
+ super(scope, name);
95
+ /*Terraform Variables are not always the best fit for getting inputs in the context of Terraform CDK.
96
+ You can read more about this at https://cdktn.io/docs/concepts/variables-and-outputs#input-variables*/
97
+ new LocalProvider(this, "local", {});
98
+ const userInformation = new TerraformVariable(this, "user_information", {
99
+ sensitive: true,
100
+ nullable: false,
101
+ type: VariableType.object({
102
+ address: VariableType.STRING,
103
+ name: VariableType.STRING,
104
+ }),
105
+ });
106
+ new DataLocalFile(this, "foo", {
107
+ filename:
108
+ "./" +
109
+ Token.asString(Fn.lookupNested(userInformation.value, ["name"])) +
110
+ ".img",
111
+ });
112
+ }
113
+ }
114
+ "
115
+ `;
116
+
117
+ exports[`variables validations snapshot typescript 1`] = `
118
+ "import { Construct } from "constructs";
119
+ import { VariableType, TerraformVariable, TerraformStack } from "cdktn";
120
+ /*
121
+ * Provider bindings are generated by running \`cdktn get\`.
122
+ * See https://cdktn.io/docs/concepts/providers#import-providers for more details.
123
+ */
124
+ import { DataLocalFile } from "./.gen/providers/local/data-local-file";
125
+ import { LocalProvider } from "./.gen/providers/local/provider";
126
+ class MyConvertedCode extends TerraformStack {
127
+ constructor(scope: Construct, name: string) {
128
+ super(scope, name);
129
+ /*Terraform Variables are not always the best fit for getting inputs in the context of Terraform CDK.
130
+ You can read more about this at https://cdktn.io/docs/concepts/variables-and-outputs#input-variables*/
131
+ new LocalProvider(this, "local", {});
132
+ const imageId = new TerraformVariable(this, "image_id", {
133
+ description: "The id of the machine image (AMI) to use for the server.",
134
+ validation: [
135
+ {
136
+ errorMessage:
137
+ "The image_id value must be a valid AMI id, starting with ami-.",
138
+ condition:
139
+ '\${length(var.image_id) > 4 && substr(var.image_id, 0, 4) == "ami-"}',
140
+ },
141
+ ],
142
+ type: VariableType.STRING,
143
+ });
144
+ new DataLocalFile(this, "foo", {
145
+ filename: "./\${" + imageId.value + "}.img",
146
+ });
147
+ }
148
+ }
149
+ "
150
+ `;
@@ -0,0 +1,75 @@
1
+ // Copyright (c) HashiCorp, Inc
2
+ // SPDX-License-Identifier: MPL-2.0
3
+ import { testCase, Synth, Snapshot } from "./helpers/convert";
4
+
5
+ describe("backends", () => {
6
+ testCase.test(
7
+ "local backend",
8
+ `
9
+ terraform {
10
+ backend "local" {
11
+ path = "relative/path/to/terraform.tfstate"
12
+ }
13
+ }
14
+ `,
15
+ [],
16
+ Snapshot.yes,
17
+ Synth.yes,
18
+ );
19
+
20
+ testCase.test(
21
+ "remote backend",
22
+ `
23
+ terraform {
24
+ backend "remote" {
25
+ hostname = "app.terraform.io"
26
+ organization = "company"
27
+
28
+ workspaces {
29
+ name = "my-app-prod"
30
+ }
31
+ }
32
+ }
33
+ `,
34
+ [],
35
+ Snapshot.yes,
36
+ Synth.yes,
37
+ );
38
+
39
+ testCase.test(
40
+ "remote state",
41
+ `
42
+ data "terraform_remote_state" "vpc" {
43
+ backend = "remote"
44
+
45
+ config = {
46
+ organization = "hashicorp"
47
+ workspaces = {
48
+ name = "vpc-prod"
49
+ }
50
+ }
51
+ }
52
+ `,
53
+ [],
54
+ Snapshot.yes,
55
+ Synth.yes,
56
+ );
57
+
58
+ testCase.test(
59
+ "remote state types",
60
+ `
61
+ data "terraform_remote_state" "s3" {
62
+ backend = "s3"
63
+
64
+ config = {
65
+ bucket = "mybucket"
66
+ key = "path/to/my/key"
67
+ region = "us-east-1"
68
+ }
69
+ }
70
+ `,
71
+ [],
72
+ Snapshot.yes,
73
+ Synth.yes,
74
+ );
75
+ });