Skip to content

Commit 221ca3d

Browse files
Add new field filter to pubsub. (#3759) (#2367)
* Add new field filter to pubsub. Fixes: hashicorp/terraform-provider-google#6727 * Fixed filter name, it was improperly set. * add filter key to pubsub subscription unit test * spaces not tabs! * hardcode filter value in test * revert remove escaped quotes Co-authored-by: Tim O'Connell <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Tim O'Connell <[email protected]>
1 parent 97f4fee commit 221ca3d

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

.changelog/3759.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
pubsub: added `filter` field to `google_pubsub_subscription` resource
3+
```

google-beta/resource_pubsub_subscription.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ Example - "3.5s".`,
168168
},
169169
},
170170
},
171+
"filter": {
172+
Type: schema.TypeString,
173+
Optional: true,
174+
ForceNew: true,
175+
Description: `The subscription only delivers the messages that match the filter.
176+
Pub/Sub automatically acknowledges the messages that don't match the filter. You can filter messages
177+
by their attributes. The maximum length of a filter is 256 bytes. After creating the subscription,
178+
you can't modify the filter.`,
179+
},
171180
"labels": {
172181
Type: schema.TypeMap,
173182
Optional: true,
@@ -338,6 +347,12 @@ func resourcePubsubSubscriptionCreate(d *schema.ResourceData, meta interface{})
338347
} else if v, ok := d.GetOkExists("expiration_policy"); ok || !reflect.DeepEqual(v, expirationPolicyProp) {
339348
obj["expirationPolicy"] = expirationPolicyProp
340349
}
350+
filterProp, err := expandPubsubSubscriptionFilter(d.Get("filter"), d, config)
351+
if err != nil {
352+
return err
353+
} else if v, ok := d.GetOkExists("filter"); !isEmptyValue(reflect.ValueOf(filterProp)) && (ok || !reflect.DeepEqual(v, filterProp)) {
354+
obj["filter"] = filterProp
355+
}
341356
deadLetterPolicyProp, err := expandPubsubSubscriptionDeadLetterPolicy(d.Get("dead_letter_policy"), d, config)
342357
if err != nil {
343358
return err
@@ -472,6 +487,9 @@ func resourcePubsubSubscriptionRead(d *schema.ResourceData, meta interface{}) er
472487
if err := d.Set("expiration_policy", flattenPubsubSubscriptionExpirationPolicy(res["expirationPolicy"], d, config)); err != nil {
473488
return fmt.Errorf("Error reading Subscription: %s", err)
474489
}
490+
if err := d.Set("filter", flattenPubsubSubscriptionFilter(res["filter"], d, config)); err != nil {
491+
return fmt.Errorf("Error reading Subscription: %s", err)
492+
}
475493
if err := d.Set("dead_letter_policy", flattenPubsubSubscriptionDeadLetterPolicy(res["deadLetterPolicy"], d, config)); err != nil {
476494
return fmt.Errorf("Error reading Subscription: %s", err)
477495
}
@@ -738,6 +756,10 @@ func flattenPubsubSubscriptionExpirationPolicyTtl(v interface{}, d *schema.Resou
738756
return v
739757
}
740758

759+
func flattenPubsubSubscriptionFilter(v interface{}, d *schema.ResourceData, config *Config) interface{} {
760+
return v
761+
}
762+
741763
func flattenPubsubSubscriptionDeadLetterPolicy(v interface{}, d *schema.ResourceData, config *Config) interface{} {
742764
if v == nil {
743765
return nil
@@ -931,6 +953,10 @@ func expandPubsubSubscriptionExpirationPolicyTtl(v interface{}, d TerraformResou
931953
return v, nil
932954
}
933955

956+
func expandPubsubSubscriptionFilter(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
957+
return v, nil
958+
}
959+
934960
func expandPubsubSubscriptionDeadLetterPolicy(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
935961
l := v.([]interface{})
936962
if len(l) == 0 || l[0] == nil {

google-beta/resource_pubsub_subscription_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,9 @@ resource "google_pubsub_topic" "foo" {
219219
}
220220
221221
resource "google_pubsub_subscription" "foo" {
222-
name = "%s"
223-
topic = google_pubsub_topic.foo.id
222+
name = "%s"
223+
topic = google_pubsub_topic.foo.id
224+
filter = "attributes.foo = \"bar\""
224225
labels = {
225226
foo = "%s"
226227
}

website/docs/r/pubsub_subscription.html.markdown

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ The following arguments are supported:
210210
is 1 day.
211211
Structure is documented below.
212212

213+
* `filter` -
214+
(Optional)
215+
The subscription only delivers the messages that match the filter.
216+
Pub/Sub automatically acknowledges the messages that don't match the filter. You can filter messages
217+
by their attributes. The maximum length of a filter is 256 bytes. After creating the subscription,
218+
you can't modify the filter.
219+
213220
* `dead_letter_policy` -
214221
(Optional)
215222
A policy that specifies the conditions for dead lettering messages in

0 commit comments

Comments
 (0)