Skip to content

Commit

Permalink
Added the DiskAttachmentID type
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Bonic committed May 5, 2022
1 parent caf4528 commit 2641112
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 21 deletions.
15 changes: 9 additions & 6 deletions disk_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
ovirtsdk4 "github.com/ovirt/go-ovirt"
)

// DiskAttachmentID is the identifier for the disk attachment.
type DiskAttachmentID string

// DiskAttachmentClient contains the methods required for handling disk attachments.
type DiskAttachmentClient interface {
// CreateDiskAttachment attaches a disk to a VM.
Expand All @@ -17,11 +20,11 @@ type DiskAttachmentClient interface {
retries ...RetryStrategy,
) (DiskAttachment, error)
// GetDiskAttachment returns a single disk attachment in a virtual machine.
GetDiskAttachment(vmID VMID, id string, retries ...RetryStrategy) (DiskAttachment, error)
GetDiskAttachment(vmID VMID, id DiskAttachmentID, retries ...RetryStrategy) (DiskAttachment, error)
// ListDiskAttachments lists all disk attachments for a virtual machine.
ListDiskAttachments(vmID VMID, retries ...RetryStrategy) ([]DiskAttachment, error)
// RemoveDiskAttachment removes the disk attachment in question.
RemoveDiskAttachment(vmID VMID, diskAttachmentID string, retries ...RetryStrategy) error
RemoveDiskAttachment(vmID VMID, diskAttachmentID DiskAttachmentID, retries ...RetryStrategy) error
}

