forked from jonludlam/docker-machine-xenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xenapiclient.go
123 lines (98 loc) · 2.5 KB
/
xenapiclient.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package xenserver
import (
"crypto/tls"
"fmt"
"net/http"
"github.com/nilshell/xmlrpc"
xsclient "github.com/xenserver/go-xenserver-client"
)
type XenAPIClient struct {
xsclient.XenAPIClient
}
func assertUnique(objs []interface{}, obj_label, name_label string) (obj interface{}, err error) {
switch len(objs) {
case 1:
return objs[0], nil
case 0:
return nil, fmt.Errorf("Unable to get a %s named %v", obj_label, name_label)
default:
return nil, fmt.Errorf("Too many %ss returned for name %v", obj_label, name_label)
}
}
// Get Unique VM By NameLabel
func (c *XenAPIClient) GetUniqueVMByNameLabel(name_label string) (vm *xsclient.VM, err error) {
vms, err := c.GetVMByNameLabel(name_label)
if err != nil {
return nil, err
}
var objs []interface{}
for _, v := range vms {
objs = append(objs, v)
}
obj, err := assertUnique(objs, "VM", name_label)
if err != nil {
return nil, err
}
return obj.(*xsclient.VM), nil
}
// Get Unique SR By NameLabel
func (c *XenAPIClient) GetUniqueSRByNameLabel(name_label string) (sr *xsclient.SR, err error) {
srs, err := c.GetSRByNameLabel(name_label)
if err != nil {
return nil, err
}
var objs []interface{}
for _, v := range srs {
objs = append(objs, v)
}
obj, err := assertUnique(objs, "SR", name_label)
if err != nil {
return nil, err
}
return obj.(*xsclient.SR), nil
}
// Get Unique Host By NameLabel
func (c *XenAPIClient) GetUniqueHostByNameLabel(name_label string) (host *xsclient.Host, err error) {
hosts, err := c.GetHostByNameLabel(name_label)
if err != nil {
return nil, err
}
var objs []interface{}
for _, v := range hosts {
objs = append(objs, v)
}
obj, err := assertUnique(objs, "Host", name_label)
if err != nil {
return nil, err
}
return obj.(*xsclient.Host), nil
}
// Get Unique Network By NameLabel
func (c *XenAPIClient) GetUniqueNetworkByNameLabel(name_label string) (network *xsclient.Network, err error) {
networks, err := c.GetNetworkByNameLabel(name_label)
if err != nil {
return nil, err
}
var objs []interface{}
for _, v := range networks {
objs = append(objs, v)
}
obj, err := assertUnique(objs, "Network", name_label)
if err != nil {
return nil, err
}
return obj.(*xsclient.Network), nil
}
func NewXenAPIClient(host, username, password string) (c XenAPIClient) {
c.Host = host
c.Url = "https://" + host
c.Username = username
c.Password = password
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
c.RPC, _ = xmlrpc.NewClient(c.Url, tr)
return
}