-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (88 loc) · 3 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"encoding/json"
"flag"
"os"
//"reflect"
)
func main() {
// Define flags
dcsserver := flag.String("dcsserver","","DataCore SANsymphony server")
restserver := flag.String("restserver","","DataCore SANsymphony REST api server")
user := flag.String("user","","DataCore SANsymphony user")
passwd := flag.String("passwd","","DataCore SANsymphony password")
//Parse
flag.Parse()
//Check flags
if *dcsserver == "" {
fmt.Println("\nError! No DataCore SANsymphony server set")
fmt.Println("Add -dcsserver=x where x is IP or DNS of DataCore SANsymphony server")
fmt.Println("Exit!")
os.Exit(0)
} else if *restserver == "" {
fmt.Println("\nWarning ! No DataCore SANsymphony REST api server set")
fmt.Println("Add -restserver=x where x is IP or DNS of DataCore SANsymphony REST api server")
fmt.Println("We will use DataCore SANsymphony server as REST api server")
restserver = dcsserver
}
if *user == "" {
fmt.Println("\nError ! No DataCore SANsymphony server user set")
fmt.Println("Add -user=x where x is user for DataCore SANsymphony server")
fmt.Println("\nExit!")
os.Exit(0)
}
if *passwd == "" {
fmt.Println("\nError ! No DataCore SANsymphony server password set")
fmt.Println("Add -passwd=x where x is password for DataCore SANsymphony server")
fmt.Println("\nExit!")
os.Exit(0)
}
req_hosts := restRequest(*dcsserver , *restserver , *user , *passwd , "hosts")
req_servers := restRequest(*dcsserver , *restserver , *user , *passwd , "servers")
//fmt.Println(reflect.TypeOf(*req))
client := &http.Client{
}
resp_hosts, err := client.Do(&req_hosts)
if err != nil {
fmt.Printf("The HTTP request failed with error %s\n", err)
os.Exit(0)
} else if resp_hosts.Status != "200 OK"{
fmt.Printf("The HTTP request failed with error %s\n", resp_hosts.Status)
os.Exit(0)
}
data, _ := ioutil.ReadAll(resp_hosts.Body)
var hosts []host
json.Unmarshal([]byte(data), &hosts)
resp_servers, err := client.Do(&req_servers)
if err != nil {
fmt.Printf("The HTTP request failed with error %s\n", err)
os.Exit(0)
} else if resp_servers.Status != "200 OK"{
fmt.Printf("The HTTP request failed with error %s\n", resp_servers.Status)
os.Exit(0)
}
data, _ = ioutil.ReadAll(resp_servers.Body)
var servers []server
json.Unmarshal([]byte(data), &servers)
for _, item := range hosts{
client := &http.Client{
}
target_preffered := "hosts/" + item.Id + "/preferredservers"
req_preferredservers := restRequest(*dcsserver , *restserver , *user , *passwd , target_preffered)
resp_preferredservers, err := client.Do(&req_preferredservers)
data, err = ioutil.ReadAll(resp_preferredservers.Body)
if err != nil {
fmt.Printf("The HTTP request failed with error %s\n", err)
}
var preferredservers []preferredserver
json.Unmarshal([]byte(data), &preferredservers)
if len(preferredservers) > 0 {
fmt.Println(preferredservers[0].Caption)
} else {
fmt.Println(item.Caption, "|", "None")
}
}
}