From f5ae22e34495c7e2d439594854198937b17107a1 Mon Sep 17 00:00:00 2001 From: Patrick Rauchfuss Date: Tue, 27 Aug 2024 19:31:13 +0200 Subject: [PATCH] `pubsub`: allow empty filter definition (#11556) Co-authored-by: Sarah French <15078782+SarahFrench@users.noreply.github.com> --- mmv1/products/pubsub/Subscription.yaml | 2 +- .../resource_pubsub_subscription_test.go | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/mmv1/products/pubsub/Subscription.yaml b/mmv1/products/pubsub/Subscription.yaml index 6f4126c4e1fc..ed39701ebc28 100644 --- a/mmv1/products/pubsub/Subscription.yaml +++ b/mmv1/products/pubsub/Subscription.yaml @@ -388,7 +388,7 @@ properties: name: 'filter' required: false validation: !ruby/object:Provider::Terraform::Validation - regex: '^.{1,256}$' + regex: '^.{0,256}$' description: | The subscription only delivers the messages that match the filter. Pub/Sub automatically acknowledges the messages that don't match the filter. You can filter messages 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 9b695d2e6974..fa98e1d0d51f 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 @@ -398,6 +398,47 @@ func TestUnitPubsubSubscription_IgnoreMissingKeyInMap(t *testing.T) { } } +func TestAccPubsubSubscription_filter(t *testing.T) { + t.Parallel() + + topic := fmt.Sprintf("tf-test-topic-%s", acctest.RandString(t, 10)) + subscriptionShort := fmt.Sprintf("tf-test-sub-%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_filter(topic, subscriptionShort, "attributes.foo = \\\"bar\\\""), + Check: resource.ComposeTestCheckFunc( + // Test schema + resource.TestCheckResourceAttr("google_pubsub_subscription.foo", "filter", "attributes.foo = \"bar\""), + ), + }, + { + ResourceName: "google_pubsub_subscription.foo", + ImportStateId: subscriptionShort, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccPubsubSubscription_filter(topic, subscriptionShort, ""), + Check: resource.ComposeTestCheckFunc( + // Test schema + resource.TestCheckResourceAttr("google_pubsub_subscription.foo", "filter", ""), + ), + }, + { + ResourceName: "google_pubsub_subscription.foo", + ImportStateId: subscriptionShort, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccPubsubSubscription_emptyTTL(topic, subscription string) string { return fmt.Sprintf(` resource "google_pubsub_topic" "foo" { @@ -796,3 +837,17 @@ func testAccCheckPubsubSubscriptionCache404(t *testing.T, subName string) resour return nil } } + +func testAccPubsubSubscription_filter(topic, subscription, filter string) string { + return fmt.Sprintf(` +resource "google_pubsub_topic" "foo" { + name = "%s" +} + +resource "google_pubsub_subscription" "foo" { + name = "%s" + topic = google_pubsub_topic.foo.id + filter = "%s" +} +`, topic, subscription, filter) +}