forked from oVirt/go-ovirt-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_disk_attachment_create.go
66 lines (61 loc) · 1.82 KB
/
client_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
package ovirtclient
import (
"fmt"
ovirtsdk "github.com/ovirt/go-ovirt"
)
func (o *oVirtClient) CreateDiskAttachment(
vmID string,
diskID string,
diskInterface DiskInterface,
params CreateDiskAttachmentOptionalParams,
retries ...RetryStrategy,
) (result DiskAttachment, err error) {
retries = defaultRetries(retries, defaultWriteTimeouts())
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(diskID).MustBuild())
attachmentBuilder.Interface(ovirtsdk.DiskInterface(diskInterface))
attachmentBuilder.Vm(ovirtsdk.NewVmBuilder().Id(vmID).MustBuild())
attachmentBuilder.Active(true)
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(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
}