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

     1  // Copyright 2018 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  	"log"
    20  	"strings"
    21  
    22  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    23  
    24  	"github.com/aws/aws-sdk-go-v2/service/acm"
    25  )
    26  
    27  var acmAllowEmptyValues = []string{}
    28  
    29  var acmAdditionalFields = map[string]interface{}{}
    30  
    31  type ACMGenerator struct {
    32  	AWSService
    33  }
    34  
    35  func (g *ACMGenerator) createCertificatesResources(svc *acm.Client) []terraformutils.Resource {
    36  	var resources []terraformutils.Resource
    37  	p := acm.NewListCertificatesPaginator(svc, &acm.ListCertificatesInput{})
    38  	for p.HasMorePages() {
    39  		page, err := p.NextPage(context.TODO())
    40  		if err != nil {
    41  			log.Println(err)
    42  			return resources
    43  		}
    44  		for _, cert := range page.CertificateSummaryList {
    45  			certArn := *cert.CertificateArn
    46  			certID := extractCertificateUUID(certArn)
    47  			resources = append(resources, terraformutils.NewResource(
    48  				certArn,
    49  				certID+"_"+strings.TrimSuffix(*cert.DomainName, "."),
    50  				"aws_acm_certificate",
    51  				"aws",
    52  				map[string]string{
    53  					"domain_name": *cert.DomainName,
    54  				},
    55  				acmAllowEmptyValues,
    56  				acmAdditionalFields,
    57  			))
    58  		}
    59  	}
    60  	return resources
    61  }
    62  
    63  // Generate TerraformResources from AWS API,
    64  // create terraform resource for each certificates
    65  func (g *ACMGenerator) InitResources() error {
    66  	config, e := g.generateConfig()
    67  	if e != nil {
    68  		return e
    69  	}
    70  	svc := acm.NewFromConfig(config)
    71  
    72  	g.Resources = g.createCertificatesResources(svc)
    73  	return nil
    74  }
    75  
    76  // extractCertificateUUID extracts UUID from ARN
    77  func extractCertificateUUID(arn string) string {
    78  	if i := strings.Index(arn, "/"); i != -1 {
    79  		return arn[i+1:]
    80  	}
    81  	return arn
    82  }