@cdktn/hcl2cdk 0.24.0-pre.43 → 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,143 @@
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("modules", () => {
9
+ testCase.test(
10
+ "modules",
11
+ `
12
+ module "vpc" {
13
+ source = "terraform-aws-modules/vpc/aws"
14
+
15
+ name = "my-vpc"
16
+ cidr = "10.0.0.0/16"
17
+
18
+ azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
19
+ private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
20
+ public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
21
+
22
+ enable_nat_gateway = true
23
+ enable_vpn_gateway = true
24
+
25
+ tags = {
26
+ Terraform = "true"
27
+ Environment = "dev"
28
+ }
29
+ }`,
30
+ [binding.awsVpc],
31
+ Snapshot.yes,
32
+ Synth.yes,
33
+ );
34
+
35
+ testCase.test(
36
+ "duplicate modules",
37
+ `
38
+ module "vpca" {
39
+ source = "terraform-aws-modules/vpc/aws"
40
+
41
+ name = "my-vpc-a"
42
+ }
43
+
44
+ module "vpcb" {
45
+ source = "terraform-aws-modules/vpc/aws"
46
+
47
+ name = "my-vpc-b"
48
+ }`,
49
+ [binding.awsVpc],
50
+ Snapshot.yes,
51
+ Synth.yes,
52
+ );
53
+
54
+ testCase.test(
55
+ "referenced modules",
56
+ `
57
+ module "vpc" {
58
+ source = "terraform-aws-modules/vpc/aws"
59
+
60
+ name = "my-vpc"
61
+ cidr = "10.0.0.0/16"
62
+
63
+ azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
64
+ private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
65
+ public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
66
+
67
+ enable_nat_gateway = true
68
+ enable_vpn_gateway = true
69
+
70
+ tags = {
71
+ Terraform = "true"
72
+ Environment = "dev"
73
+ }
74
+ }
75
+
76
+ output "subnet_ids" {
77
+ value = module.vpc.public_subnets
78
+ }`,
79
+ [binding.awsVpc],
80
+ Snapshot.yes,
81
+ Synth.yes,
82
+ );
83
+
84
+ testCase.test(
85
+ "local module",
86
+ `
87
+ module "aws_vpc" {
88
+ source = "./aws_vpc"
89
+ }
90
+ `,
91
+ [],
92
+ Snapshot.yes,
93
+ Synth.never,
94
+ );
95
+
96
+ testCase.test(
97
+ "terraform workspace",
98
+ `
99
+ module "example" {
100
+ source = "./my-module"
101
+ name_prefix = "app-\${terraform.workspace}"
102
+ }
103
+ `,
104
+ [],
105
+ Snapshot.yes,
106
+ Synth.never,
107
+ );
108
+
109
+ testCase.test(
110
+ "all module types",
111
+ `
112
+ module "consul" {
113
+ source = "./consul"
114
+ }
115
+ module "consul-registry" {
116
+ source = "hashicorp/consul/aws"
117
+ version = "0.1.0"
118
+ }
119
+ module "consul-hosted-registry" {
120
+ source = "app.terraform.io/example-corp/k8s-cluster/azurerm"
121
+ version = "1.1.0"
122
+ }
123
+ module "consul-github" {
124
+ source = "github.com/hashicorp/example"
125
+ }
126
+ module "consul-git" {
127
+ source = "git@github.com:hashicorp/example.git"
128
+ }
129
+ module "consul-butbucket" {
130
+ source = "bitbucket.org/hashicorp/terraform-consul-aws"
131
+ }
132
+ module "vpc" {
133
+ source = "git::https://example.com/vpc.git"
134
+ }
135
+ module "storage" {
136
+ source = "git::ssh://username@example.com/storage.git"
137
+ }
138
+ `,
139
+ [],
140
+ Snapshot.yes,
141
+ Synth.never,
142
+ );
143
+ });
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Copyright (c) HashiCorp, Inc.
3
+ * SPDX-License-Identifier: MPL-2.0
4
+ */
5
+
6
+ import { testCase, Synth, Snapshot } from "./helpers/convert";
7
+
8
+ describe("outputs", () => {
9
+ testCase.test(
10
+ "output",
11
+ `
12
+ output "cidr_out" {
13
+ value = "test"
14
+ }`,
15
+ [],
16
+ Snapshot.yes,
17
+ Synth.yes,
18
+ );
19
+ testCase.test(
20
+ "sensitive output",
21
+ `
22
+ output "cidr_out" {
23
+ value = "test"
24
+ sensitive = true
25
+ }`,
26
+
27
+ [],
28
+ Snapshot.yes,
29
+ Synth.yes,
30
+ );
31
+ testCase.test(
32
+ "output withdescription",
33
+ `
34
+ output "cidr_out" {
35
+ value = "test"
36
+ sensitive = true
37
+ description = "Best output"
38
+ }`,
39
+ [],
40
+ Snapshot.yes,
41
+ Synth.yes,
42
+ );
43
+
44
+ testCase.test(
45
+ "multiple outputs",
46
+ `
47
+ output "first_cidr_out" {
48
+ value = "first"
49
+ }
50
+ output "second_cidr_out" {
51
+ value = "second"
52
+ }`,
53
+ [],
54
+ Snapshot.yes,
55
+ Synth.yes,
56
+ );
57
+
58
+ testCase.test(
59
+ "number output",
60
+ `
61
+ output "test" {
62
+ value = 42
63
+ }
64
+ `,
65
+ [],
66
+ Snapshot.yes,
67
+ Synth.yes,
68
+ );
69
+ });
@@ -0,0 +1,25 @@
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("Partial code (missing properties)", () => {
9
+ testCase.test(
10
+ "missing top-level required property",
11
+ `
12
+ resource "docker_config" "service_config" {
13
+ name = "my-config"
14
+ # other required properties are missing:
15
+ # data = base64encode(...)
16
+ }
17
+ `,
18
+ [binding.docker],
19
+ Snapshot.yes_all_languages,
20
+ Synth.never,
21
+ {
22
+ resources: ["docker_config"],
23
+ },
24
+ );
25
+ });
@@ -0,0 +1,106 @@
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("provider", () => {
9
+ testCase.test(
10
+ "empty provider",
11
+ `provider "docker" {}`,
12
+ [binding.docker],
13
+ Snapshot.yes,
14
+ Synth.yes,
15
+ );
16
+ testCase.test(
17
+ "null provider",
18
+ `provider "null" {}
19
+ resource "null_resource" "test" {}
20
+ `,
21
+ [binding.null],
22
+ Snapshot.yes,
23
+ Synth.yes,
24
+ );
25
+
26
+ testCase.test(
27
+ "provider with complex config",
28
+ `
29
+ provider "aws" {
30
+ access_key = "mock_access_key"
31
+ region = "us-east-1"
32
+ secret_key = "mock_secret_key"
33
+ skip_credentials_validation = true
34
+ skip_requesting_account_id = true
35
+
36
+ endpoints {
37
+ dynamodb = "http://localhost:8000"
38
+ }
39
+ }
40
+ `,
41
+ [binding.aws],
42
+ Snapshot.yes,
43
+ Synth.yes,
44
+ {},
45
+ );
46
+
47
+ testCase.test(
48
+ "provider alias",
49
+ `
50
+ provider "aws" {
51
+ region = "us-east-1"
52
+ }
53
+
54
+ provider "aws" {
55
+ alias = "west"
56
+ region = "us-west-2"
57
+ }
58
+
59
+ resource "aws_instance" "foo" {
60
+ provider = aws.west
61
+
62
+ foo = "bar"
63
+ }
64
+
65
+ module "vpc" {
66
+ source = "terraform-aws-modules/vpc/aws"
67
+ providers = {
68
+ aws = aws.west
69
+ }
70
+ }
71
+ `,
72
+ [binding.aws, binding.awsVpc],
73
+ Snapshot.yes,
74
+ Synth.never,
75
+ {
76
+ resources: ["aws_instance"],
77
+ },
78
+ );
79
+
80
+ testCase.test(
81
+ "required namespaced provider",
82
+ `
83
+ variable "domain" {
84
+ description = "A domain"
85
+ }
86
+ provider "auth0" {
87
+ domain = var.domain
88
+ client_id = "42"
89
+ client_secret = "secret"
90
+ }
91
+
92
+ terraform {
93
+ required_providers {
94
+ auth0 = {
95
+ source = "alexkappa/auth0"
96
+ version = "0.19.0"
97
+ }
98
+ }
99
+ }
100
+ `,
101
+ [binding.auth0],
102
+ Snapshot.yes,
103
+ Synth.yes,
104
+ {},
105
+ );
106
+ });
@@ -0,0 +1,287 @@
1
+ // Copyright (c) HashiCorp, Inc
2
+ // SPDX-License-Identifier: MPL-2.0
3
+ import { testCase, Synth, binding, Snapshot } from "./helpers/convert";
4
+
5
+ describe("references", () => {
6
+ testCase.test(
7
+ "resource references",
8
+ `
9
+ provider "aws" {
10
+ region = "us-east-1"
11
+ }
12
+ resource "aws_kms_key" "examplekms" {
13
+ description = "KMS key 1"
14
+ deletion_window_in_days = 7
15
+ }
16
+
17
+ resource "aws_s3_bucket" "examplebucket" {
18
+ bucket = "examplebuckettftest"
19
+ acl = "private"
20
+ }
21
+
22
+ resource "aws_s3_bucket_object" "examplebucket_object" {
23
+ key = "someobject"
24
+ bucket = aws_s3_bucket.examplebucket.id
25
+ source = "index.html"
26
+ kms_key_id = aws_kms_key.examplekms.arn
27
+ }`,
28
+ [binding.aws],
29
+ Snapshot.yes,
30
+ Synth.yes,
31
+ {
32
+ resources: ["aws_s3_bucket", "aws_s3_bucket_object", "aws_kms_key"],
33
+ },
34
+ );
35
+
36
+ testCase.test(
37
+ "locals references",
38
+ `
39
+ provider "aws" {
40
+ region = "us-east-1"
41
+ }
42
+
43
+ locals {
44
+ bucket_name = "foo"
45
+ }
46
+
47
+ resource "aws_s3_bucket" "examplebucket" {
48
+ bucket = local.bucket_name
49
+ acl = "private"
50
+ }`,
51
+ [binding.aws],
52
+ Snapshot.yes,
53
+ Synth.yes,
54
+ {
55
+ resources: ["aws_s3_bucket"],
56
+ },
57
+ );
58
+
59
+ testCase.test(
60
+ "variable references",
61
+ `
62
+ provider "aws" {
63
+ region = "us-east-1"
64
+ }
65
+ variable "bucket_name" {
66
+ type = string
67
+ default = "demo"
68
+ }
69
+
70
+ resource "aws_s3_bucket" "examplebucket" {
71
+ bucket = var.bucket_name
72
+ acl = "private"
73
+ }`,
74
+ [binding.aws],
75
+ Snapshot.yes,
76
+ Synth.yes,
77
+ {
78
+ resources: ["aws_s3_bucket"],
79
+ },
80
+ );
81
+
82
+ testCase.test(
83
+ "data references",
84
+ `
85
+ provider "aws" {
86
+ region = "us-east-1"
87
+ }
88
+ variable "bucket_name" {
89
+ type = string
90
+ default = "demo"
91
+ }
92
+
93
+ data "aws_s3_bucket" "examplebucket" {
94
+ bucket = var.bucket_name
95
+ }
96
+
97
+ resource "aws_s3_bucket_object" "examplebucket_object" {
98
+ key = "someobject"
99
+ bucket = data.aws_s3_bucket.examplebucket.arn
100
+ source = "index.html"
101
+ }`,
102
+ [binding.aws],
103
+ Snapshot.yes,
104
+ Synth.yes,
105
+ {
106
+ resources: ["aws_s3_bucket", "aws_s3_bucket_object"],
107
+ },
108
+ );
109
+
110
+ testCase.test(
111
+ "double references",
112
+ `
113
+ provider "aws" {
114
+ region = "us-east-1"
115
+ }
116
+ variable "bucket_name" {
117
+ type = string
118
+ default = "demo"
119
+ }
120
+
121
+ resource "aws_s3_bucket" "examplebucket" {
122
+ bucket = var.bucket_name
123
+ acl = "private"
124
+ tags = {
125
+ tag-key = var.bucket_name
126
+ }
127
+ }`,
128
+ [binding.aws],
129
+ Snapshot.yes,
130
+ Synth.yes,
131
+ {
132
+ resources: ["aws_s3_bucket"],
133
+ },
134
+ );
135
+
136
+ testCase.test(
137
+ "resource references with HCL functions",
138
+ `
139
+ provider "aws" {
140
+ region = "us-east-1"
141
+ }
142
+ resource "aws_kms_key" "examplekms" {
143
+ description = "KMS key 1"
144
+ deletion_window_in_days = 7
145
+ }
146
+
147
+ resource "aws_s3_bucket" "examplebucket" {
148
+ count = 2
149
+ bucket = "examplebuckettftest-\${count.index}"
150
+ acl = "private"
151
+ }
152
+
153
+ resource "aws_s3_bucket_object" "examplebucket_object" {
154
+ key = "someobject"
155
+ bucket = element(aws_s3_bucket.examplebucket, 0).id
156
+ source = "index.html"
157
+ kms_key_id = aws_kms_key.examplekms.arn
158
+ }`,
159
+ [binding.aws],
160
+ Snapshot.yes,
161
+ Synth.no_cant_resolve_construct,
162
+ {
163
+ resources: ["aws_s3_bucket", "aws_s3_bucket_object", "aws_kms_key"],
164
+ },
165
+ );
166
+
167
+ testCase.test(
168
+ "provider with var reference",
169
+ `
170
+ variable "domain" {
171
+ description = "A domain"
172
+ }
173
+ provider "auth0" {
174
+ domain = var.domain
175
+ client_id = "client_id"
176
+ client_secret = "client_secret"
177
+ }
178
+ `,
179
+ [binding.auth0],
180
+ Snapshot.yes,
181
+ Synth.yes,
182
+ { resources: [] },
183
+ );
184
+
185
+ testCase.test(
186
+ "data local_file",
187
+ `
188
+ provider "local" {}
189
+ data "local_file" "_01_please_verify" {
190
+ filename = "./email_templates/01_please_verify/template.html"
191
+ }
192
+ `,
193
+ [binding.local],
194
+ Snapshot.yes,
195
+ Synth.yes,
196
+ {
197
+ dataSources: ["local_file"],
198
+ },
199
+ );
200
+
201
+ testCase.test(
202
+ "aliased duplicate provider with var reference",
203
+ `
204
+ variable "domain" {
205
+ description = "A domain"
206
+ }
207
+ variable "client_id" {
208
+ description = "A client_id"
209
+ }
210
+ variable "client_secret" {
211
+ description = "A client_secret"
212
+ }
213
+ provider "auth0" {
214
+ domain = var.domain
215
+ client_id = var.client_id
216
+ client_secret = var.client_secret
217
+ }
218
+ provider "auth0" {
219
+ alias = "private_auth0"
220
+ domain = var.domain
221
+ client_id = var.client_id
222
+ client_secret = var.client_secret
223
+ debug = true
224
+ }
225
+ `,
226
+ [binding.auth0],
227
+ Snapshot.yes,
228
+ Synth.yes,
229
+ { resources: [] },
230
+ );
231
+
232
+ testCase.test(
233
+ "variables with maps need to use accessor syntax",
234
+ `
235
+ provider "aws" {
236
+ region = "us-east-1"
237
+ }
238
+
239
+ variable "default_tags" {
240
+ type = map(string)
241
+ description = "Map of default tags to apply to resources"
242
+ default = {
243
+ project = "Learning Live with AWS & HashiCorp"
244
+ }
245
+ }
246
+
247
+ resource "aws_eip" "nat" {
248
+ vpc = true
249
+ tags = {
250
+ "Name" = "\${var.default_tags.project}-nat-eip"
251
+ }
252
+ }
253
+ `,
254
+ [binding.aws],
255
+ Snapshot.yes,
256
+ Synth.yes,
257
+ { resources: ["aws_eip"] },
258
+ );
259
+
260
+ testCase.test(
261
+ "resources with splat expressions should work",
262
+ `
263
+ variable "key_value_pairs" {
264
+ type = list(object({
265
+ foo = string
266
+ bar = string
267
+ }))
268
+ description = "List of key value pairs"
269
+ default = [{
270
+ foo = "foo"
271
+ bar = "not food",
272
+ }, {
273
+ foo = "bar"
274
+ bar = "not beer",
275
+ }]
276
+ }
277
+
278
+ output "values" {
279
+ value = var.key_value_pairs[*].bar
280
+ }
281
+ `,
282
+ [],
283
+ Snapshot.yes,
284
+ Synth.yes,
285
+ { resources: ["aws_eip"] },
286
+ );
287
+ });