Skip to content

Commit

Permalink
Fixes #149: Get affinity groups by name
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Bonic committed Apr 6, 2022
1 parent c15f7de commit cc95288
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 1 deletion.
2 changes: 2 additions & 0 deletions affinitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type AffinityGroupClient interface {
// GetAffinityGroup returns a specific affinity group based on its ID. An error is returned if the affinity label
// doesn't exist.
GetAffinityGroup(clusterID ClusterID, id AffinityGroupID, retries ...RetryStrategy) (AffinityGroup, error)
// GetAffinityGroupByName returns an affinity group by name.
GetAffinityGroupByName(clusterID ClusterID, name string, retries ...RetryStrategy) (AffinityGroup, error)
// RemoveAffinityGroup removes the affinity group specified.
RemoveAffinityGroup(clusterID ClusterID, id AffinityGroupID, retries ...RetryStrategy) error

Expand Down
3 changes: 3 additions & 0 deletions affinitygroup_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ func (o *oVirtClient) CreateAffinityGroup(
params CreateAffinityGroupOptionalParams,
retries ...RetryStrategy,
) (result AffinityGroup, err error) {
if params == nil {
params = CreateAffinityGroupParams()
}
retries = defaultRetries(retries, defaultWriteTimeouts())
err = retry(
fmt.Sprintf("creating affinity group in cluster %s", clusterID),
Expand Down
3 changes: 2 additions & 1 deletion affinitygroup_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ func (o *oVirtClient) GetAffinityGroup(clusterID ClusterID, id AffinityGroupID,
if !ok {
return newError(
ENotFound,
"no cluster returned when getting cluster ID %s",
"no affinity group returned when getting affinity group ID %s in cluster ID %s",
id,
clusterID,
)
}
result, err = convertSDKAffinityGroup(sdkObject, o)
Expand Down
81 changes: 81 additions & 0 deletions affinitygroup_get_by_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package ovirtclient

import (
"fmt"

ovirtsdk "github.com/ovirt/go-ovirt"
)

func (o *oVirtClient) GetAffinityGroupByName(clusterID ClusterID, name string, retries ...RetryStrategy) (result AffinityGroup, err error) {
retries = defaultRetries(retries, defaultReadTimeouts())
err = retry(
fmt.Sprintf("getting affinity group %s", name),
o.logger,
retries,
func() error {
response, err := o.conn.SystemService().ClustersService().ClusterService(string(clusterID)).AffinityGroupsService().List().Send()
if err != nil {
return err
}
sdkObject, ok := response.Groups()
if !ok {
return newError(
ENotFound,
"no affinity group returned when listing affinity groups in cluster ID %s",
clusterID,
)
}
slice := sdkObject.Slice()
if len(slice) == 0 {
return newError(ENotFound, "no affinity group named %s found in cluster %s", name, clusterID)
}
var results []*ovirtsdk.AffinityGroup
for _, item := range slice {
n, ok := item.Name()
if ok && n == name {
results = append(results, item)
}
}
if len(results) > 1 {
return newError(EMultipleResults, "no affinity group named %s found in cluster %s", name, clusterID)
}
result, err = convertSDKAffinityGroup(results[0], o)
if err != nil {
return wrap(
err,
EBug,
"failed to convert affinity group %s",
name,
)
}
return nil
})
return result, err
}

func (m *mockClient) GetAffinityGroupByName(clusterID ClusterID, name string, retries ...RetryStrategy) (result AffinityGroup, err error) {

retries = defaultRetries(retries, defaultWriteTimeouts())

err = retry(
fmt.Sprintf("getting affinity group %s from cluster %s", name, clusterID),
m.logger,
retries,
func() error {
m.lock.Lock()
defer m.lock.Unlock()

clusterAffinityGroups, ok := m.affinityGroups[clusterID]
if !ok {
return newError(ENotFound, "Cluster with ID %s not found", clusterID)
}
for _, ag := range clusterAffinityGroups {
if ag.name == name {
result = ag
return nil
}
}
return newError(ENotFound, "Affinity group with name %s not found in cluster %s", name, clusterID)
})
return
}
20 changes: 20 additions & 0 deletions affinitygroup_get_by_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ovirtclient_test

import "testing"

func TestGetAGByName(t *testing.T) {
helper := getHelper(t)

// Create a dummy AG to test if the real AG is actually returned
assertCanCreateAffinityGroup(t, helper, nil)
// Create the AG we are looking for
ag := assertCanCreateAffinityGroup(t, helper, nil)
// Fetch the AG
ag2, err := helper.GetClient().GetAffinityGroupByName(ag.ClusterID(), ag.Name())
if err != nil {
t.Fatalf("failed to fetch affinity group by name (%v)", err)
}
if ag.ID() != ag2.ID() {
t.Fatalf("Affinity group ID mismatch after fetching by name (expected: %s, got: %s)", ag.ID(), ag2.ID())
}
}
5 changes: 5 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const ETLSError ErrorCode = "tls_error"
// ENotFound signals that the resource requested was not found.
const ENotFound ErrorCode = "not_found"

// EMultipleResults indicates that multiple items were found where only one was expected.
const EMultipleResults ErrorCode = "multiple_results"

// EBug signals an error that should never happen. Please report this.
const EBug ErrorCode = "bug"

Expand Down Expand Up @@ -94,6 +97,8 @@ func (e ErrorCode) CanAutoRetry() bool {
return false
case ENotFound:
return false
case EMultipleResults:
return false
case EBug:
return false
case EUnsupported:
Expand Down

0 comments on commit cc95288

Please sign in to comment.