Skip to content

Commit

Permalink
Create diff suppresser for maps that have a value that doesn't always…
Browse files Browse the repository at this point in the history
… come back. (#4158) (#7714)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Nov 5, 2020
1 parent 03518f4 commit b68818c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/4158.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
pubsub: Fixed permadiff on push_config.attributes.
```
24 changes: 24 additions & 0 deletions google/common_diff_suppress.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package google
import (
"crypto/sha256"
"encoding/hex"
"log"
"reflect"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions google/resource_pubsub_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions google/resource_pubsub_subscription_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/pubsub_subscription.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=pubsub_subscription_push&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Pubsub Subscription Push


Expand Down

0 comments on commit b68818c

Please sign in to comment.