Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix provider attribute to block conversion #288

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions schema/convert_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,30 @@ func bodySchemaForCtyObjectType(typ cty.Type) *schema.BodySchema {
ret := &schema.BodySchema{
Attributes: make(map[string]*schema.AttributeSchema, len(attrTypes)),
}
blocks := make(map[string]*schema.BlockSchema, 0)

for name, attrType := range attrTypes {
ret.Attributes[name] = &schema.AttributeSchema{
Constraint: convertAttributeTypeToConstraint(attrType),
IsOptional: true,
}

if typeCanBeBlocks(attrType) {
fAttr := tfjson.SchemaAttribute{
AttributeType: attrType,
}
blockSchema, ok := blockSchemaForAttribute(&fAttr)
dbanck marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
continue
}
blocks[name] = blockSchema
}
}

if len(blocks) > 0 {
ret.Blocks = blocks
}

return ret
}

Expand Down
204 changes: 204 additions & 0 deletions schema/convert_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,207 @@ func TestProviderSchemaFromJson_basic(t *testing.T) {
t.Fatalf("provider schema mismatch: %s", diff)
}
}

func TestProviderSchemaFromJson_nested_set_list(t *testing.T) {
rawSchema := `{
"resource_schemas": {
"azurerm_site_recovery_replicated_vm": {
"version": 0,
"block": {
"attributes": {
"managed_disk": {
"type": [
"set",
[
"object",
{
"target_disk_encryption": [
"list",
[
"object",
{
"disk_encryption_key": "string",
"key_encryption_key": "string"
}
]
]
}
]
],
"description_kind": "plain",
"optional": true
}
},
"block_types": {},
"description_kind": "plain"
}
}
}
}`
jsonSchema := &tfjson.ProviderSchema{}
err := json.Unmarshal([]byte(rawSchema), jsonSchema)
if err != nil {
t.Fatal(err)
}
providerAddr := addr.NewDefaultProvider("aws")

ps := ProviderSchemaFromJson(jsonSchema, providerAddr)

expectedPs := &ProviderSchema{
Resources: map[string]*schema.BodySchema{
"azurerm_site_recovery_replicated_vm": {
Attributes: map[string]*schema.AttributeSchema{
"managed_disk": {
IsOptional: true,
Constraint: schema.OneOf{
schema.AnyExpression{
OfType: cty.Set(cty.Object(
map[string]cty.Type{
"target_disk_encryption": cty.List(cty.Object(
map[string]cty.Type{
"disk_encryption_key": cty.String,
"key_encryption_key": cty.String,
},
)),
},
)),
SkipLiteralComplexTypes: true,
},
schema.Set{
Elem: schema.OneOf{
schema.AnyExpression{
OfType: cty.Object(
map[string]cty.Type{
"target_disk_encryption": cty.List(cty.Object(
map[string]cty.Type{
"disk_encryption_key": cty.String,
"key_encryption_key": cty.String,
},
)),
},
),
SkipLiteralComplexTypes: true,
},
schema.Object{
Attributes: schema.ObjectAttributes{
"target_disk_encryption": {
IsRequired: true,
Constraint: schema.OneOf{
schema.AnyExpression{
OfType: cty.List(cty.Object(
map[string]cty.Type{
"disk_encryption_key": cty.String,
"key_encryption_key": cty.String,
},
)),
SkipLiteralComplexTypes: true,
},
schema.List{
Elem: schema.OneOf{
schema.AnyExpression{
OfType: cty.Object(
map[string]cty.Type{
"disk_encryption_key": cty.String,
"key_encryption_key": cty.String,
},
),
SkipLiteralComplexTypes: true,
},
schema.Object{
Attributes: schema.ObjectAttributes{
"disk_encryption_key": {
IsRequired: true,
Constraint: schema.AnyExpression{OfType: cty.String},
},
"key_encryption_key": {
IsRequired: true,
Constraint: schema.AnyExpression{OfType: cty.String},
},
},
},
},
},
},
},
},
},
},
},
},
},
},
Blocks: map[string]*schema.BlockSchema{
"managed_disk": {
Type: schema.BlockTypeSet,
Body: &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"target_disk_encryption": {
Type: schema.BlockTypeList,
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"disk_encryption_key": {
IsOptional: true,
Constraint: schema.AnyExpression{OfType: cty.String},
},
"key_encryption_key": {
IsOptional: true,
Constraint: schema.AnyExpression{OfType: cty.String},
},
},
},
},
},
Attributes: map[string]*schema.AttributeSchema{
"target_disk_encryption": {
IsOptional: true,
Constraint: schema.OneOf{
schema.AnyExpression{
OfType: cty.List(cty.Object(
map[string]cty.Type{
"disk_encryption_key": cty.String,
"key_encryption_key": cty.String,
},
)),
SkipLiteralComplexTypes: true,
},
schema.List{
Elem: schema.OneOf{
schema.AnyExpression{
OfType: cty.Object(
map[string]cty.Type{
"disk_encryption_key": cty.String,
"key_encryption_key": cty.String,
},
),
SkipLiteralComplexTypes: true,
},
schema.Object{
Attributes: schema.ObjectAttributes{
"disk_encryption_key": {
IsRequired: true,
Constraint: schema.AnyExpression{OfType: cty.String},
},
"key_encryption_key": {
IsRequired: true,
Constraint: schema.AnyExpression{OfType: cty.String},
},
},
},
},
},
},
},
},
},
},
},
Detail: "hashicorp/aws",
},
},
DataSources: map[string]*schema.BodySchema{},
}

if diff := cmp.Diff(expectedPs, ps, ctydebug.CmpOptions); diff != "" {
t.Fatalf("provider schema mismatch: %s", diff)
}
}
Loading