2015-02-05 18:47:06 -06:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-07-05 09:26:01 -05:00
|
|
|
"log"
|
2015-02-05 18:47:06 -06:00
|
|
|
"os"
|
2015-07-05 09:26:01 -05:00
|
|
|
"regexp"
|
|
|
|
"sort"
|
2015-02-05 18:47:06 -06:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2016-04-11 12:40:06 -05:00
|
|
|
"github.com/hashicorp/hil"
|
2016-01-31 01:43:41 -06:00
|
|
|
"github.com/hashicorp/hil/ast"
|
2015-02-05 18:47:06 -06:00
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/config/module"
|
|
|
|
)
|
|
|
|
|
2015-04-21 23:31:53 -05:00
|
|
|
const (
|
|
|
|
// VarEnvPrefix is the prefix of variables that are read from
|
|
|
|
// the environment to set variables here.
|
|
|
|
VarEnvPrefix = "TF_VAR_"
|
|
|
|
)
|
|
|
|
|
2015-02-05 18:47:06 -06:00
|
|
|
// Interpolater is the structure responsible for determining the values
|
|
|
|
// for interpolations such as `aws_instance.foo.bar`.
|
|
|
|
type Interpolater struct {
|
2016-04-11 12:40:06 -05:00
|
|
|
Operation walkOperation
|
|
|
|
Module *module.Tree
|
|
|
|
State *State
|
|
|
|
StateLock *sync.RWMutex
|
|
|
|
VariableValues map[string]interface{}
|
|
|
|
VariableValuesLock *sync.Mutex
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// InterpolationScope is the current scope of execution. This is required
|
|
|
|
// since some variables which are interpolated are dependent on what we're
|
|
|
|
// operating on and where we are.
|
|
|
|
type InterpolationScope struct {
|
|
|
|
Path []string
|
|
|
|
Resource *Resource
|
|
|
|
}
|
|
|
|
|
|
|
|
// Values returns the values for all the variables in the given map.
|
|
|
|
func (i *Interpolater) Values(
|
|
|
|
scope *InterpolationScope,
|
|
|
|
vars map[string]config.InterpolatedVariable) (map[string]ast.Variable, error) {
|
|
|
|
result := make(map[string]ast.Variable, len(vars))
|
2015-02-05 18:53:50 -06:00
|
|
|
|
|
|
|
// Copy the default variables
|
|
|
|
if i.Module != nil && scope != nil {
|
|
|
|
mod := i.Module
|
|
|
|
if len(scope.Path) > 1 {
|
|
|
|
mod = i.Module.Child(scope.Path[1:])
|
|
|
|
}
|
|
|
|
for _, v := range mod.Config().Variables {
|
2016-04-11 12:40:06 -05:00
|
|
|
// Set default variables
|
|
|
|
if v.Default == nil {
|
|
|
|
continue
|
2015-02-05 18:53:50 -06:00
|
|
|
}
|
2016-04-11 12:40:06 -05:00
|
|
|
|
|
|
|
n := fmt.Sprintf("var.%s", v.Name)
|
|
|
|
variable, err := hil.InterfaceToVariable(v.Default)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid default map value for %s: %v", v.Name, v.Default)
|
|
|
|
}
|
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-21 19:03:24 -05:00
|
|
|
|
2016-04-11 12:40:06 -05:00
|
|
|
result[n] = variable
|
2015-02-05 18:53:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 18:47:06 -06:00
|
|
|
for n, rawV := range vars {
|
|
|
|
var err error
|
|
|
|
switch v := rawV.(type) {
|
|
|
|
case *config.CountVariable:
|
|
|
|
err = i.valueCountVar(scope, n, v, result)
|
|
|
|
case *config.ModuleVariable:
|
|
|
|
err = i.valueModuleVar(scope, n, v, result)
|
|
|
|
case *config.PathVariable:
|
|
|
|
err = i.valuePathVar(scope, n, v, result)
|
|
|
|
case *config.ResourceVariable:
|
|
|
|
err = i.valueResourceVar(scope, n, v, result)
|
2015-02-23 16:56:02 -06:00
|
|
|
case *config.SelfVariable:
|
|
|
|
err = i.valueSelfVar(scope, n, v, result)
|
2015-11-13 11:07:02 -06:00
|
|
|
case *config.SimpleVariable:
|
|
|
|
err = i.valueSimpleVar(scope, n, v, result)
|
2015-02-05 18:47:06 -06:00
|
|
|
case *config.UserVariable:
|
|
|
|
err = i.valueUserVar(scope, n, v, result)
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("%s: unknown variable type: %T", n, rawV)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interpolater) valueCountVar(
|
|
|
|
scope *InterpolationScope,
|
|
|
|
n string,
|
|
|
|
v *config.CountVariable,
|
|
|
|
result map[string]ast.Variable) error {
|
|
|
|
switch v.Type {
|
|
|
|
case config.CountValueIndex:
|
2015-04-14 19:42:23 -05:00
|
|
|
if scope.Resource == nil {
|
|
|
|
return fmt.Errorf("%s: count.index is only valid within resources", n)
|
|
|
|
}
|
2015-02-05 18:47:06 -06:00
|
|
|
result[n] = ast.Variable{
|
|
|
|
Value: scope.Resource.CountIndex,
|
|
|
|
Type: ast.TypeInt,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("%s: unknown count type: %#v", n, v.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 09:22:33 -05:00
|
|
|
func unknownVariable() ast.Variable {
|
|
|
|
return ast.Variable{
|
|
|
|
Type: ast.TypeString,
|
|
|
|
Value: config.UnknownVariableValue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 18:47:06 -06:00
|
|
|
func (i *Interpolater) valueModuleVar(
|
|
|
|
scope *InterpolationScope,
|
|
|
|
n string,
|
|
|
|
v *config.ModuleVariable,
|
|
|
|
result map[string]ast.Variable) error {
|
|
|
|
// If we're computing all dynamic fields, then module vars count
|
|
|
|
// and we mark it as computed.
|
|
|
|
if i.Operation == walkValidate {
|
|
|
|
result[n] = ast.Variable{
|
|
|
|
Value: config.UnknownVariableValue,
|
|
|
|
Type: ast.TypeString,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the path to the child module we want
|
|
|
|
path := make([]string, len(scope.Path), len(scope.Path)+1)
|
|
|
|
copy(path, scope.Path)
|
|
|
|
path = append(path, v.Name)
|
|
|
|
|
|
|
|
// Grab the lock so that if other interpolations are running or
|
|
|
|
// state is being modified, we'll be safe.
|
|
|
|
i.StateLock.RLock()
|
|
|
|
defer i.StateLock.RUnlock()
|
|
|
|
|
|
|
|
// Get the module where we're looking for the value
|
|
|
|
mod := i.State.ModuleByPath(path)
|
|
|
|
if mod == nil {
|
|
|
|
// If the module doesn't exist, then we can return an empty string.
|
|
|
|
// This happens usually only in Refresh() when we haven't populated
|
|
|
|
// a state. During validation, we semantically verify that all
|
|
|
|
// modules reference other modules, and graph ordering should
|
|
|
|
// ensure that the module is in the state, so if we reach this
|
|
|
|
// point otherwise it really is a panic.
|
2016-03-22 09:22:33 -05:00
|
|
|
result[n] = unknownVariable()
|
2015-02-05 18:47:06 -06:00
|
|
|
} else {
|
|
|
|
// Get the value from the outputs
|
2016-03-22 09:22:33 -05:00
|
|
|
if value, ok := mod.Outputs[v.Field]; ok {
|
2016-04-11 12:40:06 -05:00
|
|
|
output, err := hil.InterfaceToVariable(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
result[n] = output
|
2016-03-22 09:22:33 -05:00
|
|
|
} else {
|
2015-02-05 18:47:06 -06:00
|
|
|
// Same reasons as the comment above.
|
2016-03-22 09:22:33 -05:00
|
|
|
result[n] = unknownVariable()
|
|
|
|
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interpolater) valuePathVar(
|
|
|
|
scope *InterpolationScope,
|
|
|
|
n string,
|
|
|
|
v *config.PathVariable,
|
|
|
|
result map[string]ast.Variable) error {
|
|
|
|
switch v.Type {
|
|
|
|
case config.PathValueCwd:
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Couldn't get cwd for var %s: %s",
|
|
|
|
v.FullKey(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result[n] = ast.Variable{
|
|
|
|
Value: wd,
|
|
|
|
Type: ast.TypeString,
|
|
|
|
}
|
|
|
|
case config.PathValueModule:
|
|
|
|
if t := i.Module.Child(scope.Path[1:]); t != nil {
|
|
|
|
result[n] = ast.Variable{
|
|
|
|
Value: t.Config().Dir,
|
|
|
|
Type: ast.TypeString,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case config.PathValueRoot:
|
|
|
|
result[n] = ast.Variable{
|
|
|
|
Value: i.Module.Config().Dir,
|
|
|
|
Type: ast.TypeString,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("%s: unknown path type: %#v", n, v.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interpolater) valueResourceVar(
|
|
|
|
scope *InterpolationScope,
|
|
|
|
n string,
|
|
|
|
v *config.ResourceVariable,
|
|
|
|
result map[string]ast.Variable) error {
|
|
|
|
// If we're computing all dynamic fields, then module vars count
|
|
|
|
// and we mark it as computed.
|
2015-04-10 15:51:22 -05:00
|
|
|
if i.Operation == walkValidate {
|
2015-02-05 18:47:06 -06:00
|
|
|
result[n] = ast.Variable{
|
|
|
|
Value: config.UnknownVariableValue,
|
|
|
|
Type: ast.TypeString,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Multi && v.Index == -1 {
|
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-21 19:03:24 -05:00
|
|
|
variable, err := i.computeResourceMultiVariable(scope, v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if variable == nil {
|
|
|
|
return fmt.Errorf("no error reported by variable %q is nil", v.Name)
|
|
|
|
}
|
|
|
|
result[n] = *variable
|
2015-02-05 18:47:06 -06:00
|
|
|
} else {
|
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-21 19:03:24 -05:00
|
|
|
variable, err := i.computeResourceVariable(scope, v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if variable == nil {
|
|
|
|
return fmt.Errorf("no error reported by variable %q is nil", v.Name)
|
|
|
|
}
|
|
|
|
result[n] = *variable
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-23 16:56:02 -06:00
|
|
|
func (i *Interpolater) valueSelfVar(
|
|
|
|
scope *InterpolationScope,
|
|
|
|
n string,
|
|
|
|
v *config.SelfVariable,
|
|
|
|
result map[string]ast.Variable) error {
|
2016-02-23 11:42:55 -06:00
|
|
|
if scope == nil || scope.Resource == nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"%s: invalid scope, self variables are only valid on resources", n)
|
|
|
|
}
|
2015-02-23 16:56:02 -06:00
|
|
|
rv, err := config.NewResourceVariable(fmt.Sprintf(
|
|
|
|
"%s.%s.%d.%s",
|
|
|
|
scope.Resource.Type,
|
|
|
|
scope.Resource.Name,
|
|
|
|
scope.Resource.CountIndex,
|
|
|
|
v.Field))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.valueResourceVar(scope, n, rv, result)
|
|
|
|
}
|
|
|
|
|
2015-11-13 11:07:02 -06:00
|
|
|
func (i *Interpolater) valueSimpleVar(
|
|
|
|
scope *InterpolationScope,
|
|
|
|
n string,
|
|
|
|
v *config.SimpleVariable,
|
|
|
|
result map[string]ast.Variable) error {
|
|
|
|
// SimpleVars are never handled by Terraform's interpolator
|
|
|
|
result[n] = ast.Variable{
|
|
|
|
Value: config.UnknownVariableValue,
|
|
|
|
Type: ast.TypeString,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-05 18:47:06 -06:00
|
|
|
func (i *Interpolater) valueUserVar(
|
|
|
|
scope *InterpolationScope,
|
|
|
|
n string,
|
|
|
|
v *config.UserVariable,
|
|
|
|
result map[string]ast.Variable) error {
|
2016-04-11 12:40:06 -05:00
|
|
|
i.VariableValuesLock.Lock()
|
|
|
|
defer i.VariableValuesLock.Unlock()
|
|
|
|
val, ok := i.VariableValues[v.Name]
|
2015-02-05 18:47:06 -06:00
|
|
|
if ok {
|
2016-04-11 12:40:06 -05:00
|
|
|
varValue, err := hil.InterfaceToVariable(val)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot convert %s value %q to an ast.Variable for interpolation: %s",
|
|
|
|
v.Name, val, err)
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
2016-04-11 12:40:06 -05:00
|
|
|
result[n] = varValue
|
2015-02-05 18:47:06 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := result[n]; !ok && i.Operation == walkValidate {
|
2016-04-11 12:40:06 -05:00
|
|
|
result[n] = unknownVariable()
|
2015-02-05 18:47:06 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up if we have any variables with this prefix because
|
|
|
|
// those are map overrides. Include those.
|
2016-04-11 12:40:06 -05:00
|
|
|
for k, val := range i.VariableValues {
|
2015-02-05 18:47:06 -06:00
|
|
|
if strings.HasPrefix(k, v.Name+".") {
|
2016-04-11 12:40:06 -05:00
|
|
|
keyComponents := strings.Split(k, ".")
|
|
|
|
overrideKey := keyComponents[len(keyComponents)-1]
|
|
|
|
|
|
|
|
mapInterface, ok := result["var."+v.Name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("override for non-existent variable: %s", v.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
mapVariable := mapInterface.Value.(map[string]ast.Variable)
|
|
|
|
|
|
|
|
varValue, err := hil.InterfaceToVariable(val)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot convert %s value %q to an ast.Variable for interpolation: %s",
|
|
|
|
v.Name, val, err)
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
2016-04-11 12:40:06 -05:00
|
|
|
mapVariable[overrideKey] = varValue
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interpolater) computeResourceVariable(
|
|
|
|
scope *InterpolationScope,
|
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-21 19:03:24 -05:00
|
|
|
v *config.ResourceVariable) (*ast.Variable, error) {
|
2015-02-05 18:47:06 -06:00
|
|
|
id := v.ResourceId()
|
|
|
|
if v.Multi {
|
|
|
|
id = fmt.Sprintf("%s.%d", id, v.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.StateLock.RLock()
|
|
|
|
defer i.StateLock.RUnlock()
|
|
|
|
|
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-21 19:03:24 -05:00
|
|
|
unknownVariable := unknownVariable()
|
|
|
|
|
2015-02-05 18:47:06 -06:00
|
|
|
// Get the information about this resource variable, and verify
|
|
|
|
// that it exists and such.
|
|
|
|
module, _, err := i.resourceVariableInfo(scope, v)
|
|
|
|
if 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-21 19:03:24 -05:00
|
|
|
return nil, err
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have no module in the state yet or count, return empty
|
|
|
|
if module == nil || len(module.Resources) == 0 {
|
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-21 19:03:24 -05:00
|
|
|
return nil, nil
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the resource out from the state. We know the state exists
|
|
|
|
// at this point and if there is a state, we expect there to be a
|
|
|
|
// resource with the given name.
|
|
|
|
r, ok := module.Resources[id]
|
|
|
|
if !ok && v.Multi && v.Index == 0 {
|
|
|
|
r, ok = module.Resources[v.ResourceId()]
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
r = nil
|
|
|
|
}
|
|
|
|
if r == nil {
|
2015-05-11 16:57:20 -05:00
|
|
|
goto MISSING
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.Primary == nil {
|
|
|
|
goto MISSING
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := r.Primary.Attributes[v.Field]; ok {
|
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-21 19:03:24 -05:00
|
|
|
return &ast.Variable{Type: ast.TypeString, Value: attr}, nil
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
|
|
|
|
2016-04-27 16:41:17 -05:00
|
|
|
// computed list or map attribute
|
2015-07-05 09:26:01 -05:00
|
|
|
if _, ok := r.Primary.Attributes[v.Field+".#"]; ok {
|
2016-04-27 16:41:17 -05:00
|
|
|
variable, err := i.interpolateComplexTypeAttribute(v.Field, r.Primary.Attributes)
|
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-21 19:03:24 -05:00
|
|
|
return &variable, err
|
2015-07-05 09:26:01 -05:00
|
|
|
}
|
|
|
|
|
2015-02-05 18:47:06 -06:00
|
|
|
// At apply time, we can't do the "maybe has it" check below
|
|
|
|
// that we need for plans since parent elements might be computed.
|
|
|
|
// Therefore, it is an error and we're missing the key.
|
|
|
|
//
|
|
|
|
// TODO: test by creating a state and configuration that is referencing
|
|
|
|
// a non-existent variable "foo.bar" where the state only has "foo"
|
|
|
|
// and verify plan works, but apply doesn't.
|
2015-10-02 13:20:09 -05:00
|
|
|
if i.Operation == walkApply || i.Operation == walkDestroy {
|
2015-02-05 18:47:06 -06:00
|
|
|
goto MISSING
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't find the exact field, so lets separate the dots
|
|
|
|
// and see if anything along the way is a computed set. i.e. if
|
|
|
|
// we have "foo.0.bar" as the field, check to see if "foo" is
|
|
|
|
// a computed list. If so, then the whole thing is computed.
|
|
|
|
if parts := strings.Split(v.Field, "."); len(parts) > 1 {
|
|
|
|
for i := 1; i < len(parts); i++ {
|
|
|
|
// Lists and sets make this
|
|
|
|
key := fmt.Sprintf("%s.#", strings.Join(parts[:i], "."))
|
|
|
|
if attr, ok := r.Primary.Attributes[key]; ok {
|
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-21 19:03:24 -05:00
|
|
|
return &ast.Variable{Type: ast.TypeString, Value: attr}, nil
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Maps make this
|
|
|
|
key = fmt.Sprintf("%s", strings.Join(parts[:i], "."))
|
|
|
|
if attr, ok := r.Primary.Attributes[key]; ok {
|
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-21 19:03:24 -05:00
|
|
|
return &ast.Variable{Type: ast.TypeString, Value: attr}, nil
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MISSING:
|
2015-05-11 16:57:20 -05:00
|
|
|
// Validation for missing interpolations should happen at a higher
|
|
|
|
// semantic level. If we reached this point and don't have variables,
|
|
|
|
// just return the computed value.
|
2015-06-25 00:25:48 -05:00
|
|
|
if scope == nil && scope.Resource == 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-21 19:03:24 -05:00
|
|
|
return &unknownVariable, nil
|
2015-05-11 16:57:20 -05:00
|
|
|
}
|
|
|
|
|
2015-04-10 15:51:22 -05:00
|
|
|
// If the operation is refresh, it isn't an error for a value to
|
|
|
|
// be unknown. Instead, we return that the value is computed so
|
|
|
|
// that the graph can continue to refresh other nodes. It doesn't
|
|
|
|
// matter because the config isn't interpolated anyways.
|
2015-08-11 11:36:56 -05:00
|
|
|
//
|
|
|
|
// For a Destroy, we're also fine with computed values, since our goal is
|
|
|
|
// only to get destroy nodes for existing resources.
|
|
|
|
//
|
|
|
|
// For an input walk, computed values are okay to return because we're only
|
|
|
|
// looking for missing variables to prompt the user for.
|
2015-10-02 13:20:09 -05:00
|
|
|
if i.Operation == walkRefresh || i.Operation == walkPlanDestroy || i.Operation == walkDestroy || i.Operation == walkInput {
|
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-21 19:03:24 -05:00
|
|
|
return &unknownVariable, nil
|
2015-04-10 15:51:22 -05: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-21 19:03:24 -05:00
|
|
|
return nil, fmt.Errorf(
|
2015-02-05 18:47:06 -06:00
|
|
|
"Resource '%s' does not have attribute '%s' "+
|
|
|
|
"for variable '%s'",
|
|
|
|
id,
|
|
|
|
v.Field,
|
|
|
|
v.FullKey())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interpolater) computeResourceMultiVariable(
|
|
|
|
scope *InterpolationScope,
|
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-21 19:03:24 -05:00
|
|
|
v *config.ResourceVariable) (*ast.Variable, error) {
|
2015-02-05 18:47:06 -06:00
|
|
|
i.StateLock.RLock()
|
|
|
|
defer i.StateLock.RUnlock()
|
|
|
|
|
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-21 19:03:24 -05:00
|
|
|
unknownVariable := unknownVariable()
|
|
|
|
|
2015-02-05 18:47:06 -06:00
|
|
|
// Get the information about this resource variable, and verify
|
|
|
|
// that it exists and such.
|
|
|
|
module, cr, err := i.resourceVariableInfo(scope, v)
|
|
|
|
if 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-21 19:03:24 -05:00
|
|
|
return nil, err
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the count so we know how many to iterate over
|
|
|
|
count, err := cr.Count()
|
|
|
|
if 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-21 19:03:24 -05:00
|
|
|
return nil, fmt.Errorf(
|
2015-02-05 18:47:06 -06:00
|
|
|
"Error reading %s count: %s",
|
|
|
|
v.ResourceId(),
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have no module in the state yet or count, return empty
|
|
|
|
if module == nil || len(module.Resources) == 0 || count == 0 {
|
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-21 19:03:24 -05:00
|
|
|
return &ast.Variable{Type: ast.TypeString, Value: ""}, nil
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var values []string
|
2015-07-05 09:26:01 -05:00
|
|
|
for j := 0; j < count; j++ {
|
|
|
|
id := fmt.Sprintf("%s.%d", v.ResourceId(), j)
|
2015-02-05 18:47:06 -06:00
|
|
|
|
|
|
|
// If we're dealing with only a single resource, then the
|
|
|
|
// ID doesn't have a trailing index.
|
|
|
|
if count == 1 {
|
|
|
|
id = v.ResourceId()
|
|
|
|
}
|
|
|
|
|
|
|
|
r, ok := module.Resources[id]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Primary == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
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-21 19:03:24 -05:00
|
|
|
if singleAttr, ok := r.Primary.Attributes[v.Field]; ok {
|
|
|
|
if singleAttr == config.UnknownVariableValue {
|
|
|
|
return &unknownVariable, nil
|
2015-07-05 09:26:01 -05: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-21 19:03:24 -05:00
|
|
|
|
|
|
|
values = append(values, singleAttr)
|
|
|
|
continue
|
2015-07-05 09:26:01 -05: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-21 19:03:24 -05:00
|
|
|
// computed list attribute
|
|
|
|
_, ok = r.Primary.Attributes[v.Field+".#"]
|
|
|
|
if !ok {
|
2015-02-05 18:47:06 -06:00
|
|
|
continue
|
|
|
|
}
|
2016-04-27 16:41:17 -05:00
|
|
|
multiAttr, err := i.interpolateComplexTypeAttribute(v.Field, r.Primary.Attributes)
|
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-21 19:03:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-05 18:47:06 -06: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-21 19:03:24 -05:00
|
|
|
if multiAttr == unknownVariable {
|
|
|
|
return &ast.Variable{Type: ast.TypeString, Value: ""}, nil
|
2015-07-19 19:27:38 -05: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-21 19:03:24 -05:00
|
|
|
for _, element := range multiAttr.Value.([]ast.Variable) {
|
|
|
|
strVal := element.Value.(string)
|
|
|
|
if strVal == config.UnknownVariableValue {
|
|
|
|
return &unknownVariable, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
values = append(values, strVal)
|
|
|
|
}
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(values) == 0 {
|
2015-06-24 23:23:37 -05:00
|
|
|
// If the operation is refresh, it isn't an error for a value to
|
|
|
|
// be unknown. Instead, we return that the value is computed so
|
|
|
|
// that the graph can continue to refresh other nodes. It doesn't
|
|
|
|
// matter because the config isn't interpolated anyways.
|
2015-08-11 11:36:56 -05:00
|
|
|
//
|
|
|
|
// For a Destroy, we're also fine with computed values, since our goal is
|
|
|
|
// only to get destroy nodes for existing resources.
|
|
|
|
//
|
|
|
|
// For an input walk, computed values are okay to return because we're only
|
|
|
|
// looking for missing variables to prompt the user for.
|
2015-10-02 13:20:09 -05:00
|
|
|
if i.Operation == walkRefresh || i.Operation == walkPlanDestroy || i.Operation == walkDestroy || i.Operation == walkInput {
|
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-21 19:03:24 -05:00
|
|
|
return &unknownVariable, nil
|
2015-06-24 23:23:37 -05: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-21 19:03:24 -05:00
|
|
|
return nil, fmt.Errorf(
|
2015-02-05 18:47:06 -06:00
|
|
|
"Resource '%s' does not have attribute '%s' "+
|
|
|
|
"for variable '%s'",
|
|
|
|
v.ResourceId(),
|
|
|
|
v.Field,
|
|
|
|
v.FullKey())
|
|
|
|
}
|
|
|
|
|
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-21 19:03:24 -05:00
|
|
|
variable, err := hil.InterfaceToVariable(values)
|
|
|
|
return &variable, err
|
2015-02-05 18:47:06 -06:00
|
|
|
}
|
|
|
|
|
2016-04-27 16:41:17 -05:00
|
|
|
func (i *Interpolater) interpolateComplexTypeAttribute(
|
2015-07-05 09:26:01 -05:00
|
|
|
resourceID string,
|
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-21 19:03:24 -05:00
|
|
|
attributes map[string]string) (ast.Variable, error) {
|
2015-07-05 09:26:01 -05:00
|
|
|
|
|
|
|
attr := attributes[resourceID+".#"]
|
2016-04-27 16:41:17 -05:00
|
|
|
log.Printf("[DEBUG] Interpolating computed complex type attribute %s (%s)",
|
2015-07-05 09:26:01 -05:00
|
|
|
resourceID, attr)
|
|
|
|
|
2016-01-26 13:18:00 -06:00
|
|
|
// In Terraform's internal dotted representation of list-like attributes, the
|
|
|
|
// ".#" count field is marked as unknown to indicate "this whole list is
|
|
|
|
// unknown". We must honor that meaning here so computed references can be
|
|
|
|
// treated properly during the plan phase.
|
|
|
|
if attr == config.UnknownVariableValue {
|
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-21 19:03:24 -05:00
|
|
|
return unknownVariable(), nil
|
2016-01-26 13:18:00 -06:00
|
|
|
}
|
|
|
|
|
2016-04-27 16:41:17 -05:00
|
|
|
// At this stage we don't know whether the item is a list or a map, so we
|
|
|
|
// examine the keys to see whether they are all numeric.
|
|
|
|
var numericKeys []string
|
|
|
|
var allKeys []string
|
|
|
|
numberedListKey := regexp.MustCompile("^" + resourceID + "\\.[0-9]+$")
|
|
|
|
otherListKey := regexp.MustCompile("^" + resourceID + "\\.([^#]+)$")
|
|
|
|
for id, _ := range attributes {
|
|
|
|
if numberedListKey.MatchString(id) {
|
|
|
|
numericKeys = append(numericKeys, id)
|
|
|
|
}
|
|
|
|
if submatches := otherListKey.FindAllStringSubmatch(id, -1); len(submatches) > 0 {
|
|
|
|
allKeys = append(allKeys, submatches[0][1])
|
2015-07-05 09:26:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 16:41:17 -05:00
|
|
|
if len(numericKeys) == len(allKeys) {
|
|
|
|
// This is a list
|
|
|
|
var members []string
|
|
|
|
for _, key := range numericKeys {
|
|
|
|
members = append(members, attributes[key])
|
|
|
|
}
|
|
|
|
sort.Strings(members)
|
|
|
|
return hil.InterfaceToVariable(members)
|
|
|
|
} else {
|
|
|
|
// This is a map
|
|
|
|
members := make(map[string]interface{})
|
|
|
|
for _, key := range allKeys {
|
|
|
|
members[key] = attributes[resourceID+"."+key]
|
|
|
|
}
|
|
|
|
return hil.InterfaceToVariable(members)
|
|
|
|
}
|
2015-07-05 09:26:01 -05:00
|
|
|
}
|
|
|
|
|
2015-02-05 18:47:06 -06:00
|
|
|
func (i *Interpolater) resourceVariableInfo(
|
|
|
|
scope *InterpolationScope,
|
|
|
|
v *config.ResourceVariable) (*ModuleState, *config.Resource, error) {
|
|
|
|
// Get the module tree that contains our current path. This is
|
|
|
|
// either the current module (path is empty) or a child.
|
|
|
|
modTree := i.Module
|
|
|
|
if len(scope.Path) > 1 {
|
|
|
|
modTree = i.Module.Child(scope.Path[1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the resource from the configuration so we can verify
|
|
|
|
// that the resource is in the configuration and so we can access
|
|
|
|
// the configuration if we need to.
|
|
|
|
var cr *config.Resource
|
|
|
|
for _, r := range modTree.Config().Resources {
|
|
|
|
if r.Id() == v.ResourceId() {
|
|
|
|
cr = r
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cr == nil {
|
|
|
|
return nil, nil, fmt.Errorf(
|
|
|
|
"Resource '%s' not found for variable '%s'",
|
|
|
|
v.ResourceId(),
|
|
|
|
v.FullKey())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the relevant module
|
|
|
|
module := i.State.ModuleByPath(scope.Path)
|
|
|
|
return module, cr, nil
|
|
|
|
}
|