Marshall resource identity schemas in jsonprovider (#36675)

output-deprecation
Daniel Banck 1 year ago committed by GitHub
parent fec6e4b552
commit cd433fa6d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -70,3 +70,27 @@ func marshalAttribute(attr *configschema.Attribute) *Attribute {
return ret
}
type IdentityAttribute struct {
IdentityType json.RawMessage `json:"type,omitempty"`
Description string `json:"description,omitempty"`
RequiredForImport bool `json:"required_for_import,omitempty"`
OptionalForImport bool `json:"optional_for_import,omitempty"`
}
func marshalIdentityAttribute(attr *configschema.Attribute) *IdentityAttribute {
ret := &IdentityAttribute{
Description: attr.Description,
RequiredForImport: attr.Required,
OptionalForImport: attr.Optional,
}
// we're not concerned about errors because at this point the schema has
// already been checked and re-checked.
if attr.Type != cty.NilType {
attrTy, _ := attr.Type.MarshalJSON()
ret.IdentityType = attrTy
}
return ret
}

@ -46,3 +46,33 @@ func TestMarshalAttribute(t *testing.T) {
}
}
}
func TestMarshalIdentityAttribute(t *testing.T) {
tests := []struct {
Input *configschema.Attribute
Want *IdentityAttribute
}{
{
&configschema.Attribute{Type: cty.String, Optional: true},
&IdentityAttribute{
IdentityType: json.RawMessage(`"string"`),
OptionalForImport: true,
},
},
{ // collection types look a little odd.
&configschema.Attribute{Type: cty.List(cty.String), Required: true},
&IdentityAttribute{
IdentityType: json.RawMessage(`["list","string"]`),
RequiredForImport: true,
},
},
}
for _, test := range tests {
got := marshalIdentityAttribute(test.Input)
if !cmp.Equal(got, test.Want) {
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, test.Want))
}
}
}

@ -28,6 +28,7 @@ type Provider struct {
DataSourceSchemas map[string]*Schema `json:"data_source_schemas,omitempty"`
EphemeralResourceSchemas map[string]*Schema `json:"ephemeral_resource_schemas,omitempty"`
Functions map[string]*jsonfunction.FunctionSignature `json:"functions,omitempty"`
ResourceIdentitySchemas map[string]*IdentitySchema `json:"resource_identity_schemas,omitempty"`
}
func newProviders() *Providers {
@ -64,5 +65,6 @@ func marshalProvider(tps providers.ProviderSchema) *Provider {
DataSourceSchemas: marshalSchemas(tps.DataSources),
EphemeralResourceSchemas: marshalSchemas(tps.EphemeralResourceTypes),
Functions: jsonfunction.MarshalProviderFunctions(tps.Functions),
ResourceIdentitySchemas: marshalIdentitySchemas(tps.ResourceTypes),
}
}

@ -30,6 +30,7 @@ func TestMarshalProvider(t *testing.T) {
ResourceSchemas: map[string]*Schema{},
DataSourceSchemas: map[string]*Schema{},
EphemeralResourceSchemas: map[string]*Schema{},
ResourceIdentitySchemas: map[string]*IdentitySchema{},
},
},
{
@ -207,6 +208,7 @@ func TestMarshalProvider(t *testing.T) {
},
},
},
ResourceIdentitySchemas: map[string]*IdentitySchema{},
},
},
}

@ -36,3 +36,35 @@ func marshalSchemas(schemas map[string]providers.Schema) map[string]*Schema {
}
return ret
}
type IdentitySchema struct {
Version uint64 `json:"version"`
Attributes map[string]*IdentityAttribute `json:"attributes,omitempty"`
}
func marshalIdentitySchema(schema providers.Schema) *IdentitySchema {
var ret IdentitySchema
ret.Version = uint64(schema.IdentityVersion)
ret.Attributes = make(map[string]*IdentityAttribute, len(schema.Identity.Attributes))
for k, v := range schema.Identity.Attributes {
ret.Attributes[k] = marshalIdentityAttribute(v)
}
return &ret
}
func marshalIdentitySchemas(schemas map[string]providers.Schema) map[string]*IdentitySchema {
if schemas == nil {
return map[string]*IdentitySchema{}
}
ret := make(map[string]*IdentitySchema, len(schemas))
for k, v := range schemas {
if v.Identity != nil {
ret[k] = marshalIdentitySchema(v)
}
}
return ret
}

Loading…
Cancel
Save