Skip to content

Commit

Permalink
Connect IgnoreChanges with planning
Browse files Browse the repository at this point in the history
  • Loading branch information
t0yv0 committed Jan 24, 2024
1 parent ae083b2 commit 1f2c1a7
Showing 1 changed file with 36 additions and 51 deletions.
87 changes: 36 additions & 51 deletions pkg/tfshim/sdk-v2/provider2.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
testing "github.com/mitchellh/go-testing-interface"

shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)

type v2Resource2 struct {
Expand Down Expand Up @@ -115,12 +116,7 @@ func (d *v2InstanceDiff2) ProposedState(
}

func (*v2InstanceDiff2) IgnoreChanges(ignored map[string]bool) {
// TODO ignoreChanges is pretty complicated due to sequencing of calls; adopt tfbridge style
// handling of ignore changes instead of this? use CustomizeDiff to perform these changes?
// add some more hooks?
if len(ignored) > 0 {
panic(fmt.Sprintf("TODO: IgnoreChanges: %", ignored))
}
contract.Failf("IgnoreChanges is deprecated and should not be called anymore")
}

type v2Provider2 struct {
Expand Down Expand Up @@ -237,8 +233,9 @@ func (p *v2Provider2) Diff(
t string,
s shim.InstanceState,
c shim.ResourceConfig,
opts ...shim.DiffOption,
) (shim.InstanceDiff, error) {
return p.DiffWithContext(context.Background(), t, s, c)
return p.DiffWithContext(context.Background(), t, s, c, opts...)
}

func (p *v2Provider2) unpackInstanceState(t string, s shim.InstanceState) *v2InstanceState2 {
Expand All @@ -261,7 +258,9 @@ func (p *v2Provider2) DiffWithContext(
t string,
s shim.InstanceState,
c shim.ResourceConfig,
opts ...shim.DiffOption,
) (shim.InstanceDiff, error) {
options := shim.NewDiffOptions(opts...)
config := configFromShim(c)
state := p.unpackInstanceState(t, s)
res := p.tf.ResourcesMap[t]
Expand All @@ -280,7 +279,7 @@ func (p *v2Provider2) DiffWithContext(
return nil, err
}
st := state.stateValue
plan, err := p.server.PlanResourceChange(ctx, t, ty, cfg, st, proposed, state.meta, meta)
plan, err := p.server.PlanResourceChange(ctx, t, ty, cfg, st, proposed, state.meta, meta, options.IgnoreChanges)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -407,60 +406,42 @@ func (s *simpleProviderServer) PlanResourceChange(
config, priorState, proposedNewState cty.Value,
priorMeta map[string]interface{},
providerMeta *cty.Value,
ignores shim.IgnoreChanges,
) (*struct {
PlannedState cty.Value
PlannedMeta map[string]interface{}
PlannedDiff *terraform.InstanceDiff
}, error) {
configVal, err := msgpack.Marshal(config, ty)
if err != nil {
return nil, err
}
priorStateVal, err := msgpack.Marshal(priorState, ty)
if err != nil {
return nil, err
}
proposedNewStateVal, err := msgpack.Marshal(proposedNewState, ty)
if err != nil {
return nil, err
}
priorPrivate, err := json.Marshal(priorMeta)
if err != nil {
return nil, err
}
req := &tfprotov5.PlanResourceChangeRequest{
TypeName: typeName,
Config: &tfprotov5.DynamicValue{MsgPack: configVal},
PriorState: &tfprotov5.DynamicValue{MsgPack: priorStateVal},
ProposedNewState: &tfprotov5.DynamicValue{MsgPack: proposedNewStateVal},
PriorPrivate: priorPrivate,
}
if providerMeta != nil {
providerMetaVal, err := msgpack.Marshal(*providerMeta, ty)
if err != nil {
return nil, err
req := &schema.SimplePlanResourceChangeLogicalRequest{
ResourceName: typeName,
ConfigVal: config,
PriorStateVal: priorState,
ProposedNewStateVal: proposedNewState,
PriorPrivateState: priorMeta,
}

if ignores != nil {
req.InstanceDiffTransform = func(d *terraform.InstanceDiff) *terraform.InstanceDiff {
dd := &v2InstanceDiff{d}
dd.processIgnoreChanges(ignores)
return dd.tf
}
req.ProviderMeta = &tfprotov5.DynamicValue{MsgPack: providerMetaVal}
}
resp, plannedDiff, err := s.gserver.PlanResourceChangeWithDiff(ctx, req)
newState, err := msgpack.Unmarshal(resp.PlannedState.MsgPack, ty)
if err != nil {

resp := s.gserver.PlanResourceChangeLogical(ctx, req)
if err := s.handle(resp.Diagnostics, nil); err != nil {
return nil, err
}
var plannedMeta map[string]interface{}
if resp.PlannedPrivate != nil {
if err := json.Unmarshal(resp.PlannedPrivate, &plannedMeta); err != nil {
return nil, err
}
}
// Ignore resp.UnsafeToUseLegacyTypeSystem - does not matter for Pulumi bridged providers.
// Ignore resp.RequiresReplace - expect replacement to be encoded in resp.InstanceDiff.
return &struct {
PlannedState cty.Value
PlannedMeta map[string]interface{}
PlannedDiff *terraform.InstanceDiff
}{
PlannedState: newState,
PlannedMeta: plannedMeta,
PlannedDiff: plannedDiff,
PlannedState: resp.PlannedState,
PlannedMeta: resp.PlannedPrivate,
PlannedDiff: resp.InstanceDiff,
}, nil
}

Expand Down Expand Up @@ -580,12 +561,16 @@ func (p *providerWithPlanResourceChange) ApplyWithContext(
}

func (p *providerWithPlanResourceChange) DiffWithContext(
ctx context.Context, t string, s shim.InstanceState, c shim.ResourceConfig,
ctx context.Context,
t string,
s shim.InstanceState,
c shim.ResourceConfig,
opts ...shim.DiffOption,
) (shim.InstanceDiff, error) {
if p.usePlanResourceChange(t) {
return p.altProvider.DiffWithContext(ctx, t, s, c)
return p.altProvider.DiffWithContext(ctx, t, s, c, opts...)
}
return p.ProviderWithContext.DiffWithContext(ctx, t, s, c)
return p.ProviderWithContext.DiffWithContext(ctx, t, s, c, opts...)
}

func (p *providerWithPlanResourceChange) RefreshWithContext(
Expand Down

0 comments on commit 1f2c1a7

Please sign in to comment.