forked from cvbarros/go-teamcity
/
agent_pool.go
164 lines (134 loc) · 4.71 KB
/
agent_pool.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
package teamcity
import (
"fmt"
"net/http"
"github.com/dghubble/sling"
)
// ListAgentPools is the response object when listing Agent Pools
type ListAgentPools struct {
Count int `json:"count,omitempty" xml:"count"`
Href string `json:"href,omitempty" xml:"href"`
AgentPools []AgentPoolReference `json:"agentPool,omitempty" xml:"agentPool"`
}
// AgentPoolReference is a reference to an Agent Pool
type AgentPoolReference struct {
Href string `json:"href,omitempty" xml:"href"`
Id int `json:"id,omitempty" xml:"id"`
Name string `json:"name,omitempty" xml:"name"`
}
// AgentPool contains information about the Agent Pool
type AgentPool struct {
Href string `json:"href,omitempty" xml:"href"`
Id int `json:"id,omitempty" xml:"id"`
Name string `json:"name,omitempty" xml:"name"`
MaxAgents *int `json:"maxAgents,omitempty" xml:"maxAgents"`
Projects *AgentPoolProjectAssignments `json:"projects,omitempty" xml:"projects"`
}
// CreateAgentPool contains information needed to create an Agent Pool
type CreateAgentPool struct {
Name string `json:"name,omitempty" xml:"name"`
MaxAgents *int `json:"maxAgents,omitempty" xml:"maxAgents"`
}
// AgentPoolProjectAssignments is a wrapper containing the Projects attached to this Agent Pool
type AgentPoolProjectAssignments struct {
Project []ProjectReference `json:"project,omitempty" xml:"project"`
}
// AgentPoolsService has operations for handling agent pools
type AgentPoolsService struct {
sling *sling.Sling
httpClient *http.Client
restHelper *restHelper
}
func newAgentPoolsService(base *sling.Sling, client *http.Client) *AgentPoolsService {
sling := base.Path("agentPools/")
return &AgentPoolsService{
sling: sling,
httpClient: client,
restHelper: newRestHelperWithSling(client, sling),
}
}
// AssignProject assigns a Project to a Agent Pool
func (s *AgentPoolsService) AssignProject(poolId int, projectId string) error {
var project struct {
ID string `json:"id" xml:"id"`
}
project.ID = projectId
var out Project
locator := LocatorIDInt(poolId).String()
err := s.restHelper.post(fmt.Sprintf("%s/projects", locator), project, &out, "Agent Pool")
if err != nil {
return err
}
return nil
}
// Create will create an Agent Pool - which must have a unique name
func (s *AgentPoolsService) Create(pool CreateAgentPool) (*AgentPool, error) {
var created AgentPool
err := s.restHelper.post("", pool, &created, "Agent Pool")
if err != nil {
return nil, err
}
return &created, nil
}
// Delete will delete an Agent Pool based on it's ID
func (s *AgentPoolsService) Delete(id int) error {
locator := LocatorIDInt(id).String()
err := s.restHelper.delete(locator, "Agent Pool")
if err != nil {
return err
}
return nil
}
// Get will return an Agent Pool based on it's ID
func (s *AgentPoolsService) GetByID(id int) (*AgentPool, error) {
var out AgentPool
locator := LocatorIDInt(id).String()
err := s.restHelper.get(locator, &out, "Agent Pool")
if err != nil {
return nil, err
}
return &out, nil
}
// Get will return an Agent Pool based on it's Name
func (s *AgentPoolsService) GetByName(name string) (*AgentPool, error) {
var out AgentPool
locator := LocatorName(name).String()
err := s.restHelper.get(locator, &out, "Agent Pool")
if err != nil {
return nil, err
}
return &out, nil
}
// List returns all of the available Agent Pools
func (s *AgentPoolsService) List() (*ListAgentPools, error) {
var out ListAgentPools
err := s.restHelper.get("", &out, "Agent Pools")
if err != nil {
return nil, err
}
return &out, nil
}
// List returns all of the assigned Agent Pools for a specific Project
func (s *AgentPoolsService) ListForProject(projectId string) (*ListAgentPools, error) {
var out ListAgentPools
locator := LocatorID(projectId) // /app/rest/agentPools/?locator=project:(id:_Root)
err := s.restHelper.get(fmt.Sprintf("?locator=project:(%s)", locator), &out, "Agent Pools")
if err != nil {
return nil, err
}
return &out, nil
}
// UnassignProject unassigns a Project from a Agent Pool
func (s *AgentPoolsService) UnassignProject(poolId int, projectId string) error {
poolLocator := LocatorIDInt(poolId).String()
projectLocator := LocatorID(projectId).String()
uri := fmt.Sprintf("%s/projects/%s", poolLocator, projectLocator)
err := s.restHelper.delete(uri, "Agent Pool")
if err != nil {
return err
}
return nil
}
// NOTE: Update support was investigated but is intentionally omitted - as the TC Documentation is incorrect
// PUT /app/rest/agentPools/id:4 at the time of writing returns a 405 Method Not Allowed
// POST /app/rest/agentPools with href & ID set also creates a new node pool