diff --git a/.changelog/3759.txt b/.changelog/3759.txt new file mode 100644 index 00000000000..8df19d57ed5 --- /dev/null +++ b/.changelog/3759.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +pubsub: added `filter` field to `google_pubsub_subscription` resource +``` diff --git a/google/resource_pubsub_subscription.go b/google/resource_pubsub_subscription.go index b8c1e787f29..065c5507985 100644 --- a/google/resource_pubsub_subscription.go +++ b/google/resource_pubsub_subscription.go @@ -168,6 +168,15 @@ Example - "3.5s".`, }, }, }, + "filter": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + 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, +you can't modify the filter.`, + }, "labels": { Type: schema.TypeMap, Optional: true, @@ -338,6 +347,12 @@ func resourcePubsubSubscriptionCreate(d *schema.ResourceData, meta interface{}) } else if v, ok := d.GetOkExists("expiration_policy"); ok || !reflect.DeepEqual(v, expirationPolicyProp) { obj["expirationPolicy"] = expirationPolicyProp } + filterProp, err := expandPubsubSubscriptionFilter(d.Get("filter"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("filter"); !isEmptyValue(reflect.ValueOf(filterProp)) && (ok || !reflect.DeepEqual(v, filterProp)) { + obj["filter"] = filterProp + } deadLetterPolicyProp, err := expandPubsubSubscriptionDeadLetterPolicy(d.Get("dead_letter_policy"), d, config) if err != nil { return err @@ -472,6 +487,9 @@ func resourcePubsubSubscriptionRead(d *schema.ResourceData, meta interface{}) er if err := d.Set("expiration_policy", flattenPubsubSubscriptionExpirationPolicy(res["expirationPolicy"], d, config)); err != nil { return fmt.Errorf("Error reading Subscription: %s", err) } + if err := d.Set("filter", flattenPubsubSubscriptionFilter(res["filter"], d, config)); err != nil { + return fmt.Errorf("Error reading Subscription: %s", err) + } if err := d.Set("dead_letter_policy", flattenPubsubSubscriptionDeadLetterPolicy(res["deadLetterPolicy"], d, config)); err != nil { return fmt.Errorf("Error reading Subscription: %s", err) } @@ -738,6 +756,10 @@ func flattenPubsubSubscriptionExpirationPolicyTtl(v interface{}, d *schema.Resou return v } +func flattenPubsubSubscriptionFilter(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + func flattenPubsubSubscriptionDeadLetterPolicy(v interface{}, d *schema.ResourceData, config *Config) interface{} { if v == nil { return nil @@ -931,6 +953,10 @@ func expandPubsubSubscriptionExpirationPolicyTtl(v interface{}, d TerraformResou return v, nil } +func expandPubsubSubscriptionFilter(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + func expandPubsubSubscriptionDeadLetterPolicy(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { l := v.([]interface{}) if len(l) == 0 || l[0] == nil { diff --git a/google/resource_pubsub_subscription_test.go b/google/resource_pubsub_subscription_test.go index 4ba1a4c3536..272651b6483 100644 --- a/google/resource_pubsub_subscription_test.go +++ b/google/resource_pubsub_subscription_test.go @@ -219,8 +219,9 @@ resource "google_pubsub_topic" "foo" { } resource "google_pubsub_subscription" "foo" { - name = "%s" - topic = google_pubsub_topic.foo.id + name = "%s" + topic = google_pubsub_topic.foo.id + filter = "attributes.foo = \"bar\"" labels = { foo = "%s" } diff --git a/website/docs/r/pubsub_subscription.html.markdown b/website/docs/r/pubsub_subscription.html.markdown index 12fd4558931..abdc28508c5 100644 --- a/website/docs/r/pubsub_subscription.html.markdown +++ b/website/docs/r/pubsub_subscription.html.markdown @@ -210,6 +210,13 @@ The following arguments are supported: is 1 day. Structure is documented below. +* `filter` - + (Optional) + 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, + you can't modify the filter. + * `dead_letter_policy` - (Optional) A policy that specifies the conditions for dead lettering messages in