/
data_source_opensearch_destination.go
186 lines (163 loc) · 5.71 KB
/
data_source_opensearch_destination.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package provider
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/olivere/elastic/uritemplates"
elastic7 "github.com/olivere/elastic/v7"
elastic6 "gopkg.in/olivere/elastic.v6"
)
const DESTINATION_NAME_FIELD = "destination.name.keyword"
var datasourceOpenDistroDestinationSchema = map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the destrination to retrieve",
},
"body": {
Type: schema.TypeMap,
Computed: true,
Description: "Map of the attributes of the destination",
},
}
func dataSourceOpenSearchDestination() *schema.Resource {
return &schema.Resource{
Description: "`opensearch_destination` can be used to retrieve the destination object by name.",
Read: dataSourceOpensearchOpenDistroDestinationRead,
Schema: datasourceOpenDistroDestinationSchema,
}
}
func dataSourceOpensearchOpenDistroDestinationRead(d *schema.ResourceData, m interface{}) error {
destinationName := d.Get("name").(string)
var id string
var destination map[string]interface{}
var err error
esClient, err := getClient(m.(*ProviderConf))
if err != nil {
return err
}
switch client := esClient.(type) {
case *elastic7.Client:
// See https://github.com/opendistro-for-elasticsearch/alerting/issues/70,
// no tags or API endpoint for searching destination. In ODFE >= 1.11.0,
// the index has become a "system index", so it cannot be searched:
// https://opendistro.github.io/for-elasticsearch-docs/docs/alerting/settings/#alerting-indices
// instead we paginate through all destinations to find the first name match :|
id, destination, err = destinationOpenSearch7GetAll(client, destinationName)
if err != nil {
id, destination, err = destinationOpenSearch7Search(client, DESTINATION_INDEX, destinationName)
}
case *elastic6.Client:
id, destination, err = destinationOpenSearch6Search(client, DESTINATION_INDEX, destinationName)
default:
err = errors.New("destination resource not implemented prior to v6")
}
if err != nil {
return err
} else if id == "" {
// short circuit
return nil
}
d.SetId(id)
// we get a non-uniform map[string]interface{} back for the body, terraform
// only accepts a mapping of string to primitive values. We want to save
// this as a map so that attributes are accessible
simplifiedBody := map[string]string{}
for key, value := range destination {
if stringified, ok := value.(string); ok {
simplifiedBody[key] = stringified
} else {
log.Printf("[INFO] couldn't simplify: %+v", value)
}
}
err = d.Set("body", simplifiedBody)
return err
}
func destinationOpenSearch7Search(client *elastic7.Client, index string, name string) (string, map[string]interface{}, error) {
termQuery := elastic7.NewTermQuery(DESTINATION_NAME_FIELD, name)
result, err := client.Search().
Index(index).
Query(termQuery).
Do(context.TODO())
destination := make(map[string]interface{})
if err != nil {
return "", destination, err
}
if result.TotalHits() == 1 {
if err := json.Unmarshal(result.Hits.Hits[0].Source, &destination); err != nil {
return "", destination, fmt.Errorf("error unmarshalling destination body: %+v", err)
}
return result.Hits.Hits[0].Id, destination["destination"].(map[string]interface{}), nil
} else if result.TotalHits() < 1 {
return "", destination, err
} else {
return "", destination, fmt.Errorf("1 result expected, found %d.", result.TotalHits())
}
}
func destinationOpenSearch6Search(client *elastic6.Client, index string, name string) (string, map[string]interface{}, error) {
termQuery := elastic6.NewTermQuery(DESTINATION_NAME_FIELD, name)
result, err := client.Search().
Index(index).
Query(termQuery).
Do(context.TODO())
destination := make(map[string]interface{})
if err != nil {
return "", destination, err
}
if result.TotalHits() == 1 {
if err := json.Unmarshal(*result.Hits.Hits[0].Source, &destination); err != nil {
return "", destination, fmt.Errorf("error unmarshalling destination body: %+v", err)
}
return result.Hits.Hits[0].Id, destination["destination"].(map[string]interface{}), nil
} else if result.TotalHits() < 1 {
return "", destination, err
} else {
return "", destination, fmt.Errorf("1 result expected, found %d.", result.TotalHits())
}
}
func destinationOpenSearch7GetAll(client *elastic7.Client, name string) (string, map[string]interface{}, error) {
offset := 0
pageSize := 1000
destination := make(map[string]interface{})
for {
path, err := uritemplates.Expand("/_opendistro/_alerting/destinations?startIndex={startIndex}&size={size}", map[string]string{
"startIndex": fmt.Sprint(offset),
"size": fmt.Sprint(pageSize),
})
if err != nil {
return "", destination, fmt.Errorf("error building URL path for destination: %+v", err)
}
httpResponse, err := client.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "GET",
Path: path,
})
if err != nil {
return "", destination, err
}
var drg destinationResponseGet
if err := json.Unmarshal(httpResponse.Body, &drg); err != nil {
return "", destination, fmt.Errorf("error unmarshalling destination body: %+v", err)
}
for _, d := range drg.Destinations {
if d.Name == name {
j, err := json.Marshal(d)
if err != nil {
return "", destination, fmt.Errorf("error marshalling destination: %+v", err)
}
if err := json.Unmarshal(j, &destination); err != nil {
return "", destination, fmt.Errorf("error unmarshalling destination body: %+v", err)
}
return d.ID, destination, nil
}
}
if drg.Total > offset {
offset += pageSize
} else {
break
}
}
return "", destination, fmt.Errorf("destination not found")
}