github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/datadog/downtime.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  
    22  	datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
    23  
    24  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    25  )
    26  
    27  var (
    28  	// DowntimeAllowEmptyValues ...
    29  	DowntimeAllowEmptyValues = []string{}
    30  )
    31  
    32  // DowntimeGenerator ...
    33  type DowntimeGenerator struct {
    34  	DatadogService
    35  }
    36  
    37  func (g *DowntimeGenerator) createResources(downtimes []datadogV1.Downtime) []terraformutils.Resource {
    38  	resources := []terraformutils.Resource{}
    39  	for _, downtime := range downtimes {
    40  		resourceName := strconv.FormatInt(downtime.GetId(), 10)
    41  		resources = append(resources, g.createResource(resourceName))
    42  	}
    43  
    44  	return resources
    45  }
    46  
    47  func (g *DowntimeGenerator) createResource(downtimeID string) terraformutils.Resource {
    48  	return terraformutils.NewSimpleResource(
    49  		downtimeID,
    50  		fmt.Sprintf("downtime_%s", downtimeID),
    51  		"datadog_downtime",
    52  		"datadog",
    53  		DowntimeAllowEmptyValues,
    54  	)
    55  }
    56  
    57  // InitResources Generate TerraformResources from Datadog API,
    58  // from each downtime create 1 TerraformResource.
    59  // Need Downtime ID as ID for terraform resource
    60  func (g *DowntimeGenerator) InitResources() error {
    61  	datadogClientV1 := g.Args["datadogClientV1"].(*datadogV1.APIClient)
    62  	authV1 := g.Args["authV1"].(context.Context)
    63  
    64  	resources := []terraformutils.Resource{}
    65  	for _, filter := range g.Filter {
    66  		if filter.FieldPath == "id" && filter.IsApplicable("downtime") {
    67  			for _, value := range filter.AcceptableValues {
    68  				i, err := strconv.ParseInt(value, 10, 64)
    69  				if err != nil {
    70  					return err
    71  				}
    72  
    73  				monitor, _, err := datadogClientV1.DowntimesApi.GetDowntime(authV1, i)
    74  				if err != nil {
    75  					return err
    76  				}
    77  
    78  				resources = append(resources, g.createResource(strconv.FormatInt(monitor.GetId(), 10)))
    79  			}
    80  		}
    81  	}
    82  
    83  	if len(resources) > 0 {
    84  		g.Resources = resources
    85  		return nil
    86  	}
    87  
    88  	downtimes, _, err := datadogClientV1.DowntimesApi.ListDowntimes(authV1)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	g.Resources = g.createResources(downtimes)
    93  	return nil
    94  }