always return original workspace, even when null

The returned value type needs to strictly match the proposed value type.
This commit is contained in:
James Bardin 2020-06-24 13:41:51 -04:00
parent 6ef11e9d9e
commit a0fad2c6b4
2 changed files with 22 additions and 11 deletions

View File

@ -97,10 +97,13 @@ func dataSourceRemoteStateRead(d cty.Value) (cty.Value, tfdiags.Diagnostics) {
newState["backend"] = d.GetAttr("backend")
newState["config"] = d.GetAttr("config")
workspaceName := backend.DefaultStateName
workspaceVal := d.GetAttr("workspace")
// This attribute is not computed, so we always have to store the state
// value, even if we implicitly use a default.
newState["workspace"] = workspaceVal
if workspaceVal := d.GetAttr("workspace"); !workspaceVal.IsNull() {
newState["workspace"] = workspaceVal
workspaceName := backend.DefaultStateName
if !workspaceVal.IsNull() {
workspaceName = workspaceVal.AsString()
}

View File

@ -40,7 +40,8 @@ func TestState_basic(t *testing.T) {
"outputs": cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("bar"),
}),
"defaults": cty.NullVal(cty.DynamicPseudoType),
"defaults": cty.NullVal(cty.DynamicPseudoType),
"workspace": cty.NullVal(cty.String),
}),
false,
},
@ -80,7 +81,8 @@ func TestState_basic(t *testing.T) {
"outputs": cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("bar"),
}),
"defaults": cty.NullVal(cty.DynamicPseudoType),
"defaults": cty.NullVal(cty.DynamicPseudoType),
"workspace": cty.NullVal(cty.String),
}),
false,
},
@ -113,7 +115,8 @@ func TestState_basic(t *testing.T) {
cty.StringVal("test2"),
}),
}),
"defaults": cty.NullVal(cty.DynamicPseudoType),
"defaults": cty.NullVal(cty.DynamicPseudoType),
"workspace": cty.NullVal(cty.String),
}),
false,
},
@ -133,7 +136,8 @@ func TestState_basic(t *testing.T) {
"map": cty.NullVal(cty.DynamicPseudoType),
"list": cty.NullVal(cty.DynamicPseudoType),
}),
"defaults": cty.NullVal(cty.DynamicPseudoType),
"defaults": cty.NullVal(cty.DynamicPseudoType),
"workspace": cty.NullVal(cty.String),
}),
false,
},
@ -158,6 +162,7 @@ func TestState_basic(t *testing.T) {
"outputs": cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("bar"),
}),
"workspace": cty.NullVal(cty.String),
}),
false,
},
@ -173,8 +178,9 @@ func TestState_basic(t *testing.T) {
"config": cty.ObjectVal(map[string]cty.Value{
"path": cty.StringVal("./testdata/missing.tfstate"),
}),
"defaults": cty.NullVal(cty.DynamicPseudoType),
"outputs": cty.EmptyObjectVal,
"defaults": cty.NullVal(cty.DynamicPseudoType),
"outputs": cty.EmptyObjectVal,
"workspace": cty.NullVal(cty.String),
}),
true,
},
@ -225,8 +231,9 @@ func TestState_basic(t *testing.T) {
"config": cty.MapVal(map[string]cty.Value{
"path": cty.StringVal("./testdata/empty.tfstate"),
}),
"defaults": cty.NullVal(cty.DynamicPseudoType),
"outputs": cty.EmptyObjectVal,
"defaults": cty.NullVal(cty.DynamicPseudoType),
"outputs": cty.EmptyObjectVal,
"workspace": cty.NullVal(cty.String),
}),
false,
},
@ -247,6 +254,7 @@ func TestState_basic(t *testing.T) {
"outputs": cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("bar"),
}),
"workspace": cty.NullVal(cty.String),
}),
false,
},