forked from oVirt/go-ovirt-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_attachment_create.go
126 lines (113 loc) · 3.32 KB
/
disk_attachment_create.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
package ovirtclient
import (
"fmt"
ovirtsdk "github.com/ovirt/go-ovirt"
)
func (o *oVirtClient) CreateDiskAttachment(
vmID VMID,
diskID DiskID,
diskInterface DiskInterface,
params CreateDiskAttachmentOptionalParams,
retries ...RetryStrategy,
) (result DiskAttachment, err error) {
retries = defaultRetries(retries, defaultWriteTimeouts(o))
if err := diskInterface.Validate(); err != nil {
return nil, wrap(err, EBadArgument, "failed to create disk attachment")
}
err = retry(
fmt.Sprintf("attaching disk %s to vm %s", diskID, vmID),
o.logger,
retries,
func() error {
attachmentBuilder := ovirtsdk.NewDiskAttachmentBuilder()
attachmentBuilder.Disk(ovirtsdk.NewDiskBuilder().Id(string(diskID)).MustBuild())
attachmentBuilder.Interface(ovirtsdk.DiskInterface(diskInterface))
attachmentBuilder.Vm(ovirtsdk.NewVmBuilder().Id(string(vmID)).MustBuild())
if params != nil {
if active := params.Active(); active != nil {
attachmentBuilder.Active(*active)
}
if bootable := params.Bootable(); bootable != nil {
attachmentBuilder.Bootable(*params.Bootable())
}
}
attachment := attachmentBuilder.MustBuild()
addRequest := o.conn.SystemService().VmsService().VmService(string(vmID)).DiskAttachmentsService().Add()
addRequest.Attachment(attachment)
response, err := addRequest.Send()
if err != nil {
return wrap(
err,
EUnidentified,
"failed to attach disk %s to VM %s using %s",
diskID,
vmID,
diskInterface,
)
}
attachment, ok := response.Attachment()
if !ok {
return newFieldNotFound("attachment response", "attachment")
}
result, err = convertSDKDiskAttachment(attachment, o)
if err != nil {
return wrap(err, EUnidentified, "failed to convert SDK disk attachment")
}
return nil
},
)
return result, err
}
func (m *mockClient) CreateDiskAttachment(
vmID VMID,
diskID DiskID,
diskInterface DiskInterface,
params CreateDiskAttachmentOptionalParams,
_ ...RetryStrategy,
) (DiskAttachment, error) {
m.lock.Lock()
defer m.lock.Unlock()
if err := diskInterface.Validate(); err != nil {
return nil, wrap(err, EBadArgument, "failed to create disk attachment")
}
vm, ok := m.vms[vmID]
if !ok {
return nil, newError(ENotFound, "VM with ID %s not found", vmID)
}
disk, ok := m.disks[diskID]
if !ok {
return nil, newError(ENotFound, "disk with ID %s not found", diskID)
}
attachment := &diskAttachment{
client: m,
id: DiskAttachmentID(m.GenerateUUID()),
vmid: vm.ID(),
diskID: disk.ID(),
diskInterface: diskInterface,
}
if params != nil {
if bootable := params.Bootable(); bootable != nil {
attachment.bootable = *bootable
}
if active := params.Active(); active != nil {
attachment.active = *active
}
}
for _, diskAttachment := range m.vmDiskAttachmentsByVM[vm.ID()] {
if diskAttachment.DiskID() == diskID {
return nil, newError(EConflict, "disk %s is already attached to VM %s", diskID, vmID)
}
}
if diskAttachment, ok := m.vmDiskAttachmentsByDisk[disk.ID()]; ok {
return nil, newError(
EConflict,
"cannot attach disk %s to VM %s, already attached to VM %s",
diskID,
vmID,
diskAttachment.VMID(),
)
}
m.vmDiskAttachmentsByDisk[disk.ID()] = attachment
m.vmDiskAttachmentsByVM[vm.ID()][attachment.ID()] = attachment
return attachment, nil
}