Skip to content

Commit

Permalink
Add support for empty expiration policies in pubsub (#783)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and rileykarson committed May 29, 2019
1 parent e76adf1 commit f44be32
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
16 changes: 10 additions & 6 deletions google-beta/resource_pubsub_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func resourcePubsubSubscriptionCreate(d *schema.ResourceData, meta interface{})
expirationPolicyProp, err := expandPubsubSubscriptionExpirationPolicy(d.Get("expiration_policy"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("expiration_policy"); !isEmptyValue(reflect.ValueOf(expirationPolicyProp)) && (ok || !reflect.DeepEqual(v, expirationPolicyProp)) {
} else if v, ok := d.GetOkExists("expiration_policy"); ok || !reflect.DeepEqual(v, expirationPolicyProp) {
obj["expirationPolicy"] = expirationPolicyProp
}

Expand Down Expand Up @@ -301,7 +301,7 @@ func resourcePubsubSubscriptionUpdate(d *schema.ResourceData, meta interface{})
expirationPolicyProp, err := expandPubsubSubscriptionExpirationPolicy(d.Get("expiration_policy"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("expiration_policy"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, expirationPolicyProp)) {
} else if v, ok := d.GetOkExists("expiration_policy"); ok || !reflect.DeepEqual(v, expirationPolicyProp) {
obj["expirationPolicy"] = expirationPolicyProp
}

Expand Down Expand Up @@ -455,9 +455,6 @@ func flattenPubsubSubscriptionExpirationPolicy(v interface{}, d *schema.Resource
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["ttl"] =
flattenPubsubSubscriptionExpirationPolicyTtl(original["ttl"], d)
Expand Down Expand Up @@ -576,9 +573,14 @@ func expandPubsubSubscriptionRetainAckedMessages(v interface{}, d TerraformResou

func expandPubsubSubscriptionExpirationPolicy(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
if len(l) == 0 {
return nil, nil
}

if l[0] == nil {
transformed := make(map[string]interface{})
return transformed, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})
Expand All @@ -605,7 +607,9 @@ func resourcePubsubSubscriptionUpdateEncoder(d *schema.ResourceData, meta interf

func resourcePubsubSubscriptionDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {

// path is a derived field from the API-side `name`
path := fmt.Sprintf("projects/%s/subscriptions/%s", d.Get("project"), d.Get("name"))
d.Set("path", path)

return res, nil
}
45 changes: 44 additions & 1 deletion google-beta/resource_pubsub_subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ import (
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccPubsubSubscription_emptyTTL(t *testing.T) {
t.Parallel()

topic := fmt.Sprintf("tf-test-topic-%s", acctest.RandString(10))
subscription := fmt.Sprintf("projects/%s/subscriptions/tf-test-sub-%s", getTestProjectFromEnv(), acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPubsubSubscriptionDestroy,
Steps: []resource.TestStep{
{
Config: testAccPubsubSubscription_emptyTTL(topic, subscription),
},
{
ResourceName: "google_pubsub_subscription.foo",
ImportStateId: subscription,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccPubsubSubscription_fullName(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -81,6 +105,24 @@ func TestAccPubsubSubscription_update(t *testing.T) {
})
}

func testAccPubsubSubscription_emptyTTL(topic, subscription string) string {
return fmt.Sprintf(`
resource "google_pubsub_topic" "foo" {
name = "%s"
}
resource "google_pubsub_subscription" "foo" {
name = "%s"
topic = "${google_pubsub_topic.foo.id}"
message_retention_duration = "1200s"
retain_acked_messages = true
ack_deadline_seconds = 20
expiration_policy {}
}
`, topic, subscription)
}

// TODO: Add acceptance test for push delivery.
//
// Testing push endpoints is tricky for the following reason:
Expand All @@ -107,7 +149,8 @@ resource "google_pubsub_subscription" "foo" {
foo = "%s"
}
ack_deadline_seconds = %d
}`, topic, subscription, label, deadline)
}
`, topic, subscription, label, deadline)
}

func TestGetComputedTopicName(t *testing.T) {
Expand Down

0 comments on commit f44be32

Please sign in to comment.