terraform: error if unknown variable retruned

This commit is contained in:
Mitchell Hashimoto 2014-06-23 13:08:25 -07:00
parent 175b720ab1
commit c5a621a47a
3 changed files with 51 additions and 13 deletions

View File

@ -106,11 +106,7 @@ func New(c *Config) (*Terraform, error) {
func (t *Terraform) Apply(p *Plan) (*State, error) {
result := new(State)
err := t.graph.Walk(t.applyWalkFn(p, result))
if err != nil {
return nil, err
}
return result, nil
return result, err
}
func (t *Terraform) Plan(s *State) (*Plan, error) {
@ -157,6 +153,17 @@ func (t *Terraform) applyWalkFn(
return nil, nil
}
var errs []error
for ak, av := range rs.Attributes {
// If the value is the unknown variable value, then it is an error.
// In this case we record the error and remove it from the state
if av == config.UnknownVariableValue {
errs = append(errs, fmt.Errorf(
"Attribute with unknown value: %s", ak))
delete(rs.Attributes, ak)
}
}
// Update the resulting diff
l.Lock()
result.Resources[r.Id] = rs
@ -168,7 +175,12 @@ func (t *Terraform) applyWalkFn(
vars[fmt.Sprintf("%s.%s", r.Id, ak)] = av
}
return vars, nil
err = nil
if len(errs) > 0 {
err = &MultiError{Errors: errs}
}
return vars, err
}
return t.genericWalkFn(p.State, p.Diff, p.Vars, cb)

View File

@ -256,6 +256,27 @@ func TestTerraformApply_compute(t *testing.T) {
}
}
func TestTerraformApply_unknownAttribute(t *testing.T) {
tf := testTerraform(t, "apply-unknown")
s := &State{}
p, err := tf.Plan(s)
if err != nil {
t.Fatalf("err: %s", err)
}
state, err := tf.Apply(p)
if err == nil {
t.Fatal("should error")
}
actual := strings.TrimSpace(state.String())
expected := strings.TrimSpace(testTerraformApplyUnknownAttrStr)
if actual != expected {
t.Fatalf("bad: \n%s", actual)
}
}
func TestTerraformApply_vars(t *testing.T) {
tf := testTerraform(t, "apply-vars")
tf.variables = map[string]string{"foo": "baz"}
@ -388,9 +409,7 @@ func testProviderFunc(n string, rs []string) ResourceProviderFactory {
}
if d != nil {
for ak, ad := range d.Attributes {
result.Attributes[ak] = ad.New
}
result = result.MergeDiff(d)
}
return result, nil
@ -451,10 +470,6 @@ func testProviderFunc(n string, rs []string) ResourceProviderFactory {
New: v.(string),
}
if strings.Contains(attrDiff.New, config.UnknownVariableValue) {
attrDiff.NewComputed = true
}
diff.Attributes[k] = attrDiff
}
@ -575,6 +590,13 @@ aws_instance.foo:
id = computed_id
`
const testTerraformApplyUnknownAttrStr = `
aws_instance.foo:
ID = foo
type = aws_instance
num = 2
`
const testTerraformApplyVarsStr = `
aws_instance.bar:
ID = foo

View File

@ -0,0 +1,4 @@
resource "aws_instance" "foo" {
num = "2"
compute = "id"
}