forked from oVirt/go-ovirt-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaffinitygroup_get_by_name.go
84 lines (77 loc) · 2.28 KB
/
affinitygroup_get_by_name.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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(o))
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) == 0 {
return newError(ENotFound, "no affinity group named %s found in cluster %s", name, clusterID)
}
if len(results) > 1 {
return newError(EMultipleResults, "multiple affinity groups with the name %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(m))
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
}