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

Commit

Permalink
Merge pull request #252 from crossplane/backport-250-to-release-0.4
Browse files Browse the repository at this point in the history
[Backport release-0.4] Cli Schema Conversion Fixes & Set ConfigMode Attribute
  • Loading branch information
turkenh authored Mar 4, 2022
2 parents c6a75d7 + 9a5f320 commit 5d929ed
Showing 1 changed file with 24 additions and 5 deletions.
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

0 comments on commit 5d929ed

Please sign in to comment.