terraform: return value for resource interpolation on refresh
Instead of returning UnknownVariableValue every time, attempt to return the real value. If we don't find it, return unknown value. This fixes removing outputs from state on refresh.
This commit is contained in:
parent
56836c236f
commit
4c3923e32a
|
@ -1845,6 +1845,54 @@ func TestContext2Refresh_noState(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContext2Refresh_output(t *testing.T) {
|
||||||
|
p := testProvider("aws")
|
||||||
|
m := testModule(t, "refresh-output")
|
||||||
|
ctx := testContext2(t, &ContextOpts{
|
||||||
|
Module: m,
|
||||||
|
Providers: map[string]ResourceProviderFactory{
|
||||||
|
"aws": testProviderFuncFixed(p),
|
||||||
|
},
|
||||||
|
State: &State{
|
||||||
|
Modules: []*ModuleState{
|
||||||
|
&ModuleState{
|
||||||
|
Path: rootModulePath,
|
||||||
|
Resources: map[string]*ResourceState{
|
||||||
|
"aws_instance.web": &ResourceState{
|
||||||
|
Type: "aws_instance",
|
||||||
|
Primary: &InstanceState{
|
||||||
|
ID: "foo",
|
||||||
|
Attributes: map[string]string{
|
||||||
|
"foo": "bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
Outputs: map[string]string{
|
||||||
|
"foo": "foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
p.RefreshFn = func(info *InstanceInfo, s *InstanceState) (*InstanceState, error) {
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err := ctx.Refresh()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := strings.TrimSpace(s.String())
|
||||||
|
expected := strings.TrimSpace(testContextRefreshOutputStr)
|
||||||
|
if actual != expected {
|
||||||
|
t.Fatalf("bad:\n\n%s\n\n%s", actual, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestContext2Refresh_outputPartial(t *testing.T) {
|
func TestContext2Refresh_outputPartial(t *testing.T) {
|
||||||
p := testProvider("aws")
|
p := testProvider("aws")
|
||||||
m := testModule(t, "refresh-output-partial")
|
m := testModule(t, "refresh-output-partial")
|
||||||
|
@ -5995,6 +6043,16 @@ module.child:
|
||||||
ID = new
|
ID = new
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const testContextRefreshOutputStr = `
|
||||||
|
aws_instance.web:
|
||||||
|
ID = foo
|
||||||
|
foo = bar
|
||||||
|
|
||||||
|
Outputs:
|
||||||
|
|
||||||
|
foo = bar
|
||||||
|
`
|
||||||
|
|
||||||
const testContextRefreshOutputPartialStr = `
|
const testContextRefreshOutputPartialStr = `
|
||||||
<no state>
|
<no state>
|
||||||
`
|
`
|
||||||
|
|
|
@ -193,7 +193,7 @@ func (i *Interpolater) valueResourceVar(
|
||||||
result map[string]ast.Variable) error {
|
result map[string]ast.Variable) error {
|
||||||
// If we're computing all dynamic fields, then module vars count
|
// If we're computing all dynamic fields, then module vars count
|
||||||
// and we mark it as computed.
|
// and we mark it as computed.
|
||||||
if i.Operation == walkValidate || i.Operation == walkRefresh {
|
if i.Operation == walkValidate {
|
||||||
result[n] = ast.Variable{
|
result[n] = ast.Variable{
|
||||||
Value: config.UnknownVariableValue,
|
Value: config.UnknownVariableValue,
|
||||||
Type: ast.TypeString,
|
Type: ast.TypeString,
|
||||||
|
@ -353,6 +353,14 @@ func (i *Interpolater) computeResourceVariable(
|
||||||
}
|
}
|
||||||
|
|
||||||
MISSING:
|
MISSING:
|
||||||
|
// If the operation is refresh, it isn't an error for a value to
|
||||||
|
// be unknown. Instead, we return that the value is computed so
|
||||||
|
// that the graph can continue to refresh other nodes. It doesn't
|
||||||
|
// matter because the config isn't interpolated anyways.
|
||||||
|
if i.Operation == walkRefresh {
|
||||||
|
return config.UnknownVariableValue, nil
|
||||||
|
}
|
||||||
|
|
||||||
return "", fmt.Errorf(
|
return "", fmt.Errorf(
|
||||||
"Resource '%s' does not have attribute '%s' "+
|
"Resource '%s' does not have attribute '%s' "+
|
||||||
"for variable '%s'",
|
"for variable '%s'",
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
resource "aws_instance" "web" {}
|
||||||
|
|
||||||
|
output "foo" {
|
||||||
|
value = "${aws_instance.web.foo}"
|
||||||
|
}
|
Loading…
Reference in New Issue