Improve the decoding logic to prevent parameter not found errors

We need to decode both the Raw config and the parsed Config to make
sure all set keys are visible. Otherwise keys that will need to be
interpolated later, will be missing causing the validation to fail.
This commit is contained in:
Sander van Harmelen 2015-06-03 14:45:30 +02:00
parent 0b2b9e844f
commit 4f6e610ff9
2 changed files with 19 additions and 1 deletions

View File

@ -45,7 +45,7 @@ func Provider() terraform.ResourceProvider {
func providerConfigure(d *schema.ResourceData) (interface{}, error) { func providerConfigure(d *schema.ResourceData) (interface{}, error) {
settingsFile, err := homedir.Expand(d.Get("settings_file").(string)) settingsFile, err := homedir.Expand(d.Get("settings_file").(string))
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("Error expanding the settings file path: %s", err)
} }
config := Config{ config := Config{

View File

@ -17,6 +17,7 @@ import (
"github.com/hashicorp/terraform/communicator" "github.com/hashicorp/terraform/communicator"
"github.com/hashicorp/terraform/communicator/remote" "github.com/hashicorp/terraform/communicator/remote"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/go-homedir"
"github.com/mitchellh/go-linereader" "github.com/mitchellh/go-linereader"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
) )
@ -182,6 +183,15 @@ func (r *ResourceProvisioner) decodeConfig(c *terraform.ResourceConfig) (*Provis
return nil, err return nil, err
} }
// We need to decode this twice. Once for the Raw config and once
// for the parsed Config. This makes sure that all values are there
// even if some still need to be interpolated later on.
// Without this the validation will fail when using a variable for
// a required parameter (the node_name for example).
if err := dec.Decode(c.Raw); err != nil {
return nil, err
}
if err := dec.Decode(c.Config); err != nil { if err := dec.Decode(c.Config); err != nil {
return nil, err return nil, err
} }
@ -190,6 +200,14 @@ func (r *ResourceProvisioner) decodeConfig(c *terraform.ResourceConfig) (*Provis
p.Environment = defaultEnv p.Environment = defaultEnv
} }
if p.ValidationKeyPath != "" {
keyPath, err := homedir.Expand(p.ValidationKeyPath)
if err != nil {
return nil, fmt.Errorf("Error expanding the validation key path: %v", err)
}
p.ValidationKeyPath = keyPath
}
if attrs, ok := c.Config["attributes"]; ok { if attrs, ok := c.Config["attributes"]; ok {
p.Attributes, err = rawToJSON(attrs) p.Attributes, err = rawToJSON(attrs)
if err != nil { if err != nil {