forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
357 lines (336 loc) · 10.7 KB
/
client.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
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
355
356
357
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package storage
import (
"time"
"github.com/juju/errors"
"github.com/juju/names/v4"
"github.com/DavinZhang/juju/api/base"
"github.com/DavinZhang/juju/apiserver/params"
"github.com/DavinZhang/juju/storage"
)
// Client allows access to the storage API end point.
type Client struct {
base.ClientFacade
facade base.FacadeCaller
}
// NewClient creates a new client for accessing the storage API.
func NewClient(st base.APICallCloser) *Client {
frontend, backend := base.NewClientFacade(st, "Storage")
return &Client{ClientFacade: frontend, facade: backend}
}
// StorageDetails retrieves details about desired storage instances.
func (c *Client) StorageDetails(tags []names.StorageTag) ([]params.StorageDetailsResult, error) {
found := params.StorageDetailsResults{}
entities := make([]params.Entity, len(tags))
for i, tag := range tags {
entities[i] = params.Entity{Tag: tag.String()}
}
if err := c.facade.FacadeCall("StorageDetails", params.Entities{Entities: entities}, &found); err != nil {
return nil, errors.Trace(err)
}
return found.Results, nil
}
// ListStorageDetails lists all storage.
func (c *Client) ListStorageDetails() ([]params.StorageDetails, error) {
args := params.StorageFilters{
[]params.StorageFilter{{}}, // one empty filter
}
var results params.StorageDetailsListResults
if err := c.facade.FacadeCall("ListStorageDetails", args, &results); err != nil {
return nil, errors.Trace(err)
}
if len(results.Results) != 1 {
return nil, errors.Errorf(
"expected 1 result, got %d",
len(results.Results),
)
}
if results.Results[0].Error != nil {
return nil, errors.Trace(results.Results[0].Error)
}
return results.Results[0].Result, nil
}
// ListPools returns a list of pools that matches given filter.
// If no filter was provided, a list of all pools is returned.
func (c *Client) ListPools(providers, names []string) ([]params.StoragePool, error) {
args := params.StoragePoolFilters{
Filters: []params.StoragePoolFilter{{
Names: names,
Providers: providers,
}},
}
var results params.StoragePoolsResults
if err := c.facade.FacadeCall("ListPools", args, &results); err != nil {
return nil, errors.Trace(err)
}
if len(results.Results) != 1 {
return nil, errors.Errorf("expected 1 result, got %d", len(results.Results))
}
if err := results.Results[0].Error; err != nil {
return nil, err
}
return results.Results[0].Result, nil
}
// CreatePool creates pool with specified parameters.
func (c *Client) CreatePool(pname, provider string, attrs map[string]interface{}) error {
// Older facade did not support bulk calls.
if c.BestAPIVersion() < 5 {
args := params.StoragePool{
Name: pname,
Provider: provider,
Attrs: attrs,
}
return c.facade.FacadeCall("CreatePool", args, nil)
}
var results params.ErrorResults
args := params.StoragePoolArgs{
Pools: []params.StoragePool{{
Name: pname,
Provider: provider,
Attrs: attrs,
}},
}
if err := c.facade.FacadeCall("CreatePool", args, &results); err != nil {
return errors.Trace(err)
}
return results.OneError()
}
// RemovePool removes the named pool
func (c *Client) RemovePool(pname string) error {
if c.BestAPIVersion() < 5 {
return errors.New("removing storage pools is not supported by this version of Juju")
}
var results params.ErrorResults
args := params.StoragePoolDeleteArgs{
Pools: []params.StoragePoolDeleteArg{{
Name: pname,
}},
}
if err := c.facade.FacadeCall("RemovePool", args, &results); err != nil {
return errors.Trace(err)
}
return results.OneError()
}
// UpdatePool updates a pool with specified parameters.
func (c *Client) UpdatePool(pname, provider string, attrs map[string]interface{}) error {
if c.BestAPIVersion() < 5 {
return errors.New("updating storage pools is not supported by this version of Juju")
}
var results params.ErrorResults
args := params.StoragePoolArgs{
Pools: []params.StoragePool{{
Name: pname,
Provider: provider,
Attrs: attrs,
}},
}
if err := c.facade.FacadeCall("UpdatePool", args, &results); err != nil {
return errors.Trace(err)
}
return results.OneError()
}
// ListVolumes lists volumes for desired machines.
// If no machines provided, a list of all volumes is returned.
func (c *Client) ListVolumes(machines []string) ([]params.VolumeDetailsListResult, error) {
filters := make([]params.VolumeFilter, len(machines))
for i, machine := range machines {
filters[i].Machines = []string{names.NewMachineTag(machine).String()}
}
if len(filters) == 0 {
filters = []params.VolumeFilter{{}}
}
args := params.VolumeFilters{filters}
var results params.VolumeDetailsListResults
if err := c.facade.FacadeCall("ListVolumes", args, &results); err != nil {
return nil, errors.Trace(err)
}
if len(results.Results) != len(filters) {
return nil, errors.Errorf(
"expected %d result(s), got %d",
len(filters), len(results.Results),
)
}
return results.Results, nil
}
// ListFilesystems lists filesystems for desired machines.
// If no machines provided, a list of all filesystems is returned.
func (c *Client) ListFilesystems(machines []string) ([]params.FilesystemDetailsListResult, error) {
filters := make([]params.FilesystemFilter, len(machines))
for i, machine := range machines {
filters[i].Machines = []string{names.NewMachineTag(machine).String()}
}
if len(filters) == 0 {
filters = []params.FilesystemFilter{{}}
}
args := params.FilesystemFilters{filters}
var results params.FilesystemDetailsListResults
if err := c.facade.FacadeCall("ListFilesystems", args, &results); err != nil {
return nil, errors.Trace(err)
}
if len(results.Results) != len(filters) {
return nil, errors.Errorf(
"expected %d result(s), got %d",
len(filters), len(results.Results),
)
}
return results.Results, nil
}
// AddToUnit adds specified storage to desired units.
//
// NOTE(axw) for old controllers, the results will only
// contain errors.
func (c *Client) AddToUnit(storages []params.StorageAddParams) ([]params.AddStorageResult, error) {
out := params.AddStorageResults{}
in := params.StoragesAddParams{Storages: storages}
err := c.facade.FacadeCall("AddToUnit", in, &out)
if err != nil {
return nil, errors.Trace(err)
}
return out.Results, nil
}
// Attach attaches existing storage to a unit.
func (c *Client) Attach(unitId string, storageIds []string) ([]params.ErrorResult, error) {
in := params.StorageAttachmentIds{
make([]params.StorageAttachmentId, len(storageIds)),
}
if !names.IsValidUnit(unitId) {
return nil, errors.NotValidf("unit ID %q", unitId)
}
for i, storageId := range storageIds {
if !names.IsValidStorage(storageId) {
return nil, errors.NotValidf("storage ID %q", storageId)
}
in.Ids[i] = params.StorageAttachmentId{
StorageTag: names.NewStorageTag(storageId).String(),
UnitTag: names.NewUnitTag(unitId).String(),
}
}
out := params.ErrorResults{}
if err := c.facade.FacadeCall("Attach", in, &out); err != nil {
return nil, errors.Trace(err)
}
if len(out.Results) != len(storageIds) {
return nil, errors.Errorf(
"expected %d result(s), got %d",
len(storageIds), len(out.Results),
)
}
return out.Results, nil
}
// Remove removes the specified storage entities from the model,
// optionally destroying them.
func (c *Client) Remove(storageIds []string, destroyAttachments, destroyStorage bool, force *bool, maxWait *time.Duration) ([]params.ErrorResult, error) {
for _, id := range storageIds {
if !names.IsValidStorage(id) {
return nil, errors.NotValidf("storage ID %q", id)
}
}
results := params.ErrorResults{}
var args interface{}
var method string
if c.BestAPIVersion() <= 3 {
if !destroyStorage {
return nil, errors.Errorf("this juju controller does not support non-destructive removal of storage")
}
// In version 3, destroyAttached is ignored; removing
// storage always causes detachment.
entities := make([]params.Entity, len(storageIds))
for i, id := range storageIds {
entities[i].Tag = names.NewStorageTag(id).String()
}
args = params.Entities{entities}
method = "Destroy"
} else {
aStorage := make([]params.RemoveStorageInstance, len(storageIds))
for i, id := range storageIds {
aStorage[i] = params.RemoveStorageInstance{
Tag: names.NewStorageTag(id).String(),
DestroyAttachments: destroyAttachments,
DestroyStorage: destroyStorage,
}
if c.BestAPIVersion() > 5 {
aStorage[i].Force = force
aStorage[i].MaxWait = maxWait
}
}
args = params.RemoveStorage{aStorage}
method = "Remove"
}
if err := c.facade.FacadeCall(method, args, &results); err != nil {
return nil, errors.Trace(err)
}
if len(results.Results) != len(storageIds) {
return nil, errors.Errorf(
"expected %d result(s), got %d",
len(storageIds), len(results.Results),
)
}
return results.Results, nil
}
// Detach detaches the specified storage entities.
func (c *Client) Detach(storageIds []string, force *bool, maxWait *time.Duration) ([]params.ErrorResult, error) {
results := params.ErrorResults{}
ids := make([]params.StorageAttachmentId, len(storageIds))
for i, id := range storageIds {
if !names.IsValidStorage(id) {
return nil, errors.NotValidf("storage ID %q", id)
}
ids[i] = params.StorageAttachmentId{
StorageTag: names.NewStorageTag(id).String(),
}
}
var args interface{}
var method string
if c.BestAPIVersion() < 6 {
method = "Detach"
args = params.StorageAttachmentIds{ids}
} else {
method = "DetachStorage"
args = params.StorageDetachmentParams{
StorageIds: params.StorageAttachmentIds{ids},
Force: force,
MaxWait: maxWait,
}
}
if err := c.facade.FacadeCall(method, args, &results); err != nil {
return nil, errors.Trace(err)
}
if len(results.Results) != len(storageIds) {
return nil, errors.Errorf(
"expected %d result(s), got %d",
len(storageIds), len(results.Results),
)
}
return results.Results, nil
}
// Import imports storage into the model.
func (c *Client) Import(
kind storage.StorageKind,
storagePool string,
storageProviderId string,
storageName string,
) (names.StorageTag, error) {
var results params.ImportStorageResults
args := params.BulkImportStorageParams{
[]params.ImportStorageParams{{
StorageName: storageName,
Kind: params.StorageKind(kind),
Pool: storagePool,
ProviderId: storageProviderId,
}},
}
if err := c.facade.FacadeCall("Import", args, &results); err != nil {
return names.StorageTag{}, errors.Trace(err)
}
if len(results.Results) != 1 {
return names.StorageTag{}, errors.Errorf(
"expected 1 result, got %d",
len(results.Results),
)
}
if err := results.Results[0].Error; err != nil {
return names.StorageTag{}, err
}
return names.ParseStorageTag(results.Results[0].Result.StorageTag)
}