github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/datadog/synthetics_private_location.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  	"regexp"
    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  	// SyntheticsPrivateLocationAllowEmptyValues ...
    29  	SyntheticsPrivateLocationAllowEmptyValues = []string{"tags."}
    30  	plIDRegex                                 = regexp.MustCompile("^pl:.*")
    31  )
    32  
    33  // SyntheticsPrivateLocationGenerator ...
    34  type SyntheticsPrivateLocationGenerator struct {
    35  	DatadogService
    36  }
    37  
    38  func (g *SyntheticsPrivateLocationGenerator) createResources(locations []datadogV1.SyntheticsLocation) []terraformutils.Resource {
    39  	resources := []terraformutils.Resource{}
    40  	for _, location := range locations {
    41  		locationID := location.GetId()
    42  		if plIDRegex.MatchString(locationID) {
    43  			resources = append(resources, g.createResource(locationID))
    44  		}
    45  	}
    46  	return resources
    47  }
    48  
    49  func (g *SyntheticsPrivateLocationGenerator) createResource(plID string) terraformutils.Resource {
    50  	return terraformutils.NewSimpleResource(
    51  		plID,
    52  		fmt.Sprintf("synthetics_private_location_%s", plID),
    53  		"datadog_synthetics_private_location",
    54  		"datadog",
    55  		SyntheticsPrivateLocationAllowEmptyValues,
    56  	)
    57  }
    58  
    59  // InitResources Generate TerraformResources from Datadog API,
    60  // from each SyntheticsPrivateLocation create 1 TerraformResource.
    61  // Need SyntheticsPrivateLocation ID as ID for terraform resource
    62  func (g *SyntheticsPrivateLocationGenerator) InitResources() error {
    63  	datadogClientV1 := g.Args["datadogClientV1"].(*datadogV1.APIClient)
    64  	authV1 := g.Args["authV1"].(context.Context)
    65  
    66  	data, _, err := datadogClientV1.SyntheticsApi.ListLocations(authV1)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	g.Resources = g.createResources(data.GetLocations())
    72  	return nil
    73  }