From 8730d993091700706ba97000ffc7f09b48c4a82a Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 8 Apr 2019 15:24:38 -0400 Subject: [PATCH] LegacyResourceSchema to remove 0.12 features This allows us to call CoreConfigSchema and return something that looks like the original schema. --- helper/schema/shims.go | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/helper/schema/shims.go b/helper/schema/shims.go index d99fb3965..eccd4eb18 100644 --- a/helper/schema/shims.go +++ b/helper/schema/shims.go @@ -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 +}