-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
354 lines (297 loc) · 9.42 KB
/
client.go
File metadata and controls
354 lines (297 loc) · 9.42 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
)
var BaseURL = "https://customer.cloudamqp.com/api"
type Client struct {
apiKey string
baseURL string
httpClient *http.Client
version string
}
func New(apiKey, version string) *Client {
baseURL := "https://customer.cloudamqp.com/api"
if envURL := os.Getenv("CLOUDAMQP_API_URL"); envURL != "" {
baseURL = envURL
}
return &Client{
apiKey: apiKey,
baseURL: baseURL,
httpClient: &http.Client{},
version: version,
}
}
func NewWithBaseURL(apiKey, baseURL, version string) *Client {
return &Client{
apiKey: apiKey,
baseURL: baseURL,
httpClient: &http.Client{},
version: version,
}
}
// NewWithHTTPClient creates a new client with a custom HTTP client.
// This is useful for testing with tools like go-vcr.
func NewWithHTTPClient(apiKey, baseURL, version string, httpClient *http.Client) *Client {
return &Client{
apiKey: apiKey,
baseURL: baseURL,
httpClient: httpClient,
version: version,
}
}
func (c *Client) makeRequest(method, endpoint string, body any) ([]byte, error) {
var reqBody io.Reader
var contentType string
if body != nil {
switch v := body.(type) {
case url.Values:
contentType = "application/x-www-form-urlencoded"
reqBody = strings.NewReader(v.Encode())
default:
contentType = "application/json"
jsonData, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("failed to marshal request body: %w", err)
}
reqBody = bytes.NewReader(jsonData)
}
}
req, err := http.NewRequest(method, c.baseURL+endpoint, reqBody)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.SetBasicAuth("", c.apiKey)
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
req.Header.Set("User-Agent", fmt.Sprintf("cloudamqp-cli/%s", c.version))
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
}
if resp.StatusCode >= 400 {
var errorResp struct {
Error string `json:"error"`
}
if err := json.Unmarshal(respBody, &errorResp); err == nil && errorResp.Error != "" {
return nil, fmt.Errorf("API error (%d): %s", resp.StatusCode, errorResp.Error)
}
return nil, fmt.Errorf("API error (%d): %s", resp.StatusCode, string(respBody))
}
return respBody, nil
}
// Instance-specific operations using /instances/{id}/ endpoints
// Node management
type Node struct {
Name string `json:"name"`
Hostname string `json:"hostname"`
Configured bool `json:"configured"`
HiPE bool `json:"hipe"`
RabbitMQVersion string `json:"rabbitmq_version"`
ErlangVersion string `json:"erlang_version"`
Running bool `json:"running"`
DiskSize int `json:"disk_size"`
AdditionalDiskSize int `json:"additional_disk_size"`
AvailabilityZone string `json:"availability_zone"`
HostnameInternal string `json:"hostname_internal"`
}
func (c *Client) ListNodes(instanceID string) ([]Node, error) {
endpoint := "/instances/" + instanceID + "/nodes"
respBody, err := c.makeRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
var nodes []Node
if err := json.Unmarshal(respBody, &nodes); err != nil {
return nil, err
}
return nodes, nil
}
// Plugin management
type Plugin struct {
Name string `json:"name"`
Version string `json:"version"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
}
func (c *Client) ListPlugins(instanceID string) ([]Plugin, error) {
endpoint := "/instances/" + instanceID + "/plugins"
respBody, err := c.makeRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
var plugins []Plugin
if err := json.Unmarshal(respBody, &plugins); err != nil {
return nil, err
}
return plugins, nil
}
func (c *Client) EnablePlugin(instanceID, pluginName string) error {
endpoint := "/instances/" + instanceID + "/plugins"
requestBody := map[string]string{
"plugin_name": pluginName,
}
_, err := c.makeRequest("POST", endpoint, requestBody)
return err
}
func (c *Client) DisablePlugin(instanceID, pluginName string) error {
endpoint := "/instances/" + instanceID + "/plugins/" + pluginName
_, err := c.makeRequest("DELETE", endpoint, nil)
return err
}
// Account operations
func (c *Client) RotatePassword(instanceID string) error {
endpoint := "/instances/" + instanceID + "/account/rotate-password"
_, err := c.makeRequest("POST", endpoint, nil)
return err
}
func (c *Client) RotateInstanceAPIKey(instanceID string) error {
endpoint := "/instances/" + instanceID + "/account/rotate-apikey"
_, err := c.makeRequest("POST", endpoint, nil)
return err
}
// Action operations
type ActionRequest struct {
Nodes []string `json:"nodes,omitempty"`
}
type HiPERequest struct {
Enable bool `json:"enable"`
Nodes []string `json:"nodes,omitempty"`
}
type FirehoseRequest struct {
Enable bool `json:"enable"`
VHost string `json:"vhost"`
}
type UpgradeRequest struct {
Version string `json:"version"`
}
type VersionInfo struct {
RabbitMQVersions []string `json:"rabbitmq_versions"`
ErlangVersions []string `json:"erlang_versions"`
LavinMQVersions []string `json:"lavinmq_versions"`
}
func (c *Client) ToggleHiPE(instanceID string, req *HiPERequest) error {
endpoint := "/instances/" + instanceID + "/actions/hipe"
_, err := c.makeRequest("PUT", endpoint, req)
return err
}
func (c *Client) ToggleFirehose(instanceID string, req *FirehoseRequest) error {
endpoint := "/instances/" + instanceID + "/actions/firehose"
_, err := c.makeRequest("PUT", endpoint, req)
return err
}
func (c *Client) RestartRabbitMQ(instanceID string, nodes []string) error {
endpoint := "/instances/" + instanceID + "/actions/restart"
req := ActionRequest{Nodes: nodes}
_, err := c.makeRequest("POST", endpoint, req)
return err
}
func (c *Client) RestartCluster(instanceID string) error {
endpoint := "/instances/" + instanceID + "/actions/cluster-restart"
_, err := c.makeRequest("POST", endpoint, nil)
return err
}
func (c *Client) StopCluster(instanceID string) error {
endpoint := "/instances/" + instanceID + "/actions/cluster-stop"
_, err := c.makeRequest("POST", endpoint, nil)
return err
}
func (c *Client) StartCluster(instanceID string) error {
endpoint := "/instances/" + instanceID + "/actions/cluster-start"
_, err := c.makeRequest("POST", endpoint, nil)
return err
}
func (c *Client) RestartManagement(instanceID string, nodes []string) error {
endpoint := "/instances/" + instanceID + "/actions/mgmt-restart"
req := ActionRequest{Nodes: nodes}
_, err := c.makeRequest("POST", endpoint, req)
return err
}
func (c *Client) StopInstance(instanceID string, nodes []string) error {
endpoint := "/instances/" + instanceID + "/actions/stop"
req := ActionRequest{Nodes: nodes}
_, err := c.makeRequest("POST", endpoint, req)
return err
}
func (c *Client) StartInstance(instanceID string, nodes []string) error {
endpoint := "/instances/" + instanceID + "/actions/start"
req := ActionRequest{Nodes: nodes}
_, err := c.makeRequest("POST", endpoint, req)
return err
}
func (c *Client) RebootInstance(instanceID string, nodes []string) error {
endpoint := "/instances/" + instanceID + "/actions/reboot"
req := ActionRequest{Nodes: nodes}
_, err := c.makeRequest("POST", endpoint, req)
return err
}
func (c *Client) UpgradeErlang(instanceID string) error {
endpoint := "/instances/" + instanceID + "/actions/upgrade-erlang"
_, err := c.makeRequest("POST", endpoint, nil)
return err
}
func (c *Client) UpgradeRabbitMQ(instanceID string, version string) error {
endpoint := "/instances/" + instanceID + "/actions/upgrade-rabbitmq"
req := UpgradeRequest{Version: version}
_, err := c.makeRequest("POST", endpoint, req)
return err
}
func (c *Client) UpgradeRabbitMQErlang(instanceID string) error {
endpoint := "/instances/" + instanceID + "/actions/upgrade-rabbitmq-erlang"
_, err := c.makeRequest("POST", endpoint, nil)
return err
}
func (c *Client) GetAvailableVersions(instanceID string) (*VersionInfo, error) {
endpoint := "/instances/" + instanceID + "/nodes/available-versions"
respBody, err := c.makeRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
var versions VersionInfo
if err := json.Unmarshal(respBody, &versions); err != nil {
return nil, err
}
return &versions, nil
}
func (c *Client) GetUpgradeVersions(instanceID string) (map[string]string, error) {
endpoint := "/instances/" + instanceID + "/actions/new-rabbitmq-erlang-versions"
respBody, err := c.makeRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
var versions map[string]string
if err := json.Unmarshal(respBody, &versions); err != nil {
return nil, err
}
return versions, nil
}
// RabbitMQ Config operations
func (c *Client) GetRabbitMQConfig(instanceID string) (map[string]interface{}, error) {
endpoint := "/instances/" + instanceID + "/config"
respBody, err := c.makeRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
var config map[string]interface{}
if err := json.Unmarshal(respBody, &config); err != nil {
return nil, err
}
return config, nil
}
func (c *Client) UpdateRabbitMQConfig(instanceID string, config map[string]interface{}) error {
endpoint := "/instances/" + instanceID + "/config"
_, err := c.makeRequest("PUT", endpoint, config)
return err
}