@cdktn/provider-generator 0.26.0-pre.3 → 0.26.0-pre.31

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 (65) hide show
  1. package/build/__tests__/erasable-syntax.test.d.ts +2 -0
  2. package/build/__tests__/erasable-syntax.test.js +96 -0
  3. package/build/get/__tests__/generator/empty-provider-resources.test.js +5 -1
  4. package/build/get/__tests__/generator/module-docs-registry.test.js +8 -10
  5. package/build/get/__tests__/generator/module-generator.test.js +7 -10
  6. package/build/get/__tests__/target-versions.test.js +4 -2
  7. package/build/get/constructs-maker.js +3 -3
  8. package/build/get/generator/models/scope.js +2 -14
  9. package/build/get/generator/resource-parser.js +3 -3
  10. package/eslint.config.mjs +11 -0
  11. package/jest.config.js +0 -1
  12. package/package.json +5 -5
  13. package/package.sh +0 -0
  14. package/src/__tests__/edge-provider-schema/builder.ts +0 -201
  15. package/src/__tests__/edge-provider-schema/cli.ts +0 -51
  16. package/src/__tests__/edge-provider-schema/index.ts +0 -284
  17. package/src/__tests__/edge-provider-schema.test.ts +0 -24
  18. package/src/__tests__/provider.test.ts +0 -208
  19. package/src/get/__tests__/constructs-maker.test.ts +0 -242
  20. package/src/get/__tests__/generator/complex-computed-types.test.ts +0 -28
  21. package/src/get/__tests__/generator/deep-nested-attributes.test.ts +0 -56
  22. package/src/get/__tests__/generator/description-escaping.test.ts +0 -84
  23. package/src/get/__tests__/generator/empty-provider-resources.test.ts +0 -26
  24. package/src/get/__tests__/generator/ephemeral-resources.test.ts +0 -46
  25. package/src/get/__tests__/generator/export-sharding.test.ts +0 -169
  26. package/src/get/__tests__/generator/import-style.test.ts +0 -129
  27. package/src/get/__tests__/generator/module-docs-registry.test.ts +0 -89
  28. package/src/get/__tests__/generator/module-generator.test.ts +0 -528
  29. package/src/get/__tests__/generator/nested-types.test.ts +0 -54
  30. package/src/get/__tests__/generator/provider-functions-name-collision.test.ts +0 -84
  31. package/src/get/__tests__/generator/provider-functions.test.ts +0 -262
  32. package/src/get/__tests__/generator/provider-source-matching.test.ts +0 -84
  33. package/src/get/__tests__/generator/provider.test.ts +0 -59
  34. package/src/get/__tests__/generator/resource-types.test.ts +0 -118
  35. package/src/get/__tests__/generator/skipped-attributes.test.ts +0 -72
  36. package/src/get/__tests__/generator/supported-stored-classes.test.ts +0 -147
  37. package/src/get/__tests__/generator/types.test.ts +0 -686
  38. package/src/get/__tests__/generator/versions-file.test.ts +0 -77
  39. package/src/get/__tests__/generator/write-only.test.ts +0 -71
  40. package/src/get/__tests__/registry-class-invalidation.test.ts +0 -123
  41. package/src/get/__tests__/target-versions.test.ts +0 -249
  42. package/src/get/__tests__/util.ts +0 -80
  43. package/src/get/constructs-maker.ts +0 -991
  44. package/src/get/generator/custom-defaults.ts +0 -494
  45. package/src/get/generator/emitter/attributes-emitter.ts +0 -316
  46. package/src/get/generator/emitter/index.ts +0 -6
  47. package/src/get/generator/emitter/provider-functions-emitter.ts +0 -117
  48. package/src/get/generator/emitter/resource-emitter.ts +0 -330
  49. package/src/get/generator/emitter/struct-emitter.ts +0 -683
  50. package/src/get/generator/loop-detection.ts +0 -81
  51. package/src/get/generator/models/attribute-model.ts +0 -273
  52. package/src/get/generator/models/attribute-type-model.ts +0 -559
  53. package/src/get/generator/models/index.ts +0 -8
  54. package/src/get/generator/models/provider-function-model.ts +0 -872
  55. package/src/get/generator/models/resource-model.ts +0 -206
  56. package/src/get/generator/models/scope.ts +0 -54
  57. package/src/get/generator/models/struct.ts +0 -124
  58. package/src/get/generator/models/supported-stored-classes.ts +0 -88
  59. package/src/get/generator/module-generator.ts +0 -236
  60. package/src/get/generator/provider-generator.ts +0 -451
  61. package/src/get/generator/resource-parser.ts +0 -782
  62. package/src/get/generator/sanitized-comments.ts +0 -49
  63. package/src/get/generator/skipped-attributes.ts +0 -27
  64. package/src/index.ts +0 -40
  65. package/src/util.ts +0 -26
