forked from buildkite/go-buildkite
/
agents.go
131 lines (103 loc) · 3.37 KB
/
agents.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
// Copyright 2014 Mark Wolfe. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package buildkite
import "fmt"
// AgentsService handles communication with the agent related
// methods of the buildkite API.
//
// buildkite API docs: https://buildkite.com/docs/api/agents
type AgentsService struct {
client *Client
}
// Agent represents a buildkite build agent.
type Agent struct {
ID *string `json:"id,omitempty"`
URL *string `json:"url,omitempty"`
WebURL *string `json:"web_url,omitempty"`
Name *string `json:"name,omitempty"`
ConnectedState *string `json:"connection_state,omitempty"`
AgentToken *string `json:"access_token,omitempty"`
Hostname *string `json:"hostname,omitempty"`
IPAddress *string `json:"ip_address,omitempty"`
UserAgent *string `json:"user_agent,omitempty"`
Version *string `json:"version,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
LastJobFinishedAt *Timestamp `json:"last_job_finished_at,omitempty"`
Priority *int `json:"priority,omitempty"`
Metadata []string `json:"meta_data,omitempty"`
// the user that created the agent
Creator *User `json:"creator,omitempty"`
Job *Job `json:"job,omitempty"`
}
// AgentListOptions specifies the optional parameters to the
// AgentService.List method.
type AgentListOptions struct {
ListOptions
}
// List the agents for a given orginisation.
//
// buildkite API docs: https://buildkite.com/docs/api/agents#list-agents
func (as *AgentsService) List(org string, opt *AgentListOptions) ([]Agent, *Response, error) {
var u string
u = fmt.Sprintf("v2/organizations/%s/agents", org)
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}
req, err := as.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
agents := new([]Agent)
resp, err := as.client.Do(req, agents)
if err != nil {
return nil, resp, err
}
return *agents, resp, err
}
// Get fetches an agent.
//
// buildkite API docs: https://buildkite.com/docs/api/agents#get-an-agent
func (as *AgentsService) Get(org string, id string) (*Agent, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/agents/%s", org, id)
req, err := as.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
agent := new(Agent)
resp, err := as.client.Do(req, agent)
if err != nil {
return nil, resp, err
}
return agent, resp, err
}
// Create a new buildkite agent.
//
// buildkite API docs: https://buildkite.com/docs/api/agents#create-an-agent
func (as *AgentsService) Create(org string, agent *Agent) (*Agent, *Response, error) {
var u string
u = fmt.Sprintf("v2/organizations/%s/agents", org)
req, err := as.client.NewRequest("POST", u, agent)
if err != nil {
return nil, nil, err
}
a := new(Agent)
resp, err := as.client.Do(req, a)
if err != nil {
return nil, resp, err
}
return a, resp, err
}
// Delete an agent.
//
// buildkite API docs: https://buildkite.com/docs/api/agents#delete-an-agent
func (as *AgentsService) Delete(org string, id string) (*Response, error) {
u := fmt.Sprintf("v2/organizations/%s/agents/%s", org, id)
req, err := as.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return as.client.Do(req, nil)
}