2014-05-24 20:36:22 +02:00
|
|
|
// The config package is responsible for loading and validating the
|
|
|
|
// configuration.
|
2014-05-23 01:56:28 +02:00
|
|
|
package config
|
|
|
|
|
2014-05-24 06:58:06 +02:00
|
|
|
import (
|
2014-05-24 22:57:51 +02:00
|
|
|
"fmt"
|
2014-10-09 00:06:04 +02:00
|
|
|
"regexp"
|
2014-10-02 20:34:08 +02:00
|
|
|
"strconv"
|
2014-05-24 06:58:06 +02:00
|
|
|
"strings"
|
2014-07-03 19:14:17 +02:00
|
|
|
|
2015-09-28 03:58:48 +02:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2016-01-31 08:38:37 +01:00
|
|
|
"github.com/hashicorp/hil"
|
|
|
|
"github.com/hashicorp/hil/ast"
|
2016-06-03 22:57:04 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/hilmapstructure"
|
2017-06-02 02:51:07 +02:00
|
|
|
"github.com/hashicorp/terraform/plugin/discovery"
|
2014-08-11 18:46:56 +02:00
|
|
|
"github.com/mitchellh/reflectwalk"
|
2014-05-24 06:58:06 +02:00
|
|
|
)
|
|
|
|
|
2014-10-09 00:06:04 +02:00
|
|
|
// NameRegexp is the regular expression that all names (modules, providers,
|
|
|
|
// resources, etc.) must follow.
|
2016-12-09 18:35:23 +01:00
|
|
|
var NameRegexp = regexp.MustCompile(`(?i)\A[A-Z0-9_][A-Z0-9\-\_]*\z`)
|
2014-10-09 00:06:04 +02:00
|
|
|
|
2014-05-23 01:56:28 +02:00
|
|
|
// Config is the configuration that comes from loading a collection
|
|
|
|
// of Terraform templates.
|
|
|
|
type Config struct {
|
2014-09-15 04:35:38 +02:00
|
|
|
// Dir is the path to the directory where this configuration was
|
|
|
|
// loaded from. If it is blank, this configuration wasn't loaded from
|
|
|
|
// any meaningful directory.
|
|
|
|
Dir string
|
|
|
|
|
2016-11-13 01:20:33 +01:00
|
|
|
Terraform *Terraform
|
2015-03-05 21:56:31 +01:00
|
|
|
Atlas *AtlasConfig
|
2014-09-12 00:58:30 +02:00
|
|
|
Modules []*Module
|
2014-07-20 01:05:48 +02:00
|
|
|
ProviderConfigs []*ProviderConfig
|
2014-05-26 03:05:18 +02:00
|
|
|
Resources []*Resource
|
2014-07-19 02:48:30 +02:00
|
|
|
Variables []*Variable
|
2017-07-01 18:12:31 +02:00
|
|
|
Locals []*Local
|
2014-07-19 07:21:52 +02:00
|
|
|
Outputs []*Output
|
2014-07-19 01:00:21 +02:00
|
|
|
|
|
|
|
// The fields below can be filled in by loaders for validation
|
|
|
|
// purposes.
|
|
|
|
unknownKeys []string
|
2014-05-26 03:05:18 +02:00
|
|
|
}
|
|
|
|
|
2015-03-05 21:56:31 +01:00
|
|
|
// AtlasConfig is the configuration for building in HashiCorp's Atlas.
|
|
|
|
type AtlasConfig struct {
|
|
|
|
Name string
|
|
|
|
Include []string
|
|
|
|
Exclude []string
|
|
|
|
}
|
|
|
|
|
2014-09-12 00:58:30 +02:00
|
|
|
// Module is a module used within a configuration.
|
|
|
|
//
|
|
|
|
// This does not represent a module itself, this represents a module
|
|
|
|
// call-site within an existing configuration.
|
|
|
|
type Module struct {
|
|
|
|
Name string
|
|
|
|
Source string
|
|
|
|
RawConfig *RawConfig
|
|
|
|
}
|
|
|
|
|
2014-05-26 03:05:18 +02:00
|
|
|
// ProviderConfig is the configuration for a resource provider.
|
|
|
|
//
|
|
|
|
// For example, Terraform needs to set the AWS access keys for the AWS
|
|
|
|
// resource provider.
|
|
|
|
type ProviderConfig struct {
|
2014-07-20 01:05:48 +02:00
|
|
|
Name string
|
2015-03-23 23:36:53 +01:00
|
|
|
Alias string
|
2017-04-12 00:38:44 +02:00
|
|
|
Version string
|
2014-06-13 02:40:59 +02:00
|
|
|
RawConfig *RawConfig
|
2014-05-23 01:56:28 +02:00
|
|
|
}
|
|
|
|
|
2014-05-24 06:58:06 +02:00
|
|
|
// A resource represents a single Terraform resource in the configuration.
|
2016-05-01 23:43:05 +02:00
|
|
|
// A Terraform resource is something that supports some or all of the
|
|
|
|
// usual "create, read, update, delete" operations, depending on
|
|
|
|
// the given Mode.
|
2014-05-23 01:56:28 +02:00
|
|
|
type Resource struct {
|
2016-05-01 23:43:05 +02:00
|
|
|
Mode ResourceMode // which operations the resource supports
|
2014-09-23 00:02:00 +02:00
|
|
|
Name string
|
|
|
|
Type string
|
2014-10-02 20:34:08 +02:00
|
|
|
RawCount *RawConfig
|
2014-09-23 00:02:00 +02:00
|
|
|
RawConfig *RawConfig
|
|
|
|
Provisioners []*Provisioner
|
2015-03-23 23:36:53 +01:00
|
|
|
Provider string
|
2014-09-23 00:02:00 +02:00
|
|
|
DependsOn []string
|
|
|
|
Lifecycle ResourceLifecycle
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:40:35 +01:00
|
|
|
// Copy returns a copy of this Resource. Helpful for avoiding shared
|
|
|
|
// config pointers across multiple pieces of the graph that need to do
|
|
|
|
// interpolation.
|
|
|
|
func (r *Resource) Copy() *Resource {
|
|
|
|
n := &Resource{
|
2016-05-01 23:43:05 +02:00
|
|
|
Mode: r.Mode,
|
2016-02-06 00:40:35 +01:00
|
|
|
Name: r.Name,
|
|
|
|
Type: r.Type,
|
|
|
|
RawCount: r.RawCount.Copy(),
|
|
|
|
RawConfig: r.RawConfig.Copy(),
|
|
|
|
Provisioners: make([]*Provisioner, 0, len(r.Provisioners)),
|
|
|
|
Provider: r.Provider,
|
|
|
|
DependsOn: make([]string, len(r.DependsOn)),
|
|
|
|
Lifecycle: *r.Lifecycle.Copy(),
|
|
|
|
}
|
|
|
|
for _, p := range r.Provisioners {
|
|
|
|
n.Provisioners = append(n.Provisioners, p.Copy())
|
|
|
|
}
|
|
|
|
copy(n.DependsOn, r.DependsOn)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2014-09-23 00:02:00 +02:00
|
|
|
// ResourceLifecycle is used to store the lifecycle tuning parameters
|
|
|
|
// to allow customized behavior
|
|
|
|
type ResourceLifecycle struct {
|
2015-08-09 10:02:28 +02:00
|
|
|
CreateBeforeDestroy bool `mapstructure:"create_before_destroy"`
|
|
|
|
PreventDestroy bool `mapstructure:"prevent_destroy"`
|
|
|
|
IgnoreChanges []string `mapstructure:"ignore_changes"`
|
2014-07-03 05:20:13 +02:00
|
|
|
}
|
|
|
|
|
2016-02-06 00:40:35 +01:00
|
|
|
// Copy returns a copy of this ResourceLifecycle
|
|
|
|
func (r *ResourceLifecycle) Copy() *ResourceLifecycle {
|
|
|
|
n := &ResourceLifecycle{
|
|
|
|
CreateBeforeDestroy: r.CreateBeforeDestroy,
|
|
|
|
PreventDestroy: r.PreventDestroy,
|
|
|
|
IgnoreChanges: make([]string, len(r.IgnoreChanges)),
|
|
|
|
}
|
|
|
|
copy(n.IgnoreChanges, r.IgnoreChanges)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2014-07-03 05:20:13 +02:00
|
|
|
// Provisioner is a configured provisioner step on a resource.
|
|
|
|
type Provisioner struct {
|
2014-05-24 06:58:06 +02:00
|
|
|
Type string
|
2014-06-13 02:40:59 +02:00
|
|
|
RawConfig *RawConfig
|
2014-07-11 02:14:47 +02:00
|
|
|
ConnInfo *RawConfig
|
2017-01-20 03:10:21 +01:00
|
|
|
|
|
|
|
When ProvisionerWhen
|
|
|
|
OnFailure ProvisionerOnFailure
|
2014-05-23 01:56:28 +02:00
|
|
|
}
|
|
|
|
|
2016-02-06 00:40:35 +01:00
|
|
|
// Copy returns a copy of this Provisioner
|
|
|
|
func (p *Provisioner) Copy() *Provisioner {
|
|
|
|
return &Provisioner{
|
|
|
|
Type: p.Type,
|
|
|
|
RawConfig: p.RawConfig.Copy(),
|
|
|
|
ConnInfo: p.ConnInfo.Copy(),
|
2017-01-20 03:10:21 +01:00
|
|
|
When: p.When,
|
|
|
|
OnFailure: p.OnFailure,
|
2016-02-06 00:40:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-01 18:12:31 +02:00
|
|
|
// Variable is a module argument defined within the configuration.
|
2014-05-23 01:56:28 +02:00
|
|
|
type Variable struct {
|
2016-01-21 20:33:16 +01:00
|
|
|
Name string
|
|
|
|
DeclaredType string `mapstructure:"type"`
|
|
|
|
Default interface{}
|
|
|
|
Description string
|
2014-05-23 01:56:28 +02:00
|
|
|
}
|
2014-05-24 06:58:06 +02:00
|
|
|
|
2017-07-01 18:12:31 +02:00
|
|
|
// Local is a local value defined within the configuration.
|
|
|
|
type Local struct {
|
|
|
|
Name string
|
|
|
|
RawConfig *RawConfig
|
|
|
|
}
|
|
|
|
|
2014-07-04 19:43:06 +02:00
|
|
|
// Output is an output defined within the configuration. An output is
|
2016-05-09 21:46:07 +02:00
|
|
|
// resulting data that is highlighted by Terraform when finished. An
|
|
|
|
// output marked Sensitive will be output in a masked form following
|
|
|
|
// application, but will still be available in state.
|
2014-07-04 19:43:06 +02:00
|
|
|
type Output struct {
|
2016-11-18 18:09:43 +01:00
|
|
|
Name string
|
|
|
|
DependsOn []string
|
|
|
|
Description string
|
|
|
|
Sensitive bool
|
|
|
|
RawConfig *RawConfig
|
2014-07-04 19:43:06 +02:00
|
|
|
}
|
|
|
|
|
2014-07-22 17:43:16 +02:00
|
|
|
// VariableType is the type of value a variable is holding, and returned
|
|
|
|
// by the Type() function on variables.
|
2014-07-22 16:41:55 +02:00
|
|
|
type VariableType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
VariableTypeUnknown VariableType = iota
|
|
|
|
VariableTypeString
|
core: support native list variables in config
This commit adds support for native list variables and outputs, building
up on the previous change to state. Interpolation functions now return
native lists in preference to StringList.
List variables are defined like this:
variable "test" {
# This can also be inferred
type = "list"
default = ["Hello", "World"]
}
output "test_out" {
value = "${var.a_list}"
}
This results in the following state:
```
...
"outputs": {
"test_out": [
"hello",
"world"
]
},
...
```
And the result of terraform output is as follows:
```
$ terraform output
test_out = [
hello
world
]
```
Using the output name, an xargs-friendly representation is output:
```
$ terraform output test_out
hello
world
```
The output command also supports indexing into the list (with
appropriate range checking and no wrapping):
```
$ terraform output test_out 1
world
```
Along with maps, list outputs from one module may be passed as variables
into another, removing the need for the `join(",", var.list_as_string)`
and `split(",", var.list_as_string)` which was previously necessary in
Terraform configuration.
This commit also updates the tests and implementations of built-in
interpolation functions to take and return native lists where
appropriate.
A backwards compatibility note: previously the concat interpolation
function was capable of concatenating either strings or lists. The
strings use case was deprectated a long time ago but still remained.
Because we cannot return `ast.TypeAny` from an interpolation function,
this use case is no longer supported for strings - `concat` is only
capable of concatenating lists. This should not be a huge issue - the
type checker picks up incorrect parameters, and the native HIL string
concatenation - or the `join` function - can be used to replicate the
missing behaviour.
2016-04-22 02:03:24 +02:00
|
|
|
VariableTypeList
|
2014-07-22 16:41:55 +02:00
|
|
|
VariableTypeMap
|
|
|
|
)
|
|
|
|
|
2016-04-15 21:07:54 +02:00
|
|
|
func (v VariableType) Printable() string {
|
|
|
|
switch v {
|
|
|
|
case VariableTypeString:
|
|
|
|
return "string"
|
|
|
|
case VariableTypeMap:
|
|
|
|
return "map"
|
core: support native list variables in config
This commit adds support for native list variables and outputs, building
up on the previous change to state. Interpolation functions now return
native lists in preference to StringList.
List variables are defined like this:
variable "test" {
# This can also be inferred
type = "list"
default = ["Hello", "World"]
}
output "test_out" {
value = "${var.a_list}"
}
This results in the following state:
```
...
"outputs": {
"test_out": [
"hello",
"world"
]
},
...
```
And the result of terraform output is as follows:
```
$ terraform output
test_out = [
hello
world
]
```
Using the output name, an xargs-friendly representation is output:
```
$ terraform output test_out
hello
world
```
The output command also supports indexing into the list (with
appropriate range checking and no wrapping):
```
$ terraform output test_out 1
world
```
Along with maps, list outputs from one module may be passed as variables
into another, removing the need for the `join(",", var.list_as_string)`
and `split(",", var.list_as_string)` which was previously necessary in
Terraform configuration.
This commit also updates the tests and implementations of built-in
interpolation functions to take and return native lists where
appropriate.
A backwards compatibility note: previously the concat interpolation
function was capable of concatenating either strings or lists. The
strings use case was deprectated a long time ago but still remained.
Because we cannot return `ast.TypeAny` from an interpolation function,
this use case is no longer supported for strings - `concat` is only
capable of concatenating lists. This should not be a huge issue - the
type checker picks up incorrect parameters, and the native HIL string
concatenation - or the `join` function - can be used to replicate the
missing behaviour.
2016-04-22 02:03:24 +02:00
|
|
|
case VariableTypeList:
|
|
|
|
return "list"
|
2016-04-15 21:07:54 +02:00
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 21:21:05 +02:00
|
|
|
// ProviderConfigName returns the name of the provider configuration in
|
|
|
|
// the given mapping that maps to the proper provider configuration
|
|
|
|
// for this resource.
|
2014-07-20 01:05:48 +02:00
|
|
|
func ProviderConfigName(t string, pcs []*ProviderConfig) string {
|
2014-06-05 21:21:05 +02:00
|
|
|
lk := ""
|
2014-07-20 01:05:48 +02:00
|
|
|
for _, v := range pcs {
|
|
|
|
k := v.Name
|
2014-06-24 22:29:07 +02:00
|
|
|
if strings.HasPrefix(t, k) && len(k) > len(lk) {
|
2014-06-05 21:21:05 +02:00
|
|
|
lk = k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lk
|
|
|
|
}
|
|
|
|
|
2014-09-12 00:58:30 +02:00
|
|
|
// A unique identifier for this module.
|
|
|
|
func (r *Module) Id() string {
|
2014-09-15 20:45:41 +02:00
|
|
|
return fmt.Sprintf("%s", r.Name)
|
2014-09-12 00:58:30 +02:00
|
|
|
}
|
|
|
|
|
2014-10-02 20:34:08 +02:00
|
|
|
// Count returns the count of this resource.
|
|
|
|
func (r *Resource) Count() (int, error) {
|
2017-02-10 19:41:41 +01:00
|
|
|
raw := r.RawCount.Value()
|
|
|
|
count, ok := r.RawCount.Value().(string)
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf(
|
|
|
|
"expected count to be a string or int, got %T", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := strconv.ParseInt(count, 0, 0)
|
2014-10-02 20:34:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(v), nil
|
|
|
|
}
|
|
|
|
|
2014-06-24 22:29:07 +02:00
|
|
|
// A unique identifier for this resource.
|
|
|
|
func (r *Resource) Id() string {
|
2016-05-01 23:43:05 +02:00
|
|
|
switch r.Mode {
|
|
|
|
case ManagedResourceMode:
|
|
|
|
return fmt.Sprintf("%s.%s", r.Type, r.Name)
|
|
|
|
case DataResourceMode:
|
|
|
|
return fmt.Sprintf("data.%s.%s", r.Type, r.Name)
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unknown resource mode %s", r.Mode))
|
|
|
|
}
|
2014-06-24 22:29:07 +02:00
|
|
|
}
|
|
|
|
|
2017-04-15 04:07:33 +02:00
|
|
|
// ProviderFullName returns the full name of the provider for this resource,
|
|
|
|
// which may either be specified explicitly using the "provider" meta-argument
|
|
|
|
// or implied by the prefix on the resource type name.
|
|
|
|
func (r *Resource) ProviderFullName() string {
|
|
|
|
return ResourceProviderFullName(r.Type, r.Provider)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceProviderFullName returns the full (dependable) name of the
|
|
|
|
// provider for a hypothetical resource with the given resource type and
|
|
|
|
// explicit provider string. If the explicit provider string is empty then
|
|
|
|
// the provider name is inferred from the resource type name.
|
|
|
|
func ResourceProviderFullName(resourceType, explicitProvider string) string {
|
|
|
|
if explicitProvider != "" {
|
|
|
|
return explicitProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := strings.IndexRune(resourceType, '_')
|
|
|
|
if idx == -1 {
|
|
|
|
// If no underscores, the resource name is assumed to be
|
|
|
|
// also the provider name, e.g. if the provider exposes
|
|
|
|
// only a single resource of each type.
|
|
|
|
return resourceType
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceType[:idx]
|
|
|
|
}
|
|
|
|
|
2014-06-05 21:47:28 +02:00
|
|
|
// Validate does some basic semantic checking of the configuration.
|
|
|
|
func (c *Config) Validate() error {
|
2014-09-25 00:48:46 +02:00
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-03 06:06:26 +02:00
|
|
|
var errs []error
|
|
|
|
|
2014-07-19 01:00:21 +02:00
|
|
|
for _, k := range c.unknownKeys {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"Unknown root level key: %s", k))
|
|
|
|
}
|
|
|
|
|
2016-11-13 01:20:33 +01:00
|
|
|
// Validate the Terraform config
|
|
|
|
if tf := c.Terraform; tf != nil {
|
2017-02-18 01:25:03 +01:00
|
|
|
errs = append(errs, c.Terraform.Validate()...)
|
2016-11-13 01:20:33 +01:00
|
|
|
}
|
|
|
|
|
2014-09-15 22:57:07 +02:00
|
|
|
vars := c.InterpolatedVariables()
|
2014-07-19 02:48:30 +02:00
|
|
|
varMap := make(map[string]*Variable)
|
|
|
|
for _, v := range c.Variables {
|
2016-08-25 23:51:49 +02:00
|
|
|
if _, ok := varMap[v.Name]; ok {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"Variable '%s': duplicate found. Variable names must be unique.",
|
|
|
|
v.Name))
|
|
|
|
}
|
|
|
|
|
2014-07-19 02:48:30 +02:00
|
|
|
varMap[v.Name] = v
|
|
|
|
}
|
2014-07-03 06:06:26 +02:00
|
|
|
|
2016-12-09 05:01:51 +01:00
|
|
|
for k, _ := range varMap {
|
|
|
|
if !NameRegexp.MatchString(k) {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"variable %q: variable name must match regular expresion %s",
|
|
|
|
k, NameRegexp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-22 17:34:24 +02:00
|
|
|
for _, v := range c.Variables {
|
|
|
|
if v.Type() == VariableTypeUnknown {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
2016-01-21 20:33:16 +01:00
|
|
|
"Variable '%s': must be a string or a map",
|
2014-07-22 17:34:24 +02:00
|
|
|
v.Name))
|
2014-08-11 18:46:56 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
interp := false
|
2017-03-29 17:34:45 +02:00
|
|
|
fn := func(n ast.Node) (interface{}, error) {
|
|
|
|
// LiteralNode is a literal string (outside of a ${ ... } sequence).
|
|
|
|
// interpolationWalker skips most of these. but in particular it
|
|
|
|
// visits those that have escaped sequences (like $${foo}) as a
|
|
|
|
// signal that *some* processing is required on this string. For
|
|
|
|
// our purposes here though, this is fine and not an interpolation.
|
|
|
|
if _, ok := n.(*ast.LiteralNode); !ok {
|
|
|
|
interp = true
|
|
|
|
}
|
2014-08-11 18:46:56 +02:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &interpolationWalker{F: fn}
|
2014-08-12 06:55:47 +02:00
|
|
|
if v.Default != nil {
|
|
|
|
if err := reflectwalk.Walk(v.Default, w); err == nil {
|
|
|
|
if interp {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"Variable '%s': cannot contain interpolations",
|
|
|
|
v.Name))
|
|
|
|
}
|
2014-08-11 18:46:56 +02:00
|
|
|
}
|
2014-07-22 17:34:24 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-21 17:34:44 +02:00
|
|
|
|
2014-07-03 06:00:06 +02:00
|
|
|
// Check for references to user variables that do not actually
|
|
|
|
// exist and record those errors.
|
2014-07-04 19:53:36 +02:00
|
|
|
for source, vs := range vars {
|
|
|
|
for _, v := range vs {
|
|
|
|
uv, ok := v.(*UserVariable)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-07-19 02:48:30 +02:00
|
|
|
if _, ok := varMap[uv.Name]; !ok {
|
2014-07-04 19:53:36 +02:00
|
|
|
errs = append(errs, fmt.Errorf(
|
2015-04-11 01:45:15 +02:00
|
|
|
"%s: unknown variable referenced: '%s'. define it with 'variable' blocks",
|
2014-07-04 19:53:36 +02:00
|
|
|
source,
|
|
|
|
uv.Name))
|
|
|
|
}
|
2014-07-03 06:00:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-03 03:24:37 +02:00
|
|
|
// Check that all count variables are valid.
|
|
|
|
for source, vs := range vars {
|
2014-10-08 03:03:11 +02:00
|
|
|
for _, rawV := range vs {
|
|
|
|
switch v := rawV.(type) {
|
|
|
|
case *CountVariable:
|
|
|
|
if v.Type == CountValueInvalid {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: invalid count variable: %s",
|
|
|
|
source,
|
|
|
|
v.FullKey()))
|
|
|
|
}
|
|
|
|
case *PathVariable:
|
|
|
|
if v.Type == PathValueInvalid {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: invalid path variable: %s",
|
|
|
|
source,
|
|
|
|
v.FullKey()))
|
|
|
|
}
|
2014-10-03 03:24:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 02:33:18 +02:00
|
|
|
// Check that providers aren't declared multiple times and that their
|
|
|
|
// version constraints, where present, are syntactically valid.
|
2015-04-20 23:25:33 +02:00
|
|
|
providerSet := make(map[string]struct{})
|
|
|
|
for _, p := range c.ProviderConfigs {
|
|
|
|
name := p.FullName()
|
|
|
|
if _, ok := providerSet[name]; ok {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"provider.%s: declared multiple times, you can only declare a provider once",
|
|
|
|
name))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-04-12 00:38:44 +02:00
|
|
|
if p.Version != "" {
|
2017-06-02 02:51:07 +02:00
|
|
|
_, err := discovery.ConstraintStr(p.Version).Parse()
|
2017-04-15 02:33:18 +02:00
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"provider.%s: invalid version constraint %q: %s",
|
|
|
|
name, p.Version, err,
|
|
|
|
))
|
|
|
|
}
|
2017-04-12 00:38:44 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 23:25:33 +02:00
|
|
|
providerSet[name] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2014-09-12 01:02:36 +02:00
|
|
|
// Check that all references to modules are valid
|
|
|
|
modules := make(map[string]*Module)
|
|
|
|
dupped := make(map[string]struct{})
|
|
|
|
for _, m := range c.Modules {
|
2014-10-08 05:19:32 +02:00
|
|
|
// Check for duplicates
|
2014-09-12 01:02:36 +02:00
|
|
|
if _, ok := modules[m.Id()]; ok {
|
|
|
|
if _, ok := dupped[m.Id()]; !ok {
|
|
|
|
dupped[m.Id()] = struct{}{}
|
|
|
|
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: module repeated multiple times",
|
|
|
|
m.Id()))
|
|
|
|
}
|
2014-12-16 07:10:16 +01:00
|
|
|
|
|
|
|
// Already seen this module, just skip it
|
|
|
|
continue
|
2014-09-12 01:02:36 +02:00
|
|
|
}
|
|
|
|
|
2014-12-16 07:10:16 +01:00
|
|
|
modules[m.Id()] = m
|
|
|
|
|
|
|
|
// Check that the source has no interpolations
|
|
|
|
rc, err := NewRawConfig(map[string]interface{}{
|
|
|
|
"root": m.Source,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: module source error: %s",
|
|
|
|
m.Id(), err))
|
|
|
|
} else if len(rc.Interpolations) > 0 {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: module source cannot contain interpolations",
|
|
|
|
m.Id()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the name matches our regexp
|
|
|
|
if !NameRegexp.Match([]byte(m.Name)) {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: module name can only contain letters, numbers, "+
|
|
|
|
"dashes, and underscores",
|
|
|
|
m.Id()))
|
|
|
|
}
|
2014-10-09 01:03:22 +02:00
|
|
|
|
core: support native list variables in config
This commit adds support for native list variables and outputs, building
up on the previous change to state. Interpolation functions now return
native lists in preference to StringList.
List variables are defined like this:
variable "test" {
# This can also be inferred
type = "list"
default = ["Hello", "World"]
}
output "test_out" {
value = "${var.a_list}"
}
This results in the following state:
```
...
"outputs": {
"test_out": [
"hello",
"world"
]
},
...
```
And the result of terraform output is as follows:
```
$ terraform output
test_out = [
hello
world
]
```
Using the output name, an xargs-friendly representation is output:
```
$ terraform output test_out
hello
world
```
The output command also supports indexing into the list (with
appropriate range checking and no wrapping):
```
$ terraform output test_out 1
world
```
Along with maps, list outputs from one module may be passed as variables
into another, removing the need for the `join(",", var.list_as_string)`
and `split(",", var.list_as_string)` which was previously necessary in
Terraform configuration.
This commit also updates the tests and implementations of built-in
interpolation functions to take and return native lists where
appropriate.
A backwards compatibility note: previously the concat interpolation
function was capable of concatenating either strings or lists. The
strings use case was deprectated a long time ago but still remained.
Because we cannot return `ast.TypeAny` from an interpolation function,
this use case is no longer supported for strings - `concat` is only
capable of concatenating lists. This should not be a huge issue - the
type checker picks up incorrect parameters, and the native HIL string
concatenation - or the `join` function - can be used to replicate the
missing behaviour.
2016-04-22 02:03:24 +02:00
|
|
|
// Check that the configuration can all be strings, lists or maps
|
2014-12-16 07:10:16 +01:00
|
|
|
raw := make(map[string]interface{})
|
|
|
|
for k, v := range m.RawConfig.Raw {
|
|
|
|
var strVal string
|
2016-06-03 22:57:04 +02:00
|
|
|
if err := hilmapstructure.WeakDecode(v, &strVal); err == nil {
|
core: support native list variables in config
This commit adds support for native list variables and outputs, building
up on the previous change to state. Interpolation functions now return
native lists in preference to StringList.
List variables are defined like this:
variable "test" {
# This can also be inferred
type = "list"
default = ["Hello", "World"]
}
output "test_out" {
value = "${var.a_list}"
}
This results in the following state:
```
...
"outputs": {
"test_out": [
"hello",
"world"
]
},
...
```
And the result of terraform output is as follows:
```
$ terraform output
test_out = [
hello
world
]
```
Using the output name, an xargs-friendly representation is output:
```
$ terraform output test_out
hello
world
```
The output command also supports indexing into the list (with
appropriate range checking and no wrapping):
```
$ terraform output test_out 1
world
```
Along with maps, list outputs from one module may be passed as variables
into another, removing the need for the `join(",", var.list_as_string)`
and `split(",", var.list_as_string)` which was previously necessary in
Terraform configuration.
This commit also updates the tests and implementations of built-in
interpolation functions to take and return native lists where
appropriate.
A backwards compatibility note: previously the concat interpolation
function was capable of concatenating either strings or lists. The
strings use case was deprectated a long time ago but still remained.
Because we cannot return `ast.TypeAny` from an interpolation function,
this use case is no longer supported for strings - `concat` is only
capable of concatenating lists. This should not be a huge issue - the
type checker picks up incorrect parameters, and the native HIL string
concatenation - or the `join` function - can be used to replicate the
missing behaviour.
2016-04-22 02:03:24 +02:00
|
|
|
raw[k] = strVal
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var mapVal map[string]interface{}
|
2016-06-03 22:57:04 +02:00
|
|
|
if err := hilmapstructure.WeakDecode(v, &mapVal); err == nil {
|
core: support native list variables in config
This commit adds support for native list variables and outputs, building
up on the previous change to state. Interpolation functions now return
native lists in preference to StringList.
List variables are defined like this:
variable "test" {
# This can also be inferred
type = "list"
default = ["Hello", "World"]
}
output "test_out" {
value = "${var.a_list}"
}
This results in the following state:
```
...
"outputs": {
"test_out": [
"hello",
"world"
]
},
...
```
And the result of terraform output is as follows:
```
$ terraform output
test_out = [
hello
world
]
```
Using the output name, an xargs-friendly representation is output:
```
$ terraform output test_out
hello
world
```
The output command also supports indexing into the list (with
appropriate range checking and no wrapping):
```
$ terraform output test_out 1
world
```
Along with maps, list outputs from one module may be passed as variables
into another, removing the need for the `join(",", var.list_as_string)`
and `split(",", var.list_as_string)` which was previously necessary in
Terraform configuration.
This commit also updates the tests and implementations of built-in
interpolation functions to take and return native lists where
appropriate.
A backwards compatibility note: previously the concat interpolation
function was capable of concatenating either strings or lists. The
strings use case was deprectated a long time ago but still remained.
Because we cannot return `ast.TypeAny` from an interpolation function,
this use case is no longer supported for strings - `concat` is only
capable of concatenating lists. This should not be a huge issue - the
type checker picks up incorrect parameters, and the native HIL string
concatenation - or the `join` function - can be used to replicate the
missing behaviour.
2016-04-22 02:03:24 +02:00
|
|
|
raw[k] = mapVal
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var sliceVal []interface{}
|
2016-06-03 22:57:04 +02:00
|
|
|
if err := hilmapstructure.WeakDecode(v, &sliceVal); err == nil {
|
core: support native list variables in config
This commit adds support for native list variables and outputs, building
up on the previous change to state. Interpolation functions now return
native lists in preference to StringList.
List variables are defined like this:
variable "test" {
# This can also be inferred
type = "list"
default = ["Hello", "World"]
}
output "test_out" {
value = "${var.a_list}"
}
This results in the following state:
```
...
"outputs": {
"test_out": [
"hello",
"world"
]
},
...
```
And the result of terraform output is as follows:
```
$ terraform output
test_out = [
hello
world
]
```
Using the output name, an xargs-friendly representation is output:
```
$ terraform output test_out
hello
world
```
The output command also supports indexing into the list (with
appropriate range checking and no wrapping):
```
$ terraform output test_out 1
world
```
Along with maps, list outputs from one module may be passed as variables
into another, removing the need for the `join(",", var.list_as_string)`
and `split(",", var.list_as_string)` which was previously necessary in
Terraform configuration.
This commit also updates the tests and implementations of built-in
interpolation functions to take and return native lists where
appropriate.
A backwards compatibility note: previously the concat interpolation
function was capable of concatenating either strings or lists. The
strings use case was deprectated a long time ago but still remained.
Because we cannot return `ast.TypeAny` from an interpolation function,
this use case is no longer supported for strings - `concat` is only
capable of concatenating lists. This should not be a huge issue - the
type checker picks up incorrect parameters, and the native HIL string
concatenation - or the `join` function - can be used to replicate the
missing behaviour.
2016-04-22 02:03:24 +02:00
|
|
|
raw[k] = sliceVal
|
|
|
|
continue
|
2014-10-09 01:03:22 +02:00
|
|
|
}
|
core: support native list variables in config
This commit adds support for native list variables and outputs, building
up on the previous change to state. Interpolation functions now return
native lists in preference to StringList.
List variables are defined like this:
variable "test" {
# This can also be inferred
type = "list"
default = ["Hello", "World"]
}
output "test_out" {
value = "${var.a_list}"
}
This results in the following state:
```
...
"outputs": {
"test_out": [
"hello",
"world"
]
},
...
```
And the result of terraform output is as follows:
```
$ terraform output
test_out = [
hello
world
]
```
Using the output name, an xargs-friendly representation is output:
```
$ terraform output test_out
hello
world
```
The output command also supports indexing into the list (with
appropriate range checking and no wrapping):
```
$ terraform output test_out 1
world
```
Along with maps, list outputs from one module may be passed as variables
into another, removing the need for the `join(",", var.list_as_string)`
and `split(",", var.list_as_string)` which was previously necessary in
Terraform configuration.
This commit also updates the tests and implementations of built-in
interpolation functions to take and return native lists where
appropriate.
A backwards compatibility note: previously the concat interpolation
function was capable of concatenating either strings or lists. The
strings use case was deprectated a long time ago but still remained.
Because we cannot return `ast.TypeAny` from an interpolation function,
this use case is no longer supported for strings - `concat` is only
capable of concatenating lists. This should not be a huge issue - the
type checker picks up incorrect parameters, and the native HIL string
concatenation - or the `join` function - can be used to replicate the
missing behaviour.
2016-04-22 02:03:24 +02:00
|
|
|
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: variable %s must be a string, list or map value",
|
|
|
|
m.Id(), k))
|
2014-10-08 05:19:32 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 17:41:56 +02:00
|
|
|
// Check for invalid count variables
|
|
|
|
for _, v := range m.RawConfig.Variables {
|
2015-04-22 10:39:07 +02:00
|
|
|
switch v.(type) {
|
|
|
|
case *CountVariable:
|
2015-04-15 17:41:56 +02:00
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: count variables are only valid within resources", m.Name))
|
2015-04-22 10:39:07 +02:00
|
|
|
case *SelfVariable:
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: self variables are only valid within resources", m.Name))
|
2015-04-15 17:41:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 07:10:16 +01:00
|
|
|
// Update the raw configuration to only contain the string values
|
|
|
|
m.RawConfig, err = NewRawConfig(raw)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: can't initialize configuration: %s",
|
|
|
|
m.Id(), err))
|
|
|
|
}
|
2014-09-12 01:02:36 +02:00
|
|
|
}
|
|
|
|
dupped = nil
|
|
|
|
|
2014-09-15 20:45:41 +02:00
|
|
|
// Check that all variables for modules reference modules that
|
|
|
|
// exist.
|
|
|
|
for source, vs := range vars {
|
|
|
|
for _, v := range vs {
|
|
|
|
mv, ok := v.(*ModuleVariable)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := modules[mv.Name]; !ok {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: unknown module referenced: %s",
|
|
|
|
source,
|
|
|
|
mv.Name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 06:06:26 +02:00
|
|
|
// Check that all references to resources are valid
|
2014-07-06 22:46:56 +02:00
|
|
|
resources := make(map[string]*Resource)
|
2014-09-12 01:02:36 +02:00
|
|
|
dupped = make(map[string]struct{})
|
2014-07-03 06:06:26 +02:00
|
|
|
for _, r := range c.Resources {
|
2014-07-19 01:31:01 +02:00
|
|
|
if _, ok := resources[r.Id()]; ok {
|
|
|
|
if _, ok := dupped[r.Id()]; !ok {
|
|
|
|
dupped[r.Id()] = struct{}{}
|
|
|
|
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: resource repeated multiple times",
|
|
|
|
r.Id()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 22:46:56 +02:00
|
|
|
resources[r.Id()] = r
|
2014-07-03 06:06:26 +02:00
|
|
|
}
|
2014-07-19 01:31:32 +02:00
|
|
|
dupped = nil
|
|
|
|
|
2014-07-26 23:49:55 +02:00
|
|
|
// Validate resources
|
2014-07-23 02:16:48 +02:00
|
|
|
for n, r := range resources {
|
2014-10-03 01:30:46 +02:00
|
|
|
// Verify count variables
|
|
|
|
for _, v := range r.RawCount.Variables {
|
|
|
|
switch v.(type) {
|
2014-10-03 03:22:32 +02:00
|
|
|
case *CountVariable:
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: resource count can't reference count variable: %s",
|
|
|
|
n,
|
|
|
|
v.FullKey()))
|
2016-08-16 22:48:12 +02:00
|
|
|
case *SimpleVariable:
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: resource count can't reference variable: %s",
|
|
|
|
n,
|
|
|
|
v.FullKey()))
|
2017-01-28 06:15:43 +01:00
|
|
|
|
|
|
|
// Good
|
|
|
|
case *ModuleVariable:
|
|
|
|
case *ResourceVariable:
|
2017-03-14 00:38:54 +01:00
|
|
|
case *TerraformVariable:
|
2014-10-03 01:30:46 +02:00
|
|
|
case *UserVariable:
|
2017-09-01 18:35:49 +02:00
|
|
|
case *LocalVariable:
|
2017-01-28 06:15:43 +01:00
|
|
|
|
2014-10-03 01:30:46 +02:00
|
|
|
default:
|
2017-03-14 00:38:54 +01:00
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"Internal error. Unknown type in count var in %s: %T",
|
|
|
|
n, v))
|
2014-10-03 01:30:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 19:27:57 +01:00
|
|
|
// Interpolate with a fixed number to verify that its a number.
|
2016-04-11 19:40:06 +02:00
|
|
|
r.RawCount.interpolate(func(root ast.Node) (interface{}, error) {
|
2015-01-13 19:27:57 +01:00
|
|
|
// Execute the node but transform the AST so that it returns
|
|
|
|
// a fixed value of "5" for all interpolations.
|
2016-04-09 03:23:36 +02:00
|
|
|
result, err := hil.Eval(
|
2016-01-31 08:38:37 +01:00
|
|
|
hil.FixedValueTransform(
|
2015-01-15 07:01:42 +01:00
|
|
|
root, &ast.LiteralNode{Value: "5", Typex: ast.TypeString}),
|
|
|
|
nil)
|
2015-01-13 19:27:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-04-11 19:40:06 +02:00
|
|
|
return result.Value, nil
|
2014-10-03 01:51:20 +02:00
|
|
|
})
|
|
|
|
_, err := strconv.ParseInt(r.RawCount.Value().(string), 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: resource count must be an integer",
|
|
|
|
n))
|
|
|
|
}
|
|
|
|
r.RawCount.init()
|
|
|
|
|
2016-11-12 17:21:27 +01:00
|
|
|
// Validate DependsOn
|
|
|
|
errs = append(errs, c.validateDependsOn(n, r.DependsOn, resources, modules)...)
|
2015-02-20 18:18:08 +01:00
|
|
|
|
2017-01-20 03:10:21 +01:00
|
|
|
// Verify provisioners
|
2015-02-20 18:18:08 +01:00
|
|
|
for _, p := range r.Provisioners {
|
2017-05-15 14:39:55 +02:00
|
|
|
// This validation checks that there are no splat variables
|
2015-02-20 18:18:08 +01:00
|
|
|
// referencing ourself. This currently is not allowed.
|
|
|
|
|
|
|
|
for _, v := range p.ConnInfo.Variables {
|
|
|
|
rv, ok := v.(*ResourceVariable)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if rv.Multi && rv.Index == -1 && rv.Type == r.Type && rv.Name == r.Name {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: connection info cannot contain splat variable "+
|
|
|
|
"referencing itself", n))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-02-20 18:21:29 +01:00
|
|
|
|
|
|
|
for _, v := range p.RawConfig.Variables {
|
|
|
|
rv, ok := v.(*ResourceVariable)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if rv.Multi && rv.Index == -1 && rv.Type == r.Type && rv.Name == r.Name {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: connection info cannot contain splat variable "+
|
|
|
|
"referencing itself", n))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-01-20 03:10:21 +01:00
|
|
|
|
|
|
|
// Check for invalid when/onFailure values, though this should be
|
|
|
|
// picked up by the loader we check here just in case.
|
|
|
|
if p.When == ProvisionerWhenInvalid {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: provisioner 'when' value is invalid", n))
|
|
|
|
}
|
|
|
|
if p.OnFailure == ProvisionerOnFailureInvalid {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: provisioner 'on_failure' value is invalid", n))
|
|
|
|
}
|
2015-02-20 18:18:08 +01:00
|
|
|
}
|
2016-09-02 15:44:35 +02:00
|
|
|
|
|
|
|
// Verify ignore_changes contains valid entries
|
|
|
|
for _, v := range r.Lifecycle.IgnoreChanges {
|
|
|
|
if strings.Contains(v, "*") && v != "*" {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: ignore_changes does not support using a partial string "+
|
|
|
|
"together with a wildcard: %s", n, v))
|
|
|
|
}
|
|
|
|
}
|
2016-10-25 08:06:33 +02:00
|
|
|
|
|
|
|
// Verify ignore_changes has no interpolations
|
|
|
|
rc, err := NewRawConfig(map[string]interface{}{
|
|
|
|
"root": r.Lifecycle.IgnoreChanges,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: lifecycle ignore_changes error: %s",
|
|
|
|
n, err))
|
|
|
|
} else if len(rc.Interpolations) > 0 {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: lifecycle ignore_changes cannot contain interpolations",
|
|
|
|
n))
|
|
|
|
}
|
2016-11-23 17:46:13 +01:00
|
|
|
|
|
|
|
// If it is a data source then it can't have provisioners
|
|
|
|
if r.Mode == DataResourceMode {
|
|
|
|
if _, ok := r.RawConfig.Raw["provisioner"]; ok {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: data sources cannot have provisioners",
|
|
|
|
n))
|
|
|
|
}
|
|
|
|
}
|
2014-07-23 02:16:48 +02:00
|
|
|
}
|
|
|
|
|
2014-07-04 19:53:36 +02:00
|
|
|
for source, vs := range vars {
|
|
|
|
for _, v := range vs {
|
|
|
|
rv, ok := v.(*ResourceVariable)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:53:42 +02:00
|
|
|
id := rv.ResourceId()
|
2014-10-02 20:14:50 +02:00
|
|
|
if _, ok := resources[id]; !ok {
|
2014-07-04 19:53:36 +02:00
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: unknown resource '%s' referenced in variable %s",
|
|
|
|
source,
|
|
|
|
id,
|
|
|
|
rv.FullKey()))
|
2014-07-06 22:46:56 +02:00
|
|
|
continue
|
|
|
|
}
|
2014-07-03 06:06:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-01 18:12:31 +02:00
|
|
|
// Check that all locals are valid
|
|
|
|
{
|
|
|
|
found := make(map[string]struct{})
|
|
|
|
for _, l := range c.Locals {
|
|
|
|
if _, ok := found[l.Name]; ok {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: duplicate local. local value names must be unique",
|
|
|
|
l.Name,
|
|
|
|
))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
found[l.Name] = struct{}{}
|
|
|
|
|
|
|
|
for _, v := range l.RawConfig.Variables {
|
|
|
|
if _, ok := v.(*CountVariable); ok {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"local %s: count variables are only valid within resources", l.Name,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 19:53:36 +02:00
|
|
|
// Check that all outputs are valid
|
2016-08-25 23:43:57 +02:00
|
|
|
{
|
|
|
|
found := make(map[string]struct{})
|
|
|
|
for _, o := range c.Outputs {
|
|
|
|
// Verify the output is new
|
|
|
|
if _, ok := found[o.Name]; ok {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: duplicate output. output names must be unique.",
|
|
|
|
o.Name))
|
2016-05-09 21:46:07 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-08-25 23:43:57 +02:00
|
|
|
found[o.Name] = struct{}{}
|
|
|
|
|
|
|
|
var invalidKeys []string
|
|
|
|
valueKeyFound := false
|
|
|
|
for k := range o.RawConfig.Raw {
|
|
|
|
if k == "value" {
|
|
|
|
valueKeyFound = true
|
2016-05-09 21:46:07 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-08-25 23:43:57 +02:00
|
|
|
if k == "sensitive" {
|
|
|
|
if sensitive, ok := o.RawConfig.config[k].(bool); ok {
|
|
|
|
if sensitive {
|
|
|
|
o.Sensitive = true
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2016-05-09 21:46:07 +02:00
|
|
|
|
2016-08-25 23:43:57 +02:00
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: value for 'sensitive' must be boolean",
|
|
|
|
o.Name))
|
|
|
|
continue
|
|
|
|
}
|
2016-11-18 18:09:43 +01:00
|
|
|
if k == "description" {
|
|
|
|
if desc, ok := o.RawConfig.config[k].(string); ok {
|
|
|
|
o.Description = desc
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: value for 'description' must be string",
|
|
|
|
o.Name))
|
|
|
|
continue
|
|
|
|
}
|
2016-08-25 23:43:57 +02:00
|
|
|
invalidKeys = append(invalidKeys, k)
|
|
|
|
}
|
|
|
|
if len(invalidKeys) > 0 {
|
2016-05-09 21:46:07 +02:00
|
|
|
errs = append(errs, fmt.Errorf(
|
2016-08-25 23:43:57 +02:00
|
|
|
"%s: output has invalid keys: %s",
|
|
|
|
o.Name, strings.Join(invalidKeys, ", ")))
|
2014-07-04 19:57:09 +02:00
|
|
|
}
|
2016-08-25 23:43:57 +02:00
|
|
|
if !valueKeyFound {
|
2015-04-15 02:42:23 +02:00
|
|
|
errs = append(errs, fmt.Errorf(
|
2016-08-25 23:43:57 +02:00
|
|
|
"%s: output is missing required 'value' key", o.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range o.RawConfig.Variables {
|
|
|
|
if _, ok := v.(*CountVariable); ok {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: count variables are only valid within resources", o.Name))
|
|
|
|
}
|
2015-04-15 02:42:23 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-04 19:57:09 +02:00
|
|
|
}
|
2014-07-04 19:53:36 +02:00
|
|
|
|
2015-02-23 23:43:14 +01:00
|
|
|
// Validate the self variable
|
|
|
|
for source, rc := range c.rawConfigs() {
|
|
|
|
// Ignore provisioners. This is a pretty brittle way to do this,
|
|
|
|
// but better than also repeating all the resources.
|
|
|
|
if strings.Contains(source, "provision") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range rc.Variables {
|
|
|
|
if _, ok := v.(*SelfVariable); ok {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: cannot contain self-reference %s", source, v.FullKey()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 06:00:06 +02:00
|
|
|
if len(errs) > 0 {
|
2014-07-03 19:14:17 +02:00
|
|
|
return &multierror.Error{Errors: errs}
|
2014-07-03 06:00:06 +02:00
|
|
|
}
|
2014-06-05 21:47:28 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-15 22:57:07 +02:00
|
|
|
// InterpolatedVariables is a helper that returns a mapping of all the interpolated
|
2014-07-03 06:00:06 +02:00
|
|
|
// variables within the configuration. This is used to verify references
|
|
|
|
// are valid in the Validate step.
|
2014-09-15 22:57:07 +02:00
|
|
|
func (c *Config) InterpolatedVariables() map[string][]InterpolatedVariable {
|
2014-07-04 19:53:36 +02:00
|
|
|
result := make(map[string][]InterpolatedVariable)
|
2014-10-10 06:15:08 +02:00
|
|
|
for source, rc := range c.rawConfigs() {
|
|
|
|
for _, v := range rc.Variables {
|
2014-07-04 19:53:36 +02:00
|
|
|
result[source] = append(result[source], v)
|
2014-07-03 06:00:06 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-10 06:15:08 +02:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// rawConfigs returns all of the RawConfigs that are available keyed by
|
|
|
|
// a human-friendly source.
|
|
|
|
func (c *Config) rawConfigs() map[string]*RawConfig {
|
|
|
|
result := make(map[string]*RawConfig)
|
2015-04-30 02:12:28 +02:00
|
|
|
for _, m := range c.Modules {
|
|
|
|
source := fmt.Sprintf("module '%s'", m.Name)
|
|
|
|
result[source] = m.RawConfig
|
|
|
|
}
|
|
|
|
|
2014-10-10 06:15:08 +02:00
|
|
|
for _, pc := range c.ProviderConfigs {
|
|
|
|
source := fmt.Sprintf("provider config '%s'", pc.Name)
|
|
|
|
result[source] = pc.RawConfig
|
|
|
|
}
|
2014-07-03 06:00:06 +02:00
|
|
|
|
|
|
|
for _, rc := range c.Resources {
|
|
|
|
source := fmt.Sprintf("resource '%s'", rc.Id())
|
2014-10-10 06:15:08 +02:00
|
|
|
result[source+" count"] = rc.RawCount
|
|
|
|
result[source+" config"] = rc.RawConfig
|
2015-02-17 22:32:45 +01:00
|
|
|
|
|
|
|
for i, p := range rc.Provisioners {
|
|
|
|
subsource := fmt.Sprintf(
|
|
|
|
"%s provisioner %s (#%d)",
|
|
|
|
source, p.Type, i+1)
|
|
|
|
result[subsource] = p.RawConfig
|
|
|
|
}
|
2014-07-04 19:53:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range c.Outputs {
|
|
|
|
source := fmt.Sprintf("output '%s'", o.Name)
|
2014-10-10 06:15:08 +02:00
|
|
|
result[source] = o.RawConfig
|
2014-07-03 06:00:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-11-12 17:21:27 +01:00
|
|
|
func (c *Config) validateDependsOn(
|
|
|
|
n string,
|
|
|
|
v []string,
|
|
|
|
resources map[string]*Resource,
|
|
|
|
modules map[string]*Module) []error {
|
|
|
|
// Verify depends on points to resources that all exist
|
|
|
|
var errs []error
|
|
|
|
for _, d := range v {
|
|
|
|
// Check if we contain interpolations
|
|
|
|
rc, err := NewRawConfig(map[string]interface{}{
|
|
|
|
"value": d,
|
|
|
|
})
|
|
|
|
if err == nil && len(rc.Variables) > 0 {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: depends on value cannot contain interpolations: %s",
|
|
|
|
n, d))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it is a module, verify it is a module
|
|
|
|
if strings.HasPrefix(d, "module.") {
|
|
|
|
name := d[len("module."):]
|
|
|
|
if _, ok := modules[name]; !ok {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: resource depends on non-existent module '%s'",
|
|
|
|
n, name))
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check resources
|
|
|
|
if _, ok := resources[d]; !ok {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: resource depends on non-existent resource '%s'",
|
|
|
|
n, d))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2014-09-12 04:54:02 +02:00
|
|
|
func (m *Module) mergerName() string {
|
|
|
|
return m.Id()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) mergerMerge(other merger) merger {
|
|
|
|
m2 := other.(*Module)
|
|
|
|
|
|
|
|
result := *m
|
|
|
|
result.Name = m2.Name
|
|
|
|
result.RawConfig = result.RawConfig.merge(m2.RawConfig)
|
|
|
|
|
|
|
|
if m2.Source != "" {
|
|
|
|
result.Source = m2.Source
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2014-07-21 02:17:03 +02:00
|
|
|
func (o *Output) mergerName() string {
|
|
|
|
return o.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Output) mergerMerge(m merger) merger {
|
|
|
|
o2 := m.(*Output)
|
|
|
|
|
|
|
|
result := *o
|
|
|
|
result.Name = o2.Name
|
2016-12-14 06:48:59 +01:00
|
|
|
result.Description = o2.Description
|
2014-07-21 02:17:03 +02:00
|
|
|
result.RawConfig = result.RawConfig.merge(o2.RawConfig)
|
2016-12-14 06:48:59 +01:00
|
|
|
result.Sensitive = o2.Sensitive
|
|
|
|
result.DependsOn = o2.DependsOn
|
2014-07-21 02:17:03 +02:00
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2015-06-25 05:58:52 +02:00
|
|
|
func (c *ProviderConfig) GoString() string {
|
|
|
|
return fmt.Sprintf("*%#v", *c)
|
|
|
|
}
|
|
|
|
|
2015-04-20 23:25:33 +02:00
|
|
|
func (c *ProviderConfig) FullName() string {
|
|
|
|
if c.Alias == "" {
|
|
|
|
return c.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s.%s", c.Name, c.Alias)
|
|
|
|
}
|
|
|
|
|
2014-07-21 02:17:03 +02:00
|
|
|
func (c *ProviderConfig) mergerName() string {
|
|
|
|
return c.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ProviderConfig) mergerMerge(m merger) merger {
|
|
|
|
c2 := m.(*ProviderConfig)
|
|
|
|
|
|
|
|
result := *c
|
|
|
|
result.Name = c2.Name
|
|
|
|
result.RawConfig = result.RawConfig.merge(c2.RawConfig)
|
|
|
|
|
2016-12-14 06:48:59 +01:00
|
|
|
if c2.Alias != "" {
|
|
|
|
result.Alias = c2.Alias
|
|
|
|
}
|
|
|
|
|
2014-07-21 02:17:03 +02:00
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resource) mergerName() string {
|
2016-05-01 23:43:05 +02:00
|
|
|
return r.Id()
|
2014-07-21 02:17:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resource) mergerMerge(m merger) merger {
|
|
|
|
r2 := m.(*Resource)
|
|
|
|
|
|
|
|
result := *r
|
2016-05-01 23:43:05 +02:00
|
|
|
result.Mode = r2.Mode
|
2014-07-21 02:17:03 +02:00
|
|
|
result.Name = r2.Name
|
|
|
|
result.Type = r2.Type
|
|
|
|
result.RawConfig = result.RawConfig.merge(r2.RawConfig)
|
|
|
|
|
2014-10-02 20:34:08 +02:00
|
|
|
if r2.RawCount.Value() != "1" {
|
|
|
|
result.RawCount = r2.RawCount
|
2014-07-21 02:17:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(r2.Provisioners) > 0 {
|
|
|
|
result.Provisioners = r2.Provisioners
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge merges two variables to create a new third variable.
|
|
|
|
func (v *Variable) Merge(v2 *Variable) *Variable {
|
|
|
|
// Shallow copy the variable
|
|
|
|
result := *v
|
|
|
|
|
|
|
|
// The names should be the same, but the second name always wins.
|
|
|
|
result.Name = v2.Name
|
|
|
|
|
2016-12-14 06:48:59 +01:00
|
|
|
if v2.DeclaredType != "" {
|
|
|
|
result.DeclaredType = v2.DeclaredType
|
|
|
|
}
|
2014-07-21 16:32:36 +02:00
|
|
|
if v2.Default != nil {
|
2014-07-21 02:17:03 +02:00
|
|
|
result.Default = v2.Default
|
|
|
|
}
|
|
|
|
if v2.Description != "" {
|
|
|
|
result.Description = v2.Description
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2016-01-21 20:33:16 +01:00
|
|
|
var typeStringMap = map[string]VariableType{
|
|
|
|
"string": VariableTypeString,
|
|
|
|
"map": VariableTypeMap,
|
core: support native list variables in config
This commit adds support for native list variables and outputs, building
up on the previous change to state. Interpolation functions now return
native lists in preference to StringList.
List variables are defined like this:
variable "test" {
# This can also be inferred
type = "list"
default = ["Hello", "World"]
}
output "test_out" {
value = "${var.a_list}"
}
This results in the following state:
```
...
"outputs": {
"test_out": [
"hello",
"world"
]
},
...
```
And the result of terraform output is as follows:
```
$ terraform output
test_out = [
hello
world
]
```
Using the output name, an xargs-friendly representation is output:
```
$ terraform output test_out
hello
world
```
The output command also supports indexing into the list (with
appropriate range checking and no wrapping):
```
$ terraform output test_out 1
world
```
Along with maps, list outputs from one module may be passed as variables
into another, removing the need for the `join(",", var.list_as_string)`
and `split(",", var.list_as_string)` which was previously necessary in
Terraform configuration.
This commit also updates the tests and implementations of built-in
interpolation functions to take and return native lists where
appropriate.
A backwards compatibility note: previously the concat interpolation
function was capable of concatenating either strings or lists. The
strings use case was deprectated a long time ago but still remained.
Because we cannot return `ast.TypeAny` from an interpolation function,
this use case is no longer supported for strings - `concat` is only
capable of concatenating lists. This should not be a huge issue - the
type checker picks up incorrect parameters, and the native HIL string
concatenation - or the `join` function - can be used to replicate the
missing behaviour.
2016-04-22 02:03:24 +02:00
|
|
|
"list": VariableTypeList,
|
2016-01-21 20:33:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type of variable this is.
|
2014-07-22 16:41:55 +02:00
|
|
|
func (v *Variable) Type() VariableType {
|
2016-01-21 20:33:16 +01:00
|
|
|
if v.DeclaredType != "" {
|
|
|
|
declaredType, ok := typeStringMap[v.DeclaredType]
|
|
|
|
if !ok {
|
|
|
|
return VariableTypeUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
return declaredType
|
2014-07-22 16:41:55 +02:00
|
|
|
}
|
|
|
|
|
2016-01-21 20:33:16 +01:00
|
|
|
return v.inferTypeFromDefault()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateTypeAndDefault ensures that default variable value is compatible
|
|
|
|
// with the declared type (if one exists), and that the type is one which is
|
|
|
|
// known to Terraform
|
|
|
|
func (v *Variable) ValidateTypeAndDefault() error {
|
|
|
|
// If an explicit type is declared, ensure it is valid
|
|
|
|
if v.DeclaredType != "" {
|
|
|
|
if _, ok := typeStringMap[v.DeclaredType]; !ok {
|
2017-04-20 21:41:33 +02:00
|
|
|
validTypes := []string{}
|
|
|
|
for k := range typeStringMap {
|
|
|
|
validTypes = append(validTypes, k)
|
|
|
|
}
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Variable '%s' type must be one of [%s] - '%s' is not a valid type",
|
|
|
|
v.Name,
|
|
|
|
strings.Join(validTypes, ", "),
|
|
|
|
v.DeclaredType,
|
|
|
|
)
|
2016-01-21 20:33:16 +01:00
|
|
|
}
|
2014-07-22 16:41:55 +02:00
|
|
|
}
|
|
|
|
|
2016-01-21 20:33:16 +01:00
|
|
|
if v.DeclaredType == "" || v.Default == nil {
|
|
|
|
return nil
|
2014-07-22 16:41:55 +02:00
|
|
|
}
|
|
|
|
|
2016-01-21 20:33:16 +01:00
|
|
|
if v.inferTypeFromDefault() != v.Type() {
|
2016-06-12 11:19:03 +02:00
|
|
|
return fmt.Errorf("'%s' has a default value which is not of type '%s' (got '%s')",
|
|
|
|
v.Name, v.DeclaredType, v.inferTypeFromDefault().Printable())
|
2016-01-21 20:33:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-07-22 16:41:55 +02:00
|
|
|
}
|
|
|
|
|
2014-07-21 02:17:03 +02:00
|
|
|
func (v *Variable) mergerName() string {
|
|
|
|
return v.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Variable) mergerMerge(m merger) merger {
|
|
|
|
return v.Merge(m.(*Variable))
|
|
|
|
}
|
|
|
|
|
2014-06-04 00:55:51 +02:00
|
|
|
// Required tests whether a variable is required or not.
|
|
|
|
func (v *Variable) Required() bool {
|
2014-07-21 16:32:36 +02:00
|
|
|
return v.Default == nil
|
2014-06-04 00:55:51 +02:00
|
|
|
}
|
2016-01-21 20:33:16 +01:00
|
|
|
|
|
|
|
// inferTypeFromDefault contains the logic for the old method of inferring
|
|
|
|
// variable types - we can also use this for validating that the declared
|
|
|
|
// type matches the type of the default value
|
|
|
|
func (v *Variable) inferTypeFromDefault() VariableType {
|
|
|
|
if v.Default == nil {
|
|
|
|
return VariableTypeString
|
|
|
|
}
|
|
|
|
|
core: support native list variables in config
This commit adds support for native list variables and outputs, building
up on the previous change to state. Interpolation functions now return
native lists in preference to StringList.
List variables are defined like this:
variable "test" {
# This can also be inferred
type = "list"
default = ["Hello", "World"]
}
output "test_out" {
value = "${var.a_list}"
}
This results in the following state:
```
...
"outputs": {
"test_out": [
"hello",
"world"
]
},
...
```
And the result of terraform output is as follows:
```
$ terraform output
test_out = [
hello
world
]
```
Using the output name, an xargs-friendly representation is output:
```
$ terraform output test_out
hello
world
```
The output command also supports indexing into the list (with
appropriate range checking and no wrapping):
```
$ terraform output test_out 1
world
```
Along with maps, list outputs from one module may be passed as variables
into another, removing the need for the `join(",", var.list_as_string)`
and `split(",", var.list_as_string)` which was previously necessary in
Terraform configuration.
This commit also updates the tests and implementations of built-in
interpolation functions to take and return native lists where
appropriate.
A backwards compatibility note: previously the concat interpolation
function was capable of concatenating either strings or lists. The
strings use case was deprectated a long time ago but still remained.
Because we cannot return `ast.TypeAny` from an interpolation function,
this use case is no longer supported for strings - `concat` is only
capable of concatenating lists. This should not be a huge issue - the
type checker picks up incorrect parameters, and the native HIL string
concatenation - or the `join` function - can be used to replicate the
missing behaviour.
2016-04-22 02:03:24 +02:00
|
|
|
var s string
|
2016-06-03 22:57:04 +02:00
|
|
|
if err := hilmapstructure.WeakDecode(v.Default, &s); err == nil {
|
core: support native list variables in config
This commit adds support for native list variables and outputs, building
up on the previous change to state. Interpolation functions now return
native lists in preference to StringList.
List variables are defined like this:
variable "test" {
# This can also be inferred
type = "list"
default = ["Hello", "World"]
}
output "test_out" {
value = "${var.a_list}"
}
This results in the following state:
```
...
"outputs": {
"test_out": [
"hello",
"world"
]
},
...
```
And the result of terraform output is as follows:
```
$ terraform output
test_out = [
hello
world
]
```
Using the output name, an xargs-friendly representation is output:
```
$ terraform output test_out
hello
world
```
The output command also supports indexing into the list (with
appropriate range checking and no wrapping):
```
$ terraform output test_out 1
world
```
Along with maps, list outputs from one module may be passed as variables
into another, removing the need for the `join(",", var.list_as_string)`
and `split(",", var.list_as_string)` which was previously necessary in
Terraform configuration.
This commit also updates the tests and implementations of built-in
interpolation functions to take and return native lists where
appropriate.
A backwards compatibility note: previously the concat interpolation
function was capable of concatenating either strings or lists. The
strings use case was deprectated a long time ago but still remained.
Because we cannot return `ast.TypeAny` from an interpolation function,
this use case is no longer supported for strings - `concat` is only
capable of concatenating lists. This should not be a huge issue - the
type checker picks up incorrect parameters, and the native HIL string
concatenation - or the `join` function - can be used to replicate the
missing behaviour.
2016-04-22 02:03:24 +02:00
|
|
|
v.Default = s
|
2016-01-21 20:33:16 +01:00
|
|
|
return VariableTypeString
|
|
|
|
}
|
|
|
|
|
2016-06-12 11:19:03 +02:00
|
|
|
var m map[string]interface{}
|
2016-06-03 22:57:04 +02:00
|
|
|
if err := hilmapstructure.WeakDecode(v.Default, &m); err == nil {
|
2016-01-21 20:33:16 +01:00
|
|
|
v.Default = m
|
|
|
|
return VariableTypeMap
|
|
|
|
}
|
|
|
|
|
2016-06-12 11:19:03 +02:00
|
|
|
var l []interface{}
|
2016-06-03 22:57:04 +02:00
|
|
|
if err := hilmapstructure.WeakDecode(v.Default, &l); err == nil {
|
core: support native list variables in config
This commit adds support for native list variables and outputs, building
up on the previous change to state. Interpolation functions now return
native lists in preference to StringList.
List variables are defined like this:
variable "test" {
# This can also be inferred
type = "list"
default = ["Hello", "World"]
}
output "test_out" {
value = "${var.a_list}"
}
This results in the following state:
```
...
"outputs": {
"test_out": [
"hello",
"world"
]
},
...
```
And the result of terraform output is as follows:
```
$ terraform output
test_out = [
hello
world
]
```
Using the output name, an xargs-friendly representation is output:
```
$ terraform output test_out
hello
world
```
The output command also supports indexing into the list (with
appropriate range checking and no wrapping):
```
$ terraform output test_out 1
world
```
Along with maps, list outputs from one module may be passed as variables
into another, removing the need for the `join(",", var.list_as_string)`
and `split(",", var.list_as_string)` which was previously necessary in
Terraform configuration.
This commit also updates the tests and implementations of built-in
interpolation functions to take and return native lists where
appropriate.
A backwards compatibility note: previously the concat interpolation
function was capable of concatenating either strings or lists. The
strings use case was deprectated a long time ago but still remained.
Because we cannot return `ast.TypeAny` from an interpolation function,
this use case is no longer supported for strings - `concat` is only
capable of concatenating lists. This should not be a huge issue - the
type checker picks up incorrect parameters, and the native HIL string
concatenation - or the `join` function - can be used to replicate the
missing behaviour.
2016-04-22 02:03:24 +02:00
|
|
|
v.Default = l
|
|
|
|
return VariableTypeList
|
|
|
|
}
|
|
|
|
|
2016-01-21 20:33:16 +01:00
|
|
|
return VariableTypeUnknown
|
|
|
|
}
|
2016-05-08 11:51:46 +02:00
|
|
|
|
|
|
|
func (m ResourceMode) Taintable() bool {
|
|
|
|
switch m {
|
|
|
|
case ManagedResourceMode:
|
|
|
|
return true
|
|
|
|
case DataResourceMode:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unsupported ResourceMode value %s", m))
|
|
|
|
}
|
|
|
|
}
|