From 32caeb2a9bf721d75acd79ad98e9d6cec0300d10 Mon Sep 17 00:00:00 2001 From: The Magician Date: Thu, 7 Sep 2023 13:41:55 -0400 Subject: [PATCH] Fix issue #15555: Set `send_empty_value` for noWrapper.writeMetadata (#8739) (#15758) Signed-off-by: Modular Magician Co-authored-by: Scott Suarez --- .changelog/8739.txt | 3 + .../pubsub/resource_pubsub_subscription.go | 18 +++--- .../resource_pubsub_subscription_test.go | 64 +++++++++++++++++++ 3 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 .changelog/8739.txt diff --git a/.changelog/8739.txt b/.changelog/8739.txt new file mode 100644 index 00000000000..33bd1111a9c --- /dev/null +++ b/.changelog/8739.txt @@ -0,0 +1,3 @@ +```release-note:bug +pubsub: fixed issue where setting `no_wrapper.write_metadata` to false wasn't passed to the API for `google_pubsub_subscription` +``` diff --git a/google/services/pubsub/resource_pubsub_subscription.go b/google/services/pubsub/resource_pubsub_subscription.go index 8ca9dd007f9..b3e012e91ef 100644 --- a/google/services/pubsub/resource_pubsub_subscription.go +++ b/google/services/pubsub/resource_pubsub_subscription.go @@ -1167,18 +1167,18 @@ func flattenPubsubSubscriptionPushConfigNoWrapper(v interface{}, d *schema.Resou if v == nil { return nil } + original := v.(map[string]interface{}) - if len(original) == 0 { - return nil - } transformed := make(map[string]interface{}) - transformed["write_metadata"] = - flattenPubsubSubscriptionPushConfigNoWrapperWriteMetadata(original["writeMetadata"], d, config) + + if original["writeMetadata"] == nil { + transformed["write_metadata"] = false + } else { + transformed["write_metadata"] = original["writeMetadata"] + } + return []interface{}{transformed} } -func flattenPubsubSubscriptionPushConfigNoWrapperWriteMetadata(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { - return v -} func flattenPubsubSubscriptionAckDeadlineSeconds(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { // Handles the string fixed64 format @@ -1592,7 +1592,7 @@ func expandPubsubSubscriptionPushConfigNoWrapper(v interface{}, d tpgresource.Te transformedWriteMetadata, err := expandPubsubSubscriptionPushConfigNoWrapperWriteMetadata(original["write_metadata"], d, config) if err != nil { return nil, err - } else if val := reflect.ValueOf(transformedWriteMetadata); val.IsValid() && !tpgresource.IsEmptyValue(val) { + } else { transformed["writeMetadata"] = transformedWriteMetadata } diff --git a/google/services/pubsub/resource_pubsub_subscription_test.go b/google/services/pubsub/resource_pubsub_subscription_test.go index e76fc56ae94..a6c96f1deff 100644 --- a/google/services/pubsub/resource_pubsub_subscription_test.go +++ b/google/services/pubsub/resource_pubsub_subscription_test.go @@ -145,6 +145,31 @@ func TestAccPubsubSubscription_pushNoWrapper(t *testing.T) { }) } +func TestAccPubsubSubscription_pushNoWrapperEmpty(t *testing.T) { + t.Parallel() + + topicFoo := fmt.Sprintf("tf-test-topic-foo-%s", acctest.RandString(t, 10)) + subscription := fmt.Sprintf("tf-test-sub-foo-%s", acctest.RandString(t, 10)) + saAccount := fmt.Sprintf("tf-test-pubsub-%s", acctest.RandString(t, 10)) + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckPubsubSubscriptionDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccPubsubSubscription_pushNoWrapperEmpty(topicFoo, saAccount, subscription), + }, + { + ResourceName: "google_pubsub_subscription.foo", + ImportStateId: subscription, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + // Context: hashicorp/terraform-provider-google#4993 // This test makes a call to GET an subscription before it is actually created. // The PubSub API negative-caches responses so this tests we are @@ -279,6 +304,45 @@ resource "google_pubsub_subscription" "foo" { `, saAccount, topicFoo, subscription) } +func testAccPubsubSubscription_pushNoWrapperEmpty(topicFoo, saAccount, subscription string) string { + return fmt.Sprintf(` +data "google_project" "project" { } + +resource "google_service_account" "pub_sub_service_account" { + account_id = "%s" +} + +data "google_iam_policy" "admin" { + binding { + role = "roles/projects.topics.publish" + + members = [ + "serviceAccount:${google_service_account.pub_sub_service_account.email}", + ] + } +} + +resource "google_pubsub_topic" "foo" { + name = "%s" +} + +resource "google_pubsub_subscription" "foo" { + name = "%s" + topic = google_pubsub_topic.foo.name + ack_deadline_seconds = 10 + push_config { + push_endpoint = "https://${data.google_project.project.project_id}.appspot.com" + oidc_token { + service_account_email = google_service_account.pub_sub_service_account.email + } + no_wrapper { + write_metadata = false + } + } +} +`, saAccount, topicFoo, subscription) +} + func testAccPubsubSubscription_basic(topic, subscription, label string, deadline int, exactlyOnceDelivery bool) string { return fmt.Sprintf(` resource "google_pubsub_topic" "foo" {