github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/datadog/logs_custom_pipeline.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 "encoding/json" 20 "fmt" 21 "strings" 22 23 datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog" 24 25 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 26 ) 27 28 var ( 29 // LogsCustomPipelineAllowEmptyValues ... 30 LogsCustomPipelineAllowEmptyValues = []string{"support_rules", "filter"} 31 ) 32 33 // LogsCustomPipelineGenerator ... 34 type LogsCustomPipelineGenerator struct { 35 DatadogService 36 } 37 38 func (g *LogsCustomPipelineGenerator) createResources(logsCustomPipelines []datadogV1.LogsPipeline) []terraformutils.Resource { 39 resources := []terraformutils.Resource{} 40 for _, logsCustomPipeline := range logsCustomPipelines { 41 // Import logs custom pipelines only 42 if !logsCustomPipeline.GetIsReadOnly() { 43 resourceName := logsCustomPipeline.GetId() 44 resources = append(resources, g.createResource(resourceName)) 45 } 46 } 47 48 return resources 49 } 50 51 func (g *LogsCustomPipelineGenerator) createResource(logsCustomPipelineID string) terraformutils.Resource { 52 return terraformutils.NewSimpleResource( 53 logsCustomPipelineID, 54 fmt.Sprintf("logs_custom_pipeline_%s", logsCustomPipelineID), 55 "datadog_logs_custom_pipeline", 56 "datadog", 57 LogsCustomPipelineAllowEmptyValues, 58 ) 59 } 60 61 // InitResources Generate TerraformResources from Datadog API, 62 // from each custom pipeline create 1 TerraformResource. 63 // Need LogsPipeline ID as ID for terraform resource 64 func (g *LogsCustomPipelineGenerator) InitResources() error { 65 datadogClientV1 := g.Args["datadogClientV1"].(*datadogV1.APIClient) 66 authV1 := g.Args["authV1"].(context.Context) 67 68 resources := []terraformutils.Resource{} 69 for _, filter := range g.Filter { 70 if filter.FieldPath == "id" && filter.IsApplicable("logs_custom_pipeline") { 71 for _, value := range filter.AcceptableValues { 72 logsCustomPipeline, _, err := datadogClientV1.LogsPipelinesApi.GetLogsPipeline(authV1, value) 73 if err != nil { 74 return err 75 } 76 77 resources = append(resources, g.createResource(logsCustomPipeline.GetId())) 78 } 79 } 80 } 81 82 if len(resources) > 0 { 83 g.Resources = resources 84 return nil 85 } 86 87 logsCustomPipelines, _, err := datadogClientV1.LogsPipelinesApi.ListLogsPipelines(authV1) 88 if err != nil { 89 return err 90 } 91 g.Resources = g.createResources(logsCustomPipelines) 92 return nil 93 } 94 95 func (g *LogsCustomPipelineGenerator) PostConvertHook() error { 96 for i, r := range g.Resources { 97 for k, v := range r.Item { 98 // Hack to properly escape `%{` used in pipeline processors 99 if k == "processor" { 100 var z interface{} 101 jsonByte, err := json.Marshal(v) 102 if err != nil { 103 continue 104 } 105 jsonByte = []byte(strings.ReplaceAll(string(jsonByte), "%{", "%%{")) 106 if err = json.Unmarshal(jsonByte, &z); err != nil { 107 continue 108 } 109 g.Resources[i].Item[k] = z 110 } 111 } 112 } 113 return nil 114 }