github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/datadog/integration_pagerduty_service_object.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  	"fmt"
    19  
    20  	datadogCommunity "github.com/zorkian/go-datadog-api"
    21  
    22  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    23  )
    24  
    25  var (
    26  	// IntegrationPagerdutyServiceObjectAllowEmptyValues ...
    27  	IntegrationPagerdutyServiceObjectAllowEmptyValues = []string{"tags."}
    28  )
    29  
    30  // IntegrationPagerdutyServiceObjectGenerator ...
    31  type IntegrationPagerdutyServiceObjectGenerator struct {
    32  	DatadogService
    33  }
    34  
    35  func (g *IntegrationPagerdutyServiceObjectGenerator) createResources(serviceNames []string) []terraformutils.Resource {
    36  	resources := []terraformutils.Resource{}
    37  	for _, name := range serviceNames {
    38  		resourceName := name
    39  		resources = append(resources, g.createResource(resourceName))
    40  	}
    41  
    42  	return resources
    43  }
    44  
    45  func (g *IntegrationPagerdutyServiceObjectGenerator) createResource(serviceName string) terraformutils.Resource {
    46  	return terraformutils.NewSimpleResource(
    47  		serviceName,
    48  		fmt.Sprintf("integration_pagerduty_service_object_%s", serviceName),
    49  		"datadog_integration_pagerduty_service_object",
    50  		"datadog",
    51  		IntegrationPagerdutyServiceObjectAllowEmptyValues,
    52  	)
    53  }
    54  
    55  // InitResources Generate TerraformResources from Datadog API,
    56  // from each PD Service create 1 TerraformResource.
    57  // Need IntegrationPagerdutyServiceObject ServiceName as ID for terraform resource
    58  func (g *IntegrationPagerdutyServiceObjectGenerator) InitResources() error {
    59  	client := datadogCommunity.NewClient(g.Args["api-key"].(string), g.Args["app-key"].(string))
    60  
    61  	pdIntegration, err := client.GetIntegrationPD()
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	var serviceNames []string
    67  	for _, service := range pdIntegration.Services {
    68  		serviceNames = append(serviceNames, *service.ServiceName)
    69  	}
    70  
    71  	g.Resources = g.createResources(serviceNames)
    72  	return nil
    73  }