/
pc.go
281 lines (237 loc) · 6.83 KB
/
pc.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package pc
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"reflect"
"sync"
"github.com/google/go-querystring/query"
)
// NUTANIX_PC_USER is the environment variable name for the PC username
const NUTANIX_PC_USER = "NUTANIX_PC_USER"
// NUTANIX_PC_PASS is the environment variables for the PC password
const NUTANIX_PC_PASS = "NUTANIX_PC_PASS"
const NUTANIX_PC_URL = "NUTANIX_PC_URL"
// Service is used to create endpoint specific services to utilize concurrency with timeouts
type Service struct {
client *Client
}
// AuthCredentials are the username and password received from ENV variables
// never hard code these values into any code!
type AuthCredentials struct {
Username string
Password string
}
// ServiceConfig is the configuration to use PC
type ServiceConfig struct {
URL *string
User *string
Pass *string
}
// Client is an HTTP Client
type Client struct {
clientMu sync.Mutex
client *http.Client
PCURL *url.URL
Login AuthCredentials
common Service
Cluster *ClusterService
User *UserService
VM *VMService
Host *HostService
ACPS *AccessControlPoliciesService
Image *ImageService
Category *CategoryService
Subnet *SubnetService
Project *ProjectService
Task *TaskService
Role *RoleService
Permission *PermissionService
NSR *NetworkSecurityRuleService
Batch *BatchService
}
// CheckConfig will ensure that the minimal amount of information is present in the config
func CheckConfig(conf *ServiceConfig) error {
errOut := errors.New("insufficient data for Prism Central config")
if conf == nil {
return errOut
}
if conf.URL == nil {
URL := os.Getenv(NUTANIX_PC_URL)
if len(URL) < 3 {
return errors.New("insufficient data for Prism Central config: no URL")
}
conf.URL = &URL
}
if conf.User == nil {
Username := os.Getenv(NUTANIX_PC_USER)
if len(Username) < 3 {
return errors.New("insufficient data for Prism Central config: no username")
}
conf.User = &Username
}
if conf.Pass == nil {
Password := os.Getenv(NUTANIX_PC_PASS)
if len(Password) < 8 {
return errors.New("insufficient data for Prism Central config: no password")
}
conf.Pass = &Password
}
if len(*conf.URL) < 8 || len(*conf.User) < 3 || len(*conf.Pass) < 8 {
return errOut
}
return nil
}
// NewClient is used for Nutanix Prism client instantiation
func NewClient(httpClient *http.Client, conf *ServiceConfig) (*Client, error) {
err := CheckConfig(conf)
if err != nil {
fmt.Println("minimal amount of information not available for Prism Central config: ", err)
return nil, err
}
if httpClient == nil {
httpClient = &http.Client{}
}
PCURL, err := url.Parse(*conf.URL)
if err != nil {
return nil, err
}
login, err := GetCredentials(conf)
if err != nil {
return nil, err
}
c := &Client{client: httpClient, PCURL: PCURL, Login: login}
c.common.client = c
c.Cluster = (*ClusterService)(&c.common)
c.User = (*UserService)(&c.common)
c.VM = (*VMService)(&c.common)
c.Host = (*HostService)(&c.common)
c.ACPS = (*AccessControlPoliciesService)(&c.common)
c.Image = (*ImageService)(&c.common)
c.Category = (*CategoryService)(&c.common)
c.Subnet = (*SubnetService)(&c.common)
c.Project = (*ProjectService)(&c.common)
c.Task = (*TaskService)(&c.common)
c.Role = (*RoleService)(&c.common)
c.Permission = (*PermissionService)(&c.common)
c.Batch = (*BatchService)(&c.common)
c.NSR = (*NetworkSecurityRuleService)(&c.common)
return c, nil
}
// NewRequest creates an API request
func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error) {
u, err := c.PCURL.Parse(urlStr)
if err != nil {
return nil, err
}
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(body)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
encodedAuth := FormatCredentials(c.Login)
basicAuthHeader := fmt.Sprintf("Basic %s", encodedAuth)
req.Header.Add("content-type", "application/json")
req.Header.Add("authorization", basicAuthHeader)
return req, nil
}
// GetCredentials gets the username and password from the ENV
// variables for use in the application
func GetCredentials(conf *ServiceConfig) (AuthCredentials, error) {
a := AuthCredentials{}
if len(*conf.User) < 3 {
a.Username = os.Getenv("NUTANIX_PC_USER")
} else {
a.Username = *conf.User
}
if len(*conf.Pass) < 8 {
a.Password = os.Getenv("NUTANIX_PC_PASS")
} else {
a.Password = *conf.Pass
}
if len(a.Username) == 0 {
return AuthCredentials{}, errors.New("No username found in environment variable NutanixUser or in provided config")
}
if len(a.Password) == 0 {
return AuthCredentials{}, errors.New("No password found in environment variable NutanixPass or in provided config")
}
return a, nil
}
// FormatCredentials takes in an AuthCredential type and returns a base64 encoded string
// in the proper format for use by the REST API
func FormatCredentials(a AuthCredentials) string {
authString := fmt.Sprintf("%s:%s", a.Username, a.Password)
encoded := base64.StdEncoding.EncodeToString([]byte(authString))
return encoded
}
// CheckResponse looks for common response errors
// Need to fill this in at some point
func CheckResponse(r *http.Response) error {
if c := r.StatusCode; 200 <= c && c <= 299 {
return nil
}
return errors.New(fmt.Sprintln("Response error: ", r.StatusCode, r.Status))
}
// Do sends an API request and returns the API response. The API response is
// JSON decoded and stored in the value pointed to by v, or returned as an
// error if an API error has occurred. If v implements the io.Writer
// interface, the raw response body will be written to v, without attempting to
// first decode it. If rate limit is exceeded and reset time is in the future,
// Do returns *RateLimitError immediately without making a network API call.
func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
resp, err := c.client.Do(req)
if err != nil {
fmt.Println("Received an error on request: ", err)
return nil, err
}
defer resp.Body.Close()
err = CheckResponse(resp)
if err != nil {
return resp, err
}
if v != nil {
if w, ok := v.(io.Writer); ok {
io.Copy(w, resp.Body)
} else {
decErr := json.NewDecoder(resp.Body).Decode(v)
if decErr == io.EOF {
decErr = nil // ignore EOF errors caused by empty response body
}
if decErr != nil {
err = decErr
}
}
}
return resp, err
}
func addOptions(s string, opt interface{}) (string, error) {
v := reflect.ValueOf(opt)
if v.Kind() == reflect.Ptr && v.IsNil() {
return s, nil
}
u, err := url.Parse(s)
if err != nil {
return s, err
}
qs, err := query.Values(opt)
if err != nil {
return s, err
}
u.RawQuery = qs.Encode()
return u.String(), nil
}