1
// Copyright 2014 ALTOROS
2
// Licensed under the AGPLv3, see LICENSE file for details.
10
"github.com/altoros/gosigma/data"
14
// DriveUnmounted defines constant for unmounted drive status
15
DriveUnmounted = "unmounted"
16
// DriveCreating defines constant for creating drive status
17
DriveCreating = "creating"
18
// DriveResizing defines constant for resizing drive status
19
DriveResizing = "resizing"
20
// DriveCloningDst defines constant for drive cloning status
21
DriveCloningDst = "cloning_dst"
22
// ... may be another values here, contact CloudSigma devs
26
// MediaCdrom defines media type for cdrom drives
28
// MediaDisk defines media type for disk drives
32
// A Drive interface represents drive instance in CloudSigma account
33
type Drive interface {
34
// CloudSigma resource
41
AllowMultimount() bool
43
// Get meta-information value stored in the drive instance
44
Get(key string) (v string, ok bool)
46
// Media of drive instance
49
// Name of drive instance
52
// Owner of drive instance
55
// Size of drive in bytes
58
// Status of drive instance
61
// StorageType of drive instance
64
// IsLibrary returns true if this drive is CloudSigma library drive
67
// OS returns operating system of the drive (defined for library drives)
70
// Arch returns operating system bit architecture the drive (defined for library drives)
73
// Paid image or free (defined for library drives)
76
// ImageType returns type of drive image (defined for library drives)
79
// Clone drive instance
80
Clone(params CloneParams, avoid []string) (Drive, error)
82
// Clone drive instance, wait for operation finished
83
CloneWait(params CloneParams, avoid []string) (Drive, error)
85
// Jobs for this drive instance.
86
// Every job object in resulting slice carries only UUID and URI.
87
// To obtain additional information for job, one should use Job.Refresh() method
88
// to query cloud for detailed job information.
91
// Refresh information about drive instance
94
// Resize drive instance
95
Resize(newSize uint64) error
97
// Resize drive instance, wait for operation finished
98
ResizeWait(newSize uint64) error
100
// Wait for user-defined event
101
Wait(stop func(Drive) bool) error
107
// A drive implements drive instance in CloudSigma account
114
var _ Drive = (*drive)(nil)
116
// String method is used to print values passed as an operand to any format that
117
// accepts a string or to an unformatted printer such as Print.
118
func (d drive) String() string {
119
return fmt.Sprintf("{Name: %q\nURI: %q\nStatus: %s\nUUID: %q\nSize: %d\nMedia: %s\nStorage: %s}",
120
d.Name(), d.URI(), d.Status(), d.UUID(), d.Size(), d.Media(), d.StorageType())
123
// URI of drive instance
124
func (d drive) URI() string { return d.obj.URI }
126
// UUID of drive instance
127
func (d drive) UUID() string { return d.obj.UUID }
130
func (d drive) Affinities() []string { return d.obj.Affinities }
133
func (d drive) AllowMultimount() bool { return d.obj.AllowMultimount }
135
// Get meta-information value stored in the drive instance
136
func (d drive) Get(key string) (v string, ok bool) {
137
v, ok = d.obj.Meta[key]
141
// Media of drive instance
142
func (d drive) Media() string { return d.obj.Media }
144
// Name of drive instance
145
func (d drive) Name() string { return d.obj.Name }
147
// Owner of drive instance
148
func (d drive) Owner() Resource {
149
if d.obj.Owner == nil {
152
return &resource{d.obj.Owner}
155
// Size of drive in bytes
156
func (d drive) Size() uint64 { return d.obj.Size }
158
// Status of drive instance
159
func (d drive) Status() string { return d.obj.Status }
161
// StorageType of drive instance
162
func (d drive) StorageType() string { return d.obj.StorageType }
164
// IsLibrary returns true if this drive is CloudSigma library drive
165
func (d drive) Library() LibrarySpec { return d.library }
167
// OS returns operating system of the drive (defined for library drives)
168
func (d drive) OS() string { return d.obj.OS }
170
// Arch returns operating system bit architecture the drive (defined for library drives)
171
func (d drive) Arch() string { return d.obj.Arch }
173
// Paid image or free (defined for library drives)
174
func (d drive) Paid() bool { return d.obj.Paid }
176
// ImageType returns type of drive image (defined for library drives)
177
func (d drive) ImageType() string { return d.obj.ImageType }
179
// Clone drive instance.
180
func (d drive) Clone(params CloneParams, avoid []string) (Drive, error) {
181
obj, err := d.clone(params, avoid)
189
library: LibraryAccount,
195
// Clone drive instance, wait for operation finished.
196
func (d drive) CloneWait(params CloneParams, avoid []string) (Drive, error) {
197
obj, err := d.clone(params, avoid)
205
library: LibraryAccount,
208
jj := newDrive.Jobs()
215
if err := j.Wait(); err != nil {
219
if err := newDrive.Refresh(); err != nil {
223
if d.Library() == LibraryMedia {
224
obj.LibraryDrive = d.obj.LibraryDrive
230
// Jobs for this drive instance.
231
// Every job object in resulting slice carries only UUID and URI.
232
// To obtain additional information for job, one should use Job.Refresh() method
233
// to query cloud for detailed job information.
234
func (d drive) Jobs() []Job {
235
r := make([]Job, 0, len(d.obj.Jobs))
236
for _, j := range d.obj.Jobs {
237
j := &job{d.client, &data.Job{Resource: j}}
243
// Refresh information about drive instance
244
func (d *drive) Refresh() error {
245
obj, err := d.client.getDrive(d.UUID(), d.Library())
253
// Resize drive instance
254
func (d *drive) Resize(newSize uint64) error {
255
return d.resize(newSize)
258
// Resize drive instance, wait for operation finished
259
func (d *drive) ResizeWait(newSize uint64) error {
260
// try to resize drive
261
if err := d.resize(newSize); err != nil {
265
// wait for status 'resizing' changes back to 'unmounted'
266
return d.Wait(func(d Drive) bool {
267
return d.Status() == DriveUnmounted
271
// Wait for user-defined event
272
func (d *drive) Wait(stop func(Drive) bool) error {
275
timeout := d.client.GetOperationTimeout()
277
timer := time.AfterFunc(timeout, func() { timedout = true })
282
if err := d.Refresh(); err != nil {
286
return ErrOperationTimeout
294
func (d *drive) Remove() error {
295
return d.client.RemoveDrive(d.UUID(), d.Library())