summaryrefslogtreecommitdiff
path: root/http/client/client.go
blob: a6e2f4534a2773dab232e8df992eb979bcb38261 (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 *  Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */

// Package client provides methods for interaction with Boruta REST API server.
//
// Provided BorutaClient type besides implementing boruta.Requests and
// boruta.Workers interfaces provides few convenient methods that allow to
// quickly check boruta.Request state, timeout and boruta.Worker state.
package client

import (
	"bytes"
	"crypto/x509"
	"encoding/json"
	"encoding/pem"
	"errors"
	"io"
	"io/ioutil"
	"net/http"
	"reflect"
	"strconv"
	"time"

	"git.tizen.org/tools/boruta"
	util "git.tizen.org/tools/boruta/http"
)

// BorutaClient handles interaction with specified Boruta server.
type BorutaClient struct {
	url string
	boruta.Requests
	boruta.Workers
}

const (
	// contentType denotes format in which we talk with Boruta server.
	contentType = "application/json"
	// apiPrefix is part of URL that is common in all uses and contains API
	// version.
	apiPrefix = "/api/v1/"
)

// NewBorutaClient provides BorutaClient ready to communicate with specified
// Boruta server.
//
//	cl := NewBorutaClient("http://127.0.0.1:1234")
func NewBorutaClient(url string) *BorutaClient {
	return &BorutaClient{
		url: url + apiPrefix,
	}
}

// readBody is simple wrapper function that reads body of http request into byte
// slice and closes the body.
func readBody(body io.ReadCloser) ([]byte, error) {
	defer body.Close()
	content, err := ioutil.ReadAll(body)
	if err != nil {
		err = errors.New("unable to read server response: " + err.Error())
	}
	return content, err
}

// bodyJSONUnmarshal is a wrapper that unmarshals server response into an
// appropriate structure.
func bodyJSONUnmarshal(body io.ReadCloser, val interface{}) error {
	content, err := readBody(body)
	if err != nil {
		return err
	}
	err = json.Unmarshal(content, val)
	if err != nil {
		return errors.New("unmarshalling JSON response failed: " + err.Error())
	}
	return nil
}

// getServerError parses Boruta server response that contains serverError and
// returns an error.
func getServerError(resp *http.Response) error {
	if resp.StatusCode < http.StatusBadRequest {
		return nil
	}
	srvErr := new(util.ServerError)
	switch resp.Header.Get("Content-Type") {
	case contentType:
		if err := bodyJSONUnmarshal(resp.Body, srvErr); err != nil {
			return err
		}
	default:
		msg, err := readBody(resp.Body)
		if err != nil {
			return err
		}
		srvErr.Err = string(msg)
	}
	srvErr.Status = resp.StatusCode
	return srvErr
}

// processResponse is helper function that parses Boruta server response and sets
// returned value or returns serverError. val must be a pointer. In case the body
// was empty (or server returned an error) it will be zeroed - if the val is a
// pointer to ReqInfo then ReqInfo members will be zeroed; to nil a pointer pass
// pointer to pointer to ReqInfo. Function may panic when passed value isn't a pointer.
func processResponse(resp *http.Response, val interface{}) error {
	var v reflect.Value

	if val != nil {
		if reflect.TypeOf(val).Kind() != reflect.Ptr {
			panic("can't set val, please pass appropriate pointer")
		}

		v = reflect.ValueOf(val).Elem()
	}

	setNil := func() {
		if val != nil && !reflect.ValueOf(val).IsNil() {
			v.Set(reflect.Zero(v.Type()))
		}
	}

	switch {
	case resp.StatusCode == http.StatusNoContent:
		setNil()
		return nil
	case resp.StatusCode >= http.StatusBadRequest:
		setNil()
		return getServerError(resp)
	default:
		return bodyJSONUnmarshal(resp.Body, val)
	}
}

// NewRequest creates new Boruta request.
func (client *BorutaClient) NewRequest(caps boruta.Capabilities,
	priority boruta.Priority, owner boruta.UserInfo, validAfter time.Time,
	deadline time.Time) (boruta.ReqID, error) {
	req, err := json.Marshal(&boruta.ReqInfo{
		Priority:   priority,
		Owner:      owner,
		Deadline:   deadline,
		ValidAfter: validAfter,
		Caps:       caps,
	})
	if err != nil {
		return 0, err
	}

	resp, err := http.Post(client.url+"reqs/", contentType, bytes.NewReader(req))
	if err != nil {
		return 0, err
	}
	var reqID util.ReqIDPack
	if err = processResponse(resp, &reqID); err != nil {
		return 0, err
	}
	return reqID.ReqID, nil
}

// CloseRequest closes or cancels Boruta request.
func (client *BorutaClient) CloseRequest(reqID boruta.ReqID) error {
	path := client.url + "reqs/" + strconv.Itoa(int(reqID)) + "/close"
	resp, err := http.Post(path, "", nil)
	if err != nil {
		return err
	}
	return processResponse(resp, nil)
}

// UpdateRequest prepares JSON with fields that should be changed for given
// request ID.
func (client *BorutaClient) UpdateRequest(reqInfo *boruta.ReqInfo) error {
	if reqInfo == nil {
		return errors.New("nil reqInfo passed")
	}
	req, err := json.Marshal(&struct {
		boruta.Priority
		Deadline   time.Time
		ValidAfter time.Time
	}{
		Priority:   reqInfo.Priority,
		Deadline:   reqInfo.Deadline,
		ValidAfter: reqInfo.ValidAfter,
	})
	if err != nil {
		return err
	}
	path := client.url + "reqs/" + strconv.Itoa(int(reqInfo.ID))
	resp, err := http.Post(path, contentType, bytes.NewReader(req))
	if err != nil {
		return err
	}
	return processResponse(resp, nil)
}

// GetRequestInfo queries Boruta server for details about given request ID.
func (client *BorutaClient) GetRequestInfo(reqID boruta.ReqID) (boruta.ReqInfo, error) {
	var reqInfo boruta.ReqInfo
	path := client.url + "reqs/" + strconv.Itoa(int(reqID))
	resp, err := http.Get(path)
	if err != nil {
		return reqInfo, err
	}
	err = processResponse(resp, &reqInfo)
	return reqInfo, err
}

// ListRequests queries Boruta server for list of requests that match given
// filter. Filter may be empty or nil to get list of all requests.
func (client *BorutaClient) ListRequests(filter boruta.ListFilter) ([]boruta.ReqInfo, error) {
	req, err := json.Marshal(filter)
	if err != nil {
		return nil, err
	}
	resp, err := http.Post(client.url+"reqs/list", contentType,
		bytes.NewReader(req))
	if err != nil {
		return nil, err
	}
	list := new([]boruta.ReqInfo)
	err = processResponse(resp, list)
	return *list, err
}

// AcquireWorker queries Boruta server for information required to access
// assigned Dryad. Access information may not be available when the call
// is issued because requests need to have assigned worker.
func (client *BorutaClient) AcquireWorker(reqID boruta.ReqID) (boruta.AccessInfo, error) {
	var accInfo boruta.AccessInfo
	path := client.url + "reqs/" + strconv.Itoa(int(reqID)) + "/acquire_worker"
	resp, err := http.Post(path, "", nil)
	if err != nil {
		return accInfo, err
	}
	accInfo2 := new(util.AccessInfo2)
	if err = processResponse(resp, &accInfo2); err != nil {
		return accInfo, err
	}
	block, _ := pem.Decode([]byte(accInfo2.Key))
	if block == nil || block.Type != "RSA PRIVATE KEY" {
		return accInfo, errors.New("wrong key: " + accInfo2.Key)
	}
	key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err != nil {
		return accInfo, err
	}
	accInfo.Addr = accInfo2.Addr
	accInfo.Username = accInfo2.Username
	accInfo.Key = *key
	return accInfo, nil
}

// ProlongAccess requests Boruta server to extend running time of job. User may
// need to call this method multiple times as long as access to Dryad is needed.
// If not called, Boruta server will terminate the tunnel when ReqInfo.Job.Timeout
// passes, and change state of request to CLOSED.
func (client *BorutaClient) ProlongAccess(reqID boruta.ReqID) error {
	path := client.url + "reqs/" + strconv.Itoa(int(reqID)) + "/prolong"
	resp, err := http.Post(path, "", nil)
	if err != nil {
		return err
	}
	return processResponse(resp, nil)
}

// ListWorkers queries Boruta server for list of workers that are in given groups
// and have provided capabilities. Setting both caps and groups to empty or nil
// lists all workers.
func (client *BorutaClient) ListWorkers(groups boruta.Groups,
	caps boruta.Capabilities) ([]boruta.WorkerInfo, error) {
	req, err := json.Marshal(&util.WorkersFilter{
		Groups:       groups,
		Capabilities: caps,
	})
	if err != nil {
		return nil, err
	}
	resp, err := http.Post(client.url+"workers/list", contentType,
		bytes.NewReader(req))
	if err != nil {
		return nil, err
	}
	list := new([]boruta.WorkerInfo)
	err = processResponse(resp, list)
	return *list, err
}

// GetWorkerInfo queries Boruta server for information about worker with given
// UUID.
func (client *BorutaClient) GetWorkerInfo(uuid boruta.WorkerUUID) (boruta.WorkerInfo, error) {
	var info boruta.WorkerInfo
	path := client.url + "workers/" + string(uuid)
	resp, err := http.Get(path)
	if err != nil {
		return info, err
	}
	err = processResponse(resp, &info)
	return info, err
}

// SetState requests Boruta server to change state of worker with provided UUID.
// SetState is intended only for Boruta server administrators.
func (client *BorutaClient) SetState(uuid boruta.WorkerUUID, state boruta.WorkerState) error {
	path := client.url + "workers/" + string(uuid) + "/setstate"
	req, err := json.Marshal(&util.WorkerStatePack{state})
	if err != nil {
		return err
	}
	resp, err := http.Post(path, contentType, bytes.NewReader(req))
	if err != nil {
		return err
	}
	return processResponse(resp, nil)
}

// SetGroups requests Boruta server to change groups of worker with provided
// UUID. SetGroups is intended only for Boruta server administrators.
func (client *BorutaClient) SetGroups(uuid boruta.WorkerUUID, groups boruta.Groups) error {
	path := client.url + "workers/" + string(uuid) + "/setgroups"
	req, err := json.Marshal(groups)
	if err != nil {
		return err
	}
	resp, err := http.Post(path, contentType, bytes.NewReader(req))
	if err != nil {
		return err
	}
	return processResponse(resp, nil)
}

// Deregister requests Boruta server to deregister worker with provided UUID.
// Deregister is intended only for Boruta server administrators.
func (client *BorutaClient) Deregister(uuid boruta.WorkerUUID) error {
	path := client.url + "workers/" + string(uuid) + "/deregister"
	resp, err := http.Post(path, "", nil)
	if err != nil {
		return err
	}
	return processResponse(resp, nil)
}

// GetRequestState is convenient way to check state of a request with given reqID.
// When error occurs then returned boruta.ReqState will make no sense. Developer
// should always check for an error before proceeding with actions dependent on
// request state.
func (client *BorutaClient) GetRequestState(reqID boruta.ReqID) (boruta.ReqState, error) {
	path := client.url + "reqs/" + strconv.Itoa(int(reqID))
	resp, err := http.Head(path)
	if err != nil {
		return boruta.FAILED, err
	}
	if resp.StatusCode != http.StatusNoContent {
		return boruta.FAILED, errors.New("bad HTTP status: " + resp.Status)
	}
	return boruta.ReqState(resp.Header.Get("Boruta-Request-State")), nil
}