github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/aws/logs.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  	"strconv"
    20  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  	"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
    23  )
    24  
    25  var logsAllowEmptyValues = []string{"tags."}
    26  
    27  type LogsGenerator struct {
    28  	AWSService
    29  }
    30  
    31  func (g *LogsGenerator) createResources(logGroups *cloudwatchlogs.DescribeLogGroupsOutput) []terraformutils.Resource {
    32  	resources := []terraformutils.Resource{}
    33  	for _, logGroup := range logGroups.LogGroups {
    34  		resourceName := StringValue(logGroup.LogGroupName)
    35  
    36  		attributes := map[string]string{}
    37  
    38  		if logGroup.RetentionInDays != nil {
    39  			attributes["retention_in_days"] = strconv.FormatInt(int64(*logGroup.RetentionInDays), 10)
    40  		}
    41  
    42  		if logGroup.KmsKeyId != nil {
    43  			attributes["kms_key_id"] = *logGroup.KmsKeyId
    44  		}
    45  
    46  		resources = append(resources, terraformutils.NewResource(
    47  			resourceName,
    48  			resourceName,
    49  			"aws_cloudwatch_log_group",
    50  			"aws",
    51  			attributes,
    52  			logsAllowEmptyValues,
    53  			map[string]interface{}{}))
    54  	}
    55  	return resources
    56  }
    57  
    58  // Generate TerraformResources from AWS API
    59  func (g *LogsGenerator) InitResources() error {
    60  	config, e := g.generateConfig()
    61  	if e != nil {
    62  		return e
    63  	}
    64  	svc := cloudwatchlogs.NewFromConfig(config)
    65  
    66  	p := cloudwatchlogs.NewDescribeLogGroupsPaginator(svc, &cloudwatchlogs.DescribeLogGroupsInput{})
    67  	for p.HasMorePages() {
    68  		page, err := p.NextPage(context.TODO())
    69  		if err != nil {
    70  			return err
    71  		}
    72  		g.Resources = append(g.Resources, g.createResources(page)...)
    73  	}
    74  	return nil
    75  }
    76  
    77  // remove retention_in_days if it is 0 (it gets added by the "refresh" stage)
    78  func (g *LogsGenerator) PostConvertHook() error {
    79  	for _, resource := range g.Resources {
    80  		if resource.Item["retention_in_days"] == "0" {
    81  			delete(resource.Item, "retention_in_days")
    82  		}
    83  	}
    84  	return nil
    85  }