-
Notifications
You must be signed in to change notification settings - Fork 21
/
datacenter.go
88 lines (73 loc) · 2.67 KB
/
datacenter.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
85
86
87
88
package ovirtclient
import (
ovirtsdk4 "github.com/ovirt/go-ovirt"
)
//go:generate go run scripts/rest/rest.go -i "DataCenter" -n "datacenter" -o "Datacenter" -T DatacenterID
// DatacenterID is the UUID of a datacenter.
type DatacenterID string
// DatacenterClient contains the functions related to handling datacenter objects in oVirt. Datacenters bind together
// resources of an environment (clusters, storage domains).
// See https://www.ovirt.org/documentation/administration_guide/#chap-Data_Centers for details.
type DatacenterClient interface {
// GetDatacenter returns a single datacenter by its ID.
GetDatacenter(id DatacenterID, retries ...RetryStrategy) (Datacenter, error)
// ListDatacenters lists all datacenters in the oVirt engine.
ListDatacenters(retries ...RetryStrategy) ([]Datacenter, error)
// ListDatacenterClusters lists all clusters in the specified datacenter.
ListDatacenterClusters(id DatacenterID, retries ...RetryStrategy) ([]Cluster, error)
}
// DatacenterData is the core of a Datacenter when client functions are not required.
type DatacenterData interface {
ID() DatacenterID
Name() string
}
// Datacenter is a logical entity that defines the set of resources used in a specific environment.
// See https://www.ovirt.org/documentation/administration_guide/#chap-Data_Centers for details.
type Datacenter interface {
DatacenterData
// Clusters lists the clusters for this datacenter. This is a network call and may be slow.
Clusters(retries ...RetryStrategy) ([]Cluster, error)
// HasCluster returns true if the cluster is in the datacenter. This is a network call and may be slow.
HasCluster(clusterID ClusterID, retries ...RetryStrategy) (bool, error)
}
func convertSDKDatacenter(sdkObject *ovirtsdk4.DataCenter, client *oVirtClient) (Datacenter, error) {
id, ok := sdkObject.Id()
if !ok {
return nil, newFieldNotFound("datacenter", "id")
}
name, ok := sdkObject.Name()
if !ok {
return nil, newFieldNotFound("datacenter", "name")
}
return &datacenter{
client: client,
id: DatacenterID(id),
name: name,
}, nil
}
type datacenter struct {
client Client
id DatacenterID
name string
}
func (d datacenter) Clusters(retries ...RetryStrategy) ([]Cluster, error) {
return d.client.ListDatacenterClusters(d.id, retries...)
}
func (d datacenter) HasCluster(clusterID ClusterID, retries ...RetryStrategy) (bool, error) {
clusters, err := d.client.ListDatacenterClusters(d.id, retries...)
if err != nil {
return false, err
}
for _, cluster := range clusters {
if cluster.ID() == clusterID {
return true, nil
}
}
return false, nil
}
func (d datacenter) ID() DatacenterID {
return d.id
}
func (d datacenter) Name() string {
return d.name
}