Skip to content

Commit 0b64895

Browse files
bbernayspull[bot]
authored andcommitted
fix: Handle SNS Subscriptions in Pending State (#1705)
#### Summary When a Subscription has not been confirmed, it doesn't have a valid ARN so CloudQuery is not able to make nested call. This PR does two things: 1. If a single call fails it will still try all other calls rather than ending early 2. It will not make a call if the ARN is equal to `PendingConfirmation` ---
1 parent afc2d97 commit 0b64895

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

plugins/source/aws/resources/services/sns/subscriptions.go

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/aws/aws-sdk-go-v2/aws"
77
"github.com/aws/aws-sdk-go-v2/service/sns"
8+
"github.com/aws/aws-sdk-go-v2/service/sns/types"
89
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
910
"github.com/cloudquery/cq-provider-sdk/provider/diag"
1011
"github.com/cloudquery/cq-provider-sdk/provider/schema"
@@ -118,6 +119,10 @@ func Subscriptions() *schema.Table {
118119
// ====================================================================================================================
119120

120121
func fetchSnsSubscriptions(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
122+
return diag.WrapError(client.ListAndDetailResolver(ctx, meta, res, listSubscriptions, subscriptionDetail))
123+
}
124+
125+
func listSubscriptions(ctx context.Context, meta schema.ClientMeta, detailChan chan<- interface{}) error {
121126
c := meta.(*client.Client)
122127
svc := c.Services().SNS
123128
config := sns.ListSubscriptionsInput{}
@@ -127,22 +132,7 @@ func fetchSnsSubscriptions(ctx context.Context, meta schema.ClientMeta, parent *
127132
return diag.WrapError(err)
128133
}
129134
for _, item := range output.Subscriptions {
130-
attrs, err := svc.GetSubscriptionAttributes(ctx, &sns.GetSubscriptionAttributesInput{SubscriptionArn: item.SubscriptionArn})
131-
if err != nil {
132-
if c.IsNotFoundError(err) {
133-
continue
134-
}
135-
return diag.WrapError(err)
136-
}
137-
s := Subscription{Subscription: item}
138-
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{WeaklyTypedInput: true, Result: &s})
139-
if err != nil {
140-
return diag.WrapError(err)
141-
}
142-
if err := dec.Decode(attrs.Attributes); err != nil {
143-
return diag.WrapError(err)
144-
}
145-
res <- s
135+
detailChan <- item
146136
}
147137

148138
if aws.ToString(output.NextToken) == "" {
@@ -152,3 +142,34 @@ func fetchSnsSubscriptions(ctx context.Context, meta schema.ClientMeta, parent *
152142
}
153143
return nil
154144
}
145+
146+
func subscriptionDetail(ctx context.Context, meta schema.ClientMeta, resultsChan chan<- interface{}, errorChan chan<- error, summary interface{}) {
147+
c := meta.(*client.Client)
148+
svc := c.Services().SNS
149+
item := summary.(types.Subscription)
150+
s := Subscription{Subscription: item}
151+
// Return early if SubscriptionARN is not set because it is still pending
152+
if aws.ToString(item.SubscriptionArn) == "PendingConfirmation" {
153+
resultsChan <- s
154+
return
155+
}
156+
157+
attrs, err := svc.GetSubscriptionAttributes(ctx, &sns.GetSubscriptionAttributesInput{SubscriptionArn: item.SubscriptionArn})
158+
if err != nil {
159+
if c.IsNotFoundError(err) {
160+
return
161+
}
162+
errorChan <- diag.WrapError(err)
163+
return
164+
}
165+
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{WeaklyTypedInput: true, Result: &s})
166+
if err != nil {
167+
errorChan <- diag.WrapError(err)
168+
return
169+
}
170+
if err := dec.Decode(attrs.Attributes); err != nil {
171+
errorChan <- diag.WrapError(err)
172+
return
173+
}
174+
resultsChan <- s
175+
}

0 commit comments

Comments
 (0)