Skip to content

Commit 86477c8

Browse files
jbardinkmoe
authored andcommitted
ensure data sources are always written to state
This commit was generated from hashicorp/terraform#22895.
1 parent 7e1a983 commit 86477c8

File tree

3 files changed

+78
-16
lines changed

3 files changed

+78
-16
lines changed

terraform/context_refresh_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,3 +1906,61 @@ data "aws_data_source" "foo" {
19061906
t.Fatal("ValidateDataSourceConfig not called during plan")
19071907
}
19081908
}
1909+
1910+
func TestContext2Refresh_dataResourceDependsOn(t *testing.T) {
1911+
m := testModule(t, "plan-data-depends-on")
1912+
p := testProvider("test")
1913+
p.GetSchemaReturn = &ProviderSchema{
1914+
ResourceTypes: map[string]*configschema.Block{
1915+
"test_resource": {
1916+
Attributes: map[string]*configschema.Attribute{
1917+
"id": {Type: cty.String, Computed: true},
1918+
"foo": {Type: cty.String, Optional: true},
1919+
},
1920+
},
1921+
},
1922+
DataSources: map[string]*configschema.Block{
1923+
"test_data": {
1924+
Attributes: map[string]*configschema.Attribute{
1925+
"compute": {Type: cty.String, Computed: true},
1926+
},
1927+
},
1928+
},
1929+
}
1930+
p.DiffFn = testDiffFn
1931+
1932+
s := MustShimLegacyState(&State{
1933+
Modules: []*ModuleState{
1934+
&ModuleState{
1935+
Path: rootModulePath,
1936+
Resources: map[string]*ResourceState{
1937+
"test_resource.a": &ResourceState{
1938+
Type: "test_resource",
1939+
Provider: "provider.test",
1940+
Primary: &InstanceState{
1941+
ID: "a",
1942+
Attributes: map[string]string{
1943+
"id": "a",
1944+
},
1945+
},
1946+
},
1947+
},
1948+
},
1949+
},
1950+
})
1951+
1952+
ctx := testContext2(t, &ContextOpts{
1953+
Config: m,
1954+
ProviderResolver: providers.ResolverFixed(
1955+
map[string]providers.Factory{
1956+
"test": testProviderFuncFixed(p),
1957+
},
1958+
),
1959+
State: s,
1960+
})
1961+
1962+
_, diags := ctx.Refresh()
1963+
if diags.HasErrors() {
1964+
t.Fatalf("unexpected errors: %s", diags.Err())
1965+
}
1966+
}

terraform/node_data_refresh.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,6 @@ func (n *NodeRefreshableDataResourceInstance) EvalTree() EvalNode {
162162
ProviderSchema: &providerSchema,
163163
},
164164

165-
&EvalIf{
166-
If: func(ctx EvalContext) (bool, error) {
167-
// If the config explicitly has a depends_on for this
168-
// data source, assume the intention is to prevent
169-
// refreshing ahead of that dependency, and therefore
170-
// we need to deal with this resource during the apply
171-
// phase..
172-
if len(n.Config.DependsOn) > 0 {
173-
return true, EvalEarlyExitError{}
174-
}
175-
176-
return true, nil
177-
},
178-
Then: EvalNoop{},
179-
},
180-
181165
// EvalReadData will _attempt_ to read the data source, but may
182166
// generate an incomplete planned object if the configuration
183167
// includes values that won't be known until apply.
@@ -191,6 +175,12 @@ func (n *NodeRefreshableDataResourceInstance) EvalTree() EvalNode {
191175
OutputChange: &change,
192176
OutputConfigValue: &configVal,
193177
OutputState: &state,
178+
// If the config explicitly has a depends_on for this data
179+
// source, assume the intention is to prevent refreshing ahead
180+
// of that dependency, and therefore we need to deal with this
181+
// resource during the apply phase. We do that by forcing this
182+
// read to result in a plan.
183+
ForcePlanRead: len(n.Config.DependsOn) > 0,
194184
},
195185

196186
&EvalIf{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
resource "test_resource" "a" {
2+
}
3+
4+
data "test_data" "d" {
5+
count = 1
6+
depends_on = [
7+
test_resource.a
8+
]
9+
}
10+
11+
resource "test_resource" "b" {
12+
count = 1
13+
foo = data.test_data.d[count.index].compute
14+
}

0 commit comments

Comments
 (0)