2021-02-05 10:03:45 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2021-03-17 13:33:45 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
awssdk "github.com/aws/aws-sdk-go/aws"
|
2021-02-05 10:03:45 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/sns"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2021-03-17 13:33:45 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/alerter"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
|
|
|
|
2021-02-05 10:03:45 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
2021-05-21 14:09:45 +00:00
|
|
|
|
2021-02-05 10:03:45 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
)
|
|
|
|
|
2021-03-17 13:33:45 +00:00
|
|
|
type pendingTopicAlert struct {
|
|
|
|
endpoint *string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pendingTopicAlert) Message() string {
|
|
|
|
return fmt.Sprintf("%s with pending confirmation status for endpoint \"%s\" will be ignored",
|
|
|
|
aws.AwsSnsTopicSubscriptionResourceType,
|
|
|
|
awssdk.StringValue(p.endpoint))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pendingTopicAlert) ShouldIgnoreResource() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-05 10:03:45 +00:00
|
|
|
type SNSTopicSubscriptionSupplier struct {
|
|
|
|
reader terraform.ResourceReader
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer *resource.Deserializer
|
2021-02-05 10:03:45 +00:00
|
|
|
client repository.SNSRepository
|
|
|
|
runner *terraform.ParallelResourceReader
|
2021-03-17 13:33:45 +00:00
|
|
|
alerter alerter.AlerterInterface
|
2021-02-05 10:03:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
func NewSNSTopicSubscriptionSupplier(provider *AWSTerraformProvider, a alerter.AlerterInterface, deserializer *resource.Deserializer) *SNSTopicSubscriptionSupplier {
|
2021-02-05 10:03:45 +00:00
|
|
|
return &SNSTopicSubscriptionSupplier{
|
|
|
|
provider,
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer,
|
2021-02-05 10:03:45 +00:00
|
|
|
repository.NewSNSClient(provider.session),
|
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2021-03-17 13:33:45 +00:00
|
|
|
a,
|
2021-02-05 10:03:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *SNSTopicSubscriptionSupplier) Resources() ([]resource.Resource, error) {
|
2021-02-05 10:03:45 +00:00
|
|
|
subscriptions, err := s.client.ListAllSubscriptions()
|
|
|
|
if err != nil {
|
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsSnsTopicSubscriptionResourceType)
|
|
|
|
}
|
|
|
|
for _, subscription := range subscriptions {
|
|
|
|
subscription := subscription
|
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
2021-03-17 13:33:45 +00:00
|
|
|
return s.readTopicSubscription(subscription, s.alerter)
|
2021-02-05 10:03:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
retrieve, err := s.runner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
return s.deserializer.Deserialize(aws.AwsSnsTopicSubscriptionResourceType, retrieve)
|
2021-02-05 10:03:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 13:33:45 +00:00
|
|
|
func (s *SNSTopicSubscriptionSupplier) readTopicSubscription(subscription *sns.Subscription, alertr alerter.AlerterInterface) (cty.Value, error) {
|
|
|
|
if subscription.SubscriptionArn != nil && *subscription.SubscriptionArn == "PendingConfirmation" {
|
|
|
|
alertr.SendAlert(
|
|
|
|
fmt.Sprintf("%s.%s", aws.AwsSnsTopicSubscriptionResourceType, *subscription.SubscriptionArn),
|
|
|
|
&pendingTopicAlert{subscription.Endpoint},
|
|
|
|
)
|
|
|
|
return cty.NilVal, nil
|
|
|
|
}
|
|
|
|
|
2021-02-05 10:03:45 +00:00
|
|
|
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
|
|
|
|
ID: *subscription.SubscriptionArn,
|
|
|
|
Ty: aws.AwsSnsTopicSubscriptionResourceType,
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"SubscriptionId": *subscription.SubscriptionArn,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
return *val, nil
|
|
|
|
}
|