1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package gitlab
18
19 import (
20 "fmt"
21 "net/http"
22 "time"
23 )
24
25
26
27
28
29 type ClusterAgentsService struct {
30 client *Client
31 }
32
33
34
35
36 type Agent struct {
37 ID int `json:"id"`
38 Name string `json:"name"`
39 CreatedAt *time.Time `json:"created_at"`
40 CreatedByUserID int `json:"created_by_user_id"`
41 ConfigProject ConfigProject `json:"config_project"`
42 }
43
44 type ConfigProject struct {
45 ID int `json:"id"`
46 Description string `json:"description"`
47 Name string `json:"name"`
48 NameWithNamespace string `json:"name_with_namespace"`
49 Path string `json:"path"`
50 PathWithNamespace string `json:"path_with_namespace"`
51 CreatedAt *time.Time `json:"created_at"`
52 }
53
54 func (a Agent) String() string {
55 return Stringify(a)
56 }
57
58
59
60
61
62 type AgentToken struct {
63 ID int `json:"id"`
64 Name string `json:"name"`
65 Description string `json:"description"`
66 AgentID int `json:"agent_id"`
67 Status string `json:"status"`
68 CreatedAt *time.Time `json:"created_at"`
69 CreatedByUserID int `json:"created_by_user_id"`
70 LastUsedAt *time.Time `json:"last_used_at"`
71 Token string `json:"token"`
72 }
73
74 func (a AgentToken) String() string {
75 return Stringify(a)
76 }
77
78
79
80
81
82 type ListAgentsOptions ListOptions
83
84
85
86
87
88 func (s *ClusterAgentsService) ListAgents(pid interface{}, opt *ListAgentsOptions, options ...RequestOptionFunc) ([]*Agent, *Response, error) {
89 project, err := parseID(pid)
90 if err != nil {
91 return nil, nil, err
92 }
93 uri := fmt.Sprintf("projects/%s/cluster_agents", PathEscape(project))
94
95 req, err := s.client.NewRequest(http.MethodGet, uri, opt, options)
96 if err != nil {
97 return nil, nil, err
98 }
99
100 var as []*Agent
101 resp, err := s.client.Do(req, &as)
102 if err != nil {
103 return nil, resp, err
104 }
105
106 return as, resp, nil
107 }
108
109
110
111
112
113 func (s *ClusterAgentsService) GetAgent(pid interface{}, id int, options ...RequestOptionFunc) (*Agent, *Response, error) {
114 project, err := parseID(pid)
115 if err != nil {
116 return nil, nil, err
117 }
118 uri := fmt.Sprintf("projects/%s/cluster_agents/%d", PathEscape(project), id)
119
120 req, err := s.client.NewRequest(http.MethodGet, uri, nil, options)
121 if err != nil {
122 return nil, nil, err
123 }
124
125 a := new(Agent)
126 resp, err := s.client.Do(req, a)
127 if err != nil {
128 return nil, resp, err
129 }
130
131 return a, resp, nil
132 }
133
134
135
136
137
138
139 type RegisterAgentOptions struct {
140 Name *string `url:"name,omitempty" json:"name,omitempty"`
141 }
142
143
144
145
146
147 func (s *ClusterAgentsService) RegisterAgent(pid interface{}, opt *RegisterAgentOptions, options ...RequestOptionFunc) (*Agent, *Response, error) {
148 project, err := parseID(pid)
149 if err != nil {
150 return nil, nil, err
151 }
152 uri := fmt.Sprintf("projects/%s/cluster_agents", PathEscape(project))
153
154 req, err := s.client.NewRequest(http.MethodPost, uri, opt, options)
155 if err != nil {
156 return nil, nil, err
157 }
158
159 a := new(Agent)
160 resp, err := s.client.Do(req, a)
161 if err != nil {
162 return nil, resp, err
163 }
164
165 return a, resp, nil
166 }
167
168
169
170
171
172 func (s *ClusterAgentsService) DeleteAgent(pid interface{}, id int, options ...RequestOptionFunc) (*Response, error) {
173 project, err := parseID(pid)
174 if err != nil {
175 return nil, err
176 }
177 uri := fmt.Sprintf("projects/%s/cluster_agents/%d", PathEscape(project), id)
178
179 req, err := s.client.NewRequest(http.MethodDelete, uri, nil, options)
180 if err != nil {
181 return nil, err
182 }
183
184 return s.client.Do(req, nil)
185 }
186
187
188
189
190
191 type ListAgentTokensOptions ListOptions
192
193
194
195
196
197 func (s *ClusterAgentsService) ListAgentTokens(pid interface{}, aid int, opt *ListAgentTokensOptions, options ...RequestOptionFunc) ([]*AgentToken, *Response, error) {
198 project, err := parseID(pid)
199 if err != nil {
200 return nil, nil, err
201 }
202 uri := fmt.Sprintf("projects/%s/cluster_agents/%d/tokens", PathEscape(project), aid)
203
204 req, err := s.client.NewRequest(http.MethodGet, uri, opt, options)
205 if err != nil {
206 return nil, nil, err
207 }
208
209 var ats []*AgentToken
210 resp, err := s.client.Do(req, &ats)
211 if err != nil {
212 return nil, resp, err
213 }
214
215 return ats, resp, nil
216 }
217
218
219
220
221
222 func (s *ClusterAgentsService) GetAgentToken(pid interface{}, aid int, id int, options ...RequestOptionFunc) (*AgentToken, *Response, error) {
223 project, err := parseID(pid)
224 if err != nil {
225 return nil, nil, err
226 }
227 uri := fmt.Sprintf("projects/%s/cluster_agents/%d/tokens/%d", PathEscape(project), aid, id)
228
229 req, err := s.client.NewRequest(http.MethodGet, uri, nil, options)
230 if err != nil {
231 return nil, nil, err
232 }
233
234 at := new(AgentToken)
235 resp, err := s.client.Do(req, at)
236 if err != nil {
237 return nil, resp, err
238 }
239
240 return at, resp, nil
241 }
242
243
244
245
246
247 type CreateAgentTokenOptions struct {
248 Name *string `url:"name,omitempty" json:"name,omitempty"`
249 Description *string `url:"description,omitempty" json:"description,omitempty"`
250 }
251
252
253
254
255
256 func (s *ClusterAgentsService) CreateAgentToken(pid interface{}, aid int, opt *CreateAgentTokenOptions, options ...RequestOptionFunc) (*AgentToken, *Response, error) {
257 project, err := parseID(pid)
258 if err != nil {
259 return nil, nil, err
260 }
261 uri := fmt.Sprintf("projects/%s/cluster_agents/%d/tokens", PathEscape(project), aid)
262
263 req, err := s.client.NewRequest(http.MethodPost, uri, opt, options)
264 if err != nil {
265 return nil, nil, err
266 }
267
268 at := new(AgentToken)
269 resp, err := s.client.Do(req, at)
270 if err != nil {
271 return nil, resp, err
272 }
273
274 return at, resp, nil
275 }
276
277
278
279
280
281 func (s *ClusterAgentsService) RevokeAgentToken(pid interface{}, aid int, id int, options ...RequestOptionFunc) (*Response, error) {
282 project, err := parseID(pid)
283 if err != nil {
284 return nil, err
285 }
286 uri := fmt.Sprintf("projects/%s/cluster_agents/%d/tokens/%d", PathEscape(project), aid, id)
287
288 req, err := s.client.NewRequest(http.MethodDelete, uri, nil, options)
289 if err != nil {
290 return nil, err
291 }
292
293 return s.client.Do(req, nil)
294 }
295
View as plain text