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-02 20:34:08 +02:00
|
|
|
"strconv"
|
2014-05-24 06:58:06 +02:00
|
|
|
"strings"
|
2014-07-03 19:14:17 +02:00
|
|
|
|
2014-07-22 16:41:55 +02:00
|
|
|
"github.com/hashicorp/terraform/flatmap"
|
2014-07-03 19:14:17 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/multierror"
|
2014-07-22 16:41:55 +02:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2014-08-11 18:46:56 +02:00
|
|
|
"github.com/mitchellh/reflectwalk"
|
2014-05-24 06:58:06 +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
|
|
|
|
|
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
|
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
|
|
|
}
|
|
|
|
|
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
|
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.
|
|
|
|
// A Terraform resource is something that represents some component that
|
|
|
|
// can be created and managed, and has some properties associated with it.
|
2014-05-23 01:56:28 +02:00
|
|
|
type Resource struct {
|
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
|
|
|
|
DependsOn []string
|
|
|
|
Lifecycle ResourceLifecycle
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceLifecycle is used to store the lifecycle tuning parameters
|
|
|
|
// to allow customized behavior
|
|
|
|
type ResourceLifecycle struct {
|
|
|
|
CreateBeforeDestroy bool `hcl:"create_before_destroy"`
|
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
|
2014-05-23 01:56:28 +02:00
|
|
|
}
|
|
|
|
|
2014-06-04 00:55:51 +02:00
|
|
|
// Variable is a variable defined within the configuration.
|
2014-05-23 01:56:28 +02:00
|
|
|
type Variable struct {
|
2014-07-19 02:48:30 +02:00
|
|
|
Name string
|
2014-07-21 16:32:36 +02:00
|
|
|
Default interface{}
|
2014-05-23 01:56:28 +02:00
|
|
|
Description string
|
|
|
|
}
|
2014-05-24 06:58:06 +02:00
|
|
|
|
2014-07-04 19:43:06 +02:00
|
|
|
// Output is an output defined within the configuration. An output is
|
|
|
|
// resulting data that is highlighted by Terraform when finished.
|
|
|
|
type Output struct {
|
|
|
|
Name string
|
|
|
|
RawConfig *RawConfig
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
VariableTypeMap
|
|
|
|
)
|
|
|
|
|
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) {
|
|
|
|
v, err := strconv.ParseInt(r.RawCount.Value().(string), 0, 0)
|
|
|
|
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 {
|
|
|
|
return fmt.Sprintf("%s.%s", r.Type, r.Name)
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
varMap[v.Name] = v
|
|
|
|
}
|
2014-07-03 06:06:26 +02:00
|
|
|
|
2014-07-22 17:34:24 +02:00
|
|
|
for _, v := range c.Variables {
|
|
|
|
if v.Type() == VariableTypeUnknown {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"Variable '%s': must be string or mapping",
|
|
|
|
v.Name))
|
2014-08-11 18:46:56 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
interp := false
|
|
|
|
fn := func(i Interpolation) (string, error) {
|
|
|
|
interp = true
|
|
|
|
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(
|
|
|
|
"%s: unknown variable referenced: %s",
|
|
|
|
source,
|
|
|
|
uv.Name))
|
|
|
|
}
|
2014-07-03 06:00:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
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()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
modules[m.Id()] = m
|
|
|
|
}
|
|
|
|
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()))
|
2014-10-03 01:30:46 +02:00
|
|
|
case *ModuleVariable:
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: resource count can't reference module variable: %s",
|
|
|
|
n,
|
|
|
|
v.FullKey()))
|
|
|
|
case *ResourceVariable:
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: resource count can't reference resource variable: %s",
|
|
|
|
n,
|
|
|
|
v.FullKey()))
|
|
|
|
case *UserVariable:
|
|
|
|
// Good
|
|
|
|
default:
|
|
|
|
panic("Unknown type in count var: " + n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-03 01:51:20 +02:00
|
|
|
// Interpolate with a fixed number to verify that its a number
|
|
|
|
r.RawCount.interpolate(func(Interpolation) (string, error) {
|
|
|
|
return "5", nil
|
|
|
|
})
|
|
|
|
_, 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()
|
|
|
|
|
2014-07-23 02:16:48 +02:00
|
|
|
for _, d := range r.DependsOn {
|
|
|
|
if _, ok := resources[d]; !ok {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: resource depends on non-existent resource '%s'",
|
|
|
|
n, d))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 19:53:36 +02:00
|
|
|
for source, vs := range vars {
|
|
|
|
for _, v := range vs {
|
|
|
|
rv, ok := v.(*ResourceVariable)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
id := fmt.Sprintf("%s.%s", rv.Type, rv.Name)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 19:53:36 +02:00
|
|
|
// Check that all outputs are valid
|
2014-07-04 19:57:09 +02:00
|
|
|
for _, o := range c.Outputs {
|
|
|
|
invalid := false
|
|
|
|
for k, _ := range o.RawConfig.Raw {
|
|
|
|
if k != "value" {
|
|
|
|
invalid = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if invalid {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: output should only have 'value' field", o.Name))
|
|
|
|
}
|
|
|
|
}
|
2014-07-04 19:53:36 +02:00
|
|
|
|
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-07-22 17:47:10 +02:00
|
|
|
for _, pc := range c.ProviderConfigs {
|
|
|
|
source := fmt.Sprintf("provider config '%s'", pc.Name)
|
2014-07-03 06:00:06 +02:00
|
|
|
for _, v := range pc.RawConfig.Variables {
|
2014-07-04 19:53:36 +02:00
|
|
|
result[source] = append(result[source], v)
|
2014-07-03 06:00:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rc := range c.Resources {
|
|
|
|
source := fmt.Sprintf("resource '%s'", rc.Id())
|
2014-10-02 20:34:08 +02:00
|
|
|
for _, v := range rc.RawCount.Variables {
|
2014-10-02 20:14:50 +02:00
|
|
|
result[source] = append(result[source], v)
|
|
|
|
}
|
2014-07-03 06:00:06 +02:00
|
|
|
for _, v := range rc.RawConfig.Variables {
|
2014-07-04 19:53:36 +02:00
|
|
|
result[source] = append(result[source], v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range c.Outputs {
|
|
|
|
source := fmt.Sprintf("output '%s'", o.Name)
|
|
|
|
for _, v := range o.RawConfig.Variables {
|
|
|
|
result[source] = append(result[source], v)
|
2014-07-03 06:00:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
result.RawConfig = result.RawConfig.merge(o2.RawConfig)
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resource) mergerName() string {
|
|
|
|
return fmt.Sprintf("%s.%s", r.Type, r.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resource) mergerMerge(m merger) merger {
|
|
|
|
r2 := m.(*Resource)
|
|
|
|
|
|
|
|
result := *r
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-07-22 16:41:55 +02:00
|
|
|
// DefaultsMap returns a map of default values for this variable.
|
|
|
|
func (v *Variable) DefaultsMap() map[string]string {
|
2014-07-22 17:10:06 +02:00
|
|
|
if v.Default == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-22 17:06:09 +02:00
|
|
|
|
2014-07-22 17:10:06 +02:00
|
|
|
n := fmt.Sprintf("var.%s", v.Name)
|
2014-07-22 16:41:55 +02:00
|
|
|
switch v.Type() {
|
|
|
|
case VariableTypeString:
|
2014-07-22 17:06:09 +02:00
|
|
|
return map[string]string{n: v.Default.(string)}
|
2014-07-22 16:41:55 +02:00
|
|
|
case VariableTypeMap:
|
2014-07-22 17:06:09 +02:00
|
|
|
result := flatmap.Flatten(map[string]interface{}{
|
|
|
|
n: v.Default.(map[string]string),
|
2014-07-22 16:41:55 +02:00
|
|
|
})
|
2014-07-22 17:06:09 +02:00
|
|
|
result[n] = v.Name
|
|
|
|
|
|
|
|
return result
|
2014-07-22 16:41:55 +02:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 02:17:03 +02:00
|
|
|
// 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
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-07-22 16:41:55 +02:00
|
|
|
// Type returns the type of varialbe this is.
|
|
|
|
func (v *Variable) Type() VariableType {
|
|
|
|
if v.Default == nil {
|
|
|
|
return VariableTypeString
|
|
|
|
}
|
|
|
|
|
|
|
|
var strVal string
|
|
|
|
if err := mapstructure.WeakDecode(v.Default, &strVal); err == nil {
|
|
|
|
v.Default = strVal
|
|
|
|
return VariableTypeString
|
|
|
|
}
|
|
|
|
|
|
|
|
var m map[string]string
|
|
|
|
if err := mapstructure.WeakDecode(v.Default, &m); err == nil {
|
|
|
|
v.Default = m
|
|
|
|
return VariableTypeMap
|
|
|
|
}
|
|
|
|
|
|
|
|
return VariableTypeUnknown
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|