Skip to content

Commit

Permalink
Add expiration policy to pubsub subscription resource
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
Ty Larrabee authored and modular-magician committed May 3, 2019
1 parent b6c460f commit d329504
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions google/pubsub_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit d329504

Please sign in to comment.