opentofu/config/config.go

365 lines
8.0 KiB
Go
Raw Normal View History

2014-05-24 13:36:22 -05:00
// The config package is responsible for loading and validating the
// configuration.
2014-05-22 18:56:28 -05:00
package config
import (
2014-05-24 15:57:51 -05:00
"fmt"
"strings"
2014-07-03 12:14:17 -05:00
2014-07-22 09:41:55 -05:00
"github.com/hashicorp/terraform/flatmap"
2014-07-03 12:14:17 -05:00
"github.com/hashicorp/terraform/helper/multierror"
2014-07-22 09:41:55 -05:00
"github.com/mitchellh/mapstructure"
)
2014-05-22 18:56:28 -05:00
// Config is the configuration that comes from loading a collection
// of Terraform templates.
type Config struct {
2014-07-19 18:05:48 -05:00
ProviderConfigs []*ProviderConfig
2014-05-25 20:05:18 -05:00
Resources []*Resource
2014-07-18 19:48:30 -05:00
Variables []*Variable
2014-07-19 00:21:52 -05:00
Outputs []*Output
2014-07-18 18:00:21 -05:00
// The fields below can be filled in by loaders for validation
// purposes.
unknownKeys []string
2014-05-25 20:05:18 -05: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-19 18:05:48 -05:00
Name string
RawConfig *RawConfig
2014-05-22 18:56:28 -05: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-22 18:56:28 -05:00
type Resource struct {
Name string
Type string
Count int
RawConfig *RawConfig
Provisioners []*Provisioner
2014-07-22 19:10:17 -05:00
DependsOn []string
}
// Provisioner is a configured provisioner step on a resource.
type Provisioner struct {
Type string
RawConfig *RawConfig
ConnInfo *RawConfig
2014-05-22 18:56:28 -05:00
}
// Variable is a variable defined within the configuration.
2014-05-22 18:56:28 -05:00
type Variable struct {
2014-07-18 19:48:30 -05:00
Name string
Default interface{}
2014-05-22 18:56:28 -05:00
Description string
}
2014-07-04 12:43:06 -05: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 10:43:16 -05:00
// VariableType is the type of value a variable is holding, and returned
// by the Type() function on variables.
2014-07-22 09:41:55 -05:00
type VariableType byte
const (
VariableTypeUnknown VariableType = iota
VariableTypeString
VariableTypeMap
)
2014-06-05 14:21:05 -05: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-19 18:05:48 -05:00
func ProviderConfigName(t string, pcs []*ProviderConfig) string {
2014-06-05 14:21:05 -05:00
lk := ""
2014-07-19 18:05:48 -05:00
for _, v := range pcs {
k := v.Name
if strings.HasPrefix(t, k) && len(k) > len(lk) {
2014-06-05 14:21:05 -05:00
lk = k
}
}
return lk
}
// A unique identifier for this resource.
func (r *Resource) Id() string {
return fmt.Sprintf("%s.%s", r.Type, r.Name)
}
// Validate does some basic semantic checking of the configuration.
func (c *Config) Validate() error {
var errs []error
2014-07-18 18:00:21 -05:00
for _, k := range c.unknownKeys {
errs = append(errs, fmt.Errorf(
"Unknown root level key: %s", k))
}
vars := c.allVariables()
2014-07-18 19:48:30 -05:00
varMap := make(map[string]*Variable)
for _, v := range c.Variables {
varMap[v.Name] = v
}
for _, v := range c.Variables {
if v.Type() == VariableTypeUnknown {
errs = append(errs, fmt.Errorf(
"Variable '%s': must be string or mapping",
v.Name))
}
}
2014-07-21 10:34:44 -05:00
// Check for references to user variables that do not actually
// exist and record those errors.
for source, vs := range vars {
for _, v := range vs {
uv, ok := v.(*UserVariable)
if !ok {
continue
}
2014-07-18 19:48:30 -05:00
if _, ok := varMap[uv.Name]; !ok {
errs = append(errs, fmt.Errorf(
"%s: unknown variable referenced: %s",
source,
uv.Name))
}
}
}
// Check that all references to resources are valid
resources := make(map[string]*Resource)
2014-07-18 18:31:01 -05:00
dupped := make(map[string]struct{})
for _, r := range c.Resources {
2014-07-18 18:31:01 -05: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()))
}
}
resources[r.Id()] = r
}
2014-07-18 18:31:32 -05:00
dupped = nil
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)
r, ok := resources[id]
if !ok {
errs = append(errs, fmt.Errorf(
"%s: unknown resource '%s' referenced in variable %s",
source,
id,
rv.FullKey()))
continue
}
// If it is a multi reference and resource has a single
// count, it is an error.
if r.Count > 1 && !rv.Multi {
errs = append(errs, fmt.Errorf(
"%s: variable '%s' must specify index for multi-count "+
"resource %s",
source,
rv.FullKey(),
id))
continue
}
}
}
// Check that all outputs are valid
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))
}
}
if len(errs) > 0 {
2014-07-03 12:14:17 -05:00
return &multierror.Error{Errors: errs}
}
return nil
}
// allVariables is a helper that returns a mapping of all the interpolated
// variables within the configuration. This is used to verify references
// are valid in the Validate step.
func (c *Config) allVariables() map[string][]InterpolatedVariable {
result := make(map[string][]InterpolatedVariable)
for _, pc := range c.ProviderConfigs {
source := fmt.Sprintf("provider config '%s'", pc.Name)
for _, v := range pc.RawConfig.Variables {
result[source] = append(result[source], v)
}
}
for _, rc := range c.Resources {
source := fmt.Sprintf("resource '%s'", rc.Id())
for _, v := range rc.RawConfig.Variables {
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)
}
}
return result
}
2014-07-20 19:17:03 -05: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)
if r2.Count > 0 {
result.Count = r2.Count
}
if len(r2.Provisioners) > 0 {
result.Provisioners = r2.Provisioners
}
return &result
}
2014-07-22 09:41:55 -05:00
// DefaultsMap returns a map of default values for this variable.
func (v *Variable) DefaultsMap() map[string]string {
if v.Default == nil {
return nil
}
n := fmt.Sprintf("var.%s", v.Name)
2014-07-22 09:41:55 -05:00
switch v.Type() {
case VariableTypeString:
return map[string]string{n: v.Default.(string)}
2014-07-22 09:41:55 -05:00
case VariableTypeMap:
result := flatmap.Flatten(map[string]interface{}{
n: v.Default.(map[string]string),
2014-07-22 09:41:55 -05:00
})
result[n] = v.Name
return result
2014-07-22 09:41:55 -05:00
default:
return nil
}
}
2014-07-20 19:17:03 -05: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
if v2.Default != nil {
2014-07-20 19:17:03 -05:00
result.Default = v2.Default
}
if v2.Description != "" {
result.Description = v2.Description
}
return &result
}
2014-07-22 09:41:55 -05: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-20 19:17:03 -05:00
func (v *Variable) mergerName() string {
return v.Name
}
func (v *Variable) mergerMerge(m merger) merger {
return v.Merge(m.(*Variable))
}
// Required tests whether a variable is required or not.
func (v *Variable) Required() bool {
return v.Default == nil
}