-
Notifications
You must be signed in to change notification settings - Fork 232
/
runner.go
178 lines (151 loc) · 4.41 KB
/
runner.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
package runner
import (
"fmt"
"net/url"
"strings"
"time"
"github.com/CircleCI-Public/circleci-cli/api/rest"
)
type Runner struct {
rc *rest.Client
}
func New(rc *rest.Client) *Runner {
return &Runner{rc: rc}
}
type ResourceClass struct {
ID string `json:"id"`
ResourceClass string `json:"resource_class"`
Description string `json:"description"`
}
func (r *Runner) CreateResourceClass(resourceClass, desc string) (rc *ResourceClass, err error) {
req, err := r.rc.NewRequest("POST", &url.URL{Path: "runner/resource"}, struct {
ResourceClass string `json:"resource_class"`
Description string `json:"description"`
}{
ResourceClass: resourceClass,
Description: desc,
})
if err != nil {
return nil, err
}
rc = &ResourceClass{}
_, err = r.rc.DoRequest(req, rc)
return rc, err
}
func (r *Runner) GetResourceClassByName(resourceClass string) (rc *ResourceClass, err error) {
namespace, err := r.GetNamespaceByResourceClass(resourceClass)
if err != nil {
return nil, err
}
rcs, err := r.GetResourceClassesByNamespace(namespace)
if err != nil {
return nil, err
}
for _, rc := range rcs {
if rc.ResourceClass == resourceClass {
return &rc, nil
}
}
return nil, fmt.Errorf("resource class %q not found", resourceClass)
}
func (r *Runner) GetNamespaceByResourceClass(resourceClass string) (ns string, err error) {
s := strings.SplitN(resourceClass, "/", 2)
if len(s) != 2 {
return "", fmt.Errorf("bad resource class: %q", resourceClass)
}
return s[0], nil
}
func (r *Runner) GetResourceClassesByNamespace(namespace string) ([]ResourceClass, error) {
query := url.Values{}
query.Set("namespace", namespace)
req, err := r.rc.NewRequest("GET", &url.URL{Path: "runner/resource", RawQuery: query.Encode()}, nil)
if err != nil {
return nil, err
}
resp := struct {
Items []ResourceClass `json:"items"`
}{}
_, err = r.rc.DoRequest(req, &resp)
return resp.Items, err
}
func (r *Runner) DeleteResourceClass(id string) error {
req, err := r.rc.NewRequest("DELETE", &url.URL{Path: "runner/resource/" + url.PathEscape(id)}, nil)
if err != nil {
return err
}
_, err = r.rc.DoRequest(req, nil)
return err
}
type Token struct {
ID string `json:"id"`
Token string `json:"token"`
ResourceClass string `json:"resource_class"`
Nickname string `json:"nickname"`
CreatedAt time.Time `json:"created_at"`
}
func (r *Runner) CreateToken(resourceClass, nickname string) (token *Token, err error) {
t := struct {
ResourceClass string `json:"resource_class"`
Nickname string `json:"nickname"`
}{
ResourceClass: resourceClass,
Nickname: nickname,
}
req, err := r.rc.NewRequest("POST", &url.URL{Path: "runner/token"}, t)
if err != nil {
return nil, err
}
token = &Token{}
_, err = r.rc.DoRequest(req, token)
return token, err
}
func (r *Runner) GetRunnerTokensByResourceClass(resourceClass string) ([]Token, error) {
query := url.Values{}
query.Set("resource-class", resourceClass)
req, err := r.rc.NewRequest("GET", &url.URL{Path: "runner/token", RawQuery: query.Encode()}, nil)
if err != nil {
return nil, err
}
resp := struct {
Items []Token `json:"items"`
}{}
_, err = r.rc.DoRequest(req, &resp)
return resp.Items, err
}
func (r *Runner) DeleteToken(id string) error {
req, err := r.rc.NewRequest("DELETE", &url.URL{Path: "runner/token/" + url.PathEscape(id)}, nil)
if err != nil {
return err
}
_, err = r.rc.DoRequest(req, nil)
return err
}
type RunnerInstance struct {
ResourceClass string `json:"resource_class,omitempty"`
Hostname string `json:"hostname"`
Name string `json:"name"`
FirstConnected *time.Time `json:"first_connected"`
LastConnected *time.Time `json:"last_connected"`
LastUsed *time.Time `json:"last_used"`
IP string `json:"ip,omitempty"`
Version string `json:"version"`
}
func runnerQueryFromString(query string) url.Values {
switch {
default:
return url.Values{"namespace": []string{query}}
case strings.Contains(query, "/"):
return url.Values{"resource-class": []string{query}}
}
}
func (r *Runner) GetRunnerInstances(query string) ([]RunnerInstance, error) {
req, err := r.rc.NewRequest("GET", &url.URL{Path: "runner", RawQuery: runnerQueryFromString(query).Encode()}, nil)
if err != nil {
return nil, err
}
resp := struct {
Items []RunnerInstance `json:"items"`
}{}
_, err = r.rc.DoRequest(req, &resp)
return resp.Items, err
}