From 7b383b801ac992f6133bbc0eb02a2b3f97e0e919 Mon Sep 17 00:00:00 2001 From: Nikolay Matrosov Date: Sun, 27 Jul 2025 19:05:50 +0200 Subject: [PATCH] feat(lib): add StringMapMap class --- go.work | 2 +- .../__snapshots__/types.test.ts.snap | 91 +++++++++++++++++++ .../map-of-map-of-string.test.fixture.json | 27 ++++++ .../lib/get/__tests__/generator/types.test.ts | 25 +++++ .../generator/models/attribute-type-model.ts | 4 + packages/cdktf/lib/complex-computed-list.ts | 26 ++++++ 6 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 packages/@cdktf/provider-generator/lib/get/__tests__/generator/fixtures/map-of-map-of-string.test.fixture.json diff --git a/go.work b/go.work index bb4b427f4f..93d767baf4 100644 --- a/go.work +++ b/go.work @@ -9,5 +9,5 @@ use ( ./examples/go/google-cloudrun ./examples/go/scaleway ./examples/go/ucloud - ./packages/cdktf/dist/go/cdktf // generated, run "yarn build" to generate + ./packages/cdktf/dist/go/cdktf // generated, run "yarn package" to generate ) diff --git a/packages/@cdktf/provider-generator/lib/get/__tests__/generator/__snapshots__/types.test.ts.snap b/packages/@cdktf/provider-generator/lib/get/__tests__/generator/__snapshots__/types.test.ts.snap index ef10089656..937cba6035 100644 --- a/packages/@cdktf/provider-generator/lib/get/__tests__/generator/__snapshots__/types.test.ts.snap +++ b/packages/@cdktf/provider-generator/lib/get/__tests__/generator/__snapshots__/types.test.ts.snap @@ -3967,6 +3967,97 @@ export class ListOfStringMap extends cdktf.TerraformResource { " `; +exports[`map of map of string attribute 1`] = ` +"// https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/map_of_map_of_string +// generated from terraform resource schema + +import { Construct } from 'constructs'; +import * as cdktf from 'cdktf'; + +// Configuration + +export interface MapOfMapOfStringConfig extends cdktf.TerraformMetaArguments { +} + +/** +* Represents a {@link https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/map_of_map_of_string aws_map_of_map_of_string} +*/ +export class MapOfMapOfString extends cdktf.TerraformResource { + + // ================= + // STATIC PROPERTIES + // ================= + public static readonly tfResourceType = "aws_map_of_map_of_string"; + + // ============== + // STATIC Methods + // ============== + /** + * Generates CDKTF code for importing a MapOfMapOfString resource upon running "cdktf plan " + * @param scope The scope in which to define this construct + * @param importToId The construct id used in the generated config for the MapOfMapOfString to import + * @param importFromId The id of the existing MapOfMapOfString that should be imported. Refer to the {@link https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/map_of_map_of_string#import import section} in the documentation of this resource for the id to use + * @param provider? Optional instance of the provider where the MapOfMapOfString to import is found + */ + public static generateConfigForImport(scope: Construct, importToId: string, importFromId: string, provider?: cdktf.TerraformProvider) { + return new cdktf.ImportableResource(scope, importToId, { terraformResourceType: "aws_map_of_map_of_string", importId: importFromId, provider }); + } + + // =========== + // INITIALIZER + // =========== + + /** + * Create a new {@link https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/map_of_map_of_string aws_map_of_map_of_string} Resource + * + * @param scope The scope in which to define this construct + * @param id The scoped construct ID. Must be unique amongst siblings in the same scope + * @param options MapOfMapOfStringConfig = {} + */ + public constructor(scope: Construct, id: string, config: MapOfMapOfStringConfig = {}) { + super(scope, id, { + terraformResourceType: 'aws_map_of_map_of_string', + terraformGeneratorMetadata: { + providerName: 'aws' + }, + provider: config.provider, + dependsOn: config.dependsOn, + count: config.count, + lifecycle: config.lifecycle, + provisioners: config.provisioners, + connection: config.connection, + forEach: config.forEach + }); + } + + // ========== + // ATTRIBUTES + // ========== + + // labels - computed: true, optional: false, required: false + private _labels = new cdktf.StringMapMap(this, "labels"); + public get labels() { + return this._labels; + } + + // ========= + // SYNTHESIS + // ========= + + protected synthesizeAttributes(): { [name: string]: any } { + return { + }; + } + + protected synthesizeHclAttributes(): { [name: string]: any } { + const attrs = { + }; + return attrs; + } +} +" +`; + exports[`map of string list attribute 1`] = ` "// https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/map_of_string_list // generated from terraform resource schema diff --git a/packages/@cdktf/provider-generator/lib/get/__tests__/generator/fixtures/map-of-map-of-string.test.fixture.json b/packages/@cdktf/provider-generator/lib/get/__tests__/generator/fixtures/map-of-map-of-string.test.fixture.json new file mode 100644 index 0000000000..2a2de5d54f --- /dev/null +++ b/packages/@cdktf/provider-generator/lib/get/__tests__/generator/fixtures/map-of-map-of-string.test.fixture.json @@ -0,0 +1,27 @@ +{ + "provider_schemas": { + "registry.terraform.io/hashicorp/aws": { + "resource_schemas": { + "aws_map_of_map_of_string": { + "version": 1, + "block": { + "attributes": { + "labels": { + "type": [ + "map", + [ + "map", + "string" + ] + ], + "description": "Map of map of string.", + "computed": true + } + } + }, + "block_types": {} + } + } + } + } +} \ No newline at end of file diff --git a/packages/@cdktf/provider-generator/lib/get/__tests__/generator/types.test.ts b/packages/@cdktf/provider-generator/lib/get/__tests__/generator/types.test.ts index 8be8e86dd8..270c0c7580 100644 --- a/packages/@cdktf/provider-generator/lib/get/__tests__/generator/types.test.ts +++ b/packages/@cdktf/provider-generator/lib/get/__tests__/generator/types.test.ts @@ -571,3 +571,28 @@ test("list of list of strings", async () => { ); expect(datasourceOutput).toMatchSnapshot(); }); + +test("map of map of string attribute", async () => { + const code = new CodeMaker(); + const workdir = fs.mkdtempSync( + path.join(os.tmpdir(), "map-of-map-of-string.test"), + ); + const spec = JSON.parse( + fs.readFileSync( + path.join( + __dirname, + "fixtures", + "map-of-map-of-string.test.fixture.json", + ), + "utf-8", + ), + ); + new TerraformProviderGenerator(code, spec).generateAll(); + await code.save(workdir); + + const output = fs.readFileSync( + path.join(workdir, "providers/aws/map-of-map-of-string/index.ts"), + "utf-8", + ); + expect(output).toMatchSnapshot(); +}); diff --git a/packages/@cdktf/provider-generator/lib/get/generator/models/attribute-type-model.ts b/packages/@cdktf/provider-generator/lib/get/generator/models/attribute-type-model.ts index 977358f1f1..8709e1145e 100644 --- a/packages/@cdktf/provider-generator/lib/get/generator/models/attribute-type-model.ts +++ b/packages/@cdktf/provider-generator/lib/get/generator/models/attribute-type-model.ts @@ -393,6 +393,10 @@ export class MapAttributeTypeModel implements CollectionAttributeTypeModel { } get storedClassType() { + // Special case for map of map of string + if (this.elementType.storedClassType === "StringMap") { + return "StringMapMap"; + } return `${this.elementType.storedClassType}Map`; } diff --git a/packages/cdktf/lib/complex-computed-list.ts b/packages/cdktf/lib/complex-computed-list.ts index 62eee0707d..05d43f2718 100644 --- a/packages/cdktf/lib/complex-computed-list.ts +++ b/packages/cdktf/lib/complex-computed-list.ts @@ -211,6 +211,32 @@ export class AnyMap extends ComplexResolvable implements ITerraformAddressable { } } +// eslint-disable-next-line jsdoc/require-jsdoc +export class StringMapMap + extends ComplexResolvable + implements ITerraformAddressable +{ + constructor( + protected terraformResource: IInterpolatingParent, + protected terraformAttribute: string, + ) { + super(terraformResource, terraformAttribute); + } + + public lookup(key: string): StringMap { + return new StringMap( + this.terraformResource, + `${this.terraformAttribute}["${key}"]`, + ); + } + + computeFqn(): string { + return Token.asString( + this.terraformResource.interpolationForAttribute(this.terraformAttribute), + ); + } +} + /** * @deprecated Going to be replaced by Array of ComplexListItem * and will be removed in the future