forked from phillbaker/terraform-provider-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_elasticsearch_watch.go
160 lines (133 loc) · 3.99 KB
/
resource_elasticsearch_watch.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
package main
import (
"context"
"errors"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
elastic7 "github.com/olivere/elastic/v7"
elastic6 "gopkg.in/olivere/elastic.v6"
)
func resourceElasticsearchWatch() *schema.Resource {
return &schema.Resource{
Create: resourceElasticsearchWatchCreate,
Read: resourceElasticsearchWatchRead,
Update: resourceElasticsearchWatchUpdate,
Delete: resourceElasticsearchWatchDelete,
Schema: map[string]*schema.Schema{
"watch_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"body": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}
func resourceElasticsearchWatchCreate(d *schema.ResourceData, m interface{}) error {
// Determine whether the watch already exists.
watchID := d.Get("watch_id").(string)
_, err := resourceElasticsearchGetWatch(watchID, m)
if !elastic6.IsNotFound(err) && !elastic7.IsNotFound(err) {
log.Printf("[INFO] watch exists: %+v", err)
return fmt.Errorf("watch already exists with ID: %v", watchID)
}
watchID, err = resourceElasticsearchPutWatch(d, m)
if err != nil {
log.Printf("[INFO] Failed to put watch: %+v", err)
return err
}
d.SetId(watchID)
log.Printf("[INFO] Object ID: %s", d.Id())
return resourceElasticsearchWatchRead(d, m)
}
func resourceElasticsearchWatchRead(d *schema.ResourceData, m interface{}) error {
res, err := resourceElasticsearchGetWatch(d.Id(), m)
if elastic6.IsNotFound(err) || elastic7.IsNotFound(err) {
log.Printf("[WARN] Watch (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return err
}
switch m.(type) {
case *elastic7.Client:
watchResponse := res.(*elastic7.XPackWatcherGetWatchResponse)
d.Set("body", watchResponse.Watch)
case *elastic6.Client:
watchResponse := res.(*elastic6.XPackWatcherGetWatchResponse)
d.Set("body", watchResponse.Watch)
}
// response := new(watcherGetWatchResponse)
// if err := json.Unmarshal(res.Body, response); err != nil {
// return fmt.Errorf("error unmarshalling watch body: %+v: %+v", err, res.Body)
// }
d.Set("watch_id", d.Id())
return nil
}
func resourceElasticsearchWatchUpdate(d *schema.ResourceData, m interface{}) error {
_, err := resourceElasticsearchPutWatch(d, m)
if err != nil {
return err
}
return resourceElasticsearchWatchRead(d, m)
}
func resourceElasticsearchWatchDelete(d *schema.ResourceData, m interface{}) error {
var err error
switch m.(type) {
case *elastic7.Client:
client := m.(*elastic7.Client)
_, err = client.XPackWatchDelete(d.Id()).Do(context.TODO())
case *elastic6.Client:
client := m.(*elastic6.Client)
_, err = client.XPackWatchDelete(d.Id()).Do(context.TODO())
default:
err = errors.New("watch resource not implemented prior to Elastic v6")
}
return err
}
func resourceElasticsearchGetWatch(watchID string, m interface{}) (interface{}, error) {
var res interface{}
var err error
switch m.(type) {
case *elastic7.Client:
client := m.(*elastic7.Client)
res, err = client.XPackWatchGet(watchID).Do(context.TODO())
case *elastic6.Client:
client := m.(*elastic6.Client)
res, err = client.XPackWatchGet(watchID).Do(context.TODO())
default:
err = errors.New("watch resource not implemented prior to Elastic v6")
}
return res, err
}
func resourceElasticsearchPutWatch(d *schema.ResourceData, m interface{}) (string, error) {
watchID := d.Get("watch_id").(string)
watchJSON := d.Get("body").(string)
var err error
switch m.(type) {
case *elastic7.Client:
client := m.(*elastic7.Client)
_, err = client.XPackWatchPut(watchID).
Body(watchJSON).
Do(context.TODO())
case *elastic6.Client:
client := m.(*elastic6.Client)
_, err = client.XPackWatchPut(watchID).
Body(watchJSON).
Do(context.TODO())
default:
err = errors.New("watch resource not implemented prior to Elastic v6")
}
if err != nil {
return "", err
}
return watchID, nil
}