From d3295048eac1618566ddce9c277cb0c82b7b8966 Mon Sep 17 00:00:00 2001 From: Ty Larrabee Date: Fri, 3 May 2019 17:54:54 +0000 Subject: [PATCH] Add expiration policy to pubsub subscription resource Signed-off-by: Modular Magician --- google/pubsub_subscription.go | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/google/pubsub_subscription.go b/google/pubsub_subscription.go index 346531a2c..b58fc5f7f 100644 --- a/google/pubsub_subscription.go +++ b/google/pubsub_subscription.go @@ -18,8 +18,23 @@ import ( "fmt" "reflect" "regexp" + "strings" + + "github.com/hashicorp/terraform/helper/schema" ) +func comparePubsubSubscriptionExpirationPolicy(_, old, new string, _ *schema.ResourceData) bool { + trimmedNew := strings.TrimLeft(new, "0") + trimmedOld := strings.TrimLeft(old, "0") + if strings.Contains(trimmedNew, ".") { + trimmedNew = strings.TrimRight(strings.TrimSuffix(trimmedNew, "s"), "0") + "s" + } + if strings.Contains(trimmedOld, ".") { + trimmedOld = strings.TrimRight(strings.TrimSuffix(trimmedOld, "s"), "0") + "s" + } + return trimmedNew == trimmedOld +} + func GetPubsubSubscriptionCaiObject(d TerraformResourceData, config *Config) (Asset, error) { name, err := assetName(d, config, "//pubsub.googleapis.com/projects/{{project}}/subscriptions/{{name}}") if err != nil { @@ -85,6 +100,12 @@ func GetPubsubSubscriptionApiObject(d TerraformResourceData, config *Config) (ma } else if v, ok := d.GetOkExists("retain_acked_messages"); !isEmptyValue(reflect.ValueOf(retainAckedMessagesProp)) && (ok || !reflect.DeepEqual(v, retainAckedMessagesProp)) { obj["retainAckedMessages"] = retainAckedMessagesProp } + expirationPolicyProp, err := expandPubsubSubscriptionExpirationPolicy(d.Get("expiration_policy"), d, config) + if err != nil { + return nil, err + } else if v, ok := d.GetOkExists("expiration_policy"); !isEmptyValue(reflect.ValueOf(expirationPolicyProp)) && (ok || !reflect.DeepEqual(v, expirationPolicyProp)) { + obj["expirationPolicy"] = expirationPolicyProp + } return obj, nil } @@ -195,3 +216,26 @@ func expandPubsubSubscriptionMessageRetentionDuration(v interface{}, d Terraform func expandPubsubSubscriptionRetainAckedMessages(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { return v, nil } + +func expandPubsubSubscriptionExpirationPolicy(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{}) + + transformedTtl, err := expandPubsubSubscriptionExpirationPolicyTtl(original["ttl"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedTtl); val.IsValid() && !isEmptyValue(val) { + transformed["ttl"] = transformedTtl + } + + return transformed, nil +} + +func expandPubsubSubscriptionExpirationPolicyTtl(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +}