terraform: error if unknown variable retruned
This commit is contained in:
parent
175b720ab1
commit
c5a621a47a
|
@ -106,11 +106,7 @@ func New(c *Config) (*Terraform, error) {
|
||||||
func (t *Terraform) Apply(p *Plan) (*State, error) {
|
func (t *Terraform) Apply(p *Plan) (*State, error) {
|
||||||
result := new(State)
|
result := new(State)
|
||||||
err := t.graph.Walk(t.applyWalkFn(p, result))
|
err := t.graph.Walk(t.applyWalkFn(p, result))
|
||||||
if err != nil {
|
return result, err
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Terraform) Plan(s *State) (*Plan, error) {
|
func (t *Terraform) Plan(s *State) (*Plan, error) {
|
||||||
|
@ -157,6 +153,17 @@ func (t *Terraform) applyWalkFn(
|
||||||
return nil, nil
|
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
|
// Update the resulting diff
|
||||||
l.Lock()
|
l.Lock()
|
||||||
result.Resources[r.Id] = rs
|
result.Resources[r.Id] = rs
|
||||||
|
@ -168,7 +175,12 @@ func (t *Terraform) applyWalkFn(
|
||||||
vars[fmt.Sprintf("%s.%s", r.Id, ak)] = av
|
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)
|
return t.genericWalkFn(p.State, p.Diff, p.Vars, cb)
|
||||||
|
|
|
@ -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) {
|
func TestTerraformApply_vars(t *testing.T) {
|
||||||
tf := testTerraform(t, "apply-vars")
|
tf := testTerraform(t, "apply-vars")
|
||||||
tf.variables = map[string]string{"foo": "baz"}
|
tf.variables = map[string]string{"foo": "baz"}
|
||||||
|
@ -388,9 +409,7 @@ func testProviderFunc(n string, rs []string) ResourceProviderFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
if d != nil {
|
if d != nil {
|
||||||
for ak, ad := range d.Attributes {
|
result = result.MergeDiff(d)
|
||||||
result.Attributes[ak] = ad.New
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
|
@ -451,10 +470,6 @@ func testProviderFunc(n string, rs []string) ResourceProviderFactory {
|
||||||
New: v.(string),
|
New: v.(string),
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.Contains(attrDiff.New, config.UnknownVariableValue) {
|
|
||||||
attrDiff.NewComputed = true
|
|
||||||
}
|
|
||||||
|
|
||||||
diff.Attributes[k] = attrDiff
|
diff.Attributes[k] = attrDiff
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -575,6 +590,13 @@ aws_instance.foo:
|
||||||
id = computed_id
|
id = computed_id
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const testTerraformApplyUnknownAttrStr = `
|
||||||
|
aws_instance.foo:
|
||||||
|
ID = foo
|
||||||
|
type = aws_instance
|
||||||
|
num = 2
|
||||||
|
`
|
||||||
|
|
||||||
const testTerraformApplyVarsStr = `
|
const testTerraformApplyVarsStr = `
|
||||||
aws_instance.bar:
|
aws_instance.bar:
|
||||||
ID = foo
|
ID = foo
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
resource "aws_instance" "foo" {
|
||||||
|
num = "2"
|
||||||
|
compute = "id"
|
||||||
|
}
|
Loading…
Reference in New Issue