@@ -1,782 +0,0 @@
1
- // Copyright (c) HashiCorp, Inc
2
- // SPDX-License-Identifier: MPL-2.0
3
- import { toCamelCase, toPascalCase, toSnakeCase } from "codemaker";
4
- import {
5
- Attribute,
6
- AttributeNestedType,
7
- AttributeType,
8
- Block,
9
- BlockType,
10
- ConstructsMakerTarget,
11
- isAttributeNestedType,
12
- isNestedTypeAttribute,
13
- Schema,
14
- TerraformTargetVersions,
15
- } from "@cdktn/commons";
16
- import { ProviderName, FQPN, parseFQPN } from "@cdktn/provider-schema";
17
- import {
18
- ResourceModel,
19
- AttributeTypeModel,
20
- Struct,
21
- Scope,
22
- AttributeModel,
23
- SimpleAttributeTypeModel,
24
- ListAttributeTypeModel,
25
- SetAttributeTypeModel,
26
- MapAttributeTypeModel,
27
- StructAttributeTypeModel,
28
- SkippedAttributeTypeModel,
29
- } from "./models";
30
- import { detectAttributeLoops } from "./loop-detection";
31
- import { shouldSkipAttribute } from "./skipped-attributes";
32
-
33
- // Can't be used in expressions like "export * as <keyword> from ... "
34
- // filtered from all keywords from: https://github.com/microsoft/TypeScript/blob/503604c884bd0557c851b11b699ef98cdb65b93b/src/compiler/types.ts#L114-L197
35
- const RESERVED_KEYWORDS_FOR_NAMESPACES = [
36
- "implements",
37
- "interface",
38
- "let",
39
- "package",
40
- "private",
41
- "protected",
42
- "public",
43
- "static",
44
- "yield",
45
- "await",
46
- ];
47
-
48
- const COLLIDING_NAMESPACE_NAMES = [
49
- // e.g. hashicorp/consul – collides with the LICENSE file on case-insensitive filesystems in the Go package (#2627)
50
- "license",
51
- // collides for Go packages
52
- "version",
53
- ];
54
-
55
- const isReservedClassOrNamespaceName = (className: string): boolean => {
56
- return [
57
- "string",
58
- "object",
59
- "function",
60
- ...RESERVED_KEYWORDS_FOR_NAMESPACES,
61
- ...COLLIDING_NAMESPACE_NAMES,
62
- ].includes(className.toLowerCase());
63
- };
64
-
65
- const isReservedStructClassName = (className: string): boolean => {
66
- return className.toLowerCase().endsWith("list");
67
- };
68
-
69
- const getFileName = (provider: ProviderName, baseName: string): string => {
70
- if (baseName === "index") {
71
- return "index-resource/index.ts";
72
- }
73
-
74
- if (baseName === `${provider}_provider`) {
75
- return "provider/index.ts";
76
- }
77
-
78
- return `${toSnakeCase(baseName).replace(/_/g, "-")}/index.ts`;
79
- };
80
-
81
- export function sanitizeClassOrNamespaceName(
82
- baseName: string,
83
- isProvider = false,
84
- ) {
85
- const resourceIsNamedProvider = !isProvider && baseName === "provider";
86
- // A resource named `<provider>_provider_functions` maps to the same
87
- // providers/<provider>/provider-functions/ directory that the provider's
88
- // functions submodule is emitted to, and would silently overwrite it.
89
- const resourceIsNamedProviderFunctions =
90
- !isProvider && baseName === "provider_functions";
91
-
92
- if (
93
- isReservedClassOrNamespaceName(baseName) ||
94
- resourceIsNamedProvider ||
95
- resourceIsNamedProviderFunctions
96
- ) {
97
- return `${baseName}_resource`;
98
- } else {
99
- return baseName;
100
- }
101
- }
102
-
103
- /**
104
- * Remove attributes that may conflict after being snake cased
105
- * Example: oci_core_ipsec_connection_tunnel_management (hashicorp/oci@=5.21.0) has bgp_ipv6_state and bgp_ipv6state
106
- * (which both result in "bgpIpv6State" when camel-cased, with the second one being deprecated: true)
107
- * As we currently don't handle any deprecated ones at all, we'll just delete one of the two attributes for now
108
- * @param attributes
109
- */
110
- function deduplicateAttributesWithSameName(
111
- attributes: AttributeModel[],
112
- ): AttributeModel[] {
113
- return attributes.filter((attr, idx) => {
114
- const hasOtherWithSameName = attributes
115
- .slice(idx + 1) // only search after the index of the current attribute to avoid deleting both
116
- .some((other) => other.name === attr.name && other !== attr);
117
- return !hasOtherWithSameName;
118
- });
119
- }
120
-
121
- class Parser {
122
- private structs = new Array<Struct>();
123
-
124
- constructor(
125
- private classNames: string[],
126
- private usedBaseNames: Map<string, string>,
127
- private targetVersions?: TerraformTargetVersions,
128
- ) {}
129
-
130
- private uniqueClassName(className: string): string {
131
- if (this.classNames.includes(className)) {
132
- className = `${className}A`;
133
- }
134
- this.classNames.push(className);
135
- return className;
136
- }
137
-
138
- /**
139
- * Ensure base names are unique case-insensitively.
140
- *
141
- * Affected languages:
142
- * - Go: JSII-generated Go package names are fully lowercased, so
143
- * `load-balancer-backendset` and `load-balancer-backend-set` both
144
- * become package `loadbalancerbackendset`, causing a hard build failure:
145
- * "case-insensitive file name collision".
146
- * - C#: JSII generates PascalCase directories like
147
- * `LoadBalancerBackendset/` and `LoadBalancerBackendSet/` which the C#
148
- * compiler treats case-insensitively, causing CS2002 and CS0234 errors.
149
- */
150
- private uniqueBaseName(baseName: string): string {
151
- const pascalName = toPascalCase(baseName);
152
- const key = pascalName.toLowerCase();
153
- const existing = this.usedBaseNames.get(key);
154
-
155
- if (existing !== undefined && existing !== pascalName) {
156
- baseName = `${baseName}A`;
157
- return this.uniqueBaseName(baseName);
158
- }
159
-
160
- this.usedBaseNames.set(key, pascalName);
161
- return baseName;
162
- }
163
-
164
- public resourceFrom(
165
- fqpn: FQPN,
166
- type: string,
167
- schema: Schema,
168
- terraformSchemaType: string,
169
- constraint?: ConstructsMakerTarget,
170
- ): ResourceModel {
171
- let baseName = type;
172
-
173
- const providerNameFromConstraint = constraint
174
- ? (constraint.name as ProviderName)
175
- : undefined;
176
- const providerNameFromFQPN = parseFQPN(fqpn).name;
177
-
178
- if (baseName.startsWith(`${providerNameFromFQPN}_`)) {
179
- baseName = baseName.substr(providerNameFromFQPN.length + 1);
180
- }
181
-
182
- const providerName = providerNameFromConstraint
183
- ? providerNameFromConstraint
184
- : providerNameFromFQPN;
185
-
186
- const isProvider = terraformSchemaType === "provider";
187
- if (isProvider) {
188
- baseName = `${providerName}_${baseName}`;
189
- if (!("attributes" in schema.block)) {
190
- schema.block = {
191
- attributes: {},
192
- block_types: (schema.block as Block).block_types || {},
193
- };
194
- }
195
- // somehow missing from provider schema
196
- schema.block.attributes["alias"] = {
197
- type: "string",
198
- description: "Alias name",
199
- optional: true,
200
- computed: false,
201
- };
202
- }
203
-
204
- baseName = sanitizeClassOrNamespaceName(baseName, isProvider);
205
- baseName = this.uniqueBaseName(baseName);
206
-
207
- const className = this.uniqueClassName(toPascalCase(baseName));
208
- // avoid naming collision - see https://github.com/hashicorp/terraform-cdk/issues/299
209
- const configStructName = this.uniqueClassName(`${className}Config`);
210
- const fileName = getFileName(providerName, baseName);
211
-
212
- const filePath = `providers/${toSnakeCase(providerName)}/${fileName}`;
213
- let attributes = this.renderAttributesForBlock(
214
- new Scope({
215
- name: baseName,
216
- isProvider,
217
- parent: isProvider
218
- ? undefined
219
- : new Scope({ name: providerName, isProvider: true }),
220
- }),
221
- schema.block,
222
- );
223
-
224
- function getStructAttribute(
225
- attributes: AttributeModel[],
226
- path: string,
227
- ): AttributeModel {
228
- const [first, ...rest] = path.split(".");
229
- const attribute = attributes.find((att) => {
230
- return att.terraformName === first;
231
- });
232
- if (!attribute)
233
- throw new Error(
234
- `Expected to find recursive attribute at path: ${path}`,
235
- );
236
- if (!attribute.type.struct)
237
- throw new Error(
238
- `Expected to find struct type attribute at path: ${path} but got ${attribute.type.storedClassType}`,
239
- );
240
- if (rest.length === 0) return attribute;
241
- return getStructAttribute(
242
- attribute.type.struct.attributes,
243
- rest.join("."),
244
- );
245
- }
246
-
247
- // Introduce recursion for some attributes
248
- const recursiveAttributePaths = detectAttributeLoops(attributes);
249
-
250
- Object.entries(recursiveAttributePaths).forEach(
251
- ([attributePath, structPath]) => {
252
- // TODO: build this to be a bit more defensive (e.g. remove ! operator)
253
- const recursionTargetStructAttribute = getStructAttribute(
254
- attributes,
255
- structPath,
256
- );
257
- const parts = attributePath.split(".");
258
- const attributeName = parts.pop();
259
- const parentAttribute = getStructAttribute(attributes, parts.join("."));
260
- const indexToReplace =
261
- parentAttribute.type.struct!.attributes.findIndex(
262
- (att) => att.terraformName === attributeName,
263
- );
264
- if (indexToReplace === -1)
265
- throw new Error("Can't find attribute at path " + attributePath);
266
- const previousAttribute =
267
- parentAttribute.type.struct!.attributes[indexToReplace];
268
-
269
- parentAttribute.type.struct!.attributes[indexToReplace] =
270
- recursionTargetStructAttribute; // introduce recursion
271
-
272
- // ugly, pls c̶a̶l̶l̶ refactor me maybe
273
- // we store all structs in this.structs – now we need to dispose all structs that are part of previousAttribute
274
- const disposeStructs = (attr: AttributeModel) => {
275
- if (attr.type.struct) {
276
- attr.type.struct.attributes.forEach(disposeStructs);
277
- this.structs = this.structs.filter((s) => s !== attr.type.struct);
278
- }
279
- };
280
-
281
- disposeStructs(previousAttribute);
282
- },
283
- );
284
-
285
- attributes = deduplicateAttributesWithSameName(attributes);
286
-
287
- const resourceModel = new ResourceModel({
288
- terraformType: type,
289
- baseName,
290
- fileName,
291
- filePath,
292
- className,
293
- schema,
294
- fqpn,
295
- attributes,
296
- terraformSchemaType,
297
- structs: this.structs,
298
- configStructName,
299
- targetVersions: this.targetVersions,
300
- });
301
-
302
- return resourceModel;
303
- }
304
-
305
- private renderAttributeType(
306
- scope: Scope[],
307
- attributeType: AttributeType | AttributeNestedType,
308
- parentKind?: string,
309
- ): AttributeTypeModel {
310
- const parent = scope[scope.length - 1];
311
- if (shouldSkipAttribute(parent.baseName)) {
312
- return new MapAttributeTypeModel(new SimpleAttributeTypeModel("any"));
313
- }
314
-
315
- if (typeof attributeType === "string") {
316
- switch (attributeType) {
317
- case "bool":
318
- return new SimpleAttributeTypeModel("boolean");
319
- case "string":
320
- return new SimpleAttributeTypeModel("string");
321
- case "number":
322
- return new SimpleAttributeTypeModel("number");
323
- case "dynamic":
324
- return new MapAttributeTypeModel(new SimpleAttributeTypeModel("any"));
325
- default:
326
- throw new Error(`invalid primitive type ${attributeType}`);
327
- }
328
- }
329
-
330
- if (Array.isArray(attributeType)) {
331
- if (attributeType.length !== 2) {
332
- throw new Error(`unexpected array`);
333
- }
334
-
335
- const [kind, type] = attributeType;
336
-
337
- if (kind === "set" || kind === "list") {
338
- const elementType = this.renderAttributeType(
339
- scope,
340
- type as AttributeType,
341
- [kind, parentKind].join(""),
342
- );
343
- return kind === "list"
344
- ? new ListAttributeTypeModel(elementType, false, false)
345
- : new SetAttributeTypeModel(elementType, false, false);
346
- }
347
-
348
- if (kind === "map") {
349
- const valueType = this.renderAttributeType(
350
- scope,
351
- type as AttributeType,
352
- [kind, parentKind].join(""),
353
- );
354
- return new MapAttributeTypeModel(valueType);
355
- }
356
-
357
- if (kind === "object") {
358
- const objAttributes = type as { [name: string]: AttributeType };
359
- const attributes: { [name: string]: Attribute } = {};
360
- for (const [name, type] of Object.entries(objAttributes)) {
361
- attributes[name] = { type };
362
- }
363
- const struct = this.addAnonymousStruct(
364
- scope,
365
- attributes,
366
- parentKind ?? kind,
367
- );
368
- return new StructAttributeTypeModel(struct);
369
- }
370
- }
371
-
372
- if (isAttributeNestedType(attributeType)) {
373
- let struct = undefined;
374
- let typeModel = undefined;
375
- switch (attributeType.nesting_mode) {
376
- case "list":
377
- struct = this.addAnonymousStruct(
378
- scope,
379
- attributeType.attributes,
380
- attributeType.nesting_mode,
381
- );
382
- typeModel = new ListAttributeTypeModel(
383
- new StructAttributeTypeModel(struct),
384
- false,
385
- false,
386
- );
387
- break;
388
- case "set":
389
- struct = this.addAnonymousStruct(
390
- scope,
391
- attributeType.attributes,
392
- attributeType.nesting_mode,
393
- );
394
- typeModel = new SetAttributeTypeModel(
395
- new StructAttributeTypeModel(struct),
396
- false,
397
- false,
398
- );
399
- break;
400
- case "map":
401
- struct = this.addAnonymousStruct(
402
- scope,
403
- attributeType.attributes,
404
- attributeType.nesting_mode,
405
- );
406
- typeModel = new MapAttributeTypeModel(
407
- new StructAttributeTypeModel(struct),
408
- );
409
- break;
410
- case "single":
411
- struct = this.addAnonymousStruct(
412
- scope,
413
- attributeType.attributes,
414
- attributeType.nesting_mode,
415
- );
416
- typeModel = new StructAttributeTypeModel(struct);
417
- break;
418
- default: {
419
- throw new Error(
420
- `nested_type with nesting_mode "${
421
- attributeType.nesting_mode
422
- }" not supported (attribute scope: ${scope
423
- .map((s) => s.fullName)
424
- .join(",")}`,
425
- );
426
- }
427
- }
428
- return typeModel;
429
- }
430
-
431
- throw new Error(`unknown type ${JSON.stringify(attributeType)}`);
432
- }
433
-
434
- public renderAttributesForBlock(parentType: Scope, block: Block) {
435
- const attributes = new Array<AttributeModel>();
436
-
437
- for (const [terraformAttributeName, att] of Object.entries(
438
- block.attributes || {},
439
- )) {
440
- let type: AttributeTypeModel;
441
- let forcePlainGetterType = false;
442
- if (shouldSkipAttribute(parentType.fullName(terraformAttributeName))) {
443
- type = new SkippedAttributeTypeModel();
444
- forcePlainGetterType = true;
445
- } else {
446
- type = this.renderAttributeType(
447
- [
448
- parentType,
449
- new Scope({
450
- name: terraformAttributeName,
451
- parent: parentType,
452
- isProvider: parentType.isProvider,
453
- isComputed: !!att.computed,
454
- isOptional: !!att.optional,
455
- isRequired: !!att.required,
456
- isNestedType: isNestedTypeAttribute(att),
457
- }),
458
- ],
459
- att.type || att.nested_type,
460
- );
461
- }
462
-
463
- const name = toCamelCase(terraformAttributeName);
464
-
465
- attributes.push(
466
- new AttributeModel({
467
- terraformFullName: parentType.fullName(terraformAttributeName),
468
- description: att.description,
469
- name,
470
- storageName: `_${name}`,
471
- computed: !!att.computed,
472
- optional: !!att.optional,
473
- terraformName: terraformAttributeName,
474
- type,
475
- provider: parentType.isProvider,
476
- required: !!att.required,
477
- forcePlainGetterType,
478
- isWriteOnly: !!att.write_only,
479
- }),
480
- );
481
- }
482
-
483
- for (const [blockTypeName, blockType] of Object.entries(
484
- block.block_types || {},
485
- )) {
486
- if (shouldSkipAttribute(parentType.fullName(blockTypeName))) {
487
- const name = toCamelCase(blockTypeName);
488
- const parent = new Scope({
489
- name: blockTypeName,
490
- parent: parentType,
491
- isProvider: parentType.isProvider,
492
- });
493
- attributes.push(
494
- new AttributeModel({
495
- name,
496
- terraformName: blockTypeName,
497
- terraformFullName: parent.fullName(blockTypeName),
498
- type: new SkippedAttributeTypeModel(),
499
- description: `${blockTypeName} block`,
500
- storageName: `_${name}`,
501
- optional: true,
502
- computed: false,
503
- provider: parentType.isProvider,
504
- required: false,
505
- }),
506
- );
507
- continue;
508
- }
509
- // create a struct for this block
510
- let blockAttributes = this.renderAttributesForBlock(
511
- new Scope({
512
- name: `${parentType.name}_${blockTypeName}`,
513
- parent: parentType,
514
- isProvider: parentType.isProvider,
515
- inBlockType: true,
516
- }),
517
- blockType.block,
518
- );
519
-
520
- blockAttributes = deduplicateAttributesWithSameName(blockAttributes);
521
-
522
- const blockStruct = this.addStruct(
523
- [
524
- parentType,
525
- new Scope({
526
- name: blockTypeName,
527
- parent: parentType,
528
- isProvider: parentType.isProvider,
529
- }),
530
- ],
531
- blockAttributes,
532
- blockType.nesting_mode,
533
- (blockType.nesting_mode === "list" ||
534
- blockType.nesting_mode === "set") &&
535
- blockType.max_items === 1,
536
- );
537
-
538
- // define the attribute
539
- attributes.push(
540
- attributeForBlockType(
541
- blockTypeName,
542
- blockType,
543
- blockStruct,
544
- parentType.isProvider,
545
- parentType,
546
- ),
547
- );
548
- }
549
-
550
- return attributes;
551
-
552
- function attributeForBlockType(
553
- terraformName: string,
554
- blockType: BlockType,
555
- struct: Struct,
556
- isProvider: boolean,
557
- parent: Scope,
558
- ): AttributeModel {
559
- const name = toCamelCase(terraformName);
560
- let optional: boolean;
561
- let required: boolean;
562
-
563
- switch (blockType.nesting_mode) {
564
- case "single":
565
- optional = !struct.attributes.some((x) => !x.optional);
566
- required = !struct.attributes.some((x) => !x.required);
567
-
568
- // This is for bug #3570 as both optional and required evaluate to false under some circumstances
569
- // (this then causes the computed block to not be part of assignableAttributes and thus skipped in the generated code)
570
- // Hence: If both optional and required are false, set optional to true IF at least one
571
- // attribute in the block has optional = true or required = true, as this would mean that at least something can be set
572
- // and the block is not all computed.
573
- if (!optional && !required) {
574
- optional = struct.attributes.some((x) => x.optional || x.required);
575
- }
576
-
577
- return new AttributeModel({
578
- name,
579
- terraformName,
580
- terraformFullName: parent.fullName(terraformName),
581
- type: new StructAttributeTypeModel(struct),
582
- description: `${terraformName} block`,
583
- storageName: `_${name}`,
584
- optional,
585
- computed: false,
586
- provider: isProvider,
587
- required,
588
- });
589
-
590
- case "map":
591
- return new AttributeModel({
592
- name,
593
- terraformName,
594
- terraformFullName: parent.fullName(terraformName),
595
- type: new MapAttributeTypeModel(
596
- new StructAttributeTypeModel(struct),
597
- ),
598
- description: `${terraformName} block`,
599
- storageName: `_${name}`,
600
- optional: false,
601
- computed: false,
602
- provider: isProvider,
603
- required: false,
604
- });
605
-
606
- case "list":
607
- case "set":
608
- optional =
609
- blockType.min_items === undefined ? true : blockType.min_items < 1;
610
- required =
611
- blockType.min_items === undefined ? false : blockType.min_items > 0;
612
- return new AttributeModel({
613
- name,
614
- terraformName: terraformName,
615
- terraformFullName: parent.fullName(terraformName),
616
- type:
617
- blockType.nesting_mode === "list"
618
- ? new ListAttributeTypeModel(
619
- new StructAttributeTypeModel(struct),
620
- blockType.max_items === 1,
621
- true,
622
- )
623
- : new SetAttributeTypeModel(
624
- new StructAttributeTypeModel(struct),
625
- blockType.max_items === 1,
626
- true,
627
- ),
628
- description: `${terraformName} block`,
629
- storageName: `_${name}`,
630
- optional,
631
- computed: false,
632
- provider: isProvider,
633
- required,
634
- });
635
- }
636
- }
637
- }
638
- private addAnonymousStruct(
639
- scope: Scope[],
640
- attrs: { [name: string]: Attribute } | undefined,
641
- nesting_mode: string,
642
- ) {
643
- let attributes = new Array<AttributeModel>();
644
- const parent = scope[scope.length - 1];
645
- if (attrs) {
646
- for (const [terraformName, att] of Object.entries(attrs || {})) {
647
- // nested types support computed, optional and required on attribute level
648
- // if parent is computed, child always is computed as well
649
- const computed =
650
- !!parent.isComputed || (parent.isNestedType && !!att.computed);
651
- const optional = parent.isNestedType
652
- ? !!att.optional
653
- : !!parent.isOptional;
654
- const required = parent.isNestedType
655
- ? !!att.required
656
- : !!parent.isRequired;
657
- const name = toCamelCase(terraformName);
658
- const type = this.renderAttributeType(
659
- [
660
- ...scope,
661
- new Scope({
662
- name: terraformName,
663
- parent,
664
- isProvider: parent.isProvider,
665
- isComputed: computed,
666
- isOptional: optional,
667
- isRequired: required,
668
- isNestedType: parent.isNestedType,
669
- }),
670
- ],
671
- att.type || att.nested_type,
672
- );
673
- attributes.push(
674
- new AttributeModel({
675
- name,
676
- storageName: `_${name}`,
677
- computed: computed,
678
- description: att.description,
679
- optional: optional,
680
- terraformName,
681
- terraformFullName: parent.fullName(terraformName),
682
- type,
683
- provider: parent.isProvider,
684
- required: required,
685
- isWriteOnly: !!att.write_only,
686
- }),
687
- );
688
- }
689
- }
690
-
691
- attributes = deduplicateAttributesWithSameName(attributes);
692
-
693
- return this.addStruct(scope, attributes, nesting_mode);
694
- }
695
-
696
- private addStruct(
697
- scope: Scope[],
698
- attributes: AttributeModel[],
699
- nesting_mode: string,
700
- isSingleItem = false,
701
- ) {
702
- const possibleName = toPascalCase(
703
- scope.map((x) => toSnakeCase(x.name)).join("_"),
704
- );
705
- const name = this.uniqueClassName(
706
- isReservedStructClassName(possibleName)
707
- ? `${possibleName}Struct`
708
- : possibleName,
709
- );
710
-
711
- const parent = scope[scope.length - 1];
712
- // blockType.nesting_mode => list/set & blockType.max_items === 1,
713
- const isClass = (parent.isComputed && !parent.isOptional) || isSingleItem;
714
- const isAnonymous = true;
715
- const s = new Struct(
716
- name,
717
- attributes,
718
- isClass,
719
- isAnonymous,
720
- isSingleItem,
721
- nesting_mode,
722
- );
723
- this.structs.push(s);
724
- return s;
725
- }
726
- }
727
-
728
- export class ResourceParser {
729
- private uniqueClassnames: string[] = [];
730
- private uniqueBaseNames: Map<string, string> = new Map();
731
- private resources: Record<string, ResourceModel> = {};
732
-
733
- constructor(private readonly targetVersions?: TerraformTargetVersions) {}
734
-
735
- public parse(
736
- fqpn: FQPN,
737
- type: string,
738
- schema: Schema,
739
- terraformType: string,
740
- constraint?: ConstructsMakerTarget,
741
- ): ResourceModel {
742
- if (this.resources[type]) {
743
- return this.resources[type];
744
- }
745
-
746
- const parser = new Parser(
747
- this.uniqueClassnames,
748
- this.uniqueBaseNames,
749
- this.targetVersions,
750
- );
751
- const resource = parser.resourceFrom(
752
- fqpn,
753
- type,
754
- schema,
755
- terraformType,
756
- constraint,
757
- );
758
- this.resources[type] = resource;
759
- return resource;
760
- }
761
-
762
- // Used by convert to determine the right name for a class
763
- public getClassNameForResource(terraformType: string) {
764
- const resource = this.resources[terraformType];
765
- return resource ? resource.className : "";
766
- }
767
-
768
- // Used by convert to determine the right name for a namespace
769
- public getNamespaceNameForResource(terraformType: string) {
770
- // Special case external provider since the name of resource doesn't have a prefix
771
- if (terraformType === "data_external_") {
772
- terraformType = "data_external";
773
- }
774
-
775
- const resource = this.resources[terraformType];
776
- if (!resource) {
777
- return "";
778
- }
779
- const folder = `providers/${resource.provider}`;
780
- return resource.filePath.replace(`${folder}/`, "").replace("/index.ts", "");
781
- }
782
- }