-
Notifications
You must be signed in to change notification settings - Fork 1
/
hmc_lpar_list.go
165 lines (134 loc) · 3.95 KB
/
hmc_lpar_list.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//author: [email protected]
// description: basic test of HMC REST api. Authenticate and get logical partition informations
package main
import (
"bytes"
"crypto/tls"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"text/template"
)
//
// XML parsing structures
//
type Feed struct {
XMLName xml.Name `xml:"feed"`
Entries []Entry `xml:"entry"`
}
type Entry struct {
XMLName xml.Name `xml:"entry"`
Contents []Content `xml:"content"`
}
type Content struct {
XMLName xml.Name `xml:"content"`
Lpar []LogicalPartition `xml:"http://www.ibm.com/xmlns/systems/power/firmware/uom/mc/2012_10/ LogicalPartition"`
}
type LogicalPartition struct {
XMLName xml.Name `xml:"http://www.ibm.com/xmlns/systems/power/firmware/uom/mc/2012_10/ LogicalPartition"`
PartitionName string
PartitionID int
PartitionUUID string
LogicalSerialNumber string
OperatingSystemVersion string
}
//
// HTTP session struct
//
type Session struct {
client *http.Client
User string
Password string
url string
}
func NewSession(user string, password string, url string) *Session {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
jar, err := cookiejar.New(nil)
if err != nil {
log.Fatal(err)
}
return &Session{client: &http.Client{Transport: tr, Jar: jar}, User: user, Password: password, url: url}
}
func (s *Session) doLogon() {
authurl := s.url + "/rest/api/web/Logon"
// template for login request
logintemplate := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LogonRequest xmlns="http://www.ibm.com/xmlns/systems/power/firmware/web/mc/2012_10/" schemaVersion="V1_1_0">
<Metadata>
<Atom/>
</Metadata>
<UserID kb="CUR" kxe="false">{{.User}}</UserID>
<Password kb="CUR" kxe="false">{{.Password}}</Password>
</LogonRequest>`
tmpl := template.New("logintemplate")
tmpl.Parse(logintemplate)
authrequest := new(bytes.Buffer)
err := tmpl.Execute(authrequest, s)
if err != nil {
log.Fatal(err)
}
request, err := http.NewRequest("PUT", authurl, authrequest)
// set request headers
request.Header.Set("Content-Type", "application/vnd.ibm.powervm.web+xml; type=LogonRequest")
request.Header.Set("Accept", "application/vnd.ibm.powervm.web+xml; type=LogonResponse")
request.Header.Set("X-Audit-Memento", "hmctest")
response, err := s.client.Do(request)
if err != nil {
log.Fatal(err)
} else {
defer response.Body.Close()
if response.StatusCode != 200 {
log.Fatalf("Error status code: %d", response.StatusCode)
}
}
}
func (s *Session) getManaged() {
mgdurl := s.url + "/rest/api/uom/LogicalPartition"
request, err := http.NewRequest("GET", mgdurl, nil)
request.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
response, err := s.client.Do(request)
if err != nil {
log.Fatal(err)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
if response.StatusCode != 200 {
log.Fatalf("Error getting LPAR informations. status code: %d", response.StatusCode)
}
var feed Feed
new_err := xml.Unmarshal(contents, &feed)
if new_err != nil {
log.Fatal(new_err)
}
fmt.Printf("\t%-10s\t%-40s\t%-8s\t%-25s\n", "partition", "UUID", "LSERIAL", "OS")
for _, entry := range feed.Entries {
for _, content := range entry.Contents {
for _, lpar := range content.Lpar {
fmt.Printf("\t%-10s\t%-40s\t%-8s\t%-25s\n", lpar.PartitionName, lpar.PartitionUUID, lpar.LogicalSerialNumber, lpar.OperatingSystemVersion)
}
}
}
}
}
//
// MAIN
//
func main() {
user := flag.String("user", "hscroot", "hmc user")
password := flag.String("password", "abc123", "hmc user password")
url := flag.String("url", "https://myhmc:12443", "hmc REST api url")
flag.Parse()
//initialize new http session
session := NewSession(*user, *password, *url)
session.doLogon()
session.getManaged()
}