Skip to content

Commit 6e778a4

Browse files
tylerwolfctrombley
andauthored
Add default agent pool and execution mode support to projects (#1185)
* Add ExcludedWorkspaces and AllowedProjects to AgentPools * Add DefaultExecutionMode, DefaultAgentPool, and SettingOverwrites to Project * Update CHANGELOG * Address lint failures * Update mocks * Private receiver to reduce duplication * Prefer t.Cleanup over defer * Use require.Equal() or require.NotNil() to prevent downstream panics when accessing the slice by index * Remove unnecessary org subscription upgrades * Added comment to updateArrayAttribute --------- Co-authored-by: Chris Trombley <[email protected]>
1 parent 7b6d762 commit 6e778a4

File tree

6 files changed

+737
-87
lines changed

6 files changed

+737
-87
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* Adds `CanceledAt`, `RunEvents`, `TriggerReason` field to `Run` by @jpadrianoGo [#1161](https://github.com/hashicorp/go-tfe/pull/1161)
99
* Adds `CreatedAt` field to `AgentPool` by @jpadrianoGo [#1150](https://github.com/hashicorp/go-tfe/pull/1150)
1010
* Adds `CreatedBy` relation to `AgentToken` by @jpadrianoGo [#1149](https://github.com/hashicorp/go-tfe/pull/1149)
11+
* Adds `AllowedProjects` and `ExcludedWorkspaces` to `AgentPool` by @tylerwolf [#1185](https://github.com/hashicorp/go-tfe/pull/1185)
12+
* Adds `DefaultExecutionMode`, `DefaultAgentPool`, and `SettingOverwrites` to `Project` by @tylerwolf [#1185](https://github.com/hashicorp/go-tfe/pull/1185)
1113

1214
# v1.90.0
1315

agent_pool.go

Lines changed: 84 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ type AgentPools interface {
3636
// UpdateAllowedWorkspaces updates the list of allowed workspaces associated with an agent pool.
3737
UpdateAllowedWorkspaces(ctx context.Context, agentPool string, options AgentPoolAllowedWorkspacesUpdateOptions) (*AgentPool, error)
3838

39+
// UpdateAllowedProjects updates the list of allowed projects associated with an agent pool.
40+
UpdateAllowedProjects(ctx context.Context, agentPool string, options AgentPoolAllowedProjectsUpdateOptions) (*AgentPool, error)
41+
42+
// UpdateExcludedWorkspaces updates the list of excluded workspaces associated with an agent pool.
43+
UpdateExcludedWorkspaces(ctx context.Context, agentPool string, options AgentPoolExcludedWorkspacesUpdateOptions) (*AgentPool, error)
44+
3945
// Delete an agent pool by its ID.
4046
Delete(ctx context.Context, agentPoolID string) error
4147
}
@@ -60,9 +66,11 @@ type AgentPool struct {
6066
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
6167

6268
// Relations
63-
Organization *Organization `jsonapi:"relation,organization"`
64-
Workspaces []*Workspace `jsonapi:"relation,workspaces"`
65-
AllowedWorkspaces []*Workspace `jsonapi:"relation,allowed-workspaces"`
69+
Organization *Organization `jsonapi:"relation,organization"`
70+
Workspaces []*Workspace `jsonapi:"relation,workspaces"`
71+
AllowedWorkspaces []*Workspace `jsonapi:"relation,allowed-workspaces"`
72+
AllowedProjects []*Project `jsonapi:"relation,allowed-projects"`
73+
ExcludedWorkspaces []*Workspace `jsonapi:"relation,excluded-workspaces"`
6674
}
6775

6876
// A list of relations to include
@@ -87,6 +95,9 @@ type AgentPoolListOptions struct {
8795

8896
// Optional: String (workspace name) used to filter the results.
8997
AllowedWorkspacesName string `url:"filter[allowed_workspaces][name],omitempty"`
98+
99+
// Optional: String (project name) used to filter the results.
100+
AllowedProjectsName string `url:"filter[allowed_projects][name],omitempty"`
90101
}
91102

92103
// AgentPoolCreateOptions represents the options for creating an agent pool.
@@ -105,6 +116,12 @@ type AgentPoolCreateOptions struct {
105116

106117
// List of workspaces that are associated with an agent pool.
107118
AllowedWorkspaces []*Workspace `jsonapi:"relation,allowed-workspaces,omitempty"`
119+
120+
// List of projects that are associated with an agent pool.
121+
AllowedProjects []*Project `jsonapi:"relation,allowed-projects,omitempty"`
122+
123+
// List of workspaces that are excluded from the scope of an agent pool.
124+
ExcludedWorkspaces []*Workspace `jsonapi:"relation,excluded-workspaces,omitempty"`
108125
}
109126

110127
// List all the agent pools of the given organization.
@@ -201,9 +218,15 @@ type AgentPoolUpdateOptions struct {
201218

202219
// A new list of workspaces that are associated with an agent pool.
203220
AllowedWorkspaces []*Workspace `jsonapi:"relation,allowed-workspaces,omitempty"`
221+
222+
// A new list of projects that are associated with an agent pool.
223+
AllowedProjects []*Project `jsonapi:"relation,allowed-projects,omitempty"`
224+
225+
// A new list of workspaces that are excluded from the scope of an agent pool.
226+
ExcludedWorkspaces []*Workspace `jsonapi:"relation,excluded-workspaces,omitempty"`
204227
}
205228

206-
// AgentPoolUpdateAllowedWorkspacesOptions represents the options for updating the allowed workspace on an agent pool
229+
// AgentPoolAllowedWorkspacesUpdateOptions represents the options for updating the allowed workspace on an agent pool
207230
type AgentPoolAllowedWorkspacesUpdateOptions struct {
208231
// Type is a public field utilized by JSON:API to
209232
// set the resource type via the field tag.
@@ -215,8 +238,33 @@ type AgentPoolAllowedWorkspacesUpdateOptions struct {
215238
AllowedWorkspaces []*Workspace `jsonapi:"relation,allowed-workspaces"`
216239
}
217240

241+
// AgentPoolAllowedProjectsUpdateOptions represents the options for updating the allowed projects on an agent pool
242+
type AgentPoolAllowedProjectsUpdateOptions struct {
243+
// Type is a public field utilized by JSON:API to
244+
// set the resource type via the field tag.
245+
// It is not a user-defined value and does not need to be set.
246+
// https://jsonapi.org/format/#crud-creating
247+
Type string `jsonapi:"primary,agent-pools"`
248+
249+
// A new list of projects that are associated with an agent pool.
250+
AllowedProjects []*Project `jsonapi:"relation,allowed-projects"`
251+
}
252+
253+
// AgentPoolExcludedWorkspacesUpdateOptions represents the options for updating the excluded workspace on an agent pool
254+
type AgentPoolExcludedWorkspacesUpdateOptions struct {
255+
// Type is a public field utilized by JSON:API to
256+
// set the resource type via the field tag.
257+
// It is not a user-defined value and does not need to be set.
258+
// https://jsonapi.org/format/#crud-creating
259+
Type string `jsonapi:"primary,agent-pools"`
260+
261+
// A new list of workspaces that are excluded from the scope of an agent pool.
262+
ExcludedWorkspaces []*Workspace `jsonapi:"relation,excluded-workspaces"`
263+
}
264+
218265
// Update an agent pool by its ID.
219-
// **Note:** This method cannot be used to clear the allowed workspaces field, instead use UpdateAllowedWorkspaces
266+
// **Note:** This method cannot be used to clear the allowed workspaces, allowed projects, or excluded workspaces fields.
267+
// instead use UpdateAllowedWorkspaces, UpdateAllowedProjects, or UpdateExcludedWorkspaces methods respectively.
220268
func (s *agentPools) Update(ctx context.Context, agentPoolID string, options AgentPoolUpdateOptions) (*AgentPool, error) {
221269
if !validStringID(&agentPoolID) {
222270
return nil, ErrInvalidAgentPoolID
@@ -242,23 +290,15 @@ func (s *agentPools) Update(ctx context.Context, agentPoolID string, options Age
242290
}
243291

244292
func (s *agentPools) UpdateAllowedWorkspaces(ctx context.Context, agentPoolID string, options AgentPoolAllowedWorkspacesUpdateOptions) (*AgentPool, error) {
245-
if !validStringID(&agentPoolID) {
246-
return nil, ErrInvalidAgentPoolID
247-
}
248-
249-
u := fmt.Sprintf("agent-pools/%s", url.PathEscape(agentPoolID))
250-
req, err := s.client.NewRequest("PATCH", u, &options)
251-
if err != nil {
252-
return nil, err
253-
}
293+
return s.updateArrayAttribute(ctx, agentPoolID, &options)
294+
}
254295

255-
k := &AgentPool{}
256-
err = req.Do(ctx, k)
257-
if err != nil {
258-
return nil, err
259-
}
296+
func (s *agentPools) UpdateAllowedProjects(ctx context.Context, agentPoolID string, options AgentPoolAllowedProjectsUpdateOptions) (*AgentPool, error) {
297+
return s.updateArrayAttribute(ctx, agentPoolID, &options)
298+
}
260299

261-
return k, nil
300+
func (s *agentPools) UpdateExcludedWorkspaces(ctx context.Context, agentPoolID string, options AgentPoolExcludedWorkspacesUpdateOptions) (*AgentPool, error) {
301+
return s.updateArrayAttribute(ctx, agentPoolID, &options)
262302
}
263303

264304
// Delete an agent pool by its ID.
@@ -276,6 +316,30 @@ func (s *agentPools) Delete(ctx context.Context, agentPoolID string) error {
276316
return req.Do(ctx, nil)
277317
}
278318

319+
// updateArrayAttribute is a helper function to update array attributes of an agent pool, such as allowed workspaces, allowed projects, or excluded workspaces.
320+
// Note: This function does not validate the options parameter, so it should be used with caution. It is intended to be used with options structs
321+
// (e.g. AgentPoolAllowedWorkspacesUpdateOptions, AgentPoolAllowedProjectsUpdateOptions, AgentPoolExcludedWorkspacesUpdateOptions) whose array
322+
// attributes are NOT marked `omitempty`, so that an empty array is sent to the API to clear the existing values.
323+
func (s *agentPools) updateArrayAttribute(ctx context.Context, agentPoolID string, options any) (*AgentPool, error) {
324+
if !validStringID(&agentPoolID) {
325+
return nil, ErrInvalidAgentPoolID
326+
}
327+
328+
u := fmt.Sprintf("agent-pools/%s", url.PathEscape(agentPoolID))
329+
req, err := s.client.NewRequest("PATCH", u, options)
330+
if err != nil {
331+
return nil, err
332+
}
333+
334+
k := &AgentPool{}
335+
err = req.Do(ctx, k)
336+
if err != nil {
337+
return nil, err
338+
}
339+
340+
return k, nil
341+
}
342+
279343
func (o AgentPoolCreateOptions) valid() error {
280344
if !validString(o.Name) {
281345
return ErrRequiredName

0 commit comments

Comments
 (0)