Merge pull request #1485 from hashicorp/b-refresh-outputs

terraform: return value for resource interpolation on refresh
This commit is contained in:
Mitchell Hashimoto 2015-04-10 14:33:47 -07:00
commit 62bc562c97
3 changed files with 72 additions and 1 deletions

View File

@ -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) {
p := testProvider("aws")
m := testModule(t, "refresh-output-partial")
@ -5995,6 +6043,16 @@ module.child:
ID = new
`
const testContextRefreshOutputStr = `
aws_instance.web:
ID = foo
foo = bar
Outputs:
foo = bar
`
const testContextRefreshOutputPartialStr = `
<no state>
`

View File

@ -193,7 +193,7 @@ func (i *Interpolater) valueResourceVar(
result map[string]ast.Variable) error {
// If we're computing all dynamic fields, then module vars count
// and we mark it as computed.
if i.Operation == walkValidate || i.Operation == walkRefresh {
if i.Operation == walkValidate {
result[n] = ast.Variable{
Value: config.UnknownVariableValue,
Type: ast.TypeString,
@ -353,6 +353,14 @@ func (i *Interpolater) computeResourceVariable(
}
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(
"Resource '%s' does not have attribute '%s' "+
"for variable '%s'",

View File

@ -0,0 +1,5 @@
resource "aws_instance" "web" {}
output "foo" {
value = "${aws_instance.web.foo}"
}