github.com/simonswine/terraform@v0.9.0-beta2/config/config_terraform.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/go-version" 8 "github.com/mitchellh/hashstructure" 9 ) 10 11 // Terraform is the Terraform meta-configuration that can be present 12 // in configuration files for configuring Terraform itself. 13 type Terraform struct { 14 RequiredVersion string `hcl:"required_version"` // Required Terraform version (constraint) 15 Backend *Backend // See Backend struct docs 16 } 17 18 // Validate performs the validation for just the Terraform configuration. 19 func (t *Terraform) Validate() []error { 20 var errs []error 21 22 if raw := t.RequiredVersion; raw != "" { 23 // Check that the value has no interpolations 24 rc, err := NewRawConfig(map[string]interface{}{ 25 "root": raw, 26 }) 27 if err != nil { 28 errs = append(errs, fmt.Errorf( 29 "terraform.required_version: %s", err)) 30 } else if len(rc.Interpolations) > 0 { 31 errs = append(errs, fmt.Errorf( 32 "terraform.required_version: cannot contain interpolations")) 33 } else { 34 // Check it is valid 35 _, err := version.NewConstraint(raw) 36 if err != nil { 37 errs = append(errs, fmt.Errorf( 38 "terraform.required_version: invalid syntax: %s", err)) 39 } 40 } 41 } 42 43 if t.Backend != nil { 44 errs = append(errs, t.Backend.Validate()...) 45 } 46 47 return errs 48 } 49 50 // Backend is the configuration for the "backend" to use with Terraform. 51 // A backend is responsible for all major behavior of Terraform's core. 52 // The abstraction layer above the core (the "backend") allows for behavior 53 // such as remote operation. 54 type Backend struct { 55 Type string 56 RawConfig *RawConfig 57 58 // Hash is a unique hash code representing the original configuration 59 // of the backend. This won't be recomputed unless Rehash is called. 60 Hash uint64 61 } 62 63 // Hash returns a unique content hash for this backend's configuration 64 // as a uint64 value. 65 func (b *Backend) Rehash() uint64 { 66 // If we have no backend, the value is zero 67 if b == nil { 68 return 0 69 } 70 71 // Use hashstructure to hash only our type with the config. 72 code, err := hashstructure.Hash(map[string]interface{}{ 73 "type": b.Type, 74 "config": b.RawConfig.Raw, 75 }, nil) 76 77 // This should never happen since we have just some basic primitives 78 // so panic if there is an error. 79 if err != nil { 80 panic(err) 81 } 82 83 return code 84 } 85 86 func (b *Backend) Validate() []error { 87 if len(b.RawConfig.Interpolations) > 0 { 88 return []error{fmt.Errorf(strings.TrimSpace(errBackendInterpolations))} 89 } 90 91 return nil 92 } 93 94 const errBackendInterpolations = ` 95 terraform.backend: configuration cannot contain interpolations 96 97 The backend configuration is loaded by Terraform extremely early, before 98 the core of Terraform can be initialized. This is necessary because the backend 99 dictates the behavior of that core. The core is what handles interpolation 100 processing. Because of this, interpolations cannot be used in backend 101 configuration. 102 103 If you'd like to parameterize backend configuration, we recommend using 104 partial configuration with the "-backend-config" flag to "terraform init". 105 `