From bfe2eb8b7dc2ee2722c64d609c0d70936e932cbb Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Thu, 7 Sep 2023 17:41:42 +0000 Subject: [PATCH] Fix issue #15555: Set `send_empty_value` for noWrapper.writeMetadata (#8739) Co-authored-by: Scott Suarez Signed-off-by: Modular Magician --- .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 0000000000..33bd1111a9 --- /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-beta/services/pubsub/resource_pubsub_subscription.go b/google-beta/services/pubsub/resource_pubsub_subscription.go index 2498adbc3a..c0473771ad 100644 --- a/google-beta/services/pubsub/resource_pubsub_subscription.go +++ b/google-beta/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-beta/services/pubsub/resource_pubsub_subscription_test.go b/google-beta/services/pubsub/resource_pubsub_subscription_test.go index 9db99f61c8..735a7478f0 100644 --- a/google-beta/services/pubsub/resource_pubsub_subscription_test.go +++ b/google-beta/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" {