forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_plugin.go
125 lines (101 loc) · 3.36 KB
/
cloud_plugin.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
package gapi
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Plugin struct {
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Version string `json:"version"`
Description string `json:"description"`
}
type CloudPluginInstallation struct {
ID int `json:"id"`
InstanceID int `json:"instanceId"`
InstanceURL string `json:"instanceUrl"`
InstanceSlug string `json:"instanceSlug"`
PluginID int `json:"pluginId"`
PluginSlug string `json:"pluginSlug"`
PluginName string `json:"pluginName"`
Version string `json:"version"`
}
// InstallCloudPlugin installs the specified plugin to the given stack.
func (c *Client) InstallCloudPlugin(stackSlug string, pluginSlug string, pluginVersion string) (*CloudPluginInstallation, error) {
installPluginRequest := struct {
Plugin string `json:"plugin"`
Version string `json:"version"`
}{
Plugin: pluginSlug,
Version: pluginVersion,
}
data, err := json.Marshal(installPluginRequest)
if err != nil {
return nil, err
}
var installation CloudPluginInstallation
err = c.request("POST", fmt.Sprintf("/api/instances/%s/plugins", stackSlug), nil, bytes.NewBuffer(data), &installation)
if err != nil {
return nil, err
}
return &installation, nil
}
// UninstallCloudPlugin uninstalls the specified plugin to the given stack.
func (c *Client) UninstallCloudPlugin(stackSlug string, pluginSlug string) error {
return c.request("DELETE", fmt.Sprintf("/api/instances/%s/plugins/%s", stackSlug, pluginSlug), nil, nil, nil)
}
// IsCloudPluginInstalled returns a boolean if the specified plugin is installed on the stack.
func (c *Client) IsCloudPluginInstalled(stackSlug string, pluginSlug string) (bool, error) {
req, err := c.newRequest("GET", fmt.Sprintf("/api/instances/%s/plugins/%s", stackSlug, pluginSlug), nil, nil)
if err != nil {
return false, err
}
resp, err := c.client.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusNotFound {
return false, nil
}
bodyContents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}
return false, fmt.Errorf("status: %d, body: %v", resp.StatusCode, string(bodyContents))
}
return true, nil
}
// GetCloudPluginInstallation returns the cloud plugin installation details for the specified plugin.
func (c *Client) GetCloudPluginInstallation(stackSlug string, pluginSlug string) (*CloudPluginInstallation, error) {
var installation CloudPluginInstallation
err := c.request("GET", fmt.Sprintf("/api/instances/%s/plugins/%s", stackSlug, pluginSlug), nil, nil, &installation)
if err != nil {
return nil, err
}
return &installation, nil
}
// PluginBySlug returns the plugin with the given slug.
// An error will be returned given an unknown slug.
func (c *Client) PluginBySlug(slug string) (*Plugin, error) {
p := Plugin{}
err := c.request("GET", fmt.Sprintf("/api/plugins/%s", slug), nil, nil, &p)
if err != nil {
return nil, err
}
return &p, nil
}
// PluginByID returns the plugin with the given id.
// An error will be returned given an unknown ID.
func (c *Client) PluginByID(pluginID int64) (*Plugin, error) {
p := Plugin{}
err := c.request("GET", fmt.Sprintf("/api/plugins/%d", pluginID), nil, nil, p)
if err != nil {
return nil, err
}
return &p, nil
}