-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource_kibana_saved_object.go
173 lines (151 loc) · 4.92 KB
/
resource_kibana_saved_object.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
package main
import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
_ "github.com/hashicorp/terraform/terraform"
"log"
"fmt"
_ "errors"
"encoding/json"
)
func resourceKibanaSavedObject() *schema.Resource {
return &schema.Resource{
Create: resourceElasticSavedObjectCreate,
Read: resourceElasticSavedObjectRead,
Update: resourceElasticSavedObjectUpdate,
Delete: resourceElasticSavedObjectDelete,
Schema: map[string]*schema.Schema{
"space_id": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},
"saved_object_type": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateFunc: validation.StringInSlice([]string{ "index-pattern", "visualization", "search", "timelion-sheet","dashboard",}, false),
},
"object_id": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"attributes": &schema.Schema{
Type: schema.TypeString,
Default: "",
Optional: true,
},
},
}
}
func resourceElasticSavedObjectCreate(d *schema.ResourceData, meta interface{}) error {
url := kibanaUrl(d, meta)
username := meta.(*ElasticInfo).kibanaUsername
password := meta.(*ElasticInfo).kibanaPassword
attributes := d.Get("attributes").(string)
saved_object_type := d.Get("saved_object_type").(string)
url = fmt.Sprintf("%v/api/saved_objects/%v", url, saved_object_type)
var savedObjectHeader SavedObjectHeader
json.Unmarshal([]byte(attributes), &savedObjectHeader.Attributes)
if savedObjectHeader.Attributes == nil {
savedObjectHeader.Attributes=make(map[string]interface{})
}
name, found := d.GetOk("name")
if found {
savedObjectHeader.Attributes["title"]=name.(string)
}
body, err := json.Marshal(&savedObjectHeader)
if err != nil {
return err
}
log.Printf("Create: %v", body)
respBody, err := postKibRequest(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 resourceElasticSavedObjectRead(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)
switch err := err.(type) {
case nil:
var savedObject SavedObjectHeader
json.Unmarshal(*respBody, &savedObject)
return nil
case *RestError:
if err.StatusCode == 404 {
}
return err
default:
return err
}
}
func resourceElasticSavedObjectUpdate(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)
name, found := d.GetOk("name")
if found {
savedObjectHeader.Attributes["title"]=name.(string)
}
body, err := json.Marshal(&savedObjectHeader)
if err != nil {
return err
}
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 resourceElasticSavedObjectDelete(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)
_, err := deleteKibRequest(d, meta, url, username, password)
if err != nil {
return err
}
d.Set("version", nil)
d.SetId("")
return nil
}