// DiskInterface describes the means by which a disk will appear to the VM.
Expand Down Expand Up @@ -152,7 +155,7 @@ func (c createDiskAttachmentParams) MustWithActive(active bool) BuildableCreateD
// DiskAttachment links together a Disk and a VM.
type DiskAttachment interface {
// ID returns the identifier of the attachment.
ID() string
ID() DiskAttachmentID
// VMID returns the ID of the virtual machine this attachment belongs to.
VMID() VMID
// DiskID returns the ID of the disk in this attachment.
Expand All @@ -176,7 +179,7 @@ type DiskAttachment interface {
type diskAttachment struct {
client Client

id string
id DiskAttachmentID
vmid VMID
diskID DiskID
diskInterface DiskInterface
Expand All @@ -192,7 +195,7 @@ func (d *diskAttachment) Remove(retries ...RetryStrategy) error {
return d.client.RemoveDiskAttachment(d.vmid, d.id, retries...)
}

func (d *diskAttachment) ID() string {
func (d *diskAttachment) ID() DiskAttachmentID {
return d.id
}

Expand Down Expand Up @@ -256,7 +259,7 @@ func convertSDKDiskAttachment(object *ovirtsdk4.DiskAttachment, o *oVirtClient)
return &diskAttachment{
client: o,

id: id,
id: DiskAttachmentID(id),
vmid: VMID(vmID),
diskID: DiskID(diskID),
diskInterface: DiskInterface(diskInterface),
Expand Down
2 changes: 1 addition & 1 deletion disk_attachment_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (m *mockClient) CreateDiskAttachment(

attachment := &diskAttachment{
client: m,
id: m.GenerateUUID(),
id: DiskAttachmentID(m.GenerateUUID()),
vmid: vm.ID(),
diskID: disk.ID(),
diskInterface: diskInterface,
Expand Down
6 changes: 3 additions & 3 deletions disk_attachment_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func (o *oVirtClient) GetDiskAttachment(
vmid VMID,
id string,
id DiskAttachmentID,
retries ...RetryStrategy,
) (result DiskAttachment, err error) {
retries = defaultRetries(retries, defaultReadTimeouts(o))
Expand All @@ -20,7 +20,7 @@ func (o *oVirtClient) GetDiskAttachment(
VmsService().
VmService(string(vmid)).
DiskAttachmentsService().
AttachmentService(id).
AttachmentService(string(id)).
Get().
Send()
if err != nil {
Expand All @@ -44,7 +44,7 @@ func (o *oVirtClient) GetDiskAttachment(
return result, err
}

func (m *mockClient) GetDiskAttachment(vmID VMID, diskAttachmentID string, _ ...RetryStrategy) (DiskAttachment, error) {
func (m *mockClient) GetDiskAttachment(vmID VMID, diskAttachmentID DiskAttachmentID, _ ...RetryStrategy) (DiskAttachment, error) {
m.lock.Lock()
defer m.lock.Unlock()

Expand Down
1 change: 1 addition & 0 deletions disk_attachment_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
)

// nolint:dupl
func (o *oVirtClient) ListDiskAttachments(
vmid VMID,
retries ...RetryStrategy,
Expand Down
6 changes: 3 additions & 3 deletions disk_attachment_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
)

func (o *oVirtClient) RemoveDiskAttachment(vmID VMID, diskAttachmentID string, retries ...RetryStrategy) error {
func (o *oVirtClient) RemoveDiskAttachment(vmID VMID, diskAttachmentID DiskAttachmentID, retries ...RetryStrategy) error {
retries = defaultRetries(retries, defaultWriteTimeouts(o))
return retry(
fmt.Sprintf("removing disk attachment %s on VM %s", diskAttachmentID, vmID),
Expand All @@ -16,15 +16,15 @@ func (o *oVirtClient) RemoveDiskAttachment(vmID VMID, diskAttachmentID string, r
VmsService().
VmService(string(vmID)).
DiskAttachmentsService().
AttachmentService(diskAttachmentID).
AttachmentService(string(diskAttachmentID)).
Remove().
Send()
return err
},
)
}

func (m *mockClient) RemoveDiskAttachment(vmID VMID, diskAttachmentID string, _ ...RetryStrategy) error {
func (m *mockClient) RemoveDiskAttachment(vmID VMID, diskAttachmentID DiskAttachmentID, _ ...RetryStrategy) error {
m.lock.Lock()
defer m.lock.Unlock()

Expand Down
2 changes: 1 addition & 1 deletion mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type mockClient struct {
vnicProfiles map[VNICProfileID]*vnicProfile
networks map[NetworkID]*network
dataCenters map[DatacenterID]*datacenterWithClusters
vmDiskAttachmentsByVM map[VMID]map[string]*diskAttachment
vmDiskAttachmentsByVM map[VMID]map[DiskAttachmentID]*diskAttachment
vmDiskAttachmentsByDisk map[DiskID]*diskAttachment
templateDiskAttachmentsByTemplate map[TemplateID][]*templateDiskAttachment
templateDiskAttachmentsByDisk map[DiskID]*templateDiskAttachment
Expand Down
2 changes: 1 addition & 1 deletion newmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func getClient(
dataCenters: map[DatacenterID]*datacenterWithClusters{
testDatacenter.ID(): testDatacenter,
},
vmDiskAttachmentsByVM: map[VMID]map[string]*diskAttachment{},
vmDiskAttachmentsByVM: map[VMID]map[DiskAttachmentID]*diskAttachment{},
vmDiskAttachmentsByDisk: map[DiskID]*diskAttachment{},
templateDiskAttachmentsByTemplate: map[TemplateID][]*templateDiskAttachment{
blankTemplate.ID(): {},
Expand Down
8 changes: 4 additions & 4 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,12 @@ type VM interface {
retries ...RetryStrategy,
) (DiskAttachment, error)
// GetDiskAttachment returns a specific disk attachment for the current VM by ID.
GetDiskAttachment(diskAttachmentID string, retries ...RetryStrategy) (DiskAttachment, error)
GetDiskAttachment(diskAttachmentID DiskAttachmentID, retries ...RetryStrategy) (DiskAttachment, error)
// ListDiskAttachments lists all disk attachments for the current VM.
ListDiskAttachments(retries ...RetryStrategy) ([]DiskAttachment, error)
// DetachDisk removes a specific disk attachment by the disk attachment ID.
DetachDisk(
diskAttachmentID string,
diskAttachmentID DiskAttachmentID,
retries ...RetryStrategy,
) error
// Tags list all tags for the current VM
Expand Down Expand Up @@ -1897,15 +1897,15 @@ func (v *vm) AttachDisk(
return v.client.CreateDiskAttachment(v.id, diskID, diskInterface, params, retries...)
}

func (v *vm) GetDiskAttachment(diskAttachmentID string, retries ...RetryStrategy) (DiskAttachment, error) {
func (v *vm) GetDiskAttachment(diskAttachmentID DiskAttachmentID, retries ...RetryStrategy) (DiskAttachment, error) {
return v.client.GetDiskAttachment(v.id, diskAttachmentID, retries...)
}

func (v *vm) ListDiskAttachments(retries ...RetryStrategy) ([]DiskAttachment, error) {
return v.client.ListDiskAttachments(v.id, retries...)
}

func (v *vm) DetachDisk(diskAttachmentID string, retries ...RetryStrategy) error {
func (v *vm) DetachDisk(diskAttachmentID DiskAttachmentID, retries ...RetryStrategy) error {
return v.client.RemoveDiskAttachment(v.id, diskAttachmentID, retries...)
}

Expand Down
4 changes: 2 additions & 2 deletions vm_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func (m *mockClient) createPlacementPolicy(params OptionalVMParameters) *vmPlace

func (m *mockClient) attachVMDisksFromTemplate(tpl *template, vm *vm, params OptionalVMParameters) {
m.vmDiskAttachmentsByVM[vm.id] = make(
map[string]*diskAttachment,
map[DiskAttachmentID]*diskAttachment,
len(m.templateDiskAttachmentsByTemplate[tpl.id]),
)
for _, attachment := range m.templateDiskAttachmentsByTemplate[tpl.id] {
Expand All @@ -464,7 +464,7 @@ func (m *mockClient) attachVMDisksFromTemplate(tpl *template, vm *vm, params Opt

diskAttachment := &diskAttachment{
client: m,
id: m.GenerateUUID(),
id: DiskAttachmentID(m.GenerateUUID()),
vmid: vm.id,
diskID: newDisk.ID(),
diskInterface: attachment.diskInterface,
Expand Down

0 comments on commit 2641112

Please sign in to comment.