-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #149: Get affinity groups by name
- Loading branch information
Janos Bonic
committed
Apr 6, 2022
1 parent
c15f7de
commit cc95288
Showing
6 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters