forked from ewilde/go-kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
space.go
112 lines (97 loc) · 2.99 KB
/
space.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
package kibana
import (
"encoding/json"
"fmt"
)
// SpaceClient declares the required methods to implement to be a client and manage spaces
type SpaceClient interface {
Create(request *Space) error
Update(request *Space) error
GetByID(id string) (*Space, error)
Delete(id string) error
}
// Space is the api definition of a space in kibana
// can be used to create, update and get a space
type Space struct {
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Color string `json:"color,omitempty"`
Initials string `json:"initials,omitempty"`
ImageUrl string `json:"imageUrl,omitempty"`
DisabledFeatures []string `json:"disabledFeatures,omitempty"`
}
// DefaultSpaceClient structure to enable operations on Spaces
// implements SpaceClient
type DefaultSpaceClient struct {
config *Config
client *HttpAgent
}
// Create creates a space
// based on https://www.elastic.co/guide/en/kibana/current/spaces-api-post.html
func (api *DefaultSpaceClient) Create(request *Space) error {
response, body, err := api.client.
Post(api.config.KibanaBaseUri+"/api/spaces/space").
Set("kbn-version", api.config.KibanaVersion).
Send(request).
End()
if err != nil {
return err[0]
}
if response.StatusCode >= 300 {
return NewError(response, body, "Could not create space")
}
return nil
}
// Update updates a space
// based on https://www.elastic.co/guide/en/kibana/master/spaces-api-put.html
func (api *DefaultSpaceClient) Update(request *Space) error {
id := request.Id
response, body, err := api.client.
Put(api.config.KibanaBaseUri+"/api/spaces/space/"+id).
Set("kbn-version", api.config.KibanaVersion).
Send(request).
End()
if err != nil {
return err[0]
}
if response.StatusCode >= 300 {
return NewError(response, body, "Could not update space")
}
return nil
}
// GetByID fetch an existing space
// https://www.elastic.co/guide/en/kibana/master/spaces-api-get.html
func (api *DefaultSpaceClient) GetByID(id string) (*Space, error) {
response, body, err := api.client.
Get(api.config.KibanaBaseUri+"/api/spaces/space/"+id).
Set("kbn-version", api.config.KibanaVersion).
End()
if err != nil {
return nil, err[0]
}
if response.StatusCode >= 300 {
return nil, NewError(response, body, "Could not fetch space")
}
space := &Space{}
error := json.Unmarshal([]byte(body), space)
if error != nil {
return nil, fmt.Errorf("could not parse fields from update visualization response, error: %v", error)
}
return space, nil
}
// Delete an existing space
// based on https://www.elastic.co/guide/en/kibana/master/spaces-api-delete.html
func (api *DefaultSpaceClient) Delete(id string) error {
response, body, err := api.client.
Delete(api.config.KibanaBaseUri+"/api/spaces/space/"+id).
Set("kbn-version", api.config.KibanaVersion).
End()
if err != nil {
return err[0]
}
if response.StatusCode >= 300 {
return NewError(response, body, "Could not delete space")
}
return nil
}