diff --git a/terraform/terraform.go b/terraform/terraform.go index 3a1cdf7be..55085ca81 100644 --- a/terraform/terraform.go +++ b/terraform/terraform.go @@ -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) diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 6eaba3b9f..216e7cb99 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -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 diff --git a/terraform/test-fixtures/apply-unknown/main.tf b/terraform/test-fixtures/apply-unknown/main.tf new file mode 100644 index 000000000..7eec0bf20 --- /dev/null +++ b/terraform/test-fixtures/apply-unknown/main.tf @@ -0,0 +1,4 @@ +resource "aws_instance" "foo" { + num = "2" + compute = "id" +}