diff options
Diffstat (limited to 'vendor/github.com/hashicorp/terraform/config/loader_hcl.go')
-rw-r--r-- | vendor/github.com/hashicorp/terraform/config/loader_hcl.go | 126 |
1 files changed, 119 insertions, 7 deletions
diff --git a/vendor/github.com/hashicorp/terraform/config/loader_hcl.go b/vendor/github.com/hashicorp/terraform/config/loader_hcl.go index e85e493..68cffe2 100644 --- a/vendor/github.com/hashicorp/terraform/config/loader_hcl.go +++ b/vendor/github.com/hashicorp/terraform/config/loader_hcl.go | |||
@@ -17,10 +17,20 @@ type hclConfigurable struct { | |||
17 | Root *ast.File | 17 | Root *ast.File |
18 | } | 18 | } |
19 | 19 | ||
20 | var ReservedDataSourceFields = []string{ | ||
21 | "connection", | ||
22 | "count", | ||
23 | "depends_on", | ||
24 | "lifecycle", | ||
25 | "provider", | ||
26 | "provisioner", | ||
27 | } | ||
28 | |||
20 | var ReservedResourceFields = []string{ | 29 | var ReservedResourceFields = []string{ |
21 | "connection", | 30 | "connection", |
22 | "count", | 31 | "count", |
23 | "depends_on", | 32 | "depends_on", |
33 | "id", | ||
24 | "lifecycle", | 34 | "lifecycle", |
25 | "provider", | 35 | "provider", |
26 | "provisioner", | 36 | "provisioner", |
@@ -35,6 +45,7 @@ func (t *hclConfigurable) Config() (*Config, error) { | |||
35 | validKeys := map[string]struct{}{ | 45 | validKeys := map[string]struct{}{ |
36 | "atlas": struct{}{}, | 46 | "atlas": struct{}{}, |
37 | "data": struct{}{}, | 47 | "data": struct{}{}, |
48 | "locals": struct{}{}, | ||
38 | "module": struct{}{}, | 49 | "module": struct{}{}, |
39 | "output": struct{}{}, | 50 | "output": struct{}{}, |
40 | "provider": struct{}{}, | 51 | "provider": struct{}{}, |
@@ -70,6 +81,15 @@ func (t *hclConfigurable) Config() (*Config, error) { | |||
70 | } | 81 | } |
71 | } | 82 | } |
72 | 83 | ||
84 | // Build local values | ||
85 | if locals := list.Filter("locals"); len(locals.Items) > 0 { | ||
86 | var err error | ||
87 | config.Locals, err = loadLocalsHcl(locals) | ||
88 | if err != nil { | ||
89 | return nil, err | ||
90 | } | ||
91 | } | ||
92 | |||
73 | // Get Atlas configuration | 93 | // Get Atlas configuration |
74 | if atlas := list.Filter("atlas"); len(atlas.Items) > 0 { | 94 | if atlas := list.Filter("atlas"); len(atlas.Items) > 0 { |
75 | var err error | 95 | var err error |
@@ -373,9 +393,6 @@ func loadModulesHcl(list *ast.ObjectList) ([]*Module, error) { | |||
373 | err) | 393 | err) |
374 | } | 394 | } |
375 | 395 | ||
376 | // Remove the fields we handle specially | ||
377 | delete(config, "source") | ||
378 | |||
379 | rawConfig, err := NewRawConfig(config) | 396 | rawConfig, err := NewRawConfig(config) |
380 | if err != nil { | 397 | if err != nil { |
381 | return nil, fmt.Errorf( | 398 | return nil, fmt.Errorf( |
@@ -384,7 +401,11 @@ func loadModulesHcl(list *ast.ObjectList) ([]*Module, error) { | |||
384 | err) | 401 | err) |
385 | } | 402 | } |
386 | 403 | ||
387 | // If we have a count, then figure it out | 404 | // Remove the fields we handle specially |
405 | delete(config, "source") | ||
406 | delete(config, "version") | ||
407 | delete(config, "providers") | ||
408 | |||
388 | var source string | 409 | var source string |
389 | if o := listVal.Filter("source"); len(o.Items) > 0 { | 410 | if o := listVal.Filter("source"); len(o.Items) > 0 { |
390 | err = hcl.DecodeObject(&source, o.Items[0].Val) | 411 | err = hcl.DecodeObject(&source, o.Items[0].Val) |
@@ -396,9 +417,33 @@ func loadModulesHcl(list *ast.ObjectList) ([]*Module, error) { | |||
396 | } | 417 | } |
397 | } | 418 | } |
398 | 419 | ||
420 | var version string | ||
421 | if o := listVal.Filter("version"); len(o.Items) > 0 { | ||
422 | err = hcl.DecodeObject(&version, o.Items[0].Val) | ||
423 | if err != nil { | ||
424 | return nil, fmt.Errorf( | ||
425 | "Error parsing version for %s: %s", | ||
426 | k, | ||
427 | err) | ||
428 | } | ||
429 | } | ||
430 | |||
431 | var providers map[string]string | ||
432 | if o := listVal.Filter("providers"); len(o.Items) > 0 { | ||
433 | err = hcl.DecodeObject(&providers, o.Items[0].Val) | ||
434 | if err != nil { | ||
435 | return nil, fmt.Errorf( | ||
436 | "Error parsing providers for %s: %s", | ||
437 | k, | ||
438 | err) | ||
439 | } | ||
440 | } | ||
441 | |||
399 | result = append(result, &Module{ | 442 | result = append(result, &Module{ |
400 | Name: k, | 443 | Name: k, |
401 | Source: source, | 444 | Source: source, |
445 | Version: version, | ||
446 | Providers: providers, | ||
402 | RawConfig: rawConfig, | 447 | RawConfig: rawConfig, |
403 | }) | 448 | }) |
404 | } | 449 | } |
@@ -406,6 +451,59 @@ func loadModulesHcl(list *ast.ObjectList) ([]*Module, error) { | |||
406 | return result, nil | 451 | return result, nil |
407 | } | 452 | } |
408 | 453 | ||
454 | // loadLocalsHcl recurses into the given HCL object turns it into | ||
455 | // a list of locals. | ||
456 | func loadLocalsHcl(list *ast.ObjectList) ([]*Local, error) { | ||
457 | |||
458 | result := make([]*Local, 0, len(list.Items)) | ||
459 | |||
460 | for _, block := range list.Items { | ||
461 | if len(block.Keys) > 0 { | ||
462 | return nil, fmt.Errorf( | ||
463 | "locals block at %s should not have label %q", | ||
464 | block.Pos(), block.Keys[0].Token.Value(), | ||
465 | ) | ||
466 | } | ||
467 | |||
468 | blockObj, ok := block.Val.(*ast.ObjectType) | ||
469 | if !ok { | ||
470 | return nil, fmt.Errorf("locals value at %s should be a block", block.Val.Pos()) | ||
471 | } | ||
472 | |||
473 | // blockObj now contains directly our local decls | ||
474 | for _, item := range blockObj.List.Items { | ||
475 | if len(item.Keys) != 1 { | ||
476 | return nil, fmt.Errorf("local declaration at %s may not be a block", item.Val.Pos()) | ||
477 | } | ||
478 | |||
479 | // By the time we get here there can only be one item left, but | ||
480 | // we'll decode into a map anyway because it's a convenient way | ||
481 | // to extract both the key and the value robustly. | ||
482 | kv := map[string]interface{}{} | ||
483 | hcl.DecodeObject(&kv, item) | ||
484 | for k, v := range kv { | ||
485 | rawConfig, err := NewRawConfig(map[string]interface{}{ | ||
486 | "value": v, | ||
487 | }) | ||
488 | |||
489 | if err != nil { | ||
490 | return nil, fmt.Errorf( | ||
491 | "error parsing local value %q at %s: %s", | ||
492 | k, item.Val.Pos(), err, | ||
493 | ) | ||
494 | } | ||
495 | |||
496 | result = append(result, &Local{ | ||
497 | Name: k, | ||
498 | RawConfig: rawConfig, | ||
499 | }) | ||
500 | } | ||
501 | } | ||
502 | } | ||
503 | |||
504 | return result, nil | ||
505 | } | ||
506 | |||
409 | // LoadOutputsHcl recurses into the given HCL object and turns | 507 | // LoadOutputsHcl recurses into the given HCL object and turns |
410 | // it into a mapping of outputs. | 508 | // it into a mapping of outputs. |
411 | func loadOutputsHcl(list *ast.ObjectList) ([]*Output, error) { | 509 | func loadOutputsHcl(list *ast.ObjectList) ([]*Output, error) { |
@@ -434,6 +532,7 @@ func loadOutputsHcl(list *ast.ObjectList) ([]*Output, error) { | |||
434 | 532 | ||
435 | // Delete special keys | 533 | // Delete special keys |
436 | delete(config, "depends_on") | 534 | delete(config, "depends_on") |
535 | delete(config, "description") | ||
437 | 536 | ||
438 | rawConfig, err := NewRawConfig(config) | 537 | rawConfig, err := NewRawConfig(config) |
439 | if err != nil { | 538 | if err != nil { |
@@ -455,10 +554,23 @@ func loadOutputsHcl(list *ast.ObjectList) ([]*Output, error) { | |||
455 | } | 554 | } |
456 | } | 555 | } |
457 | 556 | ||
557 | // If we have a description field, then filter that | ||
558 | var description string | ||
559 | if o := listVal.Filter("description"); len(o.Items) > 0 { | ||
560 | err := hcl.DecodeObject(&description, o.Items[0].Val) | ||
561 | if err != nil { | ||
562 | return nil, fmt.Errorf( | ||
563 | "Error reading description for output %q: %s", | ||
564 | n, | ||
565 | err) | ||
566 | } | ||
567 | } | ||
568 | |||
458 | result = append(result, &Output{ | 569 | result = append(result, &Output{ |
459 | Name: n, | 570 | Name: n, |
460 | RawConfig: rawConfig, | 571 | RawConfig: rawConfig, |
461 | DependsOn: dependsOn, | 572 | DependsOn: dependsOn, |
573 | Description: description, | ||
462 | }) | 574 | }) |
463 | } | 575 | } |
464 | 576 | ||