-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclusters.go
More file actions
221 lines (154 loc) · 3.64 KB
/
clusters.go
File metadata and controls
221 lines (154 loc) · 3.64 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
package rac
import "errors"
type ClustersCommandType string
func (c ClustersCommandType) Check(params map[string]string) error {
var err error
switch c {
case ClustersListCommand:
if _, ok := params["--cluster"]; ok {
delete(params, "--cluster")
}
case ClustersInfoCommand:
if val, ok := params["--cluster"]; !ok || len(val) == 0 {
err = errors.New("cluster must be identified")
}
case ClustersRemoveCommand, ClustersUpdateCommand:
if val, ok := params["--cluster"]; !ok || len(val) == 0 {
err = errors.New("cluster must be identified")
}
}
return err
}
func (c ClustersCommandType) Command() string {
return string(c)
}
const (
baseClustersCommand ClustersCommandType = "cluster"
ClustersListCommand = baseClustersCommand + " list"
ClustersInfoCommand = baseClustersCommand + " info"
ClustersInsertCommand = baseClustersCommand + " insert"
ClustersRemoveCommand = baseClustersCommand + " remove"
ClustersUpdateCommand = baseClustersCommand + " update"
)
type ClustersList struct {
}
func (_ ClustersList) Command() DoCommand {
return ClustersListCommand
}
func (i ClustersList) Values() map[string]string {
return map[string]string{}
}
func (i ClustersList) Parse(res *RawRespond) error {
var list []ClusterInfo
if !res.Status {
return res.Error
}
err := Unmarshal(res.Raw, &list)
res.Error = err
res.ParsedRespond = list
return err
}
type ClustersInfo struct {
UUID string
}
func (i ClustersInfo) Values() map[string]string {
return map[string]string{
"--cluster": i.UUID,
}
}
func (i ClustersInfo) Parse(res *RawRespond) error {
var info ClusterInfo
if !res.Status {
return res.Error
}
err := Unmarshal(res.Raw, &info)
res.Error = err
res.ParsedRespond = info
return err
}
func (_ ClustersInfo) Command() DoCommand {
return ClustersInfoCommand
}
type ClustersUpdate struct {
ClusterInfo
Auth
}
func (_ ClustersUpdate) Command() DoCommand {
return ClustersUpdateCommand
}
func (i ClustersUpdate) Values() map[string]string {
return map[string]string{
"--cluster": i.UUID,
"--agent-user": i.User,
"--agent-pwd": i.Pwd,
}
}
func (i ClustersUpdate) Parse(res *RawRespond) error {
if !res.Status {
return res.Error
}
return nil
}
type ClustersInsert struct {
ClusterInfo
Auth
}
func (_ ClustersInsert) Command() DoCommand {
return ClustersInsertCommand
}
func (i ClustersInsert) Values() map[string]string {
return map[string]string{
"--cluster": i.UUID,
"--agent-user": i.User,
"--agent-pwd": i.Pwd,
}
}
func (i ClustersInsert) Parse(res *RawRespond) error {
if !res.Status {
return res.Error
}
return nil
}
type ClustersRemove struct {
ClusterInfo
Auth
}
func (_ ClustersRemove) Command() DoCommand {
return ClustersRemoveCommand
}
func (i ClustersRemove) Values() map[string]string {
return map[string]string{
"--cluster": i.UUID,
"--cluster-user": i.User,
"--cluster-pwd": i.Pwd,
}
}
func (i ClustersRemove) Parse(res *RawRespond) error {
if !res.Status {
return res.Error
}
return nil
}
type ClustersRespond struct {
*RawRespond
List []ClusterInfo
Info ClusterInfo
}
func (m *Manager) Clusters(what interface{}, opts ...interface{}) (respond ClustersRespond, err error) {
val, ok := what.(Valued)
if !ok {
return respond, ErrUnsupportedWhat
}
respond.RawRespond, err = m.Do(val, opts...)
if err != nil {
return respond, err
}
switch v := respond.ParsedRespond.(type) {
case ClusterInfo:
respond.Info = v
respond.List = append(respond.List, v)
case []ClusterInfo:
respond.List = v
}
return respond, nil
}