forked from oVirt/go-ovirt-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature.go
80 lines (70 loc) · 2.24 KB
/
feature.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
package ovirtclient
import (
ovirtsdk "github.com/ovirt/go-ovirt"
)
// Feature is a specialized type for feature flags. These can be checked for support by using SupportsFeature in
// FeatureClient.
type Feature string
const (
// FeatureAutoPinning is a feature flag for autopinning support in the oVirt Engine supported since 4.4.5.
FeatureAutoPinning Feature = "autopinning"
// FeaturePlacementPolicy is a feature flag to indicate placement policy support in the oVirt Engine.
FeaturePlacementPolicy Feature = "placement_policy"
)
// FeatureClient provides the functions to determine the capabilities of the oVirt Engine.
type FeatureClient interface {
// SupportsFeature checks the features supported by the oVirt Engine.
SupportsFeature(feature Feature, retries ...RetryStrategy) (bool, error)
}
func (o *oVirtClient) SupportsFeature(feature Feature, retries ...RetryStrategy) (result bool, err error) {
var minimumVersion *ovirtsdk.Version
switch feature {
case FeatureAutoPinning:
minimumVersion = ovirtsdk.NewVersionBuilder().
Major(4).
Minor(4).
Build_(5).
Revision(0).
MustBuild()
case FeaturePlacementPolicy:
minimumVersion = ovirtsdk.NewVersionBuilder().
Major(4).
Minor(4).
Build_(5).
Revision(0).
MustBuild()
default:
return false, newError(EBug, "unknown feature: %s", feature)
}
retries = defaultRetries(retries, defaultReadTimeouts(o))
err = retry(
"fetching engine version",
o.logger,
retries,
func() error {
systemGetResponse, err := o.conn.SystemService().Get().Send()
if err != nil {
return err
}
engineVer := systemGetResponse.MustApi().MustProductInfo().MustVersion()
versionCompareResult := versionCompare(engineVer, minimumVersion)
result = versionCompareResult >= 0
return nil
})
return result, err
}
func versionCompare(v *ovirtsdk.Version, other *ovirtsdk.Version) int64 {
if result := v.MustMajor() - other.MustMajor(); result != 0 {
return result
}
if result := v.MustMinor() - other.MustMinor(); result != 0 {
return result
}
if result := v.MustBuild() - other.MustBuild(); result != 0 {
return result
}
return v.MustRevision() - other.MustRevision()
}
func (m *mockClient) SupportsFeature(_ Feature, _ ...RetryStrategy) (bool, error) {
return true, nil
}