]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blobdiff - vendor/github.com/hashicorp/terraform/config/loader_hcl.go
deps: github.com/hashicorp/terraform@sdk-v0.11-with-go-modules
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / terraform / config / loader_hcl.go
index e85e493555a849bc422e9f3b419205e39c05cc79..68cffe2cccf07391256965b04059427f5c752775 100644 (file)
@@ -17,10 +17,20 @@ type hclConfigurable struct {
        Root *ast.File
 }
 
+var ReservedDataSourceFields = []string{
+       "connection",
+       "count",
+       "depends_on",
+       "lifecycle",
+       "provider",
+       "provisioner",
+}
+
 var ReservedResourceFields = []string{
        "connection",
        "count",
        "depends_on",
+       "id",
        "lifecycle",
        "provider",
        "provisioner",
@@ -35,6 +45,7 @@ func (t *hclConfigurable) Config() (*Config, error) {
        validKeys := map[string]struct{}{
                "atlas":     struct{}{},
                "data":      struct{}{},
+               "locals":    struct{}{},
                "module":    struct{}{},
                "output":    struct{}{},
                "provider":  struct{}{},
@@ -70,6 +81,15 @@ func (t *hclConfigurable) Config() (*Config, error) {
                }
        }
 
+       // Build local values
+       if locals := list.Filter("locals"); len(locals.Items) > 0 {
+               var err error
+               config.Locals, err = loadLocalsHcl(locals)
+               if err != nil {
+                       return nil, err
+               }
+       }
+
        // Get Atlas configuration
        if atlas := list.Filter("atlas"); len(atlas.Items) > 0 {
                var err error
@@ -373,9 +393,6 @@ func loadModulesHcl(list *ast.ObjectList) ([]*Module, error) {
                                err)
                }
 
-               // Remove the fields we handle specially
-               delete(config, "source")
-
                rawConfig, err := NewRawConfig(config)
                if err != nil {
                        return nil, fmt.Errorf(
@@ -384,7 +401,11 @@ func loadModulesHcl(list *ast.ObjectList) ([]*Module, error) {
                                err)
                }
 
-               // If we have a count, then figure it out
+               // Remove the fields we handle specially
+               delete(config, "source")
+               delete(config, "version")
+               delete(config, "providers")
+
                var source string
                if o := listVal.Filter("source"); len(o.Items) > 0 {
                        err = hcl.DecodeObject(&source, o.Items[0].Val)
@@ -396,9 +417,33 @@ func loadModulesHcl(list *ast.ObjectList) ([]*Module, error) {
                        }
                }
 
+               var version string
+               if o := listVal.Filter("version"); len(o.Items) > 0 {
+                       err = hcl.DecodeObject(&version, o.Items[0].Val)
+                       if err != nil {
+                               return nil, fmt.Errorf(
+                                       "Error parsing version for %s: %s",
+                                       k,
+                                       err)
+                       }
+               }
+
+               var providers map[string]string
+               if o := listVal.Filter("providers"); len(o.Items) > 0 {
+                       err = hcl.DecodeObject(&providers, o.Items[0].Val)
+                       if err != nil {
+                               return nil, fmt.Errorf(
+                                       "Error parsing providers for %s: %s",
+                                       k,
+                                       err)
+                       }
+               }
+
                result = append(result, &Module{
                        Name:      k,
                        Source:    source,
+                       Version:   version,
+                       Providers: providers,
                        RawConfig: rawConfig,
                })
        }
@@ -406,6 +451,59 @@ func loadModulesHcl(list *ast.ObjectList) ([]*Module, error) {
        return result, nil
 }
 
+// loadLocalsHcl recurses into the given HCL object turns it into
+// a list of locals.
+func loadLocalsHcl(list *ast.ObjectList) ([]*Local, error) {
+
+       result := make([]*Local, 0, len(list.Items))
+
+       for _, block := range list.Items {
+               if len(block.Keys) > 0 {
+                       return nil, fmt.Errorf(
+                               "locals block at %s should not have label %q",
+                               block.Pos(), block.Keys[0].Token.Value(),
+                       )
+               }
+
+               blockObj, ok := block.Val.(*ast.ObjectType)
+               if !ok {
+                       return nil, fmt.Errorf("locals value at %s should be a block", block.Val.Pos())
+               }
+
+               // blockObj now contains directly our local decls
+               for _, item := range blockObj.List.Items {
+                       if len(item.Keys) != 1 {
+                               return nil, fmt.Errorf("local declaration at %s may not be a block", item.Val.Pos())
+                       }
+
+                       // By the time we get here there can only be one item left, but
+                       // we'll decode into a map anyway because it's a convenient way
+                       // to extract both the key and the value robustly.
+                       kv := map[string]interface{}{}
+                       hcl.DecodeObject(&kv, item)
+                       for k, v := range kv {
+                               rawConfig, err := NewRawConfig(map[string]interface{}{
+                                       "value": v,
+                               })
+
+                               if err != nil {
+                                       return nil, fmt.Errorf(
+                                               "error parsing local value %q at %s: %s",
+                                               k, item.Val.Pos(), err,
+                                       )
+                               }
+
+                               result = append(result, &Local{
+                                       Name:      k,
+                                       RawConfig: rawConfig,
+                               })
+                       }
+               }
+       }
+
+       return result, nil
+}
+
 // LoadOutputsHcl recurses into the given HCL object and turns
 // it into a mapping of outputs.
 func loadOutputsHcl(list *ast.ObjectList) ([]*Output, error) {
@@ -434,6 +532,7 @@ func loadOutputsHcl(list *ast.ObjectList) ([]*Output, error) {
 
                // Delete special keys
                delete(config, "depends_on")
+               delete(config, "description")
 
                rawConfig, err := NewRawConfig(config)
                if err != nil {
@@ -455,10 +554,23 @@ func loadOutputsHcl(list *ast.ObjectList) ([]*Output, error) {
                        }
                }
 
+               // If we have a description field, then filter that
+               var description string
+               if o := listVal.Filter("description"); len(o.Items) > 0 {
+                       err := hcl.DecodeObject(&description, o.Items[0].Val)
+                       if err != nil {
+                               return nil, fmt.Errorf(
+                                       "Error reading description for output %q: %s",
+                                       n,
+                                       err)
+                       }
+               }
+
                result = append(result, &Output{
-                       Name:      n,
-                       RawConfig: rawConfig,
-                       DependsOn: dependsOn,
+                       Name:        n,
+                       RawConfig:   rawConfig,
+                       DependsOn:   dependsOn,
+                       Description: description,
                })
        }