github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/datadog/integration_slack_channel.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  	// IntegrationSlackChannelAllowEmptyValues ...
    29  	IntegrationSlackChannelAllowEmptyValues = []string{}
    30  )
    31  
    32  // IntegrationSlackChannelGenerator ...
    33  type IntegrationSlackChannelGenerator struct {
    34  	DatadogService
    35  }
    36  
    37  func (g *IntegrationSlackChannelGenerator) createResources(accountID string, slackChannels []datadogV1.SlackIntegrationChannel) []terraformutils.Resource {
    38  	resources := []terraformutils.Resource{}
    39  	for _, slackChannel := range slackChannels {
    40  		id := fmt.Sprintf("%s:%s", accountID, slackChannel.GetName())
    41  		resources = append(resources, g.createResource(id))
    42  	}
    43  
    44  	return resources
    45  }
    46  
    47  func (g *IntegrationSlackChannelGenerator) createResource(id string) terraformutils.Resource {
    48  	return terraformutils.NewSimpleResource(
    49  		id,
    50  		fmt.Sprintf("integration_slack_channel_%s", id),
    51  		"datadog_integration_slack_channel",
    52  		"datadog",
    53  		IntegrationSlackChannelAllowEmptyValues,
    54  	)
    55  }
    56  
    57  // InitResources Generate TerraformResources from Datadog API,
    58  // from each slack channel create 1 TerraformResource.
    59  func (g *IntegrationSlackChannelGenerator) InitResources() error {
    60  	datadogClientV1 := g.Args["datadogClientV1"].(*datadogV1.APIClient)
    61  	authV1 := g.Args["authV1"].(context.Context)
    62  
    63  	resources := []terraformutils.Resource{}
    64  	for _, filter := range g.Filter {
    65  		if filter.FieldPath == "account_name" && filter.IsApplicable("integration_slack_channel") {
    66  			for _, value := range filter.AcceptableValues {
    67  				slackChannels, _, err := datadogClientV1.SlackIntegrationApi.GetSlackIntegrationChannels(authV1, value)
    68  				if err != nil {
    69  					return err
    70  				}
    71  
    72  				resources = g.createResources(value, slackChannels)
    73  			}
    74  		}
    75  		if filter.FieldPath == "id" && filter.IsApplicable("integration_slack_channel") {
    76  			for _, value := range filter.AcceptableValues {
    77  				resources = append(resources, g.createResource(value))
    78  			}
    79  		}
    80  	}
    81  
    82  	if len(resources) == 0 {
    83  		log.Print("Filter(account_name or resource id) is required to import datadog_integration_slack_channel resource")
    84  		return nil
    85  	}
    86  	g.Resources = resources
    87  	return nil
    88  }