Skip to content

Commit 98fa7d9

Browse files
committed
terraform: support data source apply for shadows
1 parent 4c95142 commit 98fa7d9

2 files changed

Lines changed: 74 additions & 6 deletions

File tree

‎terraform/diff.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,21 @@ func (d *InstanceDiff) Empty() bool {
400400
return !d.Destroy && !d.DestroyTainted && len(d.Attributes) == 0
401401
}
402402

403+
// Equal compares two diffs for exact equality.
404+
//
405+
// This is different from the Same comparison that is supported which
406+
// checks for operation equality taking into account computed values. Equal
407+
// instead checks for exact equality.
408+
func (d *InstanceDiff) Equal(d2 *InstanceDiff) bool {
409+
// If one is nil, they must both be nil
410+
if d == nil || d2 == nil {
411+
return d == d2
412+
}
413+
414+
// Use DeepEqual
415+
return reflect.DeepEqual(d, d2)
416+
}
417+
403418
// DeepCopy performs a deep copy of all parts of the InstanceDiff
404419
func (d *InstanceDiff) DeepCopy() *InstanceDiff {
405420
copy, err := copystructure.Config{Lock: true}.Copy(d)

‎terraform/shadow_resource_provider.go‎

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,22 @@ func (p *shadowResourceProviderReal) ReadDataDiff(
254254
return result, err
255255
}
256256

257+
func (p *shadowResourceProviderReal) ReadDataApply(
258+
info *InstanceInfo,
259+
diff *InstanceDiff) (*InstanceState, error) {
260+
// Thse have to be copied before the call since call can modify
261+
diffCopy := diff.DeepCopy()
262+
263+
result, err := p.ResourceProvider.ReadDataApply(info, diff)
264+
p.Shared.ReadDataApply.SetValue(info.uniqueId(), &shadowResourceProviderReadDataApply{
265+
Diff: diffCopy,
266+
Result: result.DeepCopy(),
267+
ResultErr: err,
268+
})
269+
270+
return result, err
271+
}
272+
257273
// shadowResourceProviderShadow is the shadow resource provider. Function
258274
// calls never affect real resources. This is paired with the "real" side
259275
// which must be called properly to enable recording.
@@ -282,6 +298,7 @@ type shadowResourceProviderShared struct {
282298
Refresh shadow.KeyedValue
283299
ValidateDataSource shadow.KeyedValue
284300
ReadDataDiff shadow.KeyedValue
301+
ReadDataApply shadow.KeyedValue
285302
}
286303

287304
func (p *shadowResourceProviderShared) Close() error {
@@ -679,6 +696,42 @@ func (p *shadowResourceProviderShadow) ReadDataDiff(
679696
return result.Result, result.ResultErr
680697
}
681698

699+
func (p *shadowResourceProviderShadow) ReadDataApply(
700+
info *InstanceInfo,
701+
d *InstanceDiff) (*InstanceState, error) {
702+
// Unique key
703+
key := info.uniqueId()
704+
raw := p.Shared.ReadDataApply.Value(key)
705+
if raw == nil {
706+
p.ErrorLock.Lock()
707+
defer p.ErrorLock.Unlock()
708+
p.Error = multierror.Append(p.Error, fmt.Errorf(
709+
"Unknown 'ReadDataApply' call for %q:\n\n%#v",
710+
key, d))
711+
return nil, nil
712+
}
713+
714+
result, ok := raw.(*shadowResourceProviderReadDataApply)
715+
if !ok {
716+
p.ErrorLock.Lock()
717+
defer p.ErrorLock.Unlock()
718+
p.Error = multierror.Append(p.Error, fmt.Errorf(
719+
"Unknown 'ReadDataApply' shadow value: %#v", raw))
720+
return nil, nil
721+
}
722+
723+
// Compare the parameters, which should be identical
724+
if !d.Equal(result.Diff) {
725+
p.ErrorLock.Lock()
726+
p.Error = multierror.Append(p.Error, fmt.Errorf(
727+
"ReadDataApply: unequal diffs (real, then shadow):\n\n%#v\n\n%#v",
728+
result.Diff, d))
729+
p.ErrorLock.Unlock()
730+
}
731+
732+
return result.Result, result.ResultErr
733+
}
734+
682735
// TODO
683736
// TODO
684737
// TODO
@@ -689,12 +742,6 @@ func (p *shadowResourceProviderShadow) ImportState(info *InstanceInfo, id string
689742
return nil, nil
690743
}
691744

692-
func (p *shadowResourceProviderShadow) ReadDataApply(
693-
info *InstanceInfo,
694-
d *InstanceDiff) (*InstanceState, error) {
695-
return nil, nil
696-
}
697-
698745
// The structs for the various function calls are put below. These structs
699746
// are used to carry call information across the real/shadow boundaries.
700747

@@ -764,3 +811,9 @@ type shadowResourceProviderReadDataDiff struct {
764811
Result *InstanceDiff
765812
ResultErr error
766813
}
814+
815+
type shadowResourceProviderReadDataApply struct {
816+
Diff *InstanceDiff
817+
Result *InstanceState
818+
ResultErr error
819+
}

0 commit comments

Comments
 (0)