/
cluster.go
135 lines (109 loc) · 3.69 KB
/
cluster.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
package pc
import (
"errors"
"fmt"
"net/http"
)
// ClusterService handles communication to the clusters REST API endpoint
type ClusterService Service
// ClusterListRequest is the request JSON sent to the API
type ClusterListRequest struct {
Filter string `json:"filter,omitempty"`
Kind string `json:"kind,omitempty"`
SortOrder string `json:"sort_order,omitempty"`
Offset int `json:"offset,omitempty"`
Length int `json:"length,omitempty"`
SortAttribute string `json:"sort_attribute,omitempty"`
}
// ClusterListResponse is the response received from the REST API
type ClusterListResponse struct {
Entities []Entities `json:"entities,omitempty"`
APIVersion string `json:"api_version,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
}
// ClusterGetRequest is the request used to identify a specific cluster to get...really just need the cluster UUID
type ClusterGetRequest struct {
UUID string `json:"uuid,omitempty"`
}
// ClusterGetResponse are the details associated with a cluster
type ClusterGetResponse struct {
Status Status `json:"status,omitempty"`
Spec Spec `json:"spec,omitempty"`
APIVersion string `json:"api_version,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
}
// ClusterUpdateRequest is used to update a cluster. The UUID of the cluster is needed along with
// the other data elements related to the update
type ClusterUpdateRequest struct {
UUID string `json:"uuid,omitempty"`
Spec Spec `json:"spec,omitempty"`
APIVersion string `json:"api_version,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
}
// ClusterUpdateResponse is the response from the update request
type ClusterUpdateResponse struct {
Status Status `json:"status,omitempty"`
Spec Spec `json:"spec,omitempty"`
APIVersion string `json:"api_version,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
}
// List makes the call to cluster list
func (cs *ClusterService) List(reqdata *ClusterListRequest) (*ClusterListResponse, *http.Response, error) {
u := "clusters/list"
u, err := addOptions(u, nil)
if err != nil {
return nil, nil, err
}
req, err := cs.client.NewRequest("POST", u, reqdata)
if err != nil {
return nil, nil, err
}
var clusters *ClusterListResponse
resp, err := cs.client.Do(req, &clusters)
if err != nil {
return nil, resp, err
}
return clusters, resp, nil
}
// Get passes as UUID of a specific cluster to get information about that cluster
func (cs *ClusterService) Get(reqdata *ClusterGetRequest) (*ClusterGetResponse, *http.Response, error) {
if reqdata == nil {
return nil, nil, errors.New("ClusterGetRequest cannot be nil")
}
if len(reqdata.UUID) < 8 {
return nil, nil, errors.New("invalid UUID format in ClusterGetRequest")
}
u := fmt.Sprintf("clusters/%s", reqdata.UUID)
u, err := addOptions(u, nil)
if err != nil {
return nil, nil, err
}
req, err := cs.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var cluster *ClusterGetResponse
resp, err := cs.client.Do(req, &cluster)
if err != nil {
return nil, resp, err
}
return cluster, resp, nil
}
// Update will update the cluster defined by the UUID with the provided information
func (cs *ClusterService) Update(reqdata *ClusterUpdateRequest) (*ClusterUpdateResponse, *http.Response, error) {
u := fmt.Sprintf("clusters/%s", reqdata.UUID)
u, err := addOptions(u, nil)
if err != nil {
return nil, nil, err
}
req, err := cs.client.NewRequest("PUT", u, reqdata)
if err != nil {
return nil, nil, err
}
var cluster *ClusterUpdateResponse
resp, err := cs.client.Do(req, &cluster)
if err != nil {
return nil, resp, err
}
return cluster, resp, nil
}