-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconnection.go
168 lines (156 loc) · 4.16 KB
/
connection.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
166
167
168
// Copyright (C) 2017 Battelle Memorial Institute
// All rights reserved.
//
// This software may be modified and distributed under the terms
// of the BSD-2 license. See the LICENSE file for details.
package ovirtapi
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
type Connection struct {
EndPoint *url.URL
UserName string
Password string
Debug bool
Filter bool
Links []Link `json:"link"`
SpecialObjects struct {
Links []Link `json:"link"`
} `json:"special_objects"`
ProductInfo struct {
Name string `json:"name"`
Vendor string `json:"vendor"`
Version struct {
Major string `json:"major"`
Minor string `json:"minor"`
Revision string `json:"revision"`
Build string `json:"build"`
FullVersion string `json:"full_version"`
} `json:"version"`
} `json:"product_info"`
Summary struct {
Vms struct {
Active int `json:"active,string"`
Total int `json:"total,string"`
} `json:"vms"`
Hosts struct {
Active int `json:"active,string"`
Total int `json:"total,string"`
} `json:"hosts"`
Users struct {
Active int `json:"active,string"`
Total int `json:"total,string"`
} `json:"users"`
StorageDomains struct {
Active int `json:"active,string"`
Total int `json:"total,string"`
} `json:"storage_domains"`
} `json:"summary"`
}
type Fault struct {
StatusCode int
Detail string `json:"detail"`
Reason string `json:"reason"`
}
func (f Fault) Error() string {
if f.Reason != "" && f.Detail != "" {
return fmt.Sprintf("Server Error, Reason: %s, Detail: %s", f.Reason, f.Detail)
}
return fmt.Sprintf("Error getting response from server (Response code %d )", f.StatusCode)
}
func NewConnection(endpoint string, username string, password string, debug bool) (*Connection, error) {
endpointURL, err := url.Parse(endpoint)
if err != nil {
return nil, errors.New("Error parsing endpoint URL")
}
con := &Connection{
EndPoint: endpointURL,
UserName: username,
Password: password,
Debug: debug,
Filter: true,
}
body, err := con.Request("GET", endpointURL, nil)
if err != nil {
return nil, err
}
return con, json.Unmarshal(body, &con)
}
func (con *Connection) ResolveLink(link string) *url.URL {
return con.EndPoint.ResolveReference(&url.URL{Path: link})
}
func (con *Connection) Request(verb string, requestURL *url.URL, reqBody []byte) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest(verb, requestURL.String(), bytes.NewBuffer(reqBody))
if err != nil {
return nil, err
}
if reqBody != nil {
req.Header.Add("Content-Type", "application/json")
}
if con.Filter {
req.Header.Add("Filter", "true")
}
req.Header.Add("Accept", "application/json")
req.SetBasicAuth(con.UserName, con.Password)
if con.Debug {
dump, _ := httputil.DumpRequestOut(req, true)
log.Println(">", strings.Replace(strings.Replace(string(dump), "\r\n", "\n", -1), "\n", "\n> ", -1))
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if con.Debug {
dump, _ := httputil.DumpResponse(resp, true)
log.Println("<", strings.Replace(strings.Replace(string(dump), "\r\n", "\n", -1), "\n", "\n< ", -1))
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
fault := Fault{resp.StatusCode, "", ""}
respBody, err := ioutil.ReadAll(resp.Body)
if err == nil {
json.Unmarshal(respBody, &fault)
if fault.Reason == "" {
json.Unmarshal(respBody, &Action{Fault: &fault})
}
}
return nil, fault
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return respBody, nil
}
func (con *Connection) GetLink(rel string) (*url.URL, error) {
for _, link := range con.Links {
if strings.ToLower(rel) == link.Rel {
return con.ResolveLink(link.Href), nil
}
}
return nil, fmt.Errorf("con does not have %s link", rel)
}
func (con *Connection) GetLinkBody(link string, id string) ([]byte, error) {
url, err := con.GetLink(link)
if err != nil {
return nil, err
}
if id != "" {
url.Path += "/" + id
}
body, err := con.Request("GET", url, nil)
if err != nil {
return nil, err
}
return body, nil
}