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

Prevent data sources from being aplied early #10670

Merged
merged 1 commit into from
Dec 12, 2016
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
62 changes: 62 additions & 0 deletions terraform/context_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6902,3 +6902,65 @@ module.middle.bottom:
t.Fatalf("expected: \n%s\n\nbad: \n%s", expected, actual)
}
}

// If a data source explicitly depends on another resource, it's because we need
// that resource to be applied first.
func TestContext2Apply_dataDependsOn(t *testing.T) {
p := testProvider("null")
m := testModule(t, "apply-data-depends-on")

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

// the "provisioner" here writes to this variable, because the intent is to
// create a dependency which can't be viewed through the graph, and depends
// solely on the configuration providing "depends_on"
provisionerOutput := ""

p.ApplyFn = func(info *InstanceInfo, s *InstanceState, d *InstanceDiff) (*InstanceState, error) {
// the side effect of the resource being applied
provisionerOutput = "APPLIED"
return testApplyFn(info, s, d)
}

p.DiffFn = testDiffFn
p.ReadDataDiffFn = testDataDiffFn

p.ReadDataApplyFn = func(*InstanceInfo, *InstanceDiff) (*InstanceState, error) {
// Read the artifact created by our dependency being applied.
// Without any "depends_on", this would be skipped as it's assumed the
// initial diff during refresh was all that's needed.
return &InstanceState{
ID: "read",
Attributes: map[string]string{
"foo": provisionerOutput,
},
}, nil
}

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

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

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

root := state.ModuleByPath(RootModulePath)
actual := root.Resources["data.null_data_source.read"].Primary.Attributes["foo"]

expected := "APPLIED"
if actual != expected {
t.Fatalf("bad:\n%s", strings.TrimSpace(state.String()))
}
}
8 changes: 8 additions & 0 deletions terraform/test-fixtures/apply-data-depends-on/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "null_resource" "write" {
foo = "attribute"
}

data "null_data_source" "read" {
foo = ""
depends_on = ["null_resource.write"]
}
8 changes: 8 additions & 0 deletions terraform/transform_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,18 @@ func (n *graphNodeExpandedResource) dataResourceEvalNodes(resource *Resource, in
// apply phases.)
&EvalIf{
If: func(ctx EvalContext) (bool, error) {

if config.ComputedKeys != nil && len(config.ComputedKeys) > 0 {
return true, EvalEarlyExitError{}
}

// If the config explicitly has a depends_on for this
// data source, assume the intention is to prevent
// refreshing ahead of that dependency.
if len(n.Resource.DependsOn) > 0 {
return true, EvalEarlyExitError{}
}

return true, nil
},
Then: EvalNoop{},
Expand Down