Skip to content

Commit

Permalink
Implement shim.InstanceDiff for v2InstanceDiff2
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Jan 28, 2025
1 parent 15578b7 commit 70237a9
Showing 1 changed file with 42 additions and 18 deletions.
60 changes: 42 additions & 18 deletions pkg/tfshim/sdk-v2/provider2.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (s *v2InstanceState2) Meta() map[string]interface{} {
}

type v2InstanceDiff2 struct {
v2InstanceDiff
tf *terraform.InstanceDiff

config cty.Value
plannedState cty.Value
Expand All @@ -134,13 +134,11 @@ func (d *v2InstanceDiff2) GoString() string {
return "nil"
}
return fmt.Sprintf(`&v2InstanceDiff2{
v2InstanceDiff: v2InstanceDiff{
tf: %#v,
},
config: %#v,
plannedState: %#v,
plannedPrivate: %#v,
}`, d.v2InstanceDiff.tf, d.config, d.plannedState, d.plannedPrivate)
tf: %#v,
config: %#v,
plannedState: %#v,
plannedPrivate: %#v,
}`, d.tf, d.config, d.plannedState, d.plannedPrivate)
}

var _ shim.InstanceDiff = (*v2InstanceDiff2)(nil)
Expand All @@ -150,7 +148,7 @@ func (d *v2InstanceDiff2) ProposedState(
) (shim.InstanceState, error) {
return &v2InstanceState2{
stateValue: d.plannedState,
meta: d.v2InstanceDiff.tf.Meta,
meta: d.tf.Meta,
}, nil
}

Expand All @@ -165,6 +163,32 @@ func (d *v2InstanceDiff2) DiffEqualDecisionOverride() shim.DiffOverride {
return d.diffEqualDecisionOverride
}

func (d *v2InstanceDiff2) Attribute(key string) *shim.ResourceAttrDiff {
return resourceAttrDiffToShim(d.tf.Attributes[key])
}

func (d *v2InstanceDiff2) HasNoChanges() bool {
return len(d.Attributes()) == 0
}

func (d *v2InstanceDiff2) Attributes() map[string]shim.ResourceAttrDiff {
m := map[string]shim.ResourceAttrDiff{}
for k, v := range d.tf.Attributes {
if v != nil {
m[k] = *resourceAttrDiffToShim(v)
}
}
return m
}

func (d *v2InstanceDiff2) Destroy() bool {
return d.tf.Destroy
}

func (d *v2InstanceDiff2) RequiresNew() bool {
return d.tf.RequiresNew()
}

// Provides PlanResourceChange handling for select resources.
type planResourceChangeImpl struct {
tf *schema.Provider
Expand Down Expand Up @@ -446,9 +470,7 @@ func (p *planResourceChangeImpl) Diff(
}

return &v2InstanceDiff2{
v2InstanceDiff: v2InstanceDiff{
tf: plan.PlannedDiff,
},
tf: plan.PlannedDiff,
config: cfg,
plannedState: plannedState,
diffEqualDecisionOverride: diffOverride,
Expand Down Expand Up @@ -482,14 +504,14 @@ func (p *planResourceChangeImpl) Apply(
diff := p.unpackDiff(ty, d)
cfg, st, pl := diff.config, state.stateValue, diff.plannedState

// Merge plannedPrivate and v2InstanceDiff.tf.Meta into a single map. This is necessary because
// Merge plannedPrivate and tf.Meta into a single map. This is necessary because
// timeouts are stored in the Meta and not in plannedPrivate.
priv := make(map[string]interface{})
if len(diff.plannedPrivate) > 0 {
maps.Copy(priv, diff.plannedPrivate)
}
if len(diff.v2InstanceDiff.tf.Meta) > 0 {
maps.Copy(priv, diff.v2InstanceDiff.tf.Meta)
if len(diff.tf.Meta) > 0 {
maps.Copy(priv, diff.tf.Meta)
}

return p.server.ApplyResourceChange(ctx, t, ty, cfg, st, pl, priv, meta)
Expand Down Expand Up @@ -540,9 +562,9 @@ func (p *planResourceChangeImpl) NewDestroyDiff(
ty := res.CoreConfigSchema().ImpliedType()
dd := (&v2Provider{}).NewDestroyDiff(ctx, t, opts).(v2InstanceDiff)
return &v2InstanceDiff2{
v2InstanceDiff: dd,
config: cty.NullVal(ty),
plannedState: cty.NullVal(ty),
tf: dd.tf,
config: cty.NullVal(ty),
plannedState: cty.NullVal(ty),
}
}

Expand Down Expand Up @@ -954,6 +976,8 @@ type planResourceChangeProvider interface {
Importer(t string) shim.ImportFunc
}

var _ = shim.ResourceMap(&v2ResourceCustomMap{})

type v2ResourceCustomMap struct {
resources map[string]*schema.Resource
pack func(string, *schema.Resource) shim.Resource
Expand Down

0 comments on commit 70237a9

Please sign in to comment.