github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/datadog/monitor.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 datadog
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"strconv"
    21  	"strings"
    22  
    23  	datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
    24  
    25  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    26  )
    27  
    28  var (
    29  	// MonitorAllowEmptyValues ...
    30  	MonitorAllowEmptyValues = []string{"tags.", "message"}
    31  )
    32  
    33  // MonitorGenerator ...
    34  type MonitorGenerator struct {
    35  	DatadogService
    36  }
    37  
    38  func (g *MonitorGenerator) createResources(monitors []datadogV1.Monitor) []terraformutils.Resource {
    39  	resources := []terraformutils.Resource{}
    40  	for _, monitor := range monitors {
    41  		if monitor.GetType() == datadogV1.MONITORTYPE_SYNTHETICS_ALERT {
    42  			continue
    43  		}
    44  		resourceName := strconv.FormatInt(monitor.GetId(), 10)
    45  		resources = append(resources, g.createResource(resourceName))
    46  	}
    47  
    48  	return resources
    49  }
    50  
    51  func (g *MonitorGenerator) createResource(monitorID string) terraformutils.Resource {
    52  	return terraformutils.NewSimpleResource(
    53  		monitorID,
    54  		fmt.Sprintf("monitor_%s", monitorID),
    55  		"datadog_monitor",
    56  		"datadog",
    57  		MonitorAllowEmptyValues,
    58  	)
    59  }
    60  
    61  // InitResources Generate TerraformResources from Datadog API,
    62  // from each monitor create 1 TerraformResource.
    63  // Need Monitor ID as ID for terraform resource
    64  func (g *MonitorGenerator) InitResources() error {
    65  	datadogClientV1 := g.Args["datadogClientV1"].(*datadogV1.APIClient)
    66  	authV1 := g.Args["authV1"].(context.Context)
    67  
    68  	optionalParams := datadogV1.NewListMonitorsOptionalParameters()
    69  	resources := []terraformutils.Resource{}
    70  	for _, filter := range g.Filter {
    71  		if filter.FieldPath == "id" && filter.IsApplicable("monitor") {
    72  			for _, value := range filter.AcceptableValues {
    73  				i, err := strconv.ParseInt(value, 10, 64)
    74  				if err != nil {
    75  					return err
    76  				}
    77  
    78  				monitor, _, err := datadogClientV1.MonitorsApi.GetMonitor(authV1, i)
    79  				if err != nil {
    80  					return err
    81  				}
    82  
    83  				resources = append(resources, g.createResource(strconv.FormatInt(monitor.GetId(), 10)))
    84  			}
    85  		}
    86  		if filter.FieldPath == "tags" && filter.IsApplicable("monitor") {
    87  			optionalParams.WithMonitorTags(strings.Join(filter.AcceptableValues, ","))
    88  		}
    89  	}
    90  
    91  	if len(resources) > 0 {
    92  		g.Resources = resources
    93  		return nil
    94  	}
    95  
    96  	var monitors []datadogV1.Monitor
    97  	pageSize := int32(1000)
    98  	pageNumber := int64(0)
    99  	for {
   100  		resp, _, err := datadogClientV1.MonitorsApi.ListMonitors(authV1, *optionalParams.
   101  			WithPageSize(pageSize).
   102  			WithPage(pageNumber))
   103  		if err != nil {
   104  			return err
   105  		}
   106  
   107  		if len(resp) == 0 || int32(len(resp)) < pageSize {
   108  			monitors = append(monitors, resp...)
   109  			break
   110  		}
   111  
   112  		monitors = append(monitors, resp...)
   113  		pageNumber++
   114  	}
   115  
   116  	g.Resources = g.createResources(monitors)
   117  	return nil
   118  }