/
freenas.go
230 lines (189 loc) · 5.04 KB
/
freenas.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
package freenas
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"sync"
)
type Response struct {
*http.Response
}
func newResponse(r *http.Response) *Response {
response := &Response{Response: r}
return response
}
type ListOptions struct {
// For paginated result sets, page of results to retrieve.
Offset int `url:"page,omitempty"`
// For paginated result sets, the number of results to include per page.
Limit int `url:"per_page,omitempty"`
}
const (
defaultBaseURL = "https://freenas.local/"
userAgent = "go-freenas"
mediaTypeJSON = "application/json"
)
type Client struct {
clientMu sync.Mutex // clientMu protects the client during calls that modify the CheckRedirect func.
client *http.Client // HTTP client used to communicate with the API.
// Base URL for API requests.
BaseURL *url.URL
// User agent used when communicating with the FreeNAS API.
UserAgent string
common service // Reuse a single struct instead of allocating one for each service on the heap.
Datasets *DatasetService
NfsShares *NfsShareService
Users *UserService
debug bool
}
type service struct {
client *Client
}
// BasicAuthTransport is an http.RoundTripper that authenticates all requests
// using HTTP Basic Authentication with the provided username and password.
type BasicAuthTransport struct {
Server string
Username string
Password string
// Transport is the underlying HTTP transport to use when making requests.
// It will default to http.DefaultTransport if nil.
Transport http.RoundTripper
}
// RoundTrip implements the RoundTripper interface.
func (t *BasicAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// To set extra headers, we must make a copy of the Request so
// that we don't modify the Request we were given. This is required by the
// specification of http.RoundTripper.
//
// Since we are going to modify only req.Header here, we only need a deep copy
// of req.Header.
req2 := new(http.Request)
*req2 = *req
req2.Header = make(http.Header, len(req.Header))
for k, s := range req.Header {
req2.Header[k] = append([]string(nil), s...)
}
req2.SetBasicAuth(t.Username, t.Password)
return t.transport().RoundTrip(req2)
}
// Client returns an *http.Client that makes requests that are authenticated
// using HTTP Basic Authentication.
func (t *BasicAuthTransport) Client() *http.Client {
return &http.Client{Transport: t}
}
func (t *BasicAuthTransport) transport() http.RoundTripper {
if t.Transport != nil {
return t.Transport
}
return http.DefaultTransport
}
func NewClient(server, user, password string) *Client {
t := &BasicAuthTransport{
Username: user,
Password: password,
}
baseURL, _ := url.Parse(server + "/api/v1.0/")
c := &Client{
client: t.Client(),
BaseURL: baseURL,
UserAgent: userAgent,
debug: false,
}
c.common.client = c
c.Datasets = (*DatasetService)(&c.common)
c.NfsShares = (*NfsShareService)(&c.common)
c.Users = (*UserService)(&c.common)
return c
}
func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error) {
if !strings.HasSuffix(urlStr, "/") {
urlStr = urlStr + "/"
}
u, err := c.BaseURL.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
}
if body != nil {
req.Header.Set("Content-Type", mediaTypeJSON)
}
req.Header.Set("Accept", mediaTypeJSON)
if c.UserAgent != "" {
req.Header.Set("User-Agent", c.UserAgent)
}
return req, nil
}
func withContext(ctx context.Context, req *http.Request) *http.Request {
return req.WithContext(ctx)
}
func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Response, error) {
req = withContext(ctx, req)
if c.debug == true {
d, err := httputil.DumpRequestOut(req, true)
if err != nil {
return nil, err
}
fmt.Printf("%s\n", d)
}
resp, err := c.client.Do(req)
if err != nil {
// If we got an error, and the context has been canceled,
// the context's error is probably more useful.
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
return nil, err
}
defer resp.Body.Close()
response := newResponse(resp)
err = CheckResponse(resp)
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 response, err
}
func CheckResponse(r *http.Response) error {
if c := r.StatusCode; 200 <= c && c <= 299 {
return nil
}
return &ErrorResponse{}
}
type ErrorResponse struct{}
func (*ErrorResponse) Error() string {
return "An error occured"
}
// Debug enables dumping http request objects to stdout
func (c *Client) Debug(enable bool) {
c.debug = enable
}