This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
apicalls.go
70 lines (61 loc) · 1.47 KB
/
apicalls.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
package ldapsync
import (
"crypto/tls"
"net/http"
"github.com/kolo/xmlrpc"
)
// UyuniCaller object
type UyuniCaller struct {
client *xmlrpc.Client
user string
password string
session string
}
// NewUyuniCaller is a constructor for the UyuniCaller object
func NewUyuniCaller(url string, skipSslCheck bool) *UyuniCaller {
uc := new(UyuniCaller)
uc.client, _ = xmlrpc.NewClient(url,
&http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: skipSslCheck,
},
})
return uc
}
// SetUser sets the username for the authentication
func (c *UyuniCaller) SetUser(user string) *UyuniCaller {
c.user = user
return c
}
// SetPassword sets the password for the authentication
func (c *UyuniCaller) SetPassword(password string) *UyuniCaller {
c.password = password
return c
}
// Obtain an authentication token
func (c *UyuniCaller) authenticate() {
var err error
var res interface{}
if c.user != "" && c.password != "" {
res, err = c.Call("auth.login", c.user, c.password)
if err != nil {
Log.Fatal(err)
}
c.session = res.(string)
} else {
Log.Fatalf("User and/or password for Uyuni required!")
}
}
// Session returns a token after the authentication
func (c *UyuniCaller) Session() string {
if c.session == "" {
c.authenticate()
}
return c.session
}
// Call any XML-RPC function
func (c *UyuniCaller) Call(name string, args ...interface{}) (interface{}, error) {
var res interface{}
err := c.client.Call(name, args, &res)
return res, err
}