From b8c0b8d429a326cdff4b3f3d337dd4d2b9c9b4b9 Mon Sep 17 00:00:00 2001 From: Hasan Turken Date: Thu, 3 Mar 2022 13:02:53 +0300 Subject: [PATCH 1/3] Fix cli conversion - collection elements could be nonprimitive Signed-off-by: Hasan Turken (cherry picked from commit b54efc220d9a8f7e54b915b46fe6f0a6f8d12e70) --- pkg/types/conversion/tfjson/tfjson.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/types/conversion/tfjson/tfjson.go b/pkg/types/conversion/tfjson/tfjson.go index 439802a3..99b5edbb 100644 --- a/pkg/types/conversion/tfjson/tfjson.go +++ b/pkg/types/conversion/tfjson/tfjson.go @@ -158,7 +158,10 @@ func schemaV2TypeFromCtyType(typ cty.Type, schema *schemav2.Schema) error { //no case et.IsPrimitiveType(): elemType = &schemav2.Schema{Type: primitiveToV2SchemaType(et)} case et.IsCollectionType(): - elemType = collectionToV2SchemaType(et) + elemType = &schemav2.Schema{Type: collectionToV2SchemaType(et)} + if err := schemaV2TypeFromCtyType(et, elemType.(*schemav2.Schema)); err != nil { + return err + } case et.IsObjectType(): res := &schemav2.Resource{} res.Schema = make(map[string]*schemav2.Schema, len(et.AttributeTypes())) From ab247a2eff988d655b94244a94578db413bc916e Mon Sep 17 00:00:00 2001 From: Hasan Turken Date: Thu, 3 Mar 2022 15:38:03 +0300 Subject: [PATCH 2/3] Infer optional & computed from parent Signed-off-by: Hasan Turken (cherry picked from commit 1b91b16dcb0e91cd063e01cfe8c7f2fc201b4b14) --- pkg/types/conversion/tfjson/tfjson.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/pkg/types/conversion/tfjson/tfjson.go b/pkg/types/conversion/tfjson/tfjson.go index 99b5edbb..d1aadb86 100644 --- a/pkg/types/conversion/tfjson/tfjson.go +++ b/pkg/types/conversion/tfjson/tfjson.go @@ -156,9 +156,17 @@ func schemaV2TypeFromCtyType(typ cty.Type, schema *schemav2.Schema) error { //no et := typ.ElementType() switch { case et.IsPrimitiveType(): - elemType = &schemav2.Schema{Type: primitiveToV2SchemaType(et)} + elemType = &schemav2.Schema{ + Type: primitiveToV2SchemaType(et), + Computed: schema.Computed, + Optional: schema.Optional, + } case et.IsCollectionType(): - elemType = &schemav2.Schema{Type: collectionToV2SchemaType(et)} + elemType = &schemav2.Schema{ + Type: collectionToV2SchemaType(et), + Computed: schema.Computed, + Optional: schema.Optional, + } if err := schemaV2TypeFromCtyType(et, elemType.(*schemav2.Schema)); err != nil { return err } @@ -166,13 +174,17 @@ func schemaV2TypeFromCtyType(typ cty.Type, schema *schemav2.Schema) error { //no res := &schemav2.Resource{} res.Schema = make(map[string]*schemav2.Schema, len(et.AttributeTypes())) for key, attrTyp := range et.AttributeTypes() { - sch := &schemav2.Schema{} - if err := schemaV2TypeFromCtyType(attrTyp, sch); err != nil { - return err + sch := &schemav2.Schema{ + Computed: schema.Computed, + Optional: schema.Optional, } if et.AttributeOptional(key) { sch.Optional = true } + + if err := schemaV2TypeFromCtyType(attrTyp, sch); err != nil { + return err + } res.Schema[key] = sch } elemType = res From 9a5f320e37986fe071830f216289cd4e9b79f446 Mon Sep 17 00:00:00 2001 From: Hasan Turken Date: Thu, 3 Mar 2022 15:38:50 +0300 Subject: [PATCH 3/3] Set schema.configmode in cli schema conversion Signed-off-by: Hasan Turken (cherry picked from commit dc08971cbb494b727fa378d9883dccc12b381ac0) --- pkg/types/conversion/tfjson/tfjson.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/types/conversion/tfjson/tfjson.go b/pkg/types/conversion/tfjson/tfjson.go index d1aadb86..878bb6b2 100644 --- a/pkg/types/conversion/tfjson/tfjson.go +++ b/pkg/types/conversion/tfjson/tfjson.go @@ -148,6 +148,8 @@ func tfJSONBlockTypeToV2Schema(nb *tfjson.SchemaBlockType) *schemav2.Schema { // } func schemaV2TypeFromCtyType(typ cty.Type, schema *schemav2.Schema) error { //nolint:gocyclo + configMode := schemav2.SchemaConfigModeAuto + switch { case typ.IsPrimitiveType(): schema.Type = primitiveToV2SchemaType(typ) @@ -171,6 +173,7 @@ func schemaV2TypeFromCtyType(typ cty.Type, schema *schemav2.Schema) error { //no return err } case et.IsObjectType(): + configMode = schemav2.SchemaConfigModeAttr res := &schemav2.Resource{} res.Schema = make(map[string]*schemav2.Schema, len(et.AttributeTypes())) for key, attrTyp := range et.AttributeTypes() { @@ -191,6 +194,7 @@ func schemaV2TypeFromCtyType(typ cty.Type, schema *schemav2.Schema) error { //no default: return errors.Errorf("unexpected cty.Type %s", typ.GoString()) } + schema.ConfigMode = configMode schema.Type = collectionToV2SchemaType(typ) schema.Elem = elemType case typ.IsTupleType():