-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathdata_source_datadog_logs_pipelines.go
158 lines (147 loc) · 4.8 KB
/
data_source_datadog_logs_pipelines.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package datadog
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func dataSourceDatadogLogsPipelines() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to list all existing logs pipelines for use in other resources.",
ReadContext: dataSourceDatadogLogsPipelinesRead,
SchemaFunc: func() map[string]*schema.Schema {
return map[string]*schema.Schema{
"is_read_only": {
Description: "Filter parameter for retrieved pipelines",
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"true", "false"}, true),
},
// Computed values
"logs_pipelines": {
Description: "List of logs pipelines",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Description: "ID of the pipeline",
Type: schema.TypeString,
Computed: true,
},
"filter": {
Description: "Pipelines filter",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"query": {
Description: "Pipeline filter criteria.",
Type: schema.TypeString,
Computed: true,
},
},
},
},
"name": {
Description: "The name of the pipeline.",
Type: schema.TypeString,
Computed: true,
},
"is_enabled": {
Description: "Whether or not the pipeline is enabled.",
Type: schema.TypeBool,
Computed: true,
},
"tags": {
Description: "Tags of the pipeline",
Type: schema.TypeSet,
Computed: true,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"description": {
Description: "Description of the pipeline",
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"is_read_only": {
Description: "Whether or not the pipeline can be edited.",
Type: schema.TypeBool,
Computed: true,
},
"type": {
Description: "Whether or not the pipeline can be edited.",
Type: schema.TypeString,
Computed: true,
},
},
},
},
}
},
}
}
func buildTerraformLogsPipelineFilter(ddFilter datadogV1.LogsFilter) *[]map[string]interface{} {
tfFilter := map[string]interface{}{
"query": ddFilter.GetQuery(),
}
return &[]map[string]interface{}{tfFilter}
}
func dataSourceDatadogLogsPipelinesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
logsPipelines, httpresp, err := apiInstances.GetLogsPipelinesApiV1().ListLogsPipelines(auth)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpresp, "error querying log pipelines")
}
diags := diag.Diagnostics{}
vStr, ok := d.GetOk("is_read_only")
v, _ := strconv.ParseBool(vStr.(string))
tflogsPipelines := make([]map[string]interface{}, 0)
for _, pipeline := range logsPipelines {
if !ok || (ok && v == *pipeline.IsReadOnly) {
if err := utils.CheckForUnparsed(pipeline); err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("skipping logs pipeline with id: %s", pipeline.GetId()),
Detail: fmt.Sprintf("logs pipeline contains unparsed object: %v", err),
})
continue
}
tflogsPipelines = append(tflogsPipelines, map[string]interface{}{
"name": pipeline.GetName(),
"id": pipeline.GetId(),
"filter": buildTerraformLogsPipelineFilter(pipeline.GetFilter()),
"is_enabled": pipeline.GetIsEnabled(),
"is_read_only": pipeline.GetIsReadOnly(),
"type": pipeline.GetType(),
"tags": pipeline.GetTags(),
"description": pipeline.GetDescription(),
})
}
}
if err := d.Set("logs_pipelines", tflogsPipelines); err != nil {
return diag.FromErr(err)
}
d.SetId(computePipelinesDatasourceID(d))
return diags
}
func computePipelinesDatasourceID(d *schema.ResourceData) string {
var dsID strings.Builder
dsID.WriteString("logs-pipeline")
if v, ok := d.GetOk("is_read_only"); ok {
dsID.WriteRune('|')
dsID.WriteString("is_read_only:" + v.(string))
}
return dsID.String()
}