Skip to content

Commit

Permalink
Add new field filter to pubsub. (hashicorp#3759)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
modular-magician and tim-codes committed Aug 11, 2020
1 parent 97f4fee commit ba0e368
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/3759.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
pubsub: added `filter` field to `google_pubsub_subscription` resource
```
26 changes: 26 additions & 0 deletions google-beta/resource_pubsub_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions google-beta/resource_pubsub_subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/pubsub_subscription.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ba0e368

Please sign in to comment.