-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource_kibana_saved_object_content.go
174 lines (148 loc) · 5.06 KB
/
resource_kibana_saved_object_content.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
package main
import (
"github.com/hashicorp/terraform/helper/schema"
_ "github.com/hashicorp/terraform/terraform"
"log"
"fmt"
"errors"
"encoding/json"
)
func resourceKibanaSavedObjectContent() *schema.Resource {
return &schema.Resource{
Create: resourceElasticSavedObjectContentCreate,
Read: resourceElasticSavedObjectContentRead,
Update: resourceElasticSavedObjectContentUpdate,
Delete: resourceElasticSavedObjectContentDelete,
Schema: map[string]*schema.Schema{
"space_id": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},
"saved_object_id": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"saved_object_type": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"attributes": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceElasticSavedObjectContentCreate(d *schema.ResourceData, meta interface{}) error {
url := kibanaUrl(d, meta)
username := meta.(*ElasticInfo).kibanaUsername
password := meta.(*ElasticInfo).kibanaPassword
attributes := d.Get("attributes").(string)
object_id := d.Get("saved_object_id").(string)
saved_object_type := d.Get("saved_object_type").(string)
url = fmt.Sprintf("%v/api/saved_objects/%v/%v", url, saved_object_type, object_id)
existingSavedObjectBytes, err := getKibRequest(d, meta, url, username, password)
if err != nil {
return err
}
var existingSavedObject SavedObjectHeader
json.Unmarshal(*existingSavedObjectBytes, &existingSavedObject)
existingSavedObject.UpdatedAt=""
existingSavedObject.Id=""
existingSavedObject.ObjectType=""
if len(existingSavedObject.Attributes) != 0 {
errors.New(fmt.Sprintf("Existing Object: %s, already has content: %v", object_id, existingSavedObject.Attributes))
}
json.Unmarshal([]byte(attributes), &existingSavedObject.Attributes)
body, err := json.Marshal(&existingSavedObject)
if err != nil {
return err
}
respBody, err := putKibRequest(d, meta, url, username, password, string(body))
if err != nil {
return err
}
var savedObject SavedObjectHeader
json.Unmarshal(*respBody, &savedObject)
log.Printf("Raw Body: %s", respBody)
log.Printf("ID: %s", savedObject.Id)
log.Printf("UpdatedAt: %s", savedObject.UpdatedAt)
log.Printf("Version: %v", savedObject.Version)
d.SetId(savedObject.Id)
d.Set("version", savedObject.Version)
return err
}
func resourceElasticSavedObjectContentRead(d *schema.ResourceData, meta interface{}) error {
url := kibanaUrl(d, meta)
username := meta.(*ElasticInfo).kibanaUsername
password := meta.(*ElasticInfo).kibanaPassword
id := d.Id()
saved_object_type := d.Get("saved_object_type").(string)
url = fmt.Sprintf("%v/api/saved_objects/%v/%v", url, saved_object_type, id)
respBody, err := getKibRequest(d, meta, url, username, password)
if err != nil {
return err
}
var savedObject SavedObjectHeader
json.Unmarshal(*respBody, &savedObject)
return nil
}
func resourceElasticSavedObjectContentUpdate(d *schema.ResourceData, meta interface{}) error {
url := kibanaUrl(d, meta)
username := meta.(*ElasticInfo).kibanaUsername
password := meta.(*ElasticInfo).kibanaPassword
id := d.Id()
saved_object_type := d.Get("saved_object_type").(string)
attributes := d.Get("attributes").(string)
var savedObjectHeader SavedObjectHeader
json.Unmarshal([]byte(attributes), &savedObjectHeader.Attributes)
body, err := json.Marshal(&savedObjectHeader)
if err != nil {
return err
}
savedObjectHeader.UpdatedAt=""
savedObjectHeader.Id=""
savedObjectHeader.ObjectType=""
url = fmt.Sprintf("%v/api/saved_objects/%v/%v", url, saved_object_type, id)
respBody, err := putKibRequest(d, meta, url, username, password, string(body))
if err != nil {
return err
}
var savedObject SavedObjectHeader
json.Unmarshal(*respBody, &savedObject)
log.Printf("Raw Body: %s", respBody)
log.Printf("ID: %s", savedObject.Id)
log.Printf("UpdatedAt: %s", savedObject.UpdatedAt)
log.Printf("Version: %v", savedObject.Version)
d.Set("version", savedObject.Version)
return nil
}
func resourceElasticSavedObjectContentDelete(d *schema.ResourceData, meta interface{}) error {
url := kibanaUrl(d, meta)
username := meta.(*ElasticInfo).kibanaUsername
password := meta.(*ElasticInfo).kibanaPassword
id := d.Id()
saved_object_type := d.Get("saved_object_type").(string)
url = fmt.Sprintf("%v/api/saved_objects/%v/%v", url, saved_object_type, id)
respBody, err := getKibRequest(d, meta, url, username, password)
if err != nil {
return err
}
var savedObject SavedObjectHeader
json.Unmarshal(*respBody, &savedObject)
savedObject.Attributes = make(map[string]interface{})
savedObject.UpdatedAt=""
savedObject.Id=""
savedObject.ObjectType=""
body, err := json.Marshal(&savedObject)
_, err = putKibRequest(d, meta, url, username, password, string(body))
if err != nil {
return err
}
d.Set("version", nil)
d.SetId("")
return nil
}