github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/aws/sns.go (about)

     1  // Copyright 2019 The Terraformer Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aws
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"log"
    21  	"strings"
    22  
    23  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    24  	"github.com/aws/aws-sdk-go-v2/service/sns"
    25  )
    26  
    27  var snsAllowEmptyValues = []string{"tags."}
    28  
    29  type SnsGenerator struct {
    30  	AWSService
    31  }
    32  
    33  // TF currently doesn't support email subscriptions + subscriptions with pending confirmations
    34  func (g *SnsGenerator) isSupportedSubscription(protocol, subscriptionID string) bool {
    35  	return protocol != "email" && protocol != "email-json" && subscriptionID != "PendingConfirmation"
    36  }
    37  
    38  func (g *SnsGenerator) InitResources() error {
    39  	config, e := g.generateConfig()
    40  	if e != nil {
    41  		return e
    42  	}
    43  	svc := sns.NewFromConfig(config)
    44  	p := sns.NewListTopicsPaginator(svc, &sns.ListTopicsInput{})
    45  	for p.HasMorePages() {
    46  		page, err := p.NextPage(context.TODO())
    47  		if err != nil {
    48  			return err
    49  		}
    50  		for _, topic := range page.Topics {
    51  			arnParts := strings.Split(StringValue(topic.TopicArn), ":")
    52  			topicName := arnParts[len(arnParts)-1]
    53  
    54  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    55  				StringValue(topic.TopicArn),
    56  				topicName,
    57  				"aws_sns_topic",
    58  				"aws",
    59  				snsAllowEmptyValues,
    60  			))
    61  
    62  			topicSubsPage := sns.NewListSubscriptionsByTopicPaginator(svc, &sns.ListSubscriptionsByTopicInput{
    63  				TopicArn: topic.TopicArn,
    64  			})
    65  			for topicSubsPage.HasMorePages() {
    66  				topicSubsNextPage, err := topicSubsPage.NextPage(context.TODO())
    67  				if err != nil {
    68  					log.Println(err)
    69  					continue
    70  				}
    71  				for _, subscription := range topicSubsNextPage.Subscriptions {
    72  					subscriptionArnParts := strings.Split(StringValue(subscription.SubscriptionArn), ":")
    73  					subscriptionID := subscriptionArnParts[len(subscriptionArnParts)-1]
    74  
    75  					if g.isSupportedSubscription(StringValue(subscription.Protocol), subscriptionID) {
    76  						g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    77  							StringValue(subscription.SubscriptionArn),
    78  							"subscription-"+subscriptionID,
    79  							"aws_sns_topic_subscription",
    80  							"aws",
    81  							snsAllowEmptyValues,
    82  						))
    83  					}
    84  				}
    85  			}
    86  		}
    87  	}
    88  	return nil
    89  }
    90  
    91  // PostConvertHook for add policy json as heredoc
    92  func (g *SnsGenerator) PostConvertHook() error {
    93  	for i, resource := range g.Resources {
    94  		if resource.InstanceInfo.Type == "aws_sns_topic" {
    95  			if val, ok := g.Resources[i].Item["policy"]; ok {
    96  				policy := g.escapeAwsInterpolation(val.(string))
    97  				g.Resources[i].Item["policy"] = fmt.Sprintf(`<<POLICY
    98  %s
    99  POLICY`, policy)
   100  			}
   101  		}
   102  	}
   103  	return nil
   104  }