Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

[Backport release-0.4] Cli Schema Conversion Fixes & Set ConfigMode Attribute #252

Merged
merged 3 commits into from
Mar 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions pkg/types/conversion/tfjson/tfjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -156,26 +158,43 @@ 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 = 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
}
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() {
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
default:
return errors.Errorf("unexpected cty.Type %s", typ.GoString())
}
schema.ConfigMode = configMode
schema.Type = collectionToV2SchemaType(typ)
schema.Elem = elemType
case typ.IsTupleType():
Expand Down