This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
forked from scaleway/scaleway-cli
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtasks.go
82 lines (66 loc) · 1.78 KB
/
tasks.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
package api
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// Task represents a Task
type Task struct {
// Identifier is a unique identifier for the task
Identifier string `json:"id,omitempty"`
// StartDate is the start date of the task
StartDate string `json:"started_at,omitempty"`
// TerminationDate is the termination date of the task
TerminationDate string `json:"terminated_at,omitempty"`
HrefFrom string `json:"href_from,omitempty"`
Description string `json:"description,omitempty"`
Status string `json:"status,omitempty"`
Progress int `json:"progress,omitempty"`
}
// oneTask represents the response of a GET /tasks/UUID API call
type oneTask struct {
Task Task `json:"task,omitempty"`
}
// Tasks represents a group of tasks
type Tasks struct {
// Tasks holds tasks of the response
Tasks []Task `json:"tasks,omitempty"`
}
// GetTasks get the list of tasks from the API
func (s *API) GetTasks() ([]Task, error) {
query := url.Values{}
// TODO per_page=20&page=2
resp, err := s.GetResponsePaginate(s.computeAPI, "tasks", query)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := s.handleHTTPError([]int{http.StatusOK}, resp)
if err != nil {
return nil, err
}
var tasks Tasks
if err = json.Unmarshal(body, &tasks); err != nil {
return nil, err
}
return tasks.Tasks, nil
}
// GetTask fetches a specific task
func (s *API) GetTask(id string) (*Task, error) {
query := url.Values{}
resp, err := s.GetResponsePaginate(s.computeAPI, fmt.Sprintf("tasks/%s", id), query)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := s.handleHTTPError([]int{http.StatusOK}, resp)
if err != nil {
return nil, err
}
var t oneTask
if err = json.Unmarshal(body, &t); err != nil {
return nil, err
}
return &t.Task, nil
}