-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk.go
75 lines (60 loc) · 1.72 KB
/
disk.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
package clcv2
import (
"fmt"
"regexp"
"strings"
"github.com/pkg/errors"
)
/*
* Representation of disks on a server
*/
// DiskID represents the format used by CLCv2 to refer to disks: <major-number>":"<minor-number>
type DiskID string
// DiskIDFromString attempts to parse @s as a disk ID
func DiskIDFromString(s string) (DiskID, error) {
// Allow two types of ID: (a) <major>:<minor> syntax, (b) <minor> syntax
var reMajMin = regexp.MustCompile(`^\d+:\d+$`)
var reMin = regexp.MustCompile(`^\d+$`)
s = strings.TrimSpace(s)
if reMajMin.MatchString(s) {
return DiskID(s), nil
} else if reMin.MatchString(s) {
return DiskID(fmt.Sprintf("0:%s", s)), nil
}
return "", errors.Errorf("invalid disk ID %q", s)
}
// DiskIDList is a (duplicate-free) list of disk IDs
type DiskIDList []DiskID
func (d DiskIDList) String() string {
var ret []string
for _, id := range d {
ret = append(ret, string(id))
}
return strings.Join(ret, ", ")
}
// Contains returns true if @id is contained in DiskIDList
func (d DiskIDList) Contains(id DiskID) bool {
for _, cand := range d {
if id == cand {
return true
}
}
return false
}
func (d *DiskIDList) Add(id DiskID) {
if !d.Contains(id) {
*d = append(*d, id)
}
}
// ServerAdditionalDisk is used to specify (additional) disks to attach/modify.
type ServerAdditionalDisk struct {
// Id is used by the SetServerDisks() call exclusively
Id DiskID `json:"diskId,omitempty"`
// File system path for disk (Windows drive letter or Linux mount point).
// Must not be one of the reserved names.
Path string `json:"path"`
// Amount in GB to allocate for disk, up to 1024 GB
SizeGB uint32 `json:"sizeGB"`
// Whether the disk should be "raw" or "partitioned"
Type string `json:"type"`
}