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

     1  // Copyright 2020 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  
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  	"github.com/aws/aws-sdk-go-v2/service/ses"
    22  )
    23  
    24  var sesAllowEmptyValues = []string{"tags."}
    25  
    26  type SesGenerator struct {
    27  	AWSService
    28  }
    29  
    30  func (g *SesGenerator) InitResources() error {
    31  	config, e := g.generateConfig()
    32  	if e != nil {
    33  		return e
    34  	}
    35  	svc := ses.NewFromConfig(config)
    36  
    37  	if err := g.loadDomainIdentities(svc); err != nil {
    38  		return err
    39  	}
    40  	if err := g.loadMailIdentities(svc); err != nil {
    41  		return err
    42  	}
    43  	if err := g.loadTemplates(svc); err != nil {
    44  		return err
    45  	}
    46  	if err := g.loadConfigurationSets(svc); err != nil {
    47  		return err
    48  	}
    49  	if err := g.loadRuleSets(svc); err != nil {
    50  		return err
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func (g *SesGenerator) loadDomainIdentities(svc *ses.Client) error {
    57  	p := ses.NewListIdentitiesPaginator(svc, &ses.ListIdentitiesInput{
    58  		IdentityType: "Domain",
    59  	})
    60  	for p.HasMorePages() {
    61  		page, err := p.NextPage(context.TODO())
    62  		if err != nil {
    63  			return err
    64  		}
    65  		for _, identity := range page.Identities {
    66  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    67  				identity,
    68  				identity,
    69  				"aws_ses_domain_identity",
    70  				"aws",
    71  				sesAllowEmptyValues))
    72  		}
    73  	}
    74  	return nil
    75  }
    76  
    77  func (g *SesGenerator) loadMailIdentities(svc *ses.Client) error {
    78  	p := ses.NewListIdentitiesPaginator(svc, &ses.ListIdentitiesInput{
    79  		IdentityType: "EmailAddress",
    80  	})
    81  	for p.HasMorePages() {
    82  		page, err := p.NextPage(context.TODO())
    83  		if err != nil {
    84  			return err
    85  		}
    86  		for _, identity := range page.Identities {
    87  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    88  				identity,
    89  				identity,
    90  				"aws_ses_email_identity",
    91  				"aws",
    92  				sesAllowEmptyValues))
    93  		}
    94  	}
    95  	return nil
    96  }
    97  
    98  func (g *SesGenerator) loadTemplates(svc *ses.Client) error {
    99  	templates, err := svc.ListTemplates(context.TODO(), &ses.ListTemplatesInput{})
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	for _, templateMetadata := range templates.TemplatesMetadata {
   105  		g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
   106  			StringValue(templateMetadata.Name),
   107  			StringValue(templateMetadata.Name),
   108  			"aws_ses_template",
   109  			"aws",
   110  			sesAllowEmptyValues))
   111  	}
   112  	return nil
   113  }
   114  
   115  func (g *SesGenerator) loadConfigurationSets(svc *ses.Client) error {
   116  	configurationSets, err := svc.ListConfigurationSets(context.TODO(), &ses.ListConfigurationSetsInput{})
   117  	if err != nil {
   118  		return err
   119  	}
   120  
   121  	for _, configurationSet := range configurationSets.ConfigurationSets {
   122  		g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
   123  			StringValue(configurationSet.Name),
   124  			StringValue(configurationSet.Name),
   125  			"aws_ses_configuration_set",
   126  			"aws",
   127  			sesAllowEmptyValues))
   128  	}
   129  	return nil
   130  }
   131  
   132  func (g *SesGenerator) loadRuleSets(svc *ses.Client) error {
   133  	ruleSets, err := svc.ListReceiptRuleSets(context.TODO(), &ses.ListReceiptRuleSetsInput{})
   134  	if err != nil {
   135  		return err
   136  	}
   137  
   138  	for _, ruleSet := range ruleSets.RuleSets {
   139  		ruleSetName := StringValue(ruleSet.Name)
   140  		g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
   141  			ruleSetName,
   142  			ruleSetName,
   143  			"aws_ses_receipt_rule_set",
   144  			"aws",
   145  			sesAllowEmptyValues))
   146  		rules, err := svc.DescribeReceiptRuleSet(context.TODO(), &ses.DescribeReceiptRuleSetInput{
   147  			RuleSetName: ruleSet.Name,
   148  		})
   149  		if err != nil {
   150  			return err
   151  		}
   152  		for _, rule := range rules.Rules {
   153  			ruleID := ruleSetName + ":" + *rule.Name
   154  			g.Resources = append(g.Resources, terraformutils.NewResource(
   155  				*rule.Name,
   156  				ruleID,
   157  				"aws_ses_receipt_rule",
   158  				"aws",
   159  				map[string]string{
   160  					"name":          *rule.Name,
   161  					"rule_set_name": ruleSetName,
   162  				},
   163  				sesAllowEmptyValues,
   164  				map[string]interface{}{},
   165  			))
   166  		}
   167  	}
   168  	return nil
   169  }