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 (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-05-23 01:56:28 +02:00
|
|
|
// Config is the configuration that comes from loading a collection
|
|
|
|
// of Terraform templates.
|
|
|
|
type Config struct {
|
|
|
|
Variables map[string]Variable
|
|
|
|
Resources []Resource
|
|
|
|
}
|
|
|
|
|
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-05-24 06:58:06 +02:00
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
Config map[string]interface{}
|
|
|
|
Variables map[string]InterpolatedVariable
|
2014-05-23 01:56:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Variable struct {
|
|
|
|
Default string
|
|
|
|
Description string
|
|
|
|
}
|
2014-05-24 06:58:06 +02:00
|
|
|
|
|
|
|
// An InterpolatedVariable is a variable that is embedded within a string
|
|
|
|
// in the configuration, such as "hello ${world}" (world in this case is
|
|
|
|
// an interpolated variable).
|
|
|
|
//
|
|
|
|
// These variables can come from a variety of sources, represented by
|
|
|
|
// implementations of this interface.
|
|
|
|
type InterpolatedVariable interface {
|
|
|
|
FullKey() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// A ResourceVariable is a variable that is referencing the field
|
|
|
|
// of a resource, such as "${aws_instance.foo.ami}"
|
|
|
|
type ResourceVariable struct {
|
|
|
|
Type string
|
|
|
|
Name string
|
|
|
|
Field string
|
|
|
|
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
|
|
|
// A UserVariable is a variable that is referencing a user variable
|
|
|
|
// that is inputted from outside the configuration. This looks like
|
|
|
|
// "${var.foo}"
|
|
|
|
type UserVariable struct {
|
|
|
|
name string
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewResourceVariable(key string) (*ResourceVariable, error) {
|
|
|
|
parts := strings.SplitN(key, ".", 3)
|
|
|
|
return &ResourceVariable{
|
|
|
|
Type: parts[0],
|
|
|
|
Name: parts[1],
|
|
|
|
Field: parts[2],
|
|
|
|
key: key,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *ResourceVariable) FullKey() string {
|
|
|
|
return v.key
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserVariable(key string) (*UserVariable, error) {
|
|
|
|
name := key[len("var."):]
|
|
|
|
return &UserVariable{
|
|
|
|
key: key,
|
|
|
|
name: name,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *UserVariable) FullKey() string {
|
|
|
|
return v.key
|
|
|
|
}
|