-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
150 lines (139 loc) · 4.06 KB
/
client.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
package kala
import (
"fmt"
"gopkg.in/resty.v1"
"net/http"
"strings"
)
// KalaClient is the base struct for this package.
type KalaClient struct {
apiEndpoint string
requester *resty.Request
}
// New is used to create a new KalaClient based off of the apiEndpoint
// Example:
// c := New("http://127.0.0.1:8000")
func New(apiEndpoint string) *KalaClient {
if strings.HasSuffix(apiEndpoint, "/") {
apiEndpoint = apiEndpoint[:len(apiEndpoint)-1]
}
return &KalaClient{
apiEndpoint: apiEndpoint + ApiUrlPrefix,
requester: resty.SetHostURL(apiEndpoint + ApiUrlPrefix).SetRedirectPolicy(FlexibleRedirectPolicy(10)).R(),
}
}
// CreateJob is used for creating a new job within Kala. It uses a map of
// strings to strings.
// Example:
// c := New("http://127.0.0.1:8000")
// body := map[string]string{
// "schedule": "R2/2015-06-04T19:25:16.828696-07:00/PT10S",
// "name": "test_job",
// "command": "bash -c 'date'",
// }
// id, err := c.CreateJob(body)
func (kc *KalaClient) CreateJob(body map[string]string) (string, error) {
id := &AddJobResponse{}
resp, err := kc.requester.SetBody(body).SetResult(id).Post(JobPath)
if err != nil {
return "", err
}
if resp.StatusCode() != http.StatusCreated {
return "", JobCreationError
}
return id.Id, nil
}
// GetJob is used to retrieve a Job from Kala by its ID.
// Example:
// c := New("http://127.0.0.1:8000")
// id := "93b65499-b211-49ce-57e0-19e735cc5abd"
// job, err := c.GetJob(id)
func (kc *KalaClient) GetJob(id string) (*Job, error) {
j := &JobResponse{}
resp, err := kc.requester.SetResult(j).Get(JobPath + id)
if err != nil {
return nil, err
}
if resp.StatusCode() != http.StatusOK {
return nil, JobNotFound
}
return j.Job, nil
}
// GetAllJobs returns a map of string (ID's) to job.Job's which contains
// all Jobs currently within Kala.
// Example:
// c := New("http://127.0.0.1:8000")
// jobs, err := c.GetAllJobs()
func (kc *KalaClient) GetAllJobs() (map[string]*Job, error) {
jobs := &ListJobsResponse{}
resp, err := kc.requester.SetResult(jobs).Get(JobPath)
if err != nil {
return nil, err
}
if resp.StatusCode() != http.StatusOK {
return nil, GenericError
}
return jobs.Jobs, nil
}
// DeleteJob is used to delete a Job from Kala by its ID.
// Example:
// c := New("http://127.0.0.1:8000")
// id := "93b65499-b211-49ce-57e0-19e735cc5abd"
// ok, err := c.DeleteJob(id)
func (kc *KalaClient) DeleteJob(id string) (bool, error) {
// nil is completely safe to use, as it is simply ignored in the sling library.
resp, err := kc.requester.Delete(JobPath + id)
if err != nil {
return false, err
}
if resp.StatusCode() != http.StatusNoContent {
return false, fmt.Errorf("Delete failed with a status code of %d", resp.StatusCode)
}
return true, nil
}
// GetJobStats is used to retrieve stats about a Job from Kala by its ID.
// Example:
// c := New("http://127.0.0.1:8000")
// id := "93b65499-b211-49ce-57e0-19e735cc5abd"
// stats, err := c.GetJobStats(id)
func (kc *KalaClient) GetJobStats(id string) ([]*JobStat, error) {
js := &ListJobStatsResponse{}
resp, err := kc.requester.SetResult(js).Get(JobPath + "stats/" + id)
if err != nil {
return nil, err
}
if resp.StatusCode() != http.StatusOK {
return nil, GenericError
}
return js.JobStats, nil
}
// StartJob is used to manually start a Job by its ID.
// Example:
// c := New("http://127.0.0.1:8000")
// id := "93b65499-b211-49ce-57e0-19e735cc5abd"
// ok, err := c.StartJob(id)
func (kc *KalaClient) StartJob(id string) (bool, error) {
resp, err := kc.requester.Post(JobPath + "start/" + id)
if err != nil {
return false, err
}
if resp.StatusCode() != http.StatusNoContent {
return false, nil
}
return true, nil
}
// GetKalaStats retrieves system-level metrics about Kala
// Example:
// c := New("http://127.0.0.1:8000")
// stats, err := c.GetKalaStats()
func (kc *KalaClient) GetKalaStats() (*KalaStats, error) {
ks := &KalaStatsResponse{}
resp, err := kc.requester.SetResult(ks).Get("stats")
if err != nil {
return nil, err
}
if resp.StatusCode() != http.StatusOK {
return nil, GenericError
}
return ks.Stats, nil
}