terraform: validate required variables are all set
This commit is contained in:
parent
0008ff12c7
commit
d2088463d3
|
@ -30,6 +30,27 @@ type Config struct {
|
|||
// time, as well as richer checks such as verifying that the resource providers
|
||||
// can be properly initialized, can be configured, etc.
|
||||
func New(c *Config) (*Terraform, error) {
|
||||
// Validate that all required variables have values
|
||||
required := make(map[string]struct{})
|
||||
for k, v := range c.Config.Variables {
|
||||
if v.Required() {
|
||||
required[k] = struct{}{}
|
||||
}
|
||||
}
|
||||
for k, _ := range c.Variables {
|
||||
delete(required, k)
|
||||
}
|
||||
if len(required) > 0 {
|
||||
errs := make([]error, 0, len(required))
|
||||
for k, _ := range required {
|
||||
errs = append(errs, fmt.Errorf(
|
||||
"Required variable not set: %s", k))
|
||||
}
|
||||
|
||||
// TODO(mitchellh): Return multi-error
|
||||
return nil, errs[0]
|
||||
}
|
||||
|
||||
// Go through each resource and match it up to a provider
|
||||
mapping := make(map[*config.Resource]ResourceProvider)
|
||||
providers := make(map[string]ResourceProvider)
|
||||
|
|
|
@ -79,3 +79,47 @@ func TestNew(t *testing.T) {
|
|||
t.Fatalf("bad: %#v", mapping)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew_variables(t *testing.T) {
|
||||
config := testConfig(t, "new-variables")
|
||||
tfConfig := &Config{
|
||||
Config: config,
|
||||
}
|
||||
|
||||
// Missing
|
||||
tfConfig.Variables = map[string]string{
|
||||
"bar": "baz",
|
||||
}
|
||||
tf, err := New(tfConfig)
|
||||
if err == nil {
|
||||
t.Fatal("should error")
|
||||
}
|
||||
if tf != nil {
|
||||
t.Fatalf("should not return tf")
|
||||
}
|
||||
|
||||
// Good
|
||||
tfConfig.Variables = map[string]string{
|
||||
"foo": "bar",
|
||||
}
|
||||
tf, err = New(tfConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if tf == nil {
|
||||
t.Fatal("tf should not be nil")
|
||||
}
|
||||
|
||||
// Good
|
||||
tfConfig.Variables = map[string]string{
|
||||
"foo": "bar",
|
||||
"bar": "baz",
|
||||
}
|
||||
tf, err = New(tfConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if tf == nil {
|
||||
t.Fatal("tf should not be nil")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
variable "foo" {}
|
||||
variable "bar" {
|
||||
default = "baz"
|
||||
}
|
Loading…
Reference in New Issue