LegacyResourceSchema to remove 0.12 features

This allows us to call CoreConfigSchema and return something that looks
like the original schema.
This commit is contained in:
James Bardin 2019-04-08 15:24:38 -04:00
parent c5023c7702
commit 8730d99309
1 changed files with 43 additions and 0 deletions

View File

@ -88,3 +88,46 @@ func JSONMapToStateValue(m map[string]interface{}, block *configschema.Block) (c
func StateValueFromInstanceState(is *terraform.InstanceState, ty cty.Type) (cty.Value, error) {
return is.AttrsAsObjectValue(ty)
}
// LegacyResourceSchema takes a *Resource and returns a deep copy with 0.12 specific
// features removed. This is used by the shims to get a configschema that
// directly matches the structure of the schema.Resource.
func LegacyResourceSchema(r *Resource) *Resource {
if r == nil {
return nil
}
// start with a shallow copy
newResource := new(Resource)
*newResource = *r
newResource.Schema = map[string]*Schema{}
for k, s := range r.Schema {
newResource.Schema[k] = LegacySchema(s)
}
return newResource
}
// LegacySchema takes a *Schema and returns a deep copy with 0.12 specific
// features removed. This is used by the shims to get a configschema that
// directly matches the structure of the schema.Resource.
func LegacySchema(s *Schema) *Schema {
if s == nil {
return nil
}
// start with a shallow copy
newSchema := new(Schema)
*newSchema = *s
newSchema.ConfigMode = SchemaConfigModeAuto
newSchema.PromoteSingle = false
newSchema.SkipCoreTypeCheck = false
switch e := newSchema.Elem.(type) {
case *Schema:
newSchema.Elem = LegacySchema(e)
case *Resource:
newSchema.Elem = LegacyResourceSchema(e)
}
return newSchema
}