diff --git a/.changelog/4158.txt b/.changelog/4158.txt new file mode 100644 index 00000000000..fc4d1157568 --- /dev/null +++ b/.changelog/4158.txt @@ -0,0 +1,3 @@ +```release-note:bug +pubsub: Fixed permadiff on push_config.attributes. +``` diff --git a/google/common_diff_suppress.go b/google/common_diff_suppress.go index 6190d6cac64..04abe42489d 100644 --- a/google/common_diff_suppress.go +++ b/google/common_diff_suppress.go @@ -5,7 +5,9 @@ package google import ( "crypto/sha256" "encoding/hex" + "log" "reflect" + "strconv" "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -17,6 +19,28 @@ func optionalPrefixSuppress(prefix string) schema.SchemaDiffSuppressFunc { } } +func ignoreMissingKeyInMap(key string) schema.SchemaDiffSuppressFunc { + return func(k, old, new string, d *schema.ResourceData) bool { + log.Printf("suppressing diff %q with old %q, new %q", k, old, new) + if strings.HasSuffix(k, ".%") { + oldNum, err := strconv.Atoi(old) + if err != nil { + log.Printf("[ERROR] could not parse %q as number, no longer attempting diff suppress", old) + return false + } + newNum, err := strconv.Atoi(new) + if err != nil { + log.Printf("[ERROR] could not parse %q as number, no longer attempting diff suppress", new) + return false + } + return oldNum+1 == newNum + } else if strings.HasSuffix(k, "."+key) { + return old == "" + } + return false + } +} + func optionalSurroundingSpacesSuppress(k, old, new string, d *schema.ResourceData) bool { return strings.TrimSpace(old) == strings.TrimSpace(new) } diff --git a/google/resource_pubsub_subscription.go b/google/resource_pubsub_subscription.go index 261885789ac..ba60eb832d0 100644 --- a/google/resource_pubsub_subscription.go +++ b/google/resource_pubsub_subscription.go @@ -222,8 +222,9 @@ For example, a Webhook endpoint might use "https://example.com/push".`, }, "attributes": { - Type: schema.TypeMap, - Optional: true, + Type: schema.TypeMap, + Optional: true, + DiffSuppressFunc: ignoreMissingKeyInMap("x-goog-version"), Description: `Endpoint configuration attributes. Every endpoint has a set of API supported attributes that can diff --git a/google/resource_pubsub_subscription_generated_test.go b/google/resource_pubsub_subscription_generated_test.go index 4267498ad7c..b402ad48986 100644 --- a/google/resource_pubsub_subscription_generated_test.go +++ b/google/resource_pubsub_subscription_generated_test.go @@ -23,6 +23,61 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) +func TestAccPubsubSubscription_pubsubSubscriptionPushExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + ExternalProviders: map[string]resource.ExternalProvider{ + "random": {}, + }, + CheckDestroy: testAccCheckPubsubSubscriptionDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccPubsubSubscription_pubsubSubscriptionPushExample(context), + }, + { + ResourceName: "google_pubsub_subscription.example", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"topic"}, + }, + }, + }) +} + +func testAccPubsubSubscription_pubsubSubscriptionPushExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_pubsub_topic" "example" { + name = "tf-test-example-topic%{random_suffix}" +} + +resource "google_pubsub_subscription" "example" { + name = "tf-test-example-subscription%{random_suffix}" + topic = google_pubsub_topic.example.name + + ack_deadline_seconds = 20 + + labels = { + foo = "bar" + } + + push_config { + push_endpoint = "https://example.com/push" + + attributes = { + x-goog-version = "v1" + } + } +} +`, context) +} + func TestAccPubsubSubscription_pubsubSubscriptionPullExample(t *testing.T) { t.Parallel() diff --git a/website/docs/r/pubsub_subscription.html.markdown b/website/docs/r/pubsub_subscription.html.markdown index a5fae0d2cbf..36bad62efac 100644 --- a/website/docs/r/pubsub_subscription.html.markdown +++ b/website/docs/r/pubsub_subscription.html.markdown @@ -33,6 +33,11 @@ To get more information about Subscription, see: * How-to Guides * [Managing Subscriptions](https://cloud.google.com/pubsub/docs/admin#managing_subscriptions) +
+ + Open in Cloud Shell + +
## Example Usage - Pubsub Subscription Push