Skip to content

Commit

Permalink
Fix empty labels perma-diff
Browse files Browse the repository at this point in the history
Relies on pulumi-terraform-bridge's iwahbe/add-sdkv2-edit-state
  • Loading branch information
iwahbe committed Sep 17, 2024
1 parent a1f7fa3 commit dceb55c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
6 changes: 3 additions & 3 deletions provider/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ go 1.22
toolchain go1.22.7

require (
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0
github.com/hashicorp/terraform-provider-google-beta v0.0.0
github.com/pulumi/providertest v0.0.14
github.com/pulumi/pulumi-terraform-bridge/pf v0.43.0
github.com/pulumi/pulumi-terraform-bridge/v3 v3.90.0
github.com/pulumi/pulumi-terraform-bridge/pf v0.43.1-0.20240917093314-ed4e944e34e2
github.com/pulumi/pulumi-terraform-bridge/v3 v3.90.1-0.20240917093314-ed4e944e34e2
github.com/pulumi/pulumi/pkg/v3 v3.130.0
github.com/pulumi/pulumi/sdk/v3 v3.130.0
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -129,7 +130,6 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
github.com/hashicorp/go-getter v1.7.5 // indirect
github.com/hashicorp/go-hclog v1.6.3 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions provider/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1976,10 +1976,10 @@ github.com/pulumi/providertest v0.0.14 h1:5QlAPAAs82jkQraHsJvq1xgVfC7xtW8sFJwv2p
github.com/pulumi/providertest v0.0.14/go.mod h1:GcsqEGgSngwaNOD+kICJPIUQlnA911fGBU8HDlJvVL0=
github.com/pulumi/pulumi-java/pkg v0.14.0 h1:CKL7lLF81Fq6VRhA5TNFsSMnHraTNCUzIhqCzYX8Wzk=
github.com/pulumi/pulumi-java/pkg v0.14.0/go.mod h1:VybuJMWJtJc9ZNbt1kcYH4TbpocMx9mEi7YWL2Co99c=
github.com/pulumi/pulumi-terraform-bridge/pf v0.43.0 h1:g15WgVKJBhFtzhLqOky6R77QIU3x4KkunrLHDSkK6CM=
github.com/pulumi/pulumi-terraform-bridge/pf v0.43.0/go.mod h1:xdU2rcUBjPX/alXMiywUK1GvN4goUHZxos8ZfT6sVXM=
github.com/pulumi/pulumi-terraform-bridge/v3 v3.90.0 h1:e7xfYAiXCE8LCwfKvbGeNAjdPmKwPM3kavEXECt3wvs=
github.com/pulumi/pulumi-terraform-bridge/v3 v3.90.0/go.mod h1:dIVp4qG+GsUwmpz40L7Z+PZnzHf3cQq1CAFwhz++ris=
github.com/pulumi/pulumi-terraform-bridge/pf v0.43.1-0.20240917093314-ed4e944e34e2 h1:h4Ne9Bw4fuQOHsEoAotEneas//4tbug92Ubpnpcjpgo=
github.com/pulumi/pulumi-terraform-bridge/pf v0.43.1-0.20240917093314-ed4e944e34e2/go.mod h1:xdU2rcUBjPX/alXMiywUK1GvN4goUHZxos8ZfT6sVXM=
github.com/pulumi/pulumi-terraform-bridge/v3 v3.90.1-0.20240917093314-ed4e944e34e2 h1:PgG0nRr1ym74rZMyOiR7ULUoQbSqgv00h4qAKeK7HJM=
github.com/pulumi/pulumi-terraform-bridge/v3 v3.90.1-0.20240917093314-ed4e944e34e2/go.mod h1:dIVp4qG+GsUwmpz40L7Z+PZnzHf3cQq1CAFwhz++ris=
github.com/pulumi/pulumi-terraform-bridge/x/muxer v0.0.9-0.20240227144008-2da15b3d6f6e h1:yON1jwN6gg4cjnOQF607I3fTiFyIDr9WSsQNXHD6wbM=
github.com/pulumi/pulumi-terraform-bridge/x/muxer v0.0.9-0.20240227144008-2da15b3d6f6e/go.mod h1:AvlZujvfRiEu+60frCGEhaLeGttjHwM/g+47+IdPZXw=
github.com/pulumi/pulumi-yaml v1.9.2 h1:BCUuRPA1USmFXrExiHRU8yJ+OiphLYnroPxKRgGCJrs=
Expand Down
61 changes: 60 additions & 1 deletion provider/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// Allow embedding metadata in the provider
_ "embed"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
gcpPFProvider "github.com/hashicorp/terraform-provider-google-beta/google-beta/fwprovider"
gcpProvider "github.com/hashicorp/terraform-provider-google-beta/google-beta/provider"
Expand Down Expand Up @@ -454,14 +455,72 @@ func preConfigureCallbackWithLogger(credentialsValidationRun *atomic.Bool, gcpCl
//go:embed cmd/pulumi-resource-gcp/bridge-metadata.json
var metadata []byte

// A predicate function that always returns true.
func always[T any](T) bool { return true }

// fixEmptyLabels applies a state edit to fix
// https://github.com/pulumi/pulumi-gcp/issues/2372.
func fixEmptyLabels(_ context.Context, req shimv2.PlanStateEditRequest) (cty.Value, error) {
inputs, ok := resource.PropertyPath{"labels"}.Get(resource.NewProperty(req.NewInputs))
if !ok {
return req.PlanState, nil
}

labels := resource.FromResourcePropertyValue(inputs)
if !labels.IsMap() {
// No labels found, so we don't need to correct
return req.PlanState, nil
}

// fixOutputLabels fixes falsely computed values inherited from "labels".
fixOutputLabels := func(output cty.Value) cty.Value {
if !output.Type().IsMapType() {
return output
}
m := output.AsValueMap()
for k, v := range m {
if v.IsKnown() {
// If v is known, then it is not falsely computed. No
// action is needed.
continue
}
label, ok := labels.AsMap()[k] // labels is in the Pulumi namespace
if !ok || label.IsComputed() || !label.IsString() {
// If we didn't inherit label from "labels" or the label
// is actually computed, then just continue.
continue
}

// Assign the correct label, discarding the erroneously computed
// value.
m[k] = cty.StringVal(label.AsString())
}
return cty.MapVal(m)
}

// Apply f to m[k] if k in m.
mapIfExists := func(m map[string]cty.Value, k string, f func(cty.Value) cty.Value) {
v, ok := m[k]
if ok {
m[k] = f(v)
}
}

planState := req.PlanState.AsValueMap()
mapIfExists(planState, "effective_labels", fixOutputLabels)
mapIfExists(planState, "terraform_labels", fixOutputLabels)
return cty.ObjectVal(planState), nil
}

// Provider returns additional overlaid schema and metadata associated with the gcp package.
//
//nolint:lll
func Provider() tfbridge.ProviderInfo {
p := pf.MuxShimWithDisjointgPF(
context.Background(),
shimv2.NewProvider(gcpProvider.Provider(),
shimv2.WithPlanResourceChange(func(_ string) bool { return true }),
shimv2.WithPlanResourceChange(always),
shimv2.WithPlanStateEdit(fixEmptyLabels),
),
gcpPFProvider.New())

Expand Down

0 comments on commit dceb55c

Please sign in to comment.