forked from phillbaker/terraform-provider-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_elasticsearch_monitor.go
242 lines (201 loc) · 5.9 KB
/
resource_elasticsearch_monitor.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/olivere/elastic/uritemplates"
elastic7 "github.com/olivere/elastic/v7"
elastic6 "gopkg.in/olivere/elastic.v6"
)
func resourceElasticsearchMonitor() *schema.Resource {
return &schema.Resource{
Create: resourceElasticsearchMonitorCreate,
Read: resourceElasticsearchMonitorRead,
Update: resourceElasticsearchMonitorUpdate,
Delete: resourceElasticsearchMonitorDelete,
Schema: map[string]*schema.Schema{
"body": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.ValidateJsonString,
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}
func resourceElasticsearchMonitorCreate(d *schema.ResourceData, m interface{}) error {
res, err := resourceElasticsearchPostMonitor(d, m)
if err != nil {
log.Printf("[INFO] Failed to put monitor: %+v", err)
return err
}
d.SetId(res.ID)
d.Set("body", res.Monitor)
log.Printf("[INFO] Object ID: %s", d.Id())
return nil
}
func resourceElasticsearchMonitorRead(d *schema.ResourceData, m interface{}) error {
res, err := resourceElasticsearchGetMonitor(d.Id(), m)
if elastic6.IsNotFound(err) || elastic7.IsNotFound(err) {
log.Printf("[WARN] Monitor (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return err
}
d.Set("body", res.Monitor)
d.SetId(res.ID)
return nil
}
func resourceElasticsearchMonitorUpdate(d *schema.ResourceData, m interface{}) error {
_, err := resourceElasticsearchPutMonitor(d, m)
if err != nil {
return err
}
return resourceElasticsearchMonitorRead(d, m)
}
func resourceElasticsearchMonitorDelete(d *schema.ResourceData, m interface{}) error {
var err error
path, err := uritemplates.Expand("/_opendistro/_alerting/monitors/{id}", map[string]string{
"id": d.Id(),
})
if err != nil {
return fmt.Errorf("error building URL path for monitor: %+v", err)
}
switch client := m.(type) {
case *elastic7.Client:
_, err = client.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "DELETE",
Path: path,
})
case *elastic6.Client:
_, err = client.PerformRequest(context.TODO(), elastic6.PerformRequestOptions{
Method: "DELETE",
Path: path,
})
default:
err = errors.New("monitor resource not implemented prior to Elastic v6")
}
return err
}
func resourceElasticsearchGetMonitor(monitorID string, m interface{}) (*monitorResponse, error) {
var err error
response := new(monitorResponse)
path, err := uritemplates.Expand("/_opendistro/_alerting/monitors/{id}", map[string]string{
"id": monitorID,
})
if err != nil {
return response, fmt.Errorf("error building URL path for monitor: %+v", err)
}
var body json.RawMessage
switch client := m.(type) {
case *elastic7.Client:
var res *elastic7.Response
res, err = client.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "GET",
Path: path,
})
body = res.Body
case *elastic6.Client:
var res *elastic6.Response
res, err = client.PerformRequest(context.TODO(), elastic6.PerformRequestOptions{
Method: "GET",
Path: path,
})
body = res.Body
default:
err = errors.New("monitor resource not implemented prior to Elastic v6")
}
if err != nil {
return response, err
}
if err := json.Unmarshal(body, response); err != nil {
return response, fmt.Errorf("error unmarshalling monitor body: %+v: %+v", err, body)
}
return response, err
}
func resourceElasticsearchPostMonitor(d *schema.ResourceData, m interface{}) (*monitorResponse, error) {
monitorJSON := d.Get("body").(string)
var err error
response := new(monitorResponse)
path := "/_opendistro/_alerting/monitors/"
var body json.RawMessage
switch client := m.(type) {
case *elastic7.Client:
var res *elastic7.Response
res, err = client.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "POST",
Path: path,
Body: monitorJSON,
})
body = res.Body
case *elastic6.Client:
var res *elastic6.Response
res, err = client.PerformRequest(context.TODO(), elastic6.PerformRequestOptions{
Method: "POST",
Path: path,
Body: monitorJSON,
})
body = res.Body
default:
err = errors.New("monitor resource not implemented prior to Elastic v6")
}
if err != nil {
return response, err
}
if err := json.Unmarshal(body, response); err != nil {
return response, fmt.Errorf("error unmarshalling monitor body: %+v: %+v", err, body)
}
return response, nil
}
func resourceElasticsearchPutMonitor(d *schema.ResourceData, m interface{}) (*monitorResponse, error) {
monitorJSON := d.Get("body").(string)
var err error
response := new(monitorResponse)
path, err := uritemplates.Expand("/_opendistro/_alerting/monitors/{id}", map[string]string{
"id": d.Id(),
})
if err != nil {
return response, fmt.Errorf("error building URL path for monitor: %+v", err)
}
var body json.RawMessage
switch client := m.(type) {
case *elastic7.Client:
var res *elastic7.Response
res, err = client.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "PUT",
Path: path,
Body: monitorJSON,
})
body = res.Body
case *elastic6.Client:
var res *elastic6.Response
res, err = client.PerformRequest(context.TODO(), elastic6.PerformRequestOptions{
Method: "PUT",
Path: path,
Body: monitorJSON,
})
body = res.Body
default:
err = errors.New("monitor resource not implemented prior to Elastic v6")
}
if err != nil {
return response, err
}
if err := json.Unmarshal(body, response); err != nil {
return response, fmt.Errorf("error unmarshalling monitor body: %+v: %+v", err, body)
}
return response, nil
}
type monitorResponse struct {
Version int `json:"_version"`
ID string `json:"_id"`
Monitor interface{} `json:"monitor"`
}