github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/aws/cloudwatch.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/cloudwatch"
    22  	"github.com/aws/aws-sdk-go-v2/service/cloudwatchevents"
    23  )
    24  
    25  var cloudwatchAllowEmptyValues = []string{"tags."}
    26  
    27  type CloudWatchGenerator struct {
    28  	AWSService
    29  }
    30  
    31  func (g *CloudWatchGenerator) InitResources() error {
    32  	config, e := g.generateConfig()
    33  	if e != nil {
    34  		return e
    35  	}
    36  
    37  	cloudwatchSvc := cloudwatch.NewFromConfig(config)
    38  	err := g.createMetricAlarms(cloudwatchSvc)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	err = g.createDashboards(cloudwatchSvc)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	cloudwatcheventsSvc := cloudwatchevents.NewFromConfig(config)
    48  	err = g.createRules(cloudwatcheventsSvc)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func (g *CloudWatchGenerator) createMetricAlarms(cloudwatchSvc *cloudwatch.Client) error {
    57  	var nextToken *string
    58  	for {
    59  		output, err := cloudwatchSvc.DescribeAlarms(context.TODO(), &cloudwatch.DescribeAlarmsInput{
    60  			NextToken: nextToken,
    61  		})
    62  		if err != nil {
    63  			return err
    64  		}
    65  		for _, metricAlarm := range output.MetricAlarms {
    66  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    67  				*metricAlarm.AlarmName,
    68  				*metricAlarm.AlarmName,
    69  				"aws_cloudwatch_metric_alarm",
    70  				"aws",
    71  				cloudwatchAllowEmptyValues))
    72  		}
    73  		nextToken = output.NextToken
    74  		if nextToken == nil {
    75  			break
    76  		}
    77  	}
    78  	return nil
    79  }
    80  
    81  func (g *CloudWatchGenerator) createDashboards(cloudwatchSvc *cloudwatch.Client) error {
    82  	var nextToken *string
    83  	for {
    84  		output, err := cloudwatchSvc.ListDashboards(context.TODO(), &cloudwatch.ListDashboardsInput{
    85  			NextToken: nextToken,
    86  		})
    87  		if err != nil {
    88  			return err
    89  		}
    90  		for _, dashboardEntry := range output.DashboardEntries {
    91  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    92  				*dashboardEntry.DashboardName,
    93  				*dashboardEntry.DashboardName,
    94  				"aws_cloudwatch_dashboard",
    95  				"aws",
    96  				cloudwatchAllowEmptyValues))
    97  		}
    98  		nextToken = output.NextToken
    99  		if nextToken == nil {
   100  			break
   101  		}
   102  	}
   103  	return nil
   104  }
   105  
   106  func (g *CloudWatchGenerator) createRules(cloudwatcheventsSvc *cloudwatchevents.Client) error {
   107  	var listRulesNextToken *string
   108  	for {
   109  		output, err := cloudwatcheventsSvc.ListRules(context.TODO(), &cloudwatchevents.ListRulesInput{
   110  			NextToken: listRulesNextToken,
   111  		})
   112  		if err != nil {
   113  			return err
   114  		}
   115  		for _, rule := range output.Rules {
   116  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
   117  				*rule.Name,
   118  				*rule.Name,
   119  				"aws_cloudwatch_event_rule",
   120  				"aws",
   121  				cloudwatchAllowEmptyValues))
   122  
   123  			var listTargetsNextToken *string
   124  			for {
   125  				targetResponse, err := cloudwatcheventsSvc.ListTargetsByRule(context.TODO(), &cloudwatchevents.ListTargetsByRuleInput{
   126  					Rule:      rule.Name,
   127  					NextToken: listTargetsNextToken,
   128  				})
   129  				if err != nil {
   130  					return err
   131  				}
   132  				for _, target := range targetResponse.Targets {
   133  					targetRef := *rule.Name + "/" + *target.Id
   134  					g.Resources = append(g.Resources, terraformutils.NewResource(
   135  						targetRef,
   136  						targetRef,
   137  						"aws_cloudwatch_event_target",
   138  						"aws",
   139  						map[string]string{
   140  							"rule":      *rule.Name,
   141  							"target_id": *target.Id,
   142  						},
   143  						cloudwatchAllowEmptyValues,
   144  						map[string]interface{}{}))
   145  				}
   146  				listTargetsNextToken = output.NextToken
   147  				if listTargetsNextToken == nil {
   148  					break
   149  				}
   150  			}
   151  		}
   152  		listRulesNextToken = output.NextToken
   153  		if listRulesNextToken == nil {
   154  			break
   155  		}
   156  	}
   157  
   158  	return nil
   159  }