diff --git a/.changelog/11556.txt b/.changelog/11556.txt new file mode 100644 index 0000000000..ab38018c14 --- /dev/null +++ b/.changelog/11556.txt @@ -0,0 +1,3 @@ +```release-note:bug +pubsub: fixed a validation bug that didn't allow empty filter definitions for `google_pubsub_subscription` resources +``` \ No newline at end of file diff --git a/google-beta/services/pubsub/resource_pubsub_subscription.go b/google-beta/services/pubsub/resource_pubsub_subscription.go index cc34a79240..292e1f3f12 100644 --- a/google-beta/services/pubsub/resource_pubsub_subscription.go +++ b/google-beta/services/pubsub/resource_pubsub_subscription.go @@ -356,7 +356,7 @@ Example - "3.5s".`, Type: schema.TypeString, Optional: true, ForceNew: true, - ValidateFunc: verify.ValidateRegexp(`^.{1,256}$`), + ValidateFunc: verify.ValidateRegexp(`^.{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 by their attributes. The maximum length of a filter is 256 bytes. After creating the subscription, diff --git a/google-beta/services/pubsub/resource_pubsub_subscription_test.go b/google-beta/services/pubsub/resource_pubsub_subscription_test.go index a4993f3e16..b0dc9c545e 100644 --- a/google-beta/services/pubsub/resource_pubsub_subscription_test.go +++ b/google-beta/services/pubsub/resource_pubsub_subscription_test.go @@ -400,6 +400,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" { @@ -798,3 +839,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) +}