terraform: don't refresh resources that aren't created

This commit is contained in:
Mitchell Hashimoto 2014-07-07 16:19:25 -07:00
parent 5b95271819
commit 38b3734d12
2 changed files with 46 additions and 1 deletions

View File

@ -573,6 +573,10 @@ func (c *Context) refreshWalkFn(result *State) depgraph.WalkFunc {
var l sync.Mutex var l sync.Mutex
cb := func(r *Resource) error { cb := func(r *Resource) error {
if r.State.ID == "" {
return nil
}
for _, h := range c.hooks { for _, h := range c.hooks {
handleHook(h.PreRefresh(r.Id, r.State)) handleHook(h.PreRefresh(r.Id, r.State))
} }

View File

@ -979,6 +979,14 @@ func TestContextRefresh(t *testing.T) {
Providers: map[string]ResourceProviderFactory{ Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p), "aws": testProviderFuncFixed(p),
}, },
State: &State{
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
ID: "foo",
Type: "aws_instance",
},
},
},
}) })
p.RefreshFn = nil p.RefreshFn = nil
@ -993,7 +1001,7 @@ func TestContextRefresh(t *testing.T) {
if !p.RefreshCalled { if !p.RefreshCalled {
t.Fatal("refresh should be called") t.Fatal("refresh should be called")
} }
if p.RefreshState.ID != "" { if p.RefreshState.ID != "foo" {
t.Fatalf("bad: %#v", p.RefreshState) t.Fatalf("bad: %#v", p.RefreshState)
} }
if !reflect.DeepEqual(s.Resources["aws_instance.web"], p.RefreshReturn) { if !reflect.DeepEqual(s.Resources["aws_instance.web"], p.RefreshReturn) {
@ -1007,6 +1015,31 @@ func TestContextRefresh(t *testing.T) {
} }
} }
func TestContextRefresh_ignoreUncreated(t *testing.T) {
p := testProvider("aws")
c := testConfig(t, "refresh-basic")
ctx := testContext(t, &ContextOpts{
Config: c,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
State: nil,
})
p.RefreshFn = nil
p.RefreshReturn = &ResourceState{
ID: "foo",
}
_, err := ctx.Refresh()
if err != nil {
t.Fatalf("err: %s", err)
}
if p.RefreshCalled {
t.Fatal("refresh should not be called")
}
}
func TestContextRefresh_hook(t *testing.T) { func TestContextRefresh_hook(t *testing.T) {
h := new(MockHook) h := new(MockHook)
p := testProvider("aws") p := testProvider("aws")
@ -1017,6 +1050,14 @@ func TestContextRefresh_hook(t *testing.T) {
Providers: map[string]ResourceProviderFactory{ Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p), "aws": testProviderFuncFixed(p),
}, },
State: &State{
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
ID: "foo",
Type: "aws_instance",
},
},
},
}) })
if _, err := ctx.Refresh(); err != nil { if _, err := ctx.Refresh(); err != nil {