github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/datadog/service_level_objective.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  
    21  	datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
    22  
    23  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    24  )
    25  
    26  var (
    27  	// ServiceLevelObjectiveAllowEmptyValues ...
    28  	ServiceLevelObjectiveAllowEmptyValues = []string{"tags."}
    29  )
    30  
    31  // ServiceLevelObjectiveGenerator ...
    32  type ServiceLevelObjectiveGenerator struct {
    33  	DatadogService
    34  }
    35  
    36  func (g *ServiceLevelObjectiveGenerator) createResources(sloList []datadogV1.ServiceLevelObjective) []terraformutils.Resource {
    37  	resources := []terraformutils.Resource{}
    38  	for _, slo := range sloList {
    39  		resourceID := slo.GetId()
    40  		resources = append(resources, g.createResource(resourceID))
    41  	}
    42  
    43  	return resources
    44  }
    45  
    46  func (g *ServiceLevelObjectiveGenerator) createResource(sloID string) terraformutils.Resource {
    47  	return terraformutils.NewSimpleResource(
    48  		sloID,
    49  		fmt.Sprintf("service_level_objective_%s", sloID),
    50  		"datadog_service_level_objective",
    51  		"datadog",
    52  		ServiceLevelObjectiveAllowEmptyValues,
    53  	)
    54  }
    55  
    56  // InitResources Generate TerraformResources from Datadog API,
    57  // from each service_level_objective create 1 TerraformResource.
    58  // Need ServiceLevelObjective ID as ID for terraform resource
    59  func (g *ServiceLevelObjectiveGenerator) InitResources() error {
    60  	datadogClientV1 := g.Args["datadogClientV1"].(*datadogV1.APIClient)
    61  	authV1 := g.Args["authV1"].(context.Context)
    62  
    63  	var slos []datadogV1.ServiceLevelObjective
    64  	resp, _, err := datadogClientV1.ServiceLevelObjectivesApi.ListSLOs(authV1)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	slos = append(slos, resp.GetData()...)
    70  	g.Resources = g.createResources(slos)
    71  	return nil
    72  }