github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/datadog/logs_archive.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  	datadogV2 "github.com/DataDog/datadog-api-client-go/api/v2/datadog"
    22  
    23  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    24  )
    25  
    26  var (
    27  	// LogsArchiveAllowEmptyValues ...
    28  	LogsArchiveAllowEmptyValues = []string{"path", "query"}
    29  )
    30  
    31  // LogsArchiveGenerator ...
    32  type LogsArchiveGenerator struct {
    33  	DatadogService
    34  }
    35  
    36  func (g *LogsArchiveGenerator) createResources(logsArchives []datadogV2.LogsArchiveDefinition) []terraformutils.Resource {
    37  	resources := []terraformutils.Resource{}
    38  	for _, logsArchive := range logsArchives {
    39  		logsArchiveID := logsArchive.GetId()
    40  		resources = append(resources, g.createResource(logsArchiveID))
    41  	}
    42  
    43  	return resources
    44  }
    45  
    46  func (g *LogsArchiveGenerator) createResource(logsArchiveID string) terraformutils.Resource {
    47  	return terraformutils.NewSimpleResource(
    48  		logsArchiveID,
    49  		fmt.Sprintf("logs_archive_%s", logsArchiveID),
    50  		"datadog_logs_archive",
    51  		"datadog",
    52  		LogsArchiveAllowEmptyValues,
    53  	)
    54  }
    55  
    56  // InitResources Generate TerraformResources from Datadog API,
    57  // from each archive create 1 TerraformResource.
    58  // Need LogsArchive ID as ID for terraform resource
    59  func (g *LogsArchiveGenerator) InitResources() error {
    60  	datadogClientV2 := g.Args["datadogClientV2"].(*datadogV2.APIClient)
    61  	authV2 := g.Args["authV2"].(context.Context)
    62  
    63  	resources := []terraformutils.Resource{}
    64  	for _, filter := range g.Filter {
    65  		if filter.FieldPath == "id" && filter.IsApplicable("logs_archive") {
    66  			for _, value := range filter.AcceptableValues {
    67  				resp, _, err := datadogClientV2.LogsArchivesApi.GetLogsArchive(authV2, value)
    68  				if err != nil {
    69  					return err
    70  				}
    71  				logsArchiveData := resp.GetData()
    72  				resources = append(resources, g.createResource(logsArchiveData.GetId()))
    73  			}
    74  		}
    75  	}
    76  
    77  	if len(resources) > 0 {
    78  		g.Resources = resources
    79  		return nil
    80  	}
    81  
    82  	logsArchiveListResp, _, err := datadogClientV2.LogsArchivesApi.ListLogsArchives(authV2)
    83  	logsArchiveList := logsArchiveListResp.GetData()
    84  	if err != nil {
    85  		return err
    86  	}
    87  	g.Resources = g.createResources(logsArchiveList)
    88  	return nil
    89  }