From 24c2579a0de9d2dfa32d81d3e12127f775ce3c60 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Mon, 30 Mar 2020 18:42:57 +0000 Subject: [PATCH] Add deadLetterPolicy to Pub/Sub Subscription resource (#3305) * Add deadLetterPolicy to Pub/Sub subscription resource * fix: disable allow_empty_objects, fix docstring, add example * fix: set max_delivery_attempts * fix: block and topic name Signed-off-by: Modular Magician --- google/pubsub_subscription.go | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/google/pubsub_subscription.go b/google/pubsub_subscription.go index 8ccd3296b..057e545a1 100644 --- a/google/pubsub_subscription.go +++ b/google/pubsub_subscription.go @@ -106,6 +106,12 @@ func GetPubsubSubscriptionApiObject(d TerraformResourceData, config *Config) (ma } else if v, ok := d.GetOkExists("expiration_policy"); ok || !reflect.DeepEqual(v, expirationPolicyProp) { obj["expirationPolicy"] = expirationPolicyProp } + deadLetterPolicyProp, err := expandPubsubSubscriptionDeadLetterPolicy(d.Get("dead_letter_policy"), d, config) + if err != nil { + return nil, err + } else if v, ok := d.GetOkExists("dead_letter_policy"); ok || !reflect.DeepEqual(v, deadLetterPolicyProp) { + obj["deadLetterPolicy"] = deadLetterPolicyProp + } return resourcePubsubSubscriptionEncoder(d, config, obj) } @@ -271,3 +277,37 @@ func expandPubsubSubscriptionExpirationPolicy(v interface{}, d TerraformResource func expandPubsubSubscriptionExpirationPolicyTtl(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 { + return nil, nil + } + raw := l[0] + original := raw.(map[string]interface{}) + transformed := make(map[string]interface{}) + + transformedDeadLetterTopic, err := expandPubsubSubscriptionDeadLetterPolicyDeadLetterTopic(original["dead_letter_topic"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedDeadLetterTopic); val.IsValid() && !isEmptyValue(val) { + transformed["deadLetterTopic"] = transformedDeadLetterTopic + } + + transformedMaxDeliveryAttempts, err := expandPubsubSubscriptionDeadLetterPolicyMaxDeliveryAttempts(original["max_delivery_attempts"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedMaxDeliveryAttempts); val.IsValid() && !isEmptyValue(val) { + transformed["maxDeliveryAttempts"] = transformedMaxDeliveryAttempts + } + + return transformed, nil +} + +func expandPubsubSubscriptionDeadLetterPolicyDeadLetterTopic(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandPubsubSubscriptionDeadLetterPolicyMaxDeliveryAttempts(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +}