-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxmox.go
72 lines (61 loc) · 1.47 KB
/
proxmox.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
package main
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"sort"
"unicode"
"github.com/luthermonson/go-proxmox"
)
// base
func initClient() *proxmox.Client {
credentials := proxmox.Credentials{
Username: config.ProxmoxVEUsername,
Password: config.ProxmoxVEPassword,
}
proxmoxAPIEndpoint := fmt.Sprintf("https://%s/api2/json", config.ProxmoxVEHost)
var client *proxmox.Client
isIP := unicode.IsDigit(rune(config.ProxmoxVEHost[0]))
if !isIP {
client = proxmox.NewClient(
proxmoxAPIEndpoint,
proxmox.WithCredentials(&credentials),
)
} else {
insecureHTTPClient := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
client = proxmox.NewClient(
proxmoxAPIEndpoint,
proxmox.WithCredentials(&credentials),
proxmox.WithHTTPClient(&insecureHTTPClient),
)
}
return client
}
func getVersion(client *proxmox.Client) (string, error) {
version, err := client.Version(context.Background())
return version.Release, err
}
// vm
func getNode(client *proxmox.Client) (*proxmox.Node, error) {
node, err := client.Node(context.Background(), config.ProxmoxVENodeName)
return node, err
}
func getVMs(node *proxmox.Node) (proxmox.VirtualMachines, error) {
vms, err := node.VirtualMachines(context.Background())
if err != nil {
return nil, err
} else {
// sort output by VM name
sort.Slice(vms, func(i, j int) bool {
return vms[i].Name < vms[j].Name
})
return vms, nil
}
}