@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,551 @@
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("resources", () => {
9
+ testCase.test(
10
+ "simple resource",
11
+ `
12
+ provider "aws" {
13
+ region = "us-east-1"
14
+ }
15
+ resource "aws_vpc" "example" {
16
+ cidr_block = "10.0.0.0/16"
17
+ }`,
18
+ [binding.aws],
19
+ Snapshot.yes,
20
+ Synth.yes,
21
+ {
22
+ resources: ["aws_vpc"],
23
+ },
24
+ );
25
+
26
+ testCase.test(
27
+ "complex resource",
28
+ `
29
+ provider "aws" {
30
+ region = "us-east-1"
31
+ }
32
+ resource "aws_cloudfront_distribution" "s3_distribution" {
33
+ origin {
34
+ domain_name = "aws_s3_bucket.b.bucket_regional_domain_name"
35
+ origin_id = "local_s3_origin_id"
36
+
37
+ s3_origin_config {
38
+ origin_access_identity = "origin-access-identity/cloudfront/ABCDEFG1234567"
39
+ }
40
+ }
41
+
42
+ enabled = true
43
+ is_ipv6_enabled = true
44
+ comment = "Some comment"
45
+ default_root_object = "index.html"
46
+
47
+ logging_config {
48
+ include_cookies = false
49
+ bucket = "mylogs.s3.amazonaws.com"
50
+ prefix = "myprefix"
51
+ }
52
+
53
+ aliases = ["mysite.example.com", "yoursite.example.com"]
54
+
55
+ default_cache_behavior {
56
+ allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
57
+ cached_methods = ["GET", "HEAD"]
58
+ target_origin_id = "local_s3_origin_id"
59
+
60
+ forwarded_values {
61
+ query_string = false
62
+
63
+ cookies {
64
+ forward = "none"
65
+ }
66
+ }
67
+
68
+ viewer_protocol_policy = "allow-all"
69
+ min_ttl = 0
70
+ default_ttl = 3600
71
+ max_ttl = 86400
72
+ }
73
+
74
+ # Cache behavior with precedence 0
75
+ ordered_cache_behavior {
76
+ path_pattern = "/content/immutable/*"
77
+ allowed_methods = ["GET", "HEAD", "OPTIONS"]
78
+ cached_methods = ["GET", "HEAD", "OPTIONS"]
79
+ target_origin_id = "local_s3_origin_id"
80
+
81
+ forwarded_values {
82
+ query_string = false
83
+ headers = ["Origin"]
84
+
85
+ cookies {
86
+ forward = "none"
87
+ }
88
+ }
89
+
90
+ min_ttl = 0
91
+ default_ttl = 86400
92
+ max_ttl = 31536000
93
+ compress = true
94
+ viewer_protocol_policy = "redirect-to-https"
95
+ }
96
+
97
+ # Cache behavior with precedence 1
98
+ ordered_cache_behavior {
99
+ path_pattern = "/content/*"
100
+ allowed_methods = ["GET", "HEAD", "OPTIONS"]
101
+ cached_methods = ["GET", "HEAD"]
102
+ target_origin_id = "local_s3_origin_id"
103
+
104
+ forwarded_values {
105
+ query_string = false
106
+
107
+ cookies {
108
+ forward = "none"
109
+ }
110
+ }
111
+
112
+ min_ttl = 0
113
+ default_ttl = 3600
114
+ max_ttl = 86400
115
+ compress = true
116
+ viewer_protocol_policy = "redirect-to-https"
117
+ }
118
+
119
+ price_class = "PriceClass_200"
120
+
121
+ restrictions {
122
+ geo_restriction {
123
+ restriction_type = "whitelist"
124
+ locations = ["US", "CA", "GB", "DE"]
125
+ }
126
+ }
127
+
128
+ tags = {
129
+ Environment = "production"
130
+ }
131
+
132
+ viewer_certificate {
133
+ cloudfront_default_certificate = true
134
+ }
135
+ }`,
136
+ [binding.aws],
137
+ Snapshot.yes,
138
+ Synth.yes,
139
+ {
140
+ resources: ["aws_cloudfront_distribution"],
141
+ },
142
+ );
143
+
144
+ testCase.test(
145
+ "simple data source",
146
+ `
147
+ provider "aws" {
148
+ region = "us-east-1"
149
+ }
150
+ data "aws_subnet" "selected" {
151
+ vpc_id = "subnet_id"
152
+ }`,
153
+ [binding.aws],
154
+ Snapshot.yes,
155
+ Synth.yes,
156
+ {
157
+ dataSources: ["aws_subnet"],
158
+ },
159
+ );
160
+
161
+ testCase.test(
162
+ "multiple blocks",
163
+ `
164
+ provider "aws" {
165
+ region = "us-east-1"
166
+ }
167
+ resource "aws_security_group" "allow_tls" {
168
+ name = "allow_tls"
169
+ description = "Allow TLS inbound traffic"
170
+
171
+ ingress {
172
+ description = "TLS from VPC"
173
+ from_port = 443
174
+ to_port = 443
175
+ protocol = "tcp"
176
+ }
177
+
178
+ ingress {
179
+ description = "TLS from VPC"
180
+ from_port = 80
181
+ to_port = 80
182
+ protocol = "tcp"
183
+ }
184
+
185
+ ingress {
186
+ from_port = 8080
187
+ to_port = 8080
188
+ protocol = "tcp"
189
+ }
190
+
191
+ egress {
192
+ from_port = 0
193
+ to_port = 0
194
+ protocol = "-1"
195
+ cidr_blocks = ["0.0.0.0/0"]
196
+ ipv6_cidr_blocks = ["::/0"]
197
+ }
198
+
199
+ tags = {
200
+ Name = "allow_tls"
201
+ }
202
+ }`,
203
+ [binding.aws],
204
+ Snapshot.yes,
205
+ Synth.yes,
206
+ {
207
+ resources: ["aws_security_group"],
208
+ },
209
+ );
210
+
211
+ testCase.test(
212
+ "blocks should be arrays",
213
+ `
214
+ provider "google" {
215
+ project = "my-project"
216
+ region = "us-central1"
217
+ }
218
+ resource "google_compute_autoscaler" "example" {
219
+ name = "example-autoscaler"
220
+ zone = "us-east1-b"
221
+ target = "target-for-example-autoscaler"
222
+
223
+ autoscaling_policy = {
224
+ max_replicas = 8
225
+ min_replicas = 2
226
+ cooldown_period = 60
227
+
228
+ cpu_utilization = {
229
+ target = 0.5
230
+ }
231
+ }
232
+ }
233
+ `,
234
+ [binding.google],
235
+ Snapshot.yes,
236
+ Synth.yes,
237
+ {
238
+ resources: ["google_compute_autoscaler"],
239
+ },
240
+ );
241
+
242
+ testCase.test(
243
+ "maps are not arrays",
244
+ `
245
+ provider "kubernetes" {
246
+ config_path = "~/.kube/config"
247
+ config_context = "my-context"
248
+ }
249
+
250
+ resource "kubernetes_secret" "secrets-xxx" {
251
+ metadata {
252
+ name = "secrets-xxx"
253
+ }
254
+ data = {
255
+ "xxx" : "yyy"
256
+ }
257
+ }
258
+ `,
259
+ [binding.kubernetes],
260
+ Snapshot.yes,
261
+ Synth.yes,
262
+ {
263
+ resources: ["kubernetes_secret"],
264
+ },
265
+ );
266
+
267
+ testCase.test(
268
+ "maps dont get camel case keys",
269
+ `
270
+ provider "kubernetes" {
271
+ config_path = "~/.kube/config"
272
+ config_context = "my-context"
273
+ }
274
+
275
+ resource "kubernetes_secret" "secrets-xxx" {
276
+ metadata {
277
+ name = "secrets-xxx"
278
+ }
279
+ data = {
280
+ "camel_cased_key": "yes"
281
+ }
282
+ }
283
+ `,
284
+ [binding.kubernetes],
285
+ Snapshot.yes,
286
+ Synth.yes,
287
+ {
288
+ resources: ["kubernetes_secret"],
289
+ },
290
+ );
291
+
292
+ testCase.test(
293
+ "same name local, var, out",
294
+ `
295
+ variable "test" {
296
+ type = string
297
+ }
298
+ locals {
299
+ test = "\${var.test} + 1"
300
+ }
301
+ output "test" {
302
+ value = "\${local.test}"
303
+ }
304
+ `,
305
+ [],
306
+ Snapshot.yes,
307
+ Synth.yes,
308
+ );
309
+
310
+ testCase.test(
311
+ "property level renamings",
312
+ `
313
+ provider "aws" {
314
+ region = "us-east-1"
315
+ }
316
+ resource "aws_guardduty_filter" "MyFilter" {
317
+ name = "MyFilter"
318
+ action = "ARCHIVE"
319
+ detector_id = "id"
320
+ rank = 1
321
+
322
+ finding_criteria {
323
+ criterion {
324
+ field = "region"
325
+ equals = ["eu-west-1"]
326
+ }
327
+
328
+ criterion {
329
+ field = "service.additionalInfo.threatListName"
330
+ not_equals = ["some-threat", "another-threat"]
331
+ }
332
+
333
+ criterion {
334
+ field = "updatedAt"
335
+ greater_than = "2020-01-01T00:00:00Z"
336
+ less_than = "2020-02-01T00:00:00Z"
337
+ }
338
+
339
+ criterion {
340
+ field = "severity"
341
+ greater_than_or_equal = "4"
342
+ }
343
+ }
344
+ }
345
+ `,
346
+ [binding.aws],
347
+ Snapshot.yes,
348
+ Synth.yes,
349
+ {
350
+ resources: ["aws_guardduty_filter"],
351
+ },
352
+ );
353
+
354
+ testCase.test(
355
+ "tricky to parse items",
356
+ `
357
+ provider "aws" {
358
+ region = "us-east-1"
359
+ }
360
+ variable "tags" {
361
+ type = map
362
+ }
363
+
364
+
365
+ resource "aws_instance" "play" {
366
+ ami = join("-", [var.tags.app, var.tags.env])
367
+ instance_type = "t3.small"
368
+ key_name = aws_key_pair.master_key.id
369
+ vpc_security_group_ids = [aws_security_group.ssh.id]
370
+ subnet_id = aws_subnet.main.id
371
+ associate_public_ip_address = true
372
+
373
+ connection {
374
+ type = "ssh"
375
+ user = "ubuntu"
376
+ private_key = file("./terraform_key")
377
+ host = self.public_ip
378
+ }
379
+ }
380
+ `,
381
+ [binding.aws],
382
+ Snapshot.yes,
383
+ Synth.never,
384
+ {
385
+ resources: ["aws_instance"],
386
+ },
387
+ );
388
+
389
+ /**
390
+ * Test for https://github.com/hashicorp/terraform-cdk/issues/2139
391
+ *
392
+ * This one currently fails because the convertion result looks like this:
393
+ * new scaleway.object.ObjectResource(this, "some_file", { ...
394
+ *
395
+ * Which should be:
396
+ * new scaleway.objectResource.ObjectResource(this, "some_file", { ...
397
+ *
398
+ * So it does already work for the class name but not for the namespace.
399
+ */
400
+ testCase.test(
401
+ "handle special resource names",
402
+ `
403
+ provider "scaleway" {
404
+ zone = "fr-par-1"
405
+ region = "fr-par"
406
+ }
407
+
408
+ resource "scaleway_object_bucket" "some_bucket" {
409
+ name = "some-unique-name"
410
+ }
411
+
412
+ resource scaleway_object "some_file" {
413
+ bucket = scaleway_object_bucket.some_bucket.name
414
+ key = "object_path"
415
+
416
+ file = "myfile"
417
+ hash = filemd5("myfile")
418
+ }
419
+ `,
420
+ [binding.scaleway],
421
+ Snapshot.yes,
422
+ Synth.yes,
423
+ {
424
+ resources: ["scaleway_object", "scaleway_object_bucket"],
425
+ },
426
+ );
427
+
428
+ testCase.test(
429
+ "handles special resource names without schema",
430
+ `
431
+ provider "scaleway" {
432
+ zone = "fr-par-1"
433
+ region = "fr-par"
434
+ }
435
+
436
+ resource "scaleway_object_bucket" "some_bucket" {
437
+ name = "some-unique-name"
438
+ }
439
+
440
+ resource scaleway_object "some_file" {
441
+ bucket = scaleway_object_bucket.some_bucket.name
442
+ key = "object_path"
443
+
444
+ file = "myfile"
445
+ hash = filemd5("myfile")
446
+ }
447
+ `,
448
+ [],
449
+ Snapshot.yes,
450
+ Synth.never,
451
+ );
452
+
453
+ testCase.test(
454
+ "access use fqn for data source",
455
+ `
456
+ provider "aws" {
457
+ region = "us-east-1"
458
+ }
459
+ data "aws_availability_zones" "changeme_az_list_ebs_snapshot" {
460
+ state = "available"
461
+ }
462
+ resource "aws_ebs_volume" "changeme_ebs_volume_snapshot" {
463
+ availability_zone = data.aws_availability_zones.changeme_az_list_ebs_snapshot.names[0]
464
+ size = 10
465
+ type = "standard"
466
+ encrypted = false
467
+ tags = {
468
+ Name = "changeme_ebs_volume_tag"
469
+ }
470
+ }
471
+ `,
472
+ [binding.aws],
473
+ Snapshot.yes,
474
+ Synth.yes,
475
+ {
476
+ resources: ["aws_ebs_volume"],
477
+ dataSources: ["aws_availability_zones"],
478
+ },
479
+ );
480
+
481
+ testCase.test(
482
+ "handles JSON encoded blocks",
483
+ `
484
+ provider "aws" {
485
+ region = "us-east-1"
486
+ }
487
+ resource "aws_ecs_task_definition" "client" {
488
+ family = "client"
489
+ requires_compatibilities = ["FARGATE"]
490
+ # required for Fargate launch type
491
+ memory = 512
492
+ cpu = 256
493
+ network_mode = "awsvpc"
494
+
495
+ container_definitions = jsonencode([
496
+ {
497
+ name = "client"
498
+ image = "crccheck/hello-world"
499
+ cpu = 0 # take up proportional cpu
500
+ essential = true
501
+
502
+ portMappings = [
503
+ {
504
+ containerPort = 8000
505
+ hostPort = 8000 # though, access to the ephemeral port range is needed to connect on EC2, the exact port is required on Fargate from a security group standpoint.
506
+ protocol = "tcp"
507
+ }
508
+ ]
509
+
510
+ # Fake Service settings are set via Environment variables
511
+ environment = [
512
+ {
513
+ name = "NAME"
514
+ value = "client"
515
+ },
516
+ {
517
+ name = "MESSAGE"
518
+ value = "Hello from the client!"
519
+ }
520
+ ]
521
+ }
522
+ ])
523
+ }
524
+ `,
525
+ [binding.aws],
526
+ Snapshot.yes,
527
+ Synth.no_missing_type_coercion,
528
+ {
529
+ resources: ["aws_ecs_task_definition"],
530
+ },
531
+ );
532
+
533
+ testCase.test(
534
+ "built-in data resource",
535
+ `
536
+ variable "revision" {
537
+ default = 1
538
+ }
539
+
540
+ resource "terraform_data" "replacement" {
541
+ input = var.revision
542
+ }
543
+ `,
544
+ [],
545
+ Snapshot.yes,
546
+ Synth.yes,
547
+ {
548
+ resources: ["terraform_data"],
549
+ },
550
+ );
551
+ });