@cdktn/hcl2cdk 0.24.0-pre.5 → 0.24.0-pre.51

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 (83) hide show
  1. package/.spec.swcrc +22 -0
  2. package/LICENSE +355 -0
  3. package/README.md +1 -1
  4. package/build/__tests__/jsii-rosetta-workarounds.test.js +2 -2
  5. package/build/expressions.js +3 -4
  6. package/build/function-bindings/functions.generated.js +2 -2
  7. package/build/generation.js +2 -2
  8. package/build/index.d.ts +20 -20
  9. package/build/index.js +4 -4
  10. package/build/references.js +2 -3
  11. package/build/schema.d.ts +60 -60
  12. package/build/utils.d.ts +1 -1
  13. package/build/utils.js +3 -3
  14. package/jest.config.js +18 -11
  15. package/package.json +37 -25
  16. package/package.sh +1 -1
  17. package/src/__tests__/coerceType.test.ts +207 -0
  18. package/src/__tests__/expressionToTs.test.ts +1167 -0
  19. package/src/__tests__/expressions.test.ts +541 -0
  20. package/src/__tests__/findExpressionType.test.ts +112 -0
  21. package/src/__tests__/functions.test.ts +768 -0
  22. package/src/__tests__/generation.test.ts +72 -0
  23. package/src/__tests__/jsii-rosetta-workarounds.test.ts +145 -0
  24. package/src/__tests__/partialCode.test.ts +432 -0
  25. package/src/__tests__/terraformSchema.test.ts +107 -0
  26. package/src/__tests__/testHelpers.ts +11 -0
  27. package/src/coerceType.ts +261 -0
  28. package/src/dynamic-blocks.ts +61 -0
  29. package/src/expressions.ts +968 -0
  30. package/src/function-bindings/functions.generated.ts +1139 -0
  31. package/src/function-bindings/functions.ts +104 -0
  32. package/src/generation.ts +1189 -0
  33. package/src/index.ts +584 -0
  34. package/src/iteration.ts +156 -0
  35. package/src/jsii-rosetta-workarounds.ts +145 -0
  36. package/src/partialCode.ts +132 -0
  37. package/src/provider.ts +60 -0
  38. package/src/references.ts +193 -0
  39. package/src/schema.ts +74 -0
  40. package/src/terraformSchema.ts +182 -0
  41. package/src/types.ts +58 -0
  42. package/src/utils.ts +19 -0
  43. package/src/variables.ts +214 -0
  44. package/test/__snapshots__/backends.test.ts.snap +70 -0
  45. package/test/__snapshots__/externals.test.ts.snap +37 -0
  46. package/test/__snapshots__/granular-imports.test.ts.snap +180 -0
  47. package/test/__snapshots__/imports.test.ts.snap +159 -0
  48. package/test/__snapshots__/iteration.test.ts.snap +532 -0
  49. package/test/__snapshots__/jsiiLanguage.test.ts.snap +347 -0
  50. package/test/__snapshots__/locals.test.ts.snap +55 -0
  51. package/test/__snapshots__/modules.test.ts.snap +127 -0
  52. package/test/__snapshots__/outputs.test.ts.snap +77 -0
  53. package/test/__snapshots__/partialCode.test.ts.snap +120 -0
  54. package/test/__snapshots__/provider.test.ts.snap +128 -0
  55. package/test/__snapshots__/references.test.ts.snap +376 -0
  56. package/test/__snapshots__/resource-meta-properties.test.ts.snap +342 -0
  57. package/test/__snapshots__/resources.test.ts.snap +613 -0
  58. package/test/__snapshots__/tfExpressions.test.ts.snap +537 -0
  59. package/test/__snapshots__/typeCoercion.test.ts.snap +253 -0
  60. package/test/__snapshots__/variables.test.ts.snap +150 -0
  61. package/test/backends.test.ts +75 -0
  62. package/test/convertProject.test.ts +257 -0
  63. package/test/externals.test.ts +35 -0
  64. package/test/globalSetup.ts +224 -0
  65. package/test/globalTeardown.ts +11 -0
  66. package/test/granular-imports.test.ts +161 -0
  67. package/test/hcl2cdk.test.ts +88 -0
  68. package/test/helpers/convert.ts +543 -0
  69. package/test/helpers/tmp.ts +25 -0
  70. package/test/imports.test.ts +141 -0
  71. package/test/iteration.test.ts +342 -0
  72. package/test/jsiiLanguage.test.ts +73 -0
  73. package/test/locals.test.ts +47 -0
  74. package/test/modules.test.ts +143 -0
  75. package/test/outputs.test.ts +69 -0
  76. package/test/partialCode.test.ts +25 -0
  77. package/test/provider.test.ts +106 -0
  78. package/test/references.test.ts +287 -0
  79. package/test/resource-meta-properties.test.ts +288 -0
  80. package/test/resources.test.ts +551 -0
  81. package/test/tfExpressions.test.ts +300 -0
  82. package/test/typeCoercion.test.ts +154 -0
  83. package/test/variables.test.ts +96 -0
