From 5ee74bfb71134cf9ce52e1ba5b8bceb56d264027 Mon Sep 17 00:00:00 2001 From: The Magician Date: Mon, 17 Aug 2020 09:37:46 -0700 Subject: [PATCH] Add enableMessageOrdering to Pub/Sub Subscription (#3872) (#7039) Add enableMessageOrdering to Pub/Sub Subscription Signed-off-by: Modular Magician --- .changelog/3872.txt | 3 ++ google/resource_pubsub_subscription.go | 34 +++++++++++++++++++ ...urce_pubsub_subscription_generated_test.go | 2 ++ google/resource_pubsub_subscription_test.go | 1 + .../docs/r/pubsub_subscription.html.markdown | 8 +++++ 5 files changed, 48 insertions(+) create mode 100644 .changelog/3872.txt diff --git a/.changelog/3872.txt b/.changelog/3872.txt new file mode 100644 index 00000000000..a2024de037f --- /dev/null +++ b/.changelog/3872.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +pubsub: Added `enable_message_ordering` support to `google_pubsub_subscription` +``` diff --git a/google/resource_pubsub_subscription.go b/google/resource_pubsub_subscription.go index 065c5507985..b0fc007b715 100644 --- a/google/resource_pubsub_subscription.go +++ b/google/resource_pubsub_subscription.go @@ -141,6 +141,13 @@ If this parameter is 0, a default value of 5 is used.`, }, }, }, + "enable_message_ordering": { + Type: schema.TypeBool, + Optional: true, + Description: `If 'true', messages published with the same orderingKey in PubsubMessage will be delivered to +the subscribers in the order in which they are received by the Pub/Sub system. Otherwise, they +may be delivered in any order.`, + }, "expiration_policy": { Type: schema.TypeList, Computed: true, @@ -359,6 +366,12 @@ func resourcePubsubSubscriptionCreate(d *schema.ResourceData, meta interface{}) } else if v, ok := d.GetOkExists("dead_letter_policy"); ok || !reflect.DeepEqual(v, deadLetterPolicyProp) { obj["deadLetterPolicy"] = deadLetterPolicyProp } + enableMessageOrderingProp, err := expandPubsubSubscriptionEnableMessageOrdering(d.Get("enable_message_ordering"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("enable_message_ordering"); !isEmptyValue(reflect.ValueOf(enableMessageOrderingProp)) && (ok || !reflect.DeepEqual(v, enableMessageOrderingProp)) { + obj["enableMessageOrdering"] = enableMessageOrderingProp + } obj, err = resourcePubsubSubscriptionEncoder(d, meta, obj) if err != nil { @@ -493,6 +506,9 @@ func resourcePubsubSubscriptionRead(d *schema.ResourceData, meta interface{}) er if err := d.Set("dead_letter_policy", flattenPubsubSubscriptionDeadLetterPolicy(res["deadLetterPolicy"], d, config)); err != nil { return fmt.Errorf("Error reading Subscription: %s", err) } + if err := d.Set("enable_message_ordering", flattenPubsubSubscriptionEnableMessageOrdering(res["enableMessageOrdering"], d, config)); err != nil { + return fmt.Errorf("Error reading Subscription: %s", err) + } return nil } @@ -548,6 +564,12 @@ func resourcePubsubSubscriptionUpdate(d *schema.ResourceData, meta interface{}) } else if v, ok := d.GetOkExists("dead_letter_policy"); ok || !reflect.DeepEqual(v, deadLetterPolicyProp) { obj["deadLetterPolicy"] = deadLetterPolicyProp } + enableMessageOrderingProp, err := expandPubsubSubscriptionEnableMessageOrdering(d.Get("enable_message_ordering"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("enable_message_ordering"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, enableMessageOrderingProp)) { + obj["enableMessageOrdering"] = enableMessageOrderingProp + } obj, err = resourcePubsubSubscriptionUpdateEncoder(d, meta, obj) if err != nil { @@ -589,6 +611,10 @@ func resourcePubsubSubscriptionUpdate(d *schema.ResourceData, meta interface{}) if d.HasChange("dead_letter_policy") { updateMask = append(updateMask, "deadLetterPolicy") } + + if d.HasChange("enable_message_ordering") { + updateMask = append(updateMask, "enableMessageOrdering") + } // updateMask is a URL parameter but not present in the schema, so replaceVars // won't set it url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) @@ -796,6 +822,10 @@ func flattenPubsubSubscriptionDeadLetterPolicyMaxDeliveryAttempts(v interface{}, return v // let terraform core handle it otherwise } +func flattenPubsubSubscriptionEnableMessageOrdering(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + func expandPubsubSubscriptionName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { return replaceVars(d, config, "projects/{{project}}/subscriptions/{{name}}") } @@ -991,6 +1021,10 @@ func expandPubsubSubscriptionDeadLetterPolicyMaxDeliveryAttempts(v interface{}, return v, nil } +func expandPubsubSubscriptionEnableMessageOrdering(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + func resourcePubsubSubscriptionEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) { delete(obj, "name") return obj, nil diff --git a/google/resource_pubsub_subscription_generated_test.go b/google/resource_pubsub_subscription_generated_test.go index 826ce12ce99..405ad7f2d89 100644 --- a/google/resource_pubsub_subscription_generated_test.go +++ b/google/resource_pubsub_subscription_generated_test.go @@ -70,6 +70,8 @@ resource "google_pubsub_subscription" "example" { expiration_policy { ttl = "300000.5s" } + + enable_message_ordering = false } `, context) } diff --git a/google/resource_pubsub_subscription_test.go b/google/resource_pubsub_subscription_test.go index 272651b6483..3938ab3bd47 100644 --- a/google/resource_pubsub_subscription_test.go +++ b/google/resource_pubsub_subscription_test.go @@ -172,6 +172,7 @@ resource "google_pubsub_subscription" "foo" { expiration_policy { ttl = "" } + enable_message_ordering = false } `, topic, subscription) } diff --git a/website/docs/r/pubsub_subscription.html.markdown b/website/docs/r/pubsub_subscription.html.markdown index abdc28508c5..001b55d9833 100644 --- a/website/docs/r/pubsub_subscription.html.markdown +++ b/website/docs/r/pubsub_subscription.html.markdown @@ -90,6 +90,8 @@ resource "google_pubsub_subscription" "example" { expiration_policy { ttl = "300000.5s" } + + enable_message_ordering = false } ``` ## Example Usage - Pubsub Subscription Different Project @@ -228,6 +230,12 @@ The following arguments are supported: permission to Acknowledge() messages on this subscription. Structure is documented below. +* `enable_message_ordering` - + (Optional) + If `true`, messages published with the same orderingKey in PubsubMessage will be delivered to + the subscribers in the order in which they are received by the Pub/Sub system. Otherwise, they + may be delivered in any order. + * `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used.