-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdisk_mock.go
95 lines (86 loc) · 1.92 KB
/
disk_mock.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
package ovirtclient
import (
"sync"
"github.com/google/uuid"
)
// diskWithData adds the ability to store the data directly in the disk for mocking purposes.
type diskWithData struct {
disk
lock *sync.Mutex
data []byte
}
func (d *diskWithData) Lock() error {
d.lock.Lock()
defer d.lock.Unlock()
if d.status != DiskStatusOK {
return newError(EDiskLocked, "disk %s is %s", d.id, d.status)
}
d.status = DiskStatusLocked
return nil
}
func (d *diskWithData) Unlock() {
d.lock.Lock()
defer d.lock.Unlock()
d.status = DiskStatusOK
}
func (d *diskWithData) WithAlias(alias *string) *diskWithData {
return &diskWithData{
disk{
client: d.client,
id: d.id,
alias: *alias,
provisionedSize: d.provisionedSize,
format: d.format,
storageDomainIDs: d.storageDomainIDs,
status: d.status,
totalSize: d.totalSize,
sparse: d.sparse,
},
d.lock,
d.data,
}
}
func (d *diskWithData) withProvisionedSize(ps uint64) (*diskWithData, error) {
if d.provisionedSize > ps {
return nil, newError(
EBadArgument,
"Cannot edit Virtual Disk. New disk size must be larger than the current disk size",
)
}
return &diskWithData{
disk{
client: d.client,
id: d.id,
alias: d.alias,
provisionedSize: ps,
format: d.format,
storageDomainIDs: d.storageDomainIDs,
status: d.status,
totalSize: ps,
sparse: d.sparse,
},
d.lock,
d.data,
}, nil
}
// clone is an internal function that makes a copy of the disk object with a new UUID.
func (d *diskWithData) clone(sparse *bool) *diskWithData {
if sparse == nil {
sparse = &d.sparse
}
return &diskWithData{
disk{
d.client,
DiskID(uuid.NewString()),
d.alias,
d.provisionedSize,
d.format,
d.storageDomainIDs,
d.status,
d.totalSize,
*sparse,
},
&sync.Mutex{},
d.data,
}
}