@@ -0,0 +1,300 @@
1
+ /**
2
+ * Copyright (c) HashiCorp, Inc.
3
+ * SPDX-License-Identifier: MPL-2.0
4
+ */
5
+
6
+ import { testCase, Synth, binding, Snapshot } from "./helpers/convert";
7
+
8
+ describe("tfExpressions", () => {
9
+ testCase.test(
10
+ "arithmetics",
11
+ `
12
+ variable "members" {
13
+ type = number
14
+ }
15
+ variable "admins" {
16
+ type = number
17
+ }
18
+
19
+ output "arithmetics" {
20
+ value = var.members + var.admins
21
+ }`,
22
+ [],
23
+ Snapshot.yes,
24
+ Synth.yes,
25
+ );
26
+
27
+ testCase.test(
28
+ "conditionals",
29
+ `
30
+ provider "aws" {
31
+ region = "us-east-1"
32
+ }
33
+ resource "aws_kms_key" "examplekms" {
34
+ description = "KMS key 1"
35
+ deletion_window_in_days = 7
36
+ }
37
+
38
+ resource "aws_s3_bucket" "examplebucket" {
39
+ bucket = "examplebuckettftest"
40
+ acl = "private"
41
+ }
42
+
43
+ resource "aws_s3_bucket_object" "examplebucket_object" {
44
+ key = "someobject"
45
+ bucket = aws_kms_key.examplekms.deletion_window_in_days > 3 ? aws_s3_bucket.examplebucket.id : []
46
+ source = "index.html"
47
+ kms_key_id = aws_kms_key.examplekms.arn
48
+ }`,
49
+ [binding.aws],
50
+ Snapshot.yes,
51
+ Synth.yes,
52
+ {
53
+ resources: ["aws_s3_bucket", "aws_kms_key", "aws_s3_bucket_object"],
54
+ },
55
+ );
56
+
57
+ testCase.test(
58
+ "for expression 1",
59
+ `
60
+ variable "users" {
61
+ type = map(object({
62
+ is_admin = boolean
63
+ }))
64
+ }
65
+
66
+ locals {
67
+ admin_users = {
68
+ for name, user in var.users : name => user
69
+ if user.is_admin
70
+ }
71
+ regular_users = {
72
+ for name, user in var.users : name => user
73
+ if !user.is_admin
74
+ }
75
+ }
76
+
77
+ output "combined-so-it-does-not-get-removed" {
78
+ value = "\${local.admin_users},\${local.regular_users}"
79
+ }
80
+ `,
81
+ [],
82
+ Snapshot.yes,
83
+ Synth.yes,
84
+ );
85
+
86
+ testCase.test(
87
+ "for expression 2",
88
+ `
89
+ variable "users" {
90
+ type = map(object({
91
+ role = string
92
+ }))
93
+ }
94
+
95
+ locals {
96
+ users_by_role = {
97
+ for name, user in var.users : user.role => name...
98
+ }
99
+ }
100
+
101
+ output "so-it-does-not-get-removed" {
102
+ value = local.users_by_role
103
+ }`,
104
+ [],
105
+ Snapshot.yes,
106
+ Synth.yes,
107
+ );
108
+
109
+ testCase.test(
110
+ "for expression 3",
111
+ `
112
+ provider "datadog" {
113
+ api_key = "api_key"
114
+ app_key = "app_key"
115
+ }
116
+
117
+ variable "users" {
118
+ type = list(object({
119
+ id = string
120
+ }))
121
+ }
122
+
123
+ resource "datadog_monitor" "hard_query" {
124
+ name = "queries are hard"
125
+ message = "here we go"
126
+ query = join(" && ", [for o in var.users : "!(!\${o.id})"])
127
+ type = "metric alert"
128
+ }`,
129
+ [binding.datadog],
130
+ Snapshot.yes,
131
+ Synth.yes,
132
+ {
133
+ resources: ["datadog_monitor"],
134
+ },
135
+ );
136
+
137
+ testCase.test(
138
+ "property access through square brackets",
139
+ `
140
+ provider "aws" {
141
+ region = "us-east-1"
142
+ }
143
+ variable "settings" {
144
+ type = map(string)
145
+ }
146
+
147
+ resource "aws_s3_bucket" "examplebucket" {
148
+ bucket = var.settings["bucket_name"]
149
+ acl = "private"
150
+ }
151
+ `,
152
+ [binding.aws],
153
+ Snapshot.yes,
154
+ Synth.yes,
155
+ {
156
+ resources: ["aws_s3_bucket"],
157
+ },
158
+ );
159
+
160
+ testCase.test(
161
+ "list access through square brackets",
162
+ `
163
+ provider "aws" {
164
+ region = "us-east-1"
165
+ }
166
+ variable "settings" {
167
+ type = list(map(string))
168
+ }
169
+
170
+ resource "aws_s3_bucket" "examplebucket" {
171
+ bucket = var.settings[0]["bucket_name"]
172
+ acl = "private"
173
+ }
174
+ `,
175
+ [binding.aws],
176
+ Snapshot.yes,
177
+ Synth.yes,
178
+ {
179
+ resources: ["aws_s3_bucket"],
180
+ },
181
+ );
182
+
183
+ testCase.test(
184
+ "numeric property access",
185
+ `
186
+ provider "google" {
187
+ project = "my-project"
188
+ region = "us-central1"
189
+ }
190
+ resource "google_compute_instance" "example" {
191
+ name = "example"
192
+ machine_type = "f1-micro"
193
+ zone = "us-east1-b"
194
+
195
+ boot_disk {
196
+ initialize_params {
197
+ image = "debian-cloud/debian-9"
198
+ }
199
+ }
200
+
201
+ network_interface {
202
+ network = "default"
203
+
204
+ access_config {
205
+ // Ephemeral IP
206
+ }
207
+ }
208
+ }
209
+
210
+ output "public_ip" {
211
+ value = "\${google_compute_instance.example.network_interface.0.access_config.0.assigned_nat_ip}"
212
+ }
213
+ `,
214
+ [binding.google],
215
+ Snapshot.yes,
216
+ Synth.yes,
217
+ {
218
+ resources: ["google_compute_instance"],
219
+ },
220
+ );
221
+
222
+ testCase.test(
223
+ "multi-line strings are supported",
224
+ `
225
+ output "hash" {
226
+ value = <<ITEM
227
+ {
228
+ "example_hash_key": {"S": "something"},
229
+ "example_attribute": {"N": "11111"}
230
+ }
231
+ ITEM
232
+ }
233
+ `,
234
+ [],
235
+ Snapshot.yes,
236
+ Synth.yes,
237
+ {
238
+ resources: [],
239
+ },
240
+ );
241
+
242
+ testCase.test(
243
+ "escaping dollar-curly in template strings works",
244
+ `
245
+ resource "aws_ssoadmin_instance_access_control_attributes" "example" {
246
+ attribute {
247
+ value {
248
+ source = ["$\${path:name.givenName}"]
249
+ }
250
+ }
251
+ }
252
+ `,
253
+ [binding.aws],
254
+ Snapshot.yes,
255
+ Synth.yes_but_only_typescript_right_now_because_it_breaks,
256
+ {
257
+ resources: [],
258
+ },
259
+ );
260
+
261
+ testCase.test(
262
+ "property access in maps works",
263
+ `
264
+ variable "default_tags" {
265
+ type = map(string)
266
+ description = "Map of default tags to apply to resources"
267
+ default = {
268
+ project = "Learning Live with AWS & HashiCorp"
269
+ }
270
+ }
271
+
272
+ resource "aws_eip" "nat" {
273
+ vpc = true
274
+ tags = {
275
+ "Name" = "\${var.default_tags.project}-nat-eip"
276
+ }
277
+ }
278
+ `,
279
+ [binding.aws],
280
+ Snapshot.yes_all_languages,
281
+ Synth.yes_but_only_typescript_right_now_because_it_breaks,
282
+ {
283
+ resources: [],
284
+ },
285
+ );
286
+
287
+ testCase.test(
288
+ "strings containing single outer quotes are supported",
289
+ `
290
+ output "hash" {
291
+ value = "'static'"
292
+ }`,
293
+ [],
294
+ Snapshot.yes,
295
+ Synth.yes,
296
+ {
297
+ resources: [],
298
+ },
299
+ );
300
+ });
@@ -0,0 +1,154 @@
1
+ // Copyright (c) HashiCorp, Inc
2
+ // SPDX-License-Identifier: MPL-2.0
3
+ import { testCase, Synth, binding, Snapshot } from "./helpers/convert";
4
+
5
+ describe("type coercion", () => {
6
+ testCase.test(
7
+ "variables used in resources need to use stringValue",
8
+ `
9
+ provider "aws" {
10
+ region = "us-east-1"
11
+ }
12
+ variable "zone_id" {
13
+ type = string
14
+ }
15
+ resource "aws_route53_record" "example_aws_route53_simple_record" {
16
+ zone_id = var.zone_id
17
+ name = "example.com"
18
+ type = "TXT"
19
+ ttl = "300"
20
+ records = ["example"]
21
+ }
22
+ `,
23
+ [binding.aws],
24
+ Snapshot.yes,
25
+ Synth.yes,
26
+ { resources: ["aws_route53_record"] },
27
+ );
28
+
29
+ testCase.test(
30
+ "function arguments get coerced to the correct type",
31
+ `
32
+ provider "aws" {
33
+ region = "us-east-1"
34
+ }
35
+ variable "zone_id" {
36
+ type = string
37
+ }
38
+ resource "aws_route53_record" "example_aws_route53_simple_record" {
39
+ zone_id = textencodebase64(var.zone_id, "UTF-16LE")
40
+ name = "example.com"
41
+ type = "TXT"
42
+ ttl = "300"
43
+ records = ["example"]
44
+ }
45
+ `,
46
+ [binding.aws],
47
+ Snapshot.yes,
48
+ Synth.yes,
49
+ { resources: ["aws_route53_record"] },
50
+ );
51
+
52
+ testCase.test(
53
+ "references used in resources need to use Token.asString",
54
+ `
55
+ provider "aws" {
56
+ region = "us-east-1"
57
+ }
58
+ resource "aws_route53_zone" "example_aws_route53_simple_zone" {
59
+ name = "example.com"
60
+ }
61
+ resource "aws_route53_record" "example_aws_route53_simple_record" {
62
+ zone_id = aws_route53_zone.example_aws_route53_simple_zone.zone_id
63
+ name = "example.com"
64
+ type = "TXT"
65
+ ttl = "300"
66
+ records = ["example"]
67
+ }
68
+ `,
69
+ [binding.aws],
70
+ Snapshot.yes,
71
+ Synth.yes,
72
+ { resources: ["aws_route53_zone", "aws_route53_record"] },
73
+ );
74
+
75
+ testCase.test(
76
+ "references used in resource for_eachs need to understand how the value is used",
77
+ `
78
+ provider "aws" {
79
+ region = "us-east-1"
80
+ }
81
+ variable "iam_groups_users" {
82
+ type = map(object({
83
+ groups = list(string)
84
+ }))
85
+ }
86
+ resource "aws_iam_user_group_membership" "iam_user_group_membership_user_groups" {
87
+ for_each = var.iam_groups_users
88
+ user = each.key
89
+ groups = each.value["groups"]
90
+ }
91
+ `,
92
+ [binding.aws],
93
+ Snapshot.yes,
94
+ Synth.yes,
95
+ { resources: ["aws_iam_user_group_membership"] },
96
+ );
97
+
98
+ testCase.test(
99
+ "expressions need to be coerced to required type",
100
+ `
101
+ provider "aws" {
102
+ region = "us-east-1"
103
+ }
104
+ variable "example_spot_and_fargate_name" {
105
+ type = string
106
+ }
107
+ variable "role_arn" {
108
+ type = string
109
+ }
110
+ variable "private_subnets" {
111
+ type = list(object({ id = string }))
112
+ }
113
+ variable "public_subnets" {
114
+ type = list(object({ id = string }))
115
+ }
116
+ resource "aws_eks_cluster" "example_spot_and_fargate_eks_cluster" {
117
+ name = "$\{var.example_spot_and_fargate_name}"
118
+ role_arn = var.role_arn
119
+
120
+ vpc_config {
121
+ subnet_ids = concat(var.private_subnets.*.id, var.public_subnets.*.id)
122
+ }
123
+ }
124
+ `,
125
+ [binding.aws],
126
+ Snapshot.yes,
127
+ Synth.yes,
128
+ { resources: ["aws_eks_cluster"] },
129
+ );
130
+
131
+ testCase.test(
132
+ "coerces string literal to number",
133
+ `
134
+ provider "aws" {
135
+ region = "us-east-1"
136
+ }
137
+
138
+ resource "aws_security_group_rule" "client_alb_allow_outbound" {
139
+ security_group_id = "sg-1234567890"
140
+ type = "egress"
141
+ protocol = -1
142
+ from_port = 0
143
+ to_port = 0
144
+ cidr_blocks = ["0.0.0.0/0"]
145
+ ipv6_cidr_blocks = ["::/0"]
146
+ description = "Allow any outbound traffic."
147
+ }
148
+ `,
149
+ [binding.aws],
150
+ Snapshot.yes,
151
+ Synth.yes,
152
+ { resources: ["aws_security_group_rule"] },
153
+ );
154
+ });
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Copyright (c) HashiCorp, Inc.
3
+ * SPDX-License-Identifier: MPL-2.0
4
+ */
5
+
6
+ import { testCase, Synth, binding, Snapshot } from "./helpers/convert";
7
+
8
+ describe("variables", () => {
9
+ testCase.test(
10
+ "basic",
11
+ `
12
+ provider "local" {}
13
+ variable "image_id" {
14
+ type = string
15
+ }
16
+ data "local_file" "foo" {
17
+ filename = "./\${var.image_id}.img"
18
+ }
19
+ `,
20
+ [binding.local],
21
+ Snapshot.yes,
22
+ Synth.yes,
23
+ );
24
+
25
+ testCase.test(
26
+ "complex type and default",
27
+ `
28
+ provider "local" {}
29
+ variable "docker_ports" {
30
+ type = list(object({
31
+ internal = number
32
+ external = number
33
+ protocol = string
34
+ }))
35
+ default = [
36
+ {
37
+ internal = 8300
38
+ external = 8300
39
+ protocol = "tcp"
40
+ }
41
+ ]
42
+ }
43
+
44
+ data "local_file" "foo" {
45
+ filename = "./\${var.docker_ports[0].protocol}.img"
46
+ }`,
47
+ [binding.local],
48
+ Snapshot.yes,
49
+ Synth.yes,
50
+ );
51
+
52
+ testCase.test(
53
+ "sensitive and required",
54
+ `
55
+ provider "local" {}
56
+ variable "user_information" {
57
+ type = object({
58
+ name = string
59
+ address = string
60
+ })
61
+ sensitive = true
62
+ nullable = false
63
+ }
64
+
65
+ data "local_file" "foo" {
66
+ filename = "./\${var.user_information.name}.img"
67
+ }
68
+ `,
69
+ [binding.local],
70
+ Snapshot.yes,
71
+ Synth.yes,
72
+ );
73
+
74
+ testCase.test(
75
+ "validations",
76
+ `
77
+ provider "local" {}
78
+ variable "image_id" {
79
+ type = string
80
+ description = "The id of the machine image (AMI) to use for the server."
81
+
82
+ validation {
83
+ condition = length(var.image_id) > 4 && substr(var.image_id, 0, 4) == "ami-"
84
+ error_message = "The image_id value must be a valid AMI id, starting with ami-."
85
+ }
86
+ }
87
+
88
+ data "local_file" "foo" {
89
+ filename = "./\${var.image_id}.img"
90
+ }
91
+ `,
92
+ [binding.local],
93
+ Snapshot.yes,
94
+ Synth.yes,
95
+ );
96
+ });