From 5f35692e318c61e2fad9bb76fa5b2a4d061fc007 Mon Sep 17 00:00:00 2001 From: Casey O'Hare Date: Thu, 7 Sep 2023 13:38:29 -0400 Subject: [PATCH] Fix issue #15555: Set `send_empty_value` for noWrapper.writeMetadata (#8739) Co-authored-by: Scott Suarez --- mmv1/products/pubsub/Subscription.yaml | 2 + ...b_no_wrapper_write_metadata_flatten.go.erb | 30 +++++++++ .../resource_pubsub_subscription_test.go | 64 +++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 mmv1/templates/terraform/custom_flatten/pubsub_no_wrapper_write_metadata_flatten.go.erb diff --git a/mmv1/products/pubsub/Subscription.yaml b/mmv1/products/pubsub/Subscription.yaml index 9f53faa1c286..2f40689d689c 100644 --- a/mmv1/products/pubsub/Subscription.yaml +++ b/mmv1/products/pubsub/Subscription.yaml @@ -262,6 +262,7 @@ properties: diff_suppress_func: 'tpgresource.IgnoreMissingKeyInMap("x-goog-version")' - !ruby/object:Api::Type::NestedObject name: 'noWrapper' + custom_flatten: 'templates/terraform/custom_flatten/pubsub_no_wrapper_write_metadata_flatten.go.erb' description: | When set, the payload to the push endpoint is not wrapped.Sets the `data` field as the HTTP body for delivery. @@ -269,6 +270,7 @@ properties: - !ruby/object:Api::Type::Boolean name: 'writeMetadata' required: true + send_empty_value: true description: | When true, writes the Pub/Sub message metadata to `x-goog-pubsub-:` headers of the HTTP request. Writes the diff --git a/mmv1/templates/terraform/custom_flatten/pubsub_no_wrapper_write_metadata_flatten.go.erb b/mmv1/templates/terraform/custom_flatten/pubsub_no_wrapper_write_metadata_flatten.go.erb new file mode 100644 index 000000000000..19f0d71ab1b6 --- /dev/null +++ b/mmv1/templates/terraform/custom_flatten/pubsub_no_wrapper_write_metadata_flatten.go.erb @@ -0,0 +1,30 @@ +<%# The license inside this block applies to this file. + # Copyright 2023 Google Inc. + # Licensed under the Apache License, Version 2.0 (the "License"); + # you may not use this file except in compliance with the License. + # You may obtain a copy of the License at + # + # http://www.apache.org/licenses/LICENSE-2.0 + # + # Unless required by applicable law or agreed to in writing, software + # distributed under the License is distributed on an "AS IS" BASIS, + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + # See the License for the specific language governing permissions and + # limitations under the License. +-%> +func flatten<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + if v == nil { + return nil + } + + original := v.(map[string]interface{}) + transformed := make(map[string]interface{}) + + if original["writeMetadata"] == nil { + transformed["write_metadata"] = false + } else { + transformed["write_metadata"] = original["writeMetadata"] + } + + return []interface{}{transformed} +} diff --git a/mmv1/third_party/terraform/services/pubsub/resource_pubsub_subscription_test.go b/mmv1/third_party/terraform/services/pubsub/resource_pubsub_subscription_test.go index ddaceecd7bb7..5ac29b2b44b3 100644 --- a/mmv1/third_party/terraform/services/pubsub/resource_pubsub_subscription_test.go +++ b/mmv1/third_party/terraform/services/pubsub/resource_pubsub_subscription_test.go @@ -143,6 +143,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 @@ -277,6 +302,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" {