forked from tdegiacinto/terraform-provider-elasticsearch
/
data_source_elasticsearch_destination.go
119 lines (101 loc) · 3.1 KB
/
data_source_elasticsearch_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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
elastic7 "github.com/olivere/elastic/v7"
elastic6 "gopkg.in/olivere/elastic.v6"
)
const DESTINATION_NAME_FIELD = "destination.name.keyword"
func dataSourceElasticsearchDestination() *schema.Resource {
return &schema.Resource{
Read: dataSourceElasticsearchDestinationRead,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"body": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
},
}
}
func dataSourceElasticsearchDestinationRead(d *schema.ResourceData, m interface{}) error {
destinationName := d.Get("name").(string)
response := new(destinationResponse)
// See https://github.com/opendistro-for-elasticsearch/alerting/issues/70, no tags or API endpoint for searching destination
var id string
var body *json.RawMessage
var err error
switch m.(type) {
case *elastic7.Client:
client := m.(*elastic7.Client)
id, body, err = elastic7Search(client, DESTINATION_INDEX, destinationName)
case *elastic6.Client:
client := m.(*elastic6.Client)
id, body, err = elastic6Search(client, DESTINATION_INDEX, destinationName)
default:
err = errors.New("destination resource not implemented prior to Elastic v6")
}
if err != nil {
return err
} else if id == "" {
// short circuit
return nil
}
if err := json.Unmarshal(*body, response); err != nil {
return fmt.Errorf("error unmarshalling destination body: %+v: %+v", err, body)
}
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
simplifiedBody := map[string]string{}
for key, value := range response.Destination.(map[string]interface{}) {
if stringified, ok := value.(string); ok {
simplifiedBody[key] = stringified
} else {
log.Printf("[INFO] couldn't simplify: %+v", value)
}
}
d.Set("body", simplifiedBody)
return err
}
func elastic7Search(client *elastic7.Client, index string, name string) (string, *json.RawMessage, error) {
termQuery := elastic7.NewTermQuery(DESTINATION_NAME_FIELD, name)
result, err := client.Search().
Index(index).
Query(termQuery).
Do(context.TODO())
if err != nil {
return "", nil, err
}
if result.TotalHits() == 1 {
return result.Hits.Hits[0].Id, &result.Hits.Hits[0].Source, nil
} else if result.TotalHits() < 1 {
return "", nil, err
} else {
return "", nil, fmt.Errorf("1 result expected, found %d.", result.TotalHits())
}
}
func elastic6Search(client *elastic6.Client, index string, name string) (string, *json.RawMessage, error) {
termQuery := elastic6.NewTermQuery(DESTINATION_NAME_FIELD, name)
result, err := client.Search().
Index(index).
Query(termQuery).
Do(context.TODO())
if err != nil {
return "", nil, err
}
if result.TotalHits() == 1 {
return result.Hits.Hits[0].Id, result.Hits.Hits[0].Source, nil
} else if result.TotalHits() < 1 {
return "", nil, err
} else {
return "", nil, fmt.Errorf("1 result expected, found %d.", result.TotalHits())
}
}