-
Notifications
You must be signed in to change notification settings - Fork 39
/
client.go
353 lines (312 loc) · 12.2 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
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package cinder
import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
"time"
"gopkg.in/goose.v3/errors"
goosehttp "gopkg.in/goose.v3/http"
)
// Basic returns a basic Cinder client which will handle authorization
// of requests, and routing to the correct endpoint.
func Basic(endpoint *url.URL, tenantId string, token TokenFn) *Client {
return NewClient(tenantId, endpoint,
SetAuthHeaderFn(token, http.DefaultClient.Do),
)
}
// BasicTLSConfig returns a basic Cinder client which will handle authorization
// of requests where CACerts are required, and routing to the correct endpoint.
func BasicTLSConfig(endpoint *url.URL, tenantId string, token TokenFn, tlsConfig *tls.Config) *Client {
return NewClient(tenantId, endpoint,
AuthHeaderTSLConfigDoRequestFn(token, tlsConfig),
)
}
// TokenFn represents a function signature which returns the user's
// authorization token when called.
type TokenFn func() string
// SetAuthHeaderFn returns a RequestHandlerFn which sets the
// authentication headers for a given request.
func SetAuthHeaderFn(token TokenFn, wrappedHandler RequestHandlerFn) RequestHandlerFn {
return func(req *http.Request) (*http.Response, error) {
req.Header.Set("X-Auth-Token", token())
return wrappedHandler(req)
}
}
// AuthHeaderTSLConfigDoRequestFn returns a RequestHandlerFn which sets the
// authentication headers for a given request and provides the
// client with a tls config.
func AuthHeaderTSLConfigDoRequestFn(token TokenFn, tlsConfig *tls.Config) RequestHandlerFn {
return func(req *http.Request) (*http.Response, error) {
req.Header.Set("X-Auth-Token", token())
defaultClient := *http.DefaultClient
defaultClient.Transport = &http.Transport{
TLSClientConfig: tlsConfig,
}
return defaultClient.Do(req)
}
}
// NewClient is the most flexible way to instantiate a Cinder
// Client. The handleRequest function will be responsible for
// modifying requests and dispatching them as needed. For an example
// of how to utilize this method, see the Basic function.
func NewClient(tenantId string, endpoint *url.URL, handleRequest RequestHandlerFn) *Client {
if endpoint == nil {
return nil
}
// Ensure the cinder endpoint has a trailing slash on the path.
path := endpoint.Path
if len(path) != 0 && path[len(path)-1:] != "/" {
changedEndpoint := *endpoint
changedEndpoint.Path += "/"
endpoint = &changedEndpoint
}
httpClient := goosehttp.New()
httpClient.Client = http.Client{Transport: handleRequest}
return &Client{tenantId, endpoint, httpClient}
}
// Client is a Cinder client.
type Client struct {
tenantId string
endpoint *url.URL
client *goosehttp.Client
}
// TODO(axw) update all callers of handleRequest
// to use c.client.JsonRequest instead, so we can
// benefit from the common goose error handling.
func (c *Client) handleRequest(req *http.Request) (*http.Response, error) {
return c.client.Do(req)
}
// GetSnapshot shows information for a specified snapshot.
func (c *Client) GetSnapshot(snapshotId string) (*GetSnapshotResults, error) {
return getSnapshot(
c,
GetSnapshotParams{TenantId: c.tenantId, SnapshotId: snapshotId},
)
}
// UpdateSnapshot updates a specified snapshot.
func (c *Client) UpdateSnapshot(snapshotId string, args UpdateSnapshotSnapshotParams) (*UpdateSnapshotResults, error) {
return updateSnapshot(c, UpdateSnapshotParams{
TenantId: c.tenantId,
SnapshotId: snapshotId,
Snapshot: args,
})
}
// DeleteSnapshot deletes a specified snapshot.
func (c *Client) DeleteSnapshot(snapshotId string) error {
_, err := deleteSnapshot(
c,
DeleteSnapshotParams{TenantId: c.tenantId, SnapshotId: snapshotId},
)
return err
}
// VersionDetails shows details for Block Storage API v2.
func (c *Client) VersionDetails() (*VersionDetailsResults, error) {
return versionDetails(c, VersionDetailsParams{})
}
// ListExtensionsCinderV2 lists Block Storage API extensions.
func (c *Client) ListExtensionsCinderV2() (*ListExtensionsCinderV2Results, error) {
return listExtensionsCinderV2(c, ListExtensionsCinderV2Params{})
}
// GetVolumesSimple lists summary information for all Block Storage
// volumes that the tenant who submits the request can access.
func (c *Client) GetVolumesSimple() (*GetVolumesSimpleResults, error) {
return getVolumesSimple(c, GetVolumesSimpleParams{TenantId: c.tenantId})
}
// UpdateVolumeType updates a volume type.
func (c *Client) UpdateVolumeType(volumeTypeId, volumeType string) (*UpdateVolumeTypeResults, error) {
return updateVolumeType(c, UpdateVolumeTypeParams{
TenantId: c.tenantId,
VolumeTypeId: volumeTypeId,
VolumeType: volumeType,
})
}
// DeleteVolumeType deletes a specified volume type.
func (c *Client) DeleteVolumeType(volumeTypeId string) error {
_, err := deleteVolumeType(
c,
DeleteVolumeTypeParams{TenantId: c.tenantId, VolumeTypeId: volumeTypeId},
)
return err
}
// GetVolumesDetail lists detailed information for all Block Storage
// volumes that the tenant who submits the request can access.
func (c *Client) GetVolumesDetail() (*GetVolumesDetailResults, error) {
return getVolumesDetail(c, GetVolumesDetailParams{TenantId: c.tenantId})
}
// GetVolume lists information about the volume with the given
// volumeId.
func (c *Client) GetVolume(volumeId string) (*GetVolumeResults, error) {
return getVolume(c, GetVolumeParams{TenantId: c.tenantId, VolumeId: volumeId})
}
// CreateVolumeType creates a volume type.
func (c *Client) CreateVolumeType(args CreateVolumeTypeVolumeTypeParams) (*CreateVolumeTypeResults, error) {
return createVolumeType(
c,
CreateVolumeTypeParams{TenantId: c.tenantId, VolumeType: args},
)
}
// GetVolumeType shows information about a specified volume type.
func (c *Client) GetVolumeType(volumeTypeId string) (*GetVolumeTypeResults, error) {
return getVolumeType(
c,
GetVolumeTypeParams{TenantId: c.tenantId, VolumeTypeId: volumeTypeId},
)
}
// ListVersion lists information about all Block Storage API versions.
func (c *Client) ListVersions() (*ListVersionsResults, error) {
return listVersions(c, ListVersionsParams{})
}
// UpdateVolumeTypeExtraSpecs updates the extra specifications
// assigned to a volume type.
func (c *Client) UpdateVolumeTypeExtraSpecs(volumeTypeId, volumeType, extraSpecs string) (*UpdateVolumeTypeExtraSpecsResults, error) {
return updateVolumeTypeExtraSpecs(c, UpdateVolumeTypeExtraSpecsParams{
TenantId: c.tenantId,
VolumeTypeId: volumeTypeId,
VolumeType: volumeType,
ExtraSpecs: extraSpecs,
})
}
// GetSnapshotsSimple lists summary information for all Block Storage
// snapshots that the tenant who submits the request can access.
func (c *Client) GetSnapshotsSimple() (*GetSnapshotsSimpleResults, error) {
return getSnapshotsSimple(c, GetSnapshotsSimpleParams{TenantId: c.tenantId})
}
// ShowSnapshotMetadata shows the metadata for a specified snapshot.
func (c *Client) ShowSnapshotMetadata(snapshotId string) (*ShowSnapshotMetadataResults, error) {
return showSnapshotMetadata(
c,
ShowSnapshotMetadataParams{TenantId: c.tenantId, SnapshotId: snapshotId},
)
}
// CreateSnapshot creates a snapshot, which is a point-in-time
// complete copy of a volume. You can create a volume from the
// snapshot.
func (c *Client) CreateSnapshot(args CreateSnapshotSnapshotParams) (*CreateSnapshotResults, error) {
return createSnapshot(c, CreateSnapshotParams{TenantId: c.tenantId, Snapshot: args})
}
// GetSnapshotsDetail lists detailed information for all Block Storage
// snapshots that the tenant who submits the request can access.
func (c *Client) GetSnapshotsDetail() (*GetSnapshotsDetailResults, error) {
return getSnapshotsDetail(c, GetSnapshotsDetailParams{TenantId: c.tenantId})
}
// UpdateSnapshotMetadata updates the metadata for a specified
// snapshot.
func (c *Client) UpdateSnapshotMetadata(snapshotId, key string) (*UpdateSnapshotMetadataResults, error) {
return updateSnapshotMetadata(c, UpdateSnapshotMetadataParams{
TenantId: c.tenantId,
SnapshotId: snapshotId,
Metadata: UpdateSnapshotMetadataMetadataParams{
Key: key,
},
})
}
// CreateVolume creates a volume. To create a bootable volume, include
// the image ID and set the bootable flag to true in the request body.
//
// Preconditions:
//
// - The user must have enough volume storage quota remaining to create
// a volume of size requested.
//
// Asynchronous Postconditions:
//
// - With correct permissions, you can see the volume status as
// available through API calls.
// - With correct access, you can see the created volume in the
// storage system that OpenStack Block Storage manages.
//
// Troubleshooting:
//
// - If volume status remains creating or shows another error status,
// the request failed. Ensure you meet the preconditions then
// investigate the storage backend.
// - Volume is not created in the storage system which OpenStack Block Storage manages.
// - The storage node needs enough free storage space to match the
// specified size of the volume creation request.
func (c *Client) CreateVolume(args CreateVolumeVolumeParams) (*CreateVolumeResults, error) {
return createVolume(c, CreateVolumeParams{TenantId: c.tenantId, Volume: args})
}
// UpdateVolume updates a volume.
func (c *Client) UpdateVolume(volumeId string, args UpdateVolumeVolumeParams) (*UpdateVolumeResults, error) {
return updateVolume(c, UpdateVolumeParams{TenantId: c.tenantId, VolumeId: volumeId, Volume: args})
}
// DeleteVolume flags a volume for deletion. The volume managed by
// OpenStack Block Storage is not deleted from the storage system.
func (c *Client) DeleteVolume(volumeId string) error {
_, err := deleteVolume(
c,
DeleteVolumeParams{TenantId: c.tenantId, VolumeId: volumeId},
)
return err
}
// GetVolumeTypes lists volume types.
func (c *Client) GetVolumeTypes() (*GetVolumeTypesResults, error) {
return getVolumeTypes(c, GetVolumeTypesParams{TenantId: c.tenantId})
}
type predicateFn func() (bool, error)
func notifier(predicate predicateFn, numAttempts int, waitDur time.Duration) <-chan error {
notifierChan := make(chan error)
go func() {
defer close(notifierChan)
for attemptNum := 0; attemptNum < numAttempts; attemptNum++ {
if ok, err := predicate(); err != nil {
notifierChan <- err
return
} else if ok {
return
}
time.Sleep(waitDur)
}
notifierChan <- fmt.Errorf("too many attempts")
}()
return notifierChan
}
// VolumeStatusNotifier will check a volume's status to determine
// whether it matches the given status. After a check, it waits for
// "waitDur" before attempting again. If the status does not match
// after "numAttempts", an error is returned.
func (c *Client) VolumeStatusNotifier(volId, status string, numAttempts int, waitDur time.Duration) <-chan error {
statusMatches := func() (bool, error) {
volInfo, err := c.GetVolume(volId)
if err != nil {
return false, err
}
return volInfo.Volume.Status == status, nil
}
return notifier(statusMatches, numAttempts, waitDur)
}
// SnapshotStatusNotifier will check a volume's status to determine
// whether it matches the given status. After a check, it waits for
// "waitDur" before attempting again. If the status does not match
// after "numAttempts", an error is returned.
func (c *Client) SnapshotStatusNotifier(snapId, status string, numAttempts int, waitDur time.Duration) <-chan error {
statusMatches := func() (bool, error) {
snapInfo, err := c.GetSnapshot(snapId)
if err != nil {
return false, err
}
return snapInfo.Snapshot.Status == status, nil
}
return notifier(statusMatches, numAttempts, waitDur)
}
// SetVolumeMetadata sets metadata on a server. Replaces metadata
// items that match keys - doesn't modify items that aren't in the
// request. Returns the complete, updated metadata for the volume.
func (c *Client) SetVolumeMetadata(volumeId string, metadata map[string]string) (map[string]string, error) {
response, err := updateVolumeMetadata(c, volumeId, &UpdateVolumeMetadataParams{metadata})
if err != nil {
return nil, err
}
return response.Metadata, nil
}
// ListVolumeAvailabilityZones lists any volume availability zones.
func (c *Client) ListVolumeAvailabilityZones() ([]AvailabilityZone, error) {
resp, err := listAvailabilityZones(c)
if err != nil {
return nil, errors.Newf(err, "failed to get list of availability zones")
}
return resp.AvailabilityZoneInfo, nil
}