Skip to content

Commit

Permalink
Propagate changes
Browse files Browse the repository at this point in the history
  • Loading branch information
t0yv0 committed Jan 25, 2024
1 parent 559e4fc commit 1b7d17b
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 39 deletions.
2 changes: 1 addition & 1 deletion pf/internal/schemashim/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (p *SchemaOnlyProvider) InitLogging(ctx context.Context) {
panic("schemaOnlyProvider does not implement runtime operation InitLogging")
}

func (p *SchemaOnlyProvider) NewDestroyDiff(ctx context.Context, t string) shim.InstanceDiff {
func (p *SchemaOnlyProvider) NewDestroyDiff(ctx context.Context, t string, _ shim.TimeoutOptions) shim.InstanceDiff {
panic("schemaOnlyProvider does not implement runtime operation NewDestroyDiff")
}

Expand Down
24 changes: 16 additions & 8 deletions pkg/tfbridge/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strconv"
"strings"
"testing"
"time"

structpb "github.com/golang/protobuf/ptypes/struct"
schemav1 "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -689,13 +690,16 @@ func TestMetaProperties(t *testing.T) {
ok = clearID(state)
assert.True(t, ok)
cfg := prov.NewResourceConfig(ctx, map[string]interface{}{})
diff, err := prov.Diff(ctx, resName, state, cfg, shim.DiffOptions{})
assert.NoError(t, err)

// To populate default timeouts, we take the timeouts from the resource schema and insert them into the diff
timeouts, err := res.DecodeTimeouts(cfg)
assert.NoError(t, err)
err = diff.EncodeTimeouts(timeouts)

diff, err := prov.Diff(ctx, resName, state, cfg, shim.DiffOptions{
TimeoutOptions: shim.TimeoutOptions{
ResourceTimeout: timeouts,
},
})
assert.NoError(t, err)

assert.NoError(t, err)
Expand Down Expand Up @@ -765,16 +769,20 @@ func TestInjectingCustomTimeouts(t *testing.T) {
ok = clearID(state)
assert.True(t, ok)
cfg := prov.NewResourceConfig(ctx, map[string]interface{}{})
diff, err := prov.Diff(ctx, resName, state, cfg, shim.DiffOptions{})
assert.NoError(t, err)

// To populate default timeouts, we take the timeouts from the resource schema and insert them into the diff
resourceTimeouts, err := res.DecodeTimeouts(cfg)
assert.NoError(t, err)
err = diff.EncodeTimeouts(resourceTimeouts)
assert.NoError(t, err)

diff.SetTimeout(300, schemav1.TimeoutCreate)
diff, err := prov.Diff(ctx, resName, state, cfg, shim.DiffOptions{
TimeoutOptions: shim.TimeoutOptions{
ResourceTimeout: resourceTimeouts,
TimeoutOverrides: map[shim.TimeoutKey]time.Duration{
shim.TimeoutCreate: 300 * time.Second,
},
},
})
assert.NoError(t, err)

assert.NoError(t, err)
create, err := prov.Apply(ctx, resName, state, diff)
Expand Down
2 changes: 1 addition & 1 deletion pkg/tfshim/schema/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (ProviderShim) InitLogging(ctx context.Context) {
panic("this provider is schema-only and does not support runtime operations")
}

func (ProviderShim) NewDestroyDiff(ctx context.Context, t string) shim.InstanceDiff {
func (ProviderShim) NewDestroyDiff(ctx context.Context, t string, _ shim.TimeoutOptions) shim.InstanceDiff {
panic("this provider is schema-only and does not support runtime operations")
}

Expand Down
23 changes: 18 additions & 5 deletions pkg/tfshim/sdk-v1/instance_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package sdkv1

import (
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"

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

var _ = shim.InstanceDiff(v1InstanceDiff{})
Expand Down Expand Up @@ -41,6 +43,16 @@ type v1InstanceDiff struct {
tf *terraform.InstanceDiff
}

func (d v1InstanceDiff) applyTimeoutOptions(opts shim.TimeoutOptions) {
if opts.ResourceTimeout != nil {
err := d.encodeTimeouts(opts.ResourceTimeout)
contract.AssertNoErrorf(err, "encodeTimeouts should never fail")
}
for timeoutKey, dur := range opts.TimeoutOverrides {
d.setTimeout(dur, timeoutKey)
}
}

func (d v1InstanceDiff) Attribute(key string) *shim.ResourceAttrDiff {
return resourceAttrDiffToShim(d.tf.Attributes[key])
}
Expand Down Expand Up @@ -93,7 +105,7 @@ func (d v1InstanceDiff) processIgnoreChanges(ignored shim.IgnoreChanges) {
}
}

func (d v1InstanceDiff) EncodeTimeouts(timeouts *shim.ResourceTimeout) error {
func (d v1InstanceDiff) encodeTimeouts(timeouts *shim.ResourceTimeout) error {
v1Timeouts := &schema.ResourceTimeout{}
if timeouts != nil {
v1Timeouts.Create = timeouts.Create
Expand All @@ -105,8 +117,9 @@ func (d v1InstanceDiff) EncodeTimeouts(timeouts *shim.ResourceTimeout) error {
return v1Timeouts.DiffEncode(d.tf)
}

func (d v1InstanceDiff) SetTimeout(timeout float64, timeoutKey string) {
timeoutValue := int64(timeout * 1000000000) //this turns seconds to nanoseconds - TF wants it in this format
func (d v1InstanceDiff) setTimeout(timeout time.Duration, timeoutKey shim.TimeoutKey) {
// this turns seconds to nanoseconds - TF wants it in this format
timeoutValue := timeout.Nanoseconds()

switch timeoutKey {
case shim.TimeoutCreate:
Expand All @@ -130,9 +143,9 @@ func (d v1InstanceDiff) SetTimeout(timeout float64, timeoutKey string) {
timeouts, ok := d.tf.Meta[schema.TimeoutKey].(map[string]interface{})
if !ok {
d.tf.Meta[schema.TimeoutKey] = map[string]interface{}{
timeoutKey: timeoutValue,
string(timeoutKey): timeoutValue,
}
} else {
timeouts[timeoutKey] = timeoutValue
timeouts[string(timeoutKey)] = timeoutValue
}
}
15 changes: 12 additions & 3 deletions pkg/tfshim/sdk-v1/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ func (p v1Provider) Configure(_ context.Context, c shim.ResourceConfig) error {

func (p v1Provider) Diff(
_ context.Context, t string, s shim.InstanceState, c shim.ResourceConfig, opts shim.DiffOptions,
) (shim.InstanceDiff, error) {
) (theDiff shim.InstanceDiff, theError error) {
defer func() {
switch theDiff := theDiff.(type) {
case v1InstanceDiff:
theDiff.applyTimeoutOptions(opts.TimeoutOptions)
}
}()

if c == nil {
return diffToShim(&terraform.InstanceDiff{Destroy: true}), nil
}
Expand Down Expand Up @@ -144,8 +151,10 @@ func (p v1Provider) InitLogging(_ context.Context) {
logging.SetOutput()
}

func (p v1Provider) NewDestroyDiff(_ context.Context, t string) shim.InstanceDiff {
return v1InstanceDiff{&terraform.InstanceDiff{Destroy: true}}
func (p v1Provider) NewDestroyDiff(_ context.Context, t string, opts shim.TimeoutOptions) shim.InstanceDiff {
d := v1InstanceDiff{&terraform.InstanceDiff{Destroy: true}}
d.applyTimeoutOptions(opts)
return d
}

func (p v1Provider) NewResourceConfig(
Expand Down
23 changes: 18 additions & 5 deletions pkg/tfshim/sdk-v2/instance_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package sdkv2

import (
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)

var _ = shim.InstanceDiff(v2InstanceDiff{})
Expand All @@ -30,6 +32,16 @@ type v2InstanceDiff struct {
tf *terraform.InstanceDiff
}

func (d v2InstanceDiff) applyTimeoutOptions(opts shim.TimeoutOptions) {
if opts.ResourceTimeout != nil {
err := d.encodeTimeouts(opts.ResourceTimeout)
contract.AssertNoErrorf(err, "encodeTimeouts should never fail")
}
for timeoutKey, dur := range opts.TimeoutOverrides {
d.setTimeout(dur, timeoutKey)
}
}

func (d v2InstanceDiff) Attribute(key string) *shim.ResourceAttrDiff {
return resourceAttrDiffToShim(d.tf.Attributes[key])
}
Expand Down Expand Up @@ -86,7 +98,7 @@ func (d v2InstanceDiff) processIgnoreChanges(ignored shim.IgnoreChanges) {
}
}

func (d v2InstanceDiff) EncodeTimeouts(timeouts *shim.ResourceTimeout) error {
func (d v2InstanceDiff) encodeTimeouts(timeouts *shim.ResourceTimeout) error {
v2Timeouts := &schema.ResourceTimeout{}
if timeouts != nil {
v2Timeouts.Create = timeouts.Create
Expand All @@ -98,8 +110,9 @@ func (d v2InstanceDiff) EncodeTimeouts(timeouts *shim.ResourceTimeout) error {
return v2Timeouts.DiffEncode(d.tf)
}

func (d v2InstanceDiff) SetTimeout(timeout float64, timeoutKey string) {
timeoutValue := int64(timeout * 1000000000) //this turns seconds to nanoseconds - TF wants it in this format
func (d v2InstanceDiff) setTimeout(timeout time.Duration, timeoutKey shim.TimeoutKey) {
// this turns seconds to nanoseconds - TF wants it in this format
timeoutValue := timeout.Nanoseconds()

switch timeoutKey {
case shim.TimeoutCreate:
Expand All @@ -123,9 +136,9 @@ func (d v2InstanceDiff) SetTimeout(timeout float64, timeoutKey string) {
timeouts, ok := d.tf.Meta[schema.TimeoutKey].(map[string]interface{})
if !ok {
d.tf.Meta[schema.TimeoutKey] = map[string]interface{}{
timeoutKey: timeoutValue,
string(timeoutKey): timeoutValue,
}
} else {
timeouts[timeoutKey] = timeoutValue
timeouts[string(timeoutKey)] = timeoutValue
}
}
6 changes: 4 additions & 2 deletions pkg/tfshim/sdk-v2/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ func (p v2Provider) InitLogging(_ context.Context) {
logging.SetOutput(&testing.RuntimeT{})
}

func (p v2Provider) NewDestroyDiff(_ context.Context, t string) shim.InstanceDiff {
return v2InstanceDiff{&terraform.InstanceDiff{Destroy: true}}
func (p v2Provider) NewDestroyDiff(_ context.Context, t string, opts shim.TimeoutOptions) shim.InstanceDiff {
d := v2InstanceDiff{&terraform.InstanceDiff{Destroy: true}}
d.applyTimeoutOptions(opts)
return d
}

func (p v2Provider) NewResourceConfig(
Expand Down
8 changes: 7 additions & 1 deletion pkg/tfshim/sdk-v2/provider_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ func (p v2Provider) Diff(
s shim.InstanceState,
c shim.ResourceConfig,
opts shim.DiffOptions,
) (shim.InstanceDiff, error) {
) (theDiff shim.InstanceDiff, theError error) {
defer func() {
switch theDiff := theDiff.(type) {
case v2InstanceDiff:
theDiff.applyTimeoutOptions(opts.TimeoutOptions)
}
}()
if c == nil {
return diffToShim(&terraform.InstanceDiff{Destroy: true}), nil
}
Expand Down
27 changes: 19 additions & 8 deletions pkg/tfshim/tfplugin5/instance_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ type instanceDiff struct {
attributes map[string]shim.ResourceAttrDiff
}

func (d instanceDiff) applyTimeoutOptions(opts shim.TimeoutOptions) {
if opts.ResourceTimeout != nil {
err := d.encodeTimeouts(opts.ResourceTimeout)
contract.AssertNoErrorf(err, "encodeTimeouts should never fail")
}
for timeoutKey, dur := range opts.TimeoutOverrides {
d.setTimeout(dur, timeoutKey)
}
}

func newInstanceDiff(config, prior, planned cty.Value, meta map[string]interface{},
requiresReplace []*proto.AttributePath) *instanceDiff {

Expand Down Expand Up @@ -80,7 +90,7 @@ func (d *instanceDiff) RequiresNew() bool {
return d.requiresNew
}

func (d *instanceDiff) EncodeTimeouts(timeouts *shim.ResourceTimeout) error {
func (d *instanceDiff) encodeTimeouts(timeouts *shim.ResourceTimeout) error {
if timeouts == nil {
return nil
}
Expand Down Expand Up @@ -109,8 +119,9 @@ func (d *instanceDiff) EncodeTimeouts(timeouts *shim.ResourceTimeout) error {
return nil
}

func (d *instanceDiff) SetTimeout(timeout float64, timeoutKey string) {
timeoutValue := time.Duration(timeout * 1000000000) //this turns seconds to nanoseconds - TF wants it in this format
func (d *instanceDiff) setTimeout(timeout time.Duration, timeoutKey shim.TimeoutKey) {
// this turns seconds to nanoseconds - TF wants it in this format
timeoutValue := timeout.Nanoseconds()

if d.meta == nil {
d.meta = map[string]interface{}{}
Expand All @@ -123,15 +134,15 @@ func (d *instanceDiff) SetTimeout(timeout float64, timeoutKey string) {

switch timeoutKey {
case shim.TimeoutCreate:
timeoutsMap["create"] = timeoutValue.Nanoseconds()
timeoutsMap["create"] = timeoutValue
case shim.TimeoutRead:
timeoutsMap["read"] = timeoutValue.Nanoseconds()
timeoutsMap["read"] = timeoutValue
case shim.TimeoutUpdate:
timeoutsMap["update"] = timeoutValue.Nanoseconds()
timeoutsMap["update"] = timeoutValue
case shim.TimeoutDelete:
timeoutsMap["delete"] = timeoutValue.Nanoseconds()
timeoutsMap["delete"] = timeoutValue
case shim.TimeoutDefault:
timeoutsMap["default"] = timeoutValue.Nanoseconds()
timeoutsMap["default"] = timeoutValue
}
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/tfshim/tfplugin5/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,10 @@ func (p *provider) InitLogging(ctx context.Context) {
// Nothing to do.
}

func (p *provider) NewDestroyDiff(ctx context.Context, t string) shim.InstanceDiff {
return &instanceDiff{destroy: true}
func (p *provider) NewDestroyDiff(ctx context.Context, t string, opts shim.TimeoutOptions) shim.InstanceDiff {
d := &instanceDiff{destroy: true}
d.applyTimeoutOptions(opts)
return d
}

func (p *provider) NewResourceConfig(ctx context.Context, object map[string]interface{}) shim.ResourceConfig {
Expand Down
6 changes: 4 additions & 2 deletions pkg/tfshim/util/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ func (p *FilteringProvider) InitLogging(ctx context.Context) {
p.Provider.InitLogging(ctx)
}

func (p *FilteringProvider) NewDestroyDiff(ctx context.Context, t string) shim.InstanceDiff {
return p.Provider.NewDestroyDiff(ctx, t)
func (p *FilteringProvider) NewDestroyDiff(
ctx context.Context, t string, opts shim.TimeoutOptions,
) shim.InstanceDiff {
return p.Provider.NewDestroyDiff(ctx, t, opts)
}

func (p *FilteringProvider) NewResourceConfig(
Expand Down
4 changes: 3 additions & 1 deletion pkg/tfshim/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ func (UnimplementedProvider) Stop(ctx context.Context) error { panic("unim

func (UnimplementedProvider) InitLogging(ctx context.Context) { panic("unimplemented") }

func (UnimplementedProvider) NewDestroyDiff(ctx context.Context, t string) shim.InstanceDiff {
func (UnimplementedProvider) NewDestroyDiff(
ctx context.Context, t string, _ shim.TimeoutOptions,
) shim.InstanceDiff {
panic("unimplemented")
}

Expand Down

0 comments on commit 1b7d17b

Please sign in to comment.