github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/datadog/synthetics_global_variable.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  	"log"
    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  	// SyntheticsGlobalVariableAllowEmptyValues ...
    29  	SyntheticsGlobalVariableAllowEmptyValues = []string{"tags."}
    30  )
    31  
    32  // SyntheticsGlobalVariableGenerator ...
    33  type SyntheticsGlobalVariableGenerator struct {
    34  	DatadogService
    35  }
    36  
    37  func (g *SyntheticsGlobalVariableGenerator) createResources(globalVariables []datadogV1.SyntheticsGlobalVariable) []terraformutils.Resource {
    38  	resources := []terraformutils.Resource{}
    39  	for _, globalVariable := range globalVariables {
    40  		resourceID := globalVariable.GetId()
    41  		resources = append(resources, g.createResource(resourceID))
    42  	}
    43  
    44  	return resources
    45  }
    46  
    47  func (g *SyntheticsGlobalVariableGenerator) createResource(globalVariableID string) terraformutils.Resource {
    48  	return terraformutils.NewSimpleResource(
    49  		globalVariableID,
    50  		fmt.Sprintf("synthetics_global_variable_%s", globalVariableID),
    51  		"datadog_synthetics_global_variable",
    52  		"datadog",
    53  		SyntheticsGlobalVariableAllowEmptyValues,
    54  	)
    55  }
    56  
    57  // InitResources Generate TerraformResources from Datadog API,
    58  // from each SyntheticsGlobalVariable create 1 TerraformResource.
    59  // Need SyntheticsGlobalVariable ID as ID for terraform resource
    60  func (g *SyntheticsGlobalVariableGenerator) InitResources() error {
    61  	datadogClientV1 := g.Args["datadogClientV1"].(*datadogV1.APIClient)
    62  	authV1 := g.Args["authV1"].(context.Context)
    63  
    64  	var globalVariableIDs []datadogV1.SyntheticsGlobalVariable
    65  	for _, filter := range g.Filter {
    66  		if filter.FieldPath == "id" && filter.IsApplicable("synthetics_global_variable") {
    67  			for _, v := range filter.AcceptableValues {
    68  				resp, _, err := datadogClientV1.SyntheticsApi.GetGlobalVariable(authV1, v)
    69  				if err != nil {
    70  					log.Printf("error retrieving synthetics gloval variable with id:%s - %s", v, err)
    71  					continue
    72  				}
    73  				globalVariableIDs = append(globalVariableIDs, resp)
    74  			}
    75  		}
    76  	}
    77  
    78  	if len(globalVariableIDs) == 0 {
    79  		log.Print("Filter(Synthetics Global Variable IDs) is required for importing datadog_synthetics_global_variable resource")
    80  		return nil
    81  	}
    82  
    83  	g.Resources = g.createResources(globalVariableIDs)
    84  	return nil
    85  }