terraform: ResourceProvider.Refresh (shadow)
This commit is contained in:
parent
82a1158f55
commit
51ac3c5969
|
@ -138,6 +138,19 @@ func (p *shadowResourceProviderReal) Diff(
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *shadowResourceProviderReal) Refresh(
|
||||||
|
info *InstanceInfo,
|
||||||
|
state *InstanceState) (*InstanceState, error) {
|
||||||
|
result, err := p.ResourceProvider.Refresh(info, state)
|
||||||
|
p.Shared.Refresh.SetValue(info.HumanId(), &shadowResourceProviderRefresh{
|
||||||
|
State: state,
|
||||||
|
Result: result,
|
||||||
|
ResultErr: err,
|
||||||
|
})
|
||||||
|
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
// shadowResourceProviderShadow is the shadow resource provider. Function
|
// shadowResourceProviderShadow is the shadow resource provider. Function
|
||||||
// calls never affect real resources. This is paired with the "real" side
|
// calls never affect real resources. This is paired with the "real" side
|
||||||
// which must be called properly to enable recording.
|
// which must be called properly to enable recording.
|
||||||
|
@ -159,6 +172,7 @@ type shadowResourceProviderShared struct {
|
||||||
Configure shadow.Value
|
Configure shadow.Value
|
||||||
Apply shadow.KeyedValue
|
Apply shadow.KeyedValue
|
||||||
Diff shadow.KeyedValue
|
Diff shadow.KeyedValue
|
||||||
|
Refresh shadow.KeyedValue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *shadowResourceProviderShadow) CloseShadow() error {
|
func (p *shadowResourceProviderShadow) CloseShadow() error {
|
||||||
|
@ -356,6 +370,42 @@ func (p *shadowResourceProviderShadow) Diff(
|
||||||
return result.Result, result.ResultErr
|
return result.Result, result.ResultErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *shadowResourceProviderShadow) Refresh(
|
||||||
|
info *InstanceInfo,
|
||||||
|
state *InstanceState) (*InstanceState, error) {
|
||||||
|
// Unique key
|
||||||
|
key := info.HumanId()
|
||||||
|
raw := p.Shared.Refresh.Value(key)
|
||||||
|
if raw == nil {
|
||||||
|
p.ErrorLock.Lock()
|
||||||
|
defer p.ErrorLock.Unlock()
|
||||||
|
p.Error = multierror.Append(p.Error, fmt.Errorf(
|
||||||
|
"Unknown 'refresh' call for %q:\n\n%#v",
|
||||||
|
key, state))
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
result, ok := raw.(*shadowResourceProviderRefresh)
|
||||||
|
if !ok {
|
||||||
|
p.ErrorLock.Lock()
|
||||||
|
defer p.ErrorLock.Unlock()
|
||||||
|
p.Error = multierror.Append(p.Error, fmt.Errorf(
|
||||||
|
"Unknown 'refresh' shadow value: %#v", raw))
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compare the parameters, which should be identical
|
||||||
|
if !state.Equal(result.State) {
|
||||||
|
p.ErrorLock.Lock()
|
||||||
|
p.Error = multierror.Append(p.Error, fmt.Errorf(
|
||||||
|
"Refresh %q had unequal states (real, then shadow):\n\n%#v\n\n%#v",
|
||||||
|
key, result.State, state))
|
||||||
|
p.ErrorLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.Result, result.ResultErr
|
||||||
|
}
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
// TODO
|
// TODO
|
||||||
// TODO
|
// TODO
|
||||||
|
@ -366,12 +416,6 @@ func (p *shadowResourceProviderShadow) ValidateResource(t string, c *ResourceCon
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *shadowResourceProviderShadow) Refresh(
|
|
||||||
info *InstanceInfo,
|
|
||||||
s *InstanceState) (*InstanceState, error) {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *shadowResourceProviderShadow) ImportState(info *InstanceInfo, id string) ([]*InstanceState, error) {
|
func (p *shadowResourceProviderShadow) ImportState(info *InstanceInfo, id string) ([]*InstanceState, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -425,3 +469,9 @@ type shadowResourceProviderDiff struct {
|
||||||
Result *InstanceDiff
|
Result *InstanceDiff
|
||||||
ResultErr error
|
ResultErr error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type shadowResourceProviderRefresh struct {
|
||||||
|
State *InstanceState
|
||||||
|
Result *InstanceState
|
||||||
|
ResultErr error
|
||||||
|
}
|
||||||
|
|
|
@ -373,3 +373,56 @@ func TestShadowResourceProviderDiff(t *testing.T) {
|
||||||
t.Fatalf("bad: %s", err)
|
t.Fatalf("bad: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestShadowResourceProviderRefresh(t *testing.T) {
|
||||||
|
mock := new(MockResourceProvider)
|
||||||
|
real, shadow := newShadowResourceProvider(mock)
|
||||||
|
|
||||||
|
// Test values
|
||||||
|
info := &InstanceInfo{Id: "foo"}
|
||||||
|
state := &InstanceState{ID: "foo"}
|
||||||
|
mockResult := &InstanceState{ID: "bar"}
|
||||||
|
|
||||||
|
// Configure the mock
|
||||||
|
mock.RefreshReturn = mockResult
|
||||||
|
|
||||||
|
// Verify that it blocks until the real func is called
|
||||||
|
var result *InstanceState
|
||||||
|
var err error
|
||||||
|
doneCh := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
defer close(doneCh)
|
||||||
|
result, err = shadow.Refresh(info, state)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-doneCh:
|
||||||
|
t.Fatal("should block until finished")
|
||||||
|
case <-time.After(10 * time.Millisecond):
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the real func
|
||||||
|
realResult, realErr := real.Refresh(info, state)
|
||||||
|
if !realResult.Equal(mockResult) {
|
||||||
|
t.Fatalf("bad: %#v", realResult)
|
||||||
|
}
|
||||||
|
if realErr != nil {
|
||||||
|
t.Fatalf("bad: %#v", realErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The shadow should finish now
|
||||||
|
<-doneCh
|
||||||
|
|
||||||
|
// Verify the shadow returned the same values
|
||||||
|
if !result.Equal(mockResult) {
|
||||||
|
t.Fatalf("bad: %#v", result)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("bad: %#v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify we have no errors
|
||||||
|
if err := shadow.CloseShadow(); err != nil {
|
||||||
|
t.Fatalf("bad: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue