Skip to content

Commit

Permalink
Added the TagID type
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Bonic committed May 5, 2022
1 parent b661925 commit 97980ab
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type mockClient struct {
vmDiskAttachmentsByDisk map[DiskID]*diskAttachment
templateDiskAttachmentsByTemplate map[TemplateID][]*templateDiskAttachment
templateDiskAttachmentsByDisk map[DiskID]*templateDiskAttachment
tags map[string]*tag
tags map[TagID]*tag
affinityGroups map[ClusterID]map[AffinityGroupID]*affinityGroup
vmIPs map[VMID]map[string][]net.IP
instanceTypes map[InstanceTypeID]*instanceType
Expand Down
2 changes: 1 addition & 1 deletion newmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func getClient(
url: "https://localhost/ovirt-engine/api",
lock: &sync.Mutex{},
vms: map[VMID]*vm{},
tags: map[string]*tag{},
tags: map[TagID]*tag{},
nonSecureRandom: rand.New(rand.NewSource(time.Now().UnixNano())), //nolint:gosec
storageDomains: map[StorageDomainID]*storageDomain{
testStorageDomain.ID(): testStorageDomain,
Expand Down
17 changes: 10 additions & 7 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@ import (
ovirtsdk4 "github.com/ovirt/go-ovirt"
)

//go:generate go run scripts/rest/rest.go -i "Tag" -n "tag"
//go:generate go run scripts/rest/rest.go -i "Tag" -n "tag" -T TagID

// TagID is the UUID of a tag.
type TagID string

// TagClient describes the functions related to oVirt tags.
type TagClient interface {
// GetTag returns a single tag based on its ID.
GetTag(id string, retries ...RetryStrategy) (Tag, error)
GetTag(id TagID, retries ...RetryStrategy) (Tag, error)
// ListTags returns all tags on the oVirt engine.
ListTags(retries ...RetryStrategy) ([]Tag, error)
// CreateTag creates a new tag with a name.
CreateTag(name string, params CreateTagParams, retries ...RetryStrategy) (result Tag, err error)
// RemoveTag removes the tag with the specified ID.
RemoveTag(tagID string, retries ...RetryStrategy) error
RemoveTag(tagID TagID, retries ...RetryStrategy) error
}

// TagData is the core of Tag, providing only the data access functions, but not the client
// functions.
type TagData interface {
// ID returns the auto-generated identifier for this tag.
ID() string
ID() TagID
// Name returns the user-give name for this tag.
Name() string
// Description returns the user-give description for this tag. It may be nil if no decription is set.
Expand Down Expand Up @@ -90,20 +93,20 @@ func convertSDKTag(sdkObject *ovirtsdk4.Tag, client *oVirtClient) (Tag, error) {
}
return &tag{
client: client,
id: id,
id: TagID(id),
name: name,
description: description,
}, nil
}

type tag struct {
client Client
id string
id TagID
name string
description *string
}

func (n tag) ID() string {
func (n tag) ID() TagID {
return n.id
}

Expand Down
2 changes: 1 addition & 1 deletion tag_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (o *oVirtClient) CreateTag(name string, params CreateTagParams, retries ...
func (m *mockClient) CreateTag(name string, params CreateTagParams, _ ...RetryStrategy) (result Tag, err error) {
m.lock.Lock()
defer m.lock.Unlock()
id := m.GenerateUUID()
id := TagID(m.GenerateUUID())
if params == nil {
params = NewCreateTagParams()
}
Expand Down
6 changes: 3 additions & 3 deletions tag_get.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tag_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import (
"fmt"
)

func (o *oVirtClient) RemoveTag(tagID string, retries ...RetryStrategy) (err error) {
func (o *oVirtClient) RemoveTag(tagID TagID, retries ...RetryStrategy) (err error) {
retries = defaultRetries(retries, defaultWriteTimeouts(o))
err = retry(
fmt.Sprintf("removing tag %s", tagID),
o.logger,
retries,
func() error {
_, err := o.conn.SystemService().TagsService().TagService(tagID).Remove().Send()
_, err := o.conn.SystemService().TagsService().TagService(string(tagID)).Remove().Send()
return err
})
return
}

func (m *mockClient) RemoveTag(id string, _ ...RetryStrategy) (err error) {
func (m *mockClient) RemoveTag(id TagID, _ ...RetryStrategy) (err error) {
m.lock.Lock()
defer m.lock.Unlock()

Expand Down
2 changes: 1 addition & 1 deletion tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestAddTagToVM(t *testing.T) {
func assertCanGetTag(
t *testing.T,
helper ovirtclient.TestHelper,
tagID string,
tagID ovirtclient.TagID,
) ovirtclient.Tag {
client := helper.GetClient()
tag, err := client.GetTag(tagID)
Expand Down
24 changes: 12 additions & 12 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ type VMClient interface {
// RemoveVM removes a virtual machine specified by id.
RemoveVM(id VMID, retries ...RetryStrategy) error
// AddTagToVM Add tag specified by id to a VM.
AddTagToVM(id VMID, tagID string, retries ...RetryStrategy) error
AddTagToVM(id VMID, tagID TagID, retries ...RetryStrategy) error
// AddTagToVMByName Add tag specified by Name to a VM.
AddTagToVMByName(id VMID, tagName string, retries ...RetryStrategy) error
// RemoveTagFromVM removes the specified tag from the specified VM.
RemoveTagFromVM(id VMID, tagID string, retries ...RetryStrategy) error
RemoveTagFromVM(id VMID, tagID TagID, retries ...RetryStrategy) error
// ListVMTags lists the tags attached to a VM.
ListVMTags(id VMID, retries ...RetryStrategy) (result []Tag, err error)
// GetVMIPAddresses fetches the IP addresses reported by the guest agent in the VM.
Expand Down Expand Up @@ -217,7 +217,7 @@ type VMData interface {
// memory policy is set.
MemoryPolicy() (MemoryPolicy, bool)
// TagIDs returns a list of tags for this VM.
TagIDs() []string
TagIDs() []TagID
// HugePages returns the hugepage settings for the VM, if any.
HugePages() *VMHugePages
// Initialization returns the virtual machine’s initialization configuration.
Expand Down Expand Up @@ -516,9 +516,9 @@ type VM interface {
WaitForNonLocalIPAddress(retries ...RetryStrategy) (map[string][]net.IP, error)

// AddTag adds the specified tag to the current VM.
AddTag(tagID string, retries ...RetryStrategy) (err error)
AddTag(tagID TagID, retries ...RetryStrategy) (err error)
// RemoveTag removes the tag from the current VM.
RemoveTag(tagID string, retries ...RetryStrategy) (err error)
RemoveTag(tagID TagID, retries ...RetryStrategy) (err error)
// ListTags lists the tags attached to the current VM.
ListTags(retries ...RetryStrategy) (result []Tag, err error)
}
Expand Down Expand Up @@ -1731,7 +1731,7 @@ type vm struct {
status VMStatus
cpu *vmCPU
memory int64
tagIDs []string
tagIDs []TagID
hugePages *VMHugePages
initialization Initialization
hostID *HostID
Expand All @@ -1754,11 +1754,11 @@ func (v *vm) InstanceTypeID() *InstanceTypeID {
return v.instanceTypeID
}

func (v *vm) AddTag(tagID string, retries ...RetryStrategy) (err error) {
func (v *vm) AddTag(tagID TagID, retries ...RetryStrategy) (err error) {
return v.client.AddTagToVM(v.id, tagID, retries...)
}

func (v *vm) RemoveTag(tagID string, retries ...RetryStrategy) (err error) {
func (v *vm) RemoveTag(tagID TagID, retries ...RetryStrategy) (err error) {
return v.client.RemoveTagFromVM(v.id, tagID, retries...)
}

Expand Down Expand Up @@ -1948,7 +1948,7 @@ func (v *vm) Name() string {
return v.name
}

func (v *vm) TagIDs() []string {
func (v *vm) TagIDs() []TagID {
return v.tagIDs
}

Expand All @@ -1964,7 +1964,7 @@ func (v *vm) Tags(retries ...RetryStrategy) ([]Tag, error) {
return tags, nil
}

func (v *vm) AddTagToVM(tagID string, retries ...RetryStrategy) error {
func (v *vm) AddTagToVM(tagID TagID, retries ...RetryStrategy) error {
return v.client.AddTagToVM(v.id, tagID, retries...)
}
func (v *vm) AddTagToVMByName(tagName string, retries ...RetryStrategy) error {
Expand Down Expand Up @@ -2192,10 +2192,10 @@ func vmInitializationConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
}

func vmTagsConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
var tagIDs []string
var tagIDs []TagID
if sdkTags, ok := sdkObject.Tags(); ok {
for _, tag := range sdkTags.Slice() {
tagID, _ := tag.Id()
tagID := TagID(tag.MustId())
tagIDs = append(tagIDs, tagID)
}
}
Expand Down
6 changes: 3 additions & 3 deletions vm_tag_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
ovirtsdk "github.com/ovirt/go-ovirt"
)

func (o *oVirtClient) AddTagToVM(id VMID, tagID string, retries ...RetryStrategy) (err error) {
func (o *oVirtClient) AddTagToVM(id VMID, tagID TagID, retries ...RetryStrategy) (err error) {
retries = defaultRetries(retries, defaultWriteTimeouts(o))
err = retry(
fmt.Sprintf("adding tag %s to VM %s", tagID, id),
o.logger,
retries,
func() error {
_, err := o.conn.SystemService().VmsService().VmService(string(id)).TagsService().Add().
Tag(ovirtsdk.NewTagBuilder().Id(tagID).MustBuild()).Send()
Tag(ovirtsdk.NewTagBuilder().Id(string(tagID)).MustBuild()).Send()

if err != nil {
return err
Expand All @@ -24,7 +24,7 @@ func (o *oVirtClient) AddTagToVM(id VMID, tagID string, retries ...RetryStrategy
return
}

func (m *mockClient) AddTagToVM(id VMID, tagID string, retries ...RetryStrategy) (err error) {
func (m *mockClient) AddTagToVM(id VMID, tagID TagID, _ ...RetryStrategy) (err error) {
m.lock.Lock()
defer m.lock.Unlock()

Expand Down
6 changes: 3 additions & 3 deletions vm_tag_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
)

func (o *oVirtClient) RemoveTagFromVM(id VMID, tagID string, retries ...RetryStrategy) (err error) {
func (o *oVirtClient) RemoveTagFromVM(id VMID, tagID TagID, retries ...RetryStrategy) (err error) {
retries = defaultRetries(retries, defaultWriteTimeouts(o))
err = retry(
fmt.Sprintf("removing tag from VM %s", id),
Expand All @@ -16,15 +16,15 @@ func (o *oVirtClient) RemoveTagFromVM(id VMID, tagID string, retries ...RetryStr
VmsService().
VmService(string(id)).
TagsService().
TagService(tagID).
TagService(string(tagID)).
Remove().
Send()
return err
})
return
}

func (m *mockClient) RemoveTagFromVM(id VMID, tagID string, retries ...RetryStrategy) error {
func (m *mockClient) RemoveTagFromVM(id VMID, tagID TagID, _ ...RetryStrategy) error {
m.lock.Lock()
defer m.lock.Unlock()
if _, ok := m.vms[id]; !ok {
Expand Down

0 comments on commit 97980ab

Please sign in to comment.