diff --git a/terraform/context.go b/terraform/context.go index 166d14497..895def246 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -573,6 +573,10 @@ func (c *Context) refreshWalkFn(result *State) depgraph.WalkFunc { var l sync.Mutex cb := func(r *Resource) error { + if r.State.ID == "" { + return nil + } + for _, h := range c.hooks { handleHook(h.PreRefresh(r.Id, r.State)) } diff --git a/terraform/context_test.go b/terraform/context_test.go index eb49dc1ac..51dd95d27 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -979,6 +979,14 @@ func TestContextRefresh(t *testing.T) { Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, + State: &State{ + Resources: map[string]*ResourceState{ + "aws_instance.web": &ResourceState{ + ID: "foo", + Type: "aws_instance", + }, + }, + }, }) p.RefreshFn = nil @@ -993,7 +1001,7 @@ func TestContextRefresh(t *testing.T) { if !p.RefreshCalled { t.Fatal("refresh should be called") } - if p.RefreshState.ID != "" { + if p.RefreshState.ID != "foo" { t.Fatalf("bad: %#v", p.RefreshState) } 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) { h := new(MockHook) p := testProvider("aws") @@ -1017,6 +1050,14 @@ func TestContextRefresh_hook(t *testing.T) { Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, + State: &State{ + Resources: map[string]*ResourceState{ + "aws_instance.web": &ResourceState{ + ID: "foo", + Type: "aws_instance", + }, + }, + }, }) if _, err := ctx.Refresh(); err != nil {