Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Computed provider variables properly validate #2457

Merged
merged 4 commits into from
Jun 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3469,6 +3469,7 @@ func TestSchemaMap_Validate(t *testing.T) {
},
Err: true,
},

"ValidateFunc gets decoded type": {
Schema: map[string]*Schema{
"maybe": &Schema{
Expand All @@ -3486,6 +3487,27 @@ func TestSchemaMap_Validate(t *testing.T) {
"maybe": "true",
},
},

"ValidateFunc is not called with a computed value": {
Schema: map[string]*Schema{
"validate_me": &Schema{
Type: TypeString,
Required: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) {
es = append(es, fmt.Errorf("something is not right here"))
return
},
},
},
Config: map[string]interface{}{
"validate_me": "${var.foo}",
},
Vars: map[string]string{
"var.foo": config.UnknownVariableValue,
},

Err: false,
},
}

for tn, tc := range cases {
Expand Down
75 changes: 75 additions & 0 deletions terraform/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2447,6 +2447,38 @@ func TestContext2Validate_badVar(t *testing.T) {
}
}

func TestContext2Validate_computedVar(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-computed-var")
c := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
"test": testProviderFuncFixed(testProvider("test")),
},
})

p.ValidateFn = func(c *ResourceConfig) ([]string, []error) {
if !c.IsComputed("value") {
return nil, []error{fmt.Errorf("value isn't computed")}
}

return nil, c.CheckSet([]string{"value"})
}

p.ConfigureFn = func(c *ResourceConfig) error {
return fmt.Errorf("Configure should not be called for provider")
}

w, e := c.Validate()
if len(w) > 0 {
t.Fatalf("bad: %#v", w)
}
if len(e) > 0 {
t.Fatalf("bad: %#v", e)
}
}

func TestContext2Validate_countNegative(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-count-negative")
Expand Down Expand Up @@ -4583,6 +4615,49 @@ func TestContext2Apply_outputOrphan(t *testing.T) {
}
}

func TestContext2Apply_providerComputedVar(t *testing.T) {
m := testModule(t, "apply-provider-computed")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn

pTest := testProvider("test")
pTest.ApplyFn = testApplyFn
pTest.DiffFn = testDiffFn

ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
"test": testProviderFuncFixed(pTest),
},
})

p.ConfigureFn = func(c *ResourceConfig) error {
if c.IsComputed("value") {
return fmt.Errorf("value is computed")
}

v, ok := c.Get("value")
if !ok {
return fmt.Errorf("value is not found")
}
if v != "yes" {
return fmt.Errorf("value is not 'yes': %v", v)
}

return nil
}

if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}

if _, err := ctx.Apply(); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestContext2Apply_Provisioner_compute(t *testing.T) {
m := testModule(t, "apply-provisioner-compute")
p := testProvider("aws")
Expand Down
14 changes: 14 additions & 0 deletions terraform/evaltree_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ func ProviderEvalTree(n string, config *config.RawConfig) EvalNode {
Provider: &provider,
Config: &resourceConfig,
},
&EvalSetProviderConfig{
Provider: n,
Config: &resourceConfig,
},
},
},
})

// We configure on everything but validate, since validate may
// not have access to all the variables.
seq = append(seq, &EvalOpFilter{
Ops: []walkOperation{walkRefresh, walkPlan, walkApply},
Node: &EvalSequence{
Nodes: []EvalNode{
&EvalConfigProvider{
Provider: n,
Config: &resourceConfig,
Expand Down
9 changes: 9 additions & 0 deletions terraform/test-fixtures/apply-provider-computed/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
provider "aws" {
value = "${test_instance.foo.value}"
}

resource "aws_instance" "bar" {}

resource "test_instance" "foo" {
value = "yes"
}
9 changes: 9 additions & 0 deletions terraform/test-fixtures/validate-computed-var/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
provider "aws" {
value = "${test_instance.foo.value}"
}

resource "aws_instance" "bar" {}

resource "test_instance" "foo" {
value = "yes"
}