kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/configs/backend.go (about) 1 package configs 2 3 import ( 4 "github.com/hashicorp/hcl/v2" 5 "github.com/hashicorp/hcl/v2/hcldec" 6 "kubeform.dev/terraform-backend-sdk/configs/configschema" 7 "github.com/zclconf/go-cty/cty" 8 ) 9 10 // Backend represents a "backend" block inside a "terraform" block in a module 11 // or file. 12 type Backend struct { 13 Type string 14 Config hcl.Body 15 16 TypeRange hcl.Range 17 DeclRange hcl.Range 18 } 19 20 func decodeBackendBlock(block *hcl.Block) (*Backend, hcl.Diagnostics) { 21 return &Backend{ 22 Type: block.Labels[0], 23 TypeRange: block.LabelRanges[0], 24 Config: block.Body, 25 DeclRange: block.DefRange, 26 }, nil 27 } 28 29 // Hash produces a hash value for the reciever that covers the type and the 30 // portions of the config that conform to the given schema. 31 // 32 // If the config does not conform to the schema then the result is not 33 // meaningful for comparison since it will be based on an incomplete result. 34 // 35 // As an exception, required attributes in the schema are treated as optional 36 // for the purpose of hashing, so that an incomplete configuration can still 37 // be hashed. Other errors, such as extraneous attributes, have no such special 38 // case. 39 func (b *Backend) Hash(schema *configschema.Block) int { 40 // Don't fail if required attributes are not set. Instead, we'll just 41 // hash them as nulls. 42 schema = schema.NoneRequired() 43 spec := schema.DecoderSpec() 44 val, _ := hcldec.Decode(b.Config, spec, nil) 45 if val == cty.NilVal { 46 val = cty.UnknownVal(schema.ImpliedType()) 47 } 48 49 toHash := cty.TupleVal([]cty.Value{ 50 cty.StringVal(b.Type), 51 val, 52 }) 53 54 return toHash.Hash() 55 }