Skip to content

Commit 335b2ce

Browse files
committed
commit
1 parent 4475c5d commit 335b2ce

5 files changed

Lines changed: 181 additions & 95 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package finder
2+
3+
import (
4+
"github.com/aws/aws-sdk-go/aws"
5+
"github.com/aws/aws-sdk-go/service/sns"
6+
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
7+
)
8+
9+
func SubscriptionByARN(conn *sns.SNS, id string) (*sns.GetSubscriptionAttributesOutput, error) {
10+
output, err := conn.GetSubscriptionAttributes(&sns.GetSubscriptionAttributesInput{
11+
SubscriptionArn: aws.String(id),
12+
})
13+
if tfawserr.ErrCodeEquals(err, sns.ErrCodeNotFoundException) {
14+
return nil, nil
15+
}
16+
17+
if err != nil {
18+
return nil, err
19+
}
20+
21+
if output == nil || output.Attributes == nil || len(output.Attributes) == 0 {
22+
return nil, nil
23+
}
24+
25+
return output, nil
26+
}

aws/internal/service/sns/waiter/status.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,13 @@ package waiter
33
import (
44
"github.com/aws/aws-sdk-go/aws"
55
"github.com/aws/aws-sdk-go/service/sns"
6-
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
76
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/sns/finder"
88
)
99

1010
func SubscriptionPendingConfirmation(conn *sns.SNS, id string) resource.StateRefreshFunc {
1111
return func() (interface{}, string, error) {
12-
output, err := conn.GetSubscriptionAttributes(&sns.GetSubscriptionAttributesInput{
13-
SubscriptionArn: aws.String(id),
14-
})
15-
16-
if tfawserr.ErrCodeEquals(err, sns.ErrCodeResourceNotFoundException) {
17-
return nil, "", nil
18-
}
19-
12+
output, err := finder.SubscriptionByARN(conn, id)
2013
if err != nil {
2114
return nil, "", err
2215
}

aws/internal/service/sns/waiter/waiter.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
const (
1111
SubscriptionPendingConfirmationTimeout = 2 * time.Minute
12+
SubscriptionDeleteTimeout = 2 * time.Minute
1213
)
1314

1415
func SubscriptionConfirmed(conn *sns.SNS, id, expectedValue string, timeout time.Duration) (*sns.GetSubscriptionAttributesOutput, error) {
@@ -26,3 +27,20 @@ func SubscriptionConfirmed(conn *sns.SNS, id, expectedValue string, timeout time
2627

2728
return nil, err
2829
}
30+
31+
func SubscriptionDeleted(conn *sns.SNS, id string) (*sns.GetSubscriptionAttributesOutput, error) {
32+
stateConf := &resource.StateChangeConf{
33+
Pending: []string{"false", "true"},
34+
Target: []string{},
35+
Refresh: SubscriptionPendingConfirmation(conn, id),
36+
Timeout: SubscriptionDeleteTimeout,
37+
}
38+
39+
outputRaw, err := stateConf.WaitForState()
40+
41+
if output, ok := outputRaw.(*sns.GetSubscriptionAttributesOutput); ok {
42+
return output, err
43+
}
44+
45+
return nil, err
46+
}

aws/resource_aws_sns_topic_subscription.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1616
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
1717
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
18+
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/sns/finder"
1819
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/sns/waiter"
1920
)
2021

@@ -104,13 +105,15 @@ func resourceAwsSnsTopicSubscription() *schema.Resource {
104105
DiffSuppressFunc: suppressEquivalentJsonDiffs,
105106
},
106107
"subscription_role_arn": {
107-
Type: schema.TypeString,
108-
Optional: true,
108+
Type: schema.TypeString,
109+
Optional: true,
110+
ValidateFunc: validateArn,
109111
},
110112
"topic_arn": {
111-
Type: schema.TypeString,
112-
Required: true,
113-
ForceNew: true,
113+
Type: schema.TypeString,
114+
Required: true,
115+
ForceNew: true,
116+
ValidateFunc: validateArn,
114117
},
115118
},
116119
}
@@ -168,22 +171,27 @@ func resourceAwsSnsTopicSubscriptionRead(d *schema.ResourceData, meta interface{
168171

169172
log.Printf("[DEBUG] Loading subscription %s", d.Id())
170173

171-
output, err := conn.GetSubscriptionAttributes(&sns.GetSubscriptionAttributesInput{
172-
SubscriptionArn: aws.String(d.Id()),
173-
})
174+
input := &sns.ListSubscriptionsByTopicInput{
175+
TopicArn: aws.String(d.Get("topic_arn").(string)),
176+
}
174177

175-
if !d.IsNewResource() && (tfawserr.ErrCodeEquals(err, sns.ErrCodeResourceNotFoundException) || tfawserr.ErrCodeEquals(err, sns.ErrCodeNotFoundException)) {
176-
log.Printf("[WARN] SNS subscription attributes (%s) not found, removing from state", d.Id())
178+
_, err := conn.ListSubscriptionsByTopic(input)
179+
180+
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, sns.ErrCodeNotFoundException) {
181+
log.Printf("[WARN] SNS Topic Subscription (%s) not found, removing from state", d.Id())
177182
d.SetId("")
178183
return nil
179184
}
180185

186+
output, err := finder.SubscriptionByARN(conn, d.Id())
181187
if err != nil {
182188
return fmt.Errorf("getting SNS subscription attributes (%s): %w", d.Id(), err)
183189
}
184190

185-
if output == nil || output.Attributes == nil || len(output.Attributes) == 0 {
186-
return fmt.Errorf("getting SNS subscription attributes (%s): empty response", d.Id())
191+
if output == nil {
192+
log.Printf("[WARN] SNS subscription attributes (%s) not found, removing from state", d.Id())
193+
d.SetId("")
194+
return nil
187195
}
188196

189197
attributes := output.Attributes
@@ -282,6 +290,13 @@ func resourceAwsSnsTopicSubscriptionDelete(d *schema.ResourceData, meta interfac
282290
return nil
283291
}
284292

293+
if _, err := waiter.SubscriptionDeleted(conn, d.Id()); err != nil {
294+
if tfawserr.ErrCodeEquals(err, sns.ErrCodeNotFoundException) {
295+
return nil
296+
}
297+
return fmt.Errorf("error waiting for SNS topic subscription (%s) deletion: %w", d.Id(), err)
298+
}
299+
285300
return err
286301
}
287302

0 commit comments

Comments
 (0)