terraform: call refresh before every diff

In the future, we'll make this a separate thing...
This commit is contained in:
Mitchell Hashimoto 2014-06-19 21:22:07 -07:00
parent 74d75e87d6
commit dc193f5f33
4 changed files with 42 additions and 1 deletions

View File

@ -46,6 +46,10 @@ type ResourceProvider interface {
Diff(
*ResourceState,
*ResourceConfig) (*ResourceDiff, error)
// Refresh refreshes a resource and updates all of its attributes
// with the latest information.
Refresh(*ResourceState) (*ResourceState, error)
}
// ResourceConfig holds the configuration given for a resource. This is

View File

@ -21,6 +21,11 @@ type MockResourceProvider struct {
DiffFn func(*ResourceState, *ResourceConfig) (*ResourceDiff, error)
DiffReturn *ResourceDiff
DiffReturnError error
RefreshCalled bool
RefreshState *ResourceState
RefreshFn func(*ResourceState) (*ResourceState, error)
RefreshReturn *ResourceState
RefreshReturnError error
ResourcesCalled bool
ResourcesReturn []ResourceType
ValidateCalled bool
@ -67,6 +72,18 @@ func (p *MockResourceProvider) Diff(
return p.DiffReturn, p.DiffReturnError
}
func (p *MockResourceProvider) Refresh(
s *ResourceState) (*ResourceState, error) {
p.RefreshCalled = true
p.RefreshState = s
if p.RefreshFn != nil {
return p.RefreshFn(s)
}
return p.RefreshReturn, p.RefreshReturnError
}
func (p *MockResourceProvider) Resources() []ResourceType {
p.ResourcesCalled = true
return p.ResourcesReturn

View File

@ -173,7 +173,14 @@ func (t *Terraform) diffWalkFn(
result.init()
cb := func(r *Resource) (map[string]string, error) {
diff, err := r.Provider.Diff(r.State, r.Config)
// Refresh the state so we're working with the latest resource info
newState, err := r.Provider.Refresh(r.State)
if err != nil {
return nil, err
}
// Get a diff from the newest state
diff, err := r.Provider.Diff(newState, r.Config)
if err != nil {
return nil, err
}

View File

@ -245,6 +245,14 @@ func TestTerraformDiff(t *testing.T) {
if actual != expected {
t.Fatalf("bad:\n%s", actual)
}
p := testProviderMock(testProvider(tf, "aws_instance.foo"))
if !p.RefreshCalled {
t.Fatal("refresh should be called")
}
if p.RefreshState == nil {
t.Fatal("refresh should have state")
}
}
func TestTerraformDiff_nil(t *testing.T) {
@ -383,10 +391,15 @@ func testProviderFunc(n string, rs []string) ResourceProviderFactory {
return &diff, nil
}
refreshFn := func(s *ResourceState) (*ResourceState, error) {
return s, nil
}
result := &MockResourceProvider{
Meta: n,
ApplyFn: applyFn,
DiffFn: diffFn,
RefreshFn: refreshFn,
ResourcesReturn: resources,
}