Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for empty expiration policies in pubsub #783

Merged
merged 1 commit into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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