Skip to content

Commit

Permalink
Fix issue #15555: Set send_empty_value for noWrapper.writeMetadata (G…
Browse files Browse the repository at this point in the history
…oogleCloudPlatform#8739)

Co-authored-by: Scott Suarez <[email protected]>
  • Loading branch information
2 people authored and joelkattapuram committed Sep 20, 2023
1 parent 82cec50 commit 5f35692
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mmv1/products/pubsub/Subscription.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,15 @@ 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.
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-<KEY>:<VAL>` headers of the HTTP request. Writes the
Expand Down
Original file line number Diff line number Diff line change
@@ -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}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit 5f35692

Please sign in to comment.