From 0568f2963a757059da1d0aa866a22262da21857f Mon Sep 17 00:00:00 2001 From: Netra Mali <42544158+Netra2104@users.noreply.github.com> Date: Tue, 12 Jul 2022 11:21:55 -0400 Subject: [PATCH] Add API Changes for Agent Pools to Workspaces Feature (#453) * added tests for agent pools * pointer fix * test fix * more fixes * added omit attribute * removed pointer from AgentPool * added change logs * unnecessary change removed * ran go fmt --- CHANGELOG.md | 1 + agent_pool.go | 16 ++++++++++++---- agent_pool_integration_test.go | 35 ++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a31fdb74..29884dbd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Unreleased * [beta] Add support for triggering Workspace runs through matching Git tags [#434](https://github.com/hashicorp/go-tfe/pull/434) +* Add organization scope and allowed workspaces field for scope agents by @Netra2104 [#453](https://github.com/hashicorp/go-tfe/pull/453) ## Bug fixes * Fixed JSON mapping for Configuration Versions failing to properly set the `speculative` property [#459](https://github.com/hashicorp/go-tfe/pull/459) diff --git a/agent_pool.go b/agent_pool.go index a5db842a6..8905cc4b7 100644 --- a/agent_pool.go +++ b/agent_pool.go @@ -46,12 +46,14 @@ type AgentPoolList struct { // AgentPool represents a Terraform Cloud agent pool. type AgentPool struct { - ID string `jsonapi:"primary,agent-pools"` - Name string `jsonapi:"attr,name"` + ID string `jsonapi:"primary,agent-pools"` + Name string `jsonapi:"attr,name"` + OrganizationScoped bool `jsonapi:"attr,organization-scoped"` // Relations - Organization *Organization `jsonapi:"relation,organization"` - Workspaces []*Workspace `jsonapi:"relation,workspaces"` + Organization *Organization `jsonapi:"relation,organization"` + Workspaces []*Workspace `jsonapi:"relation,workspaces"` + AllowedWorkspaces []*Workspace `jsonapi:"relation,allowed-workspaces"` } // A list of relations to include @@ -172,6 +174,12 @@ type AgentPoolUpdateOptions struct { // A new name to identify the agent pool. Name *string `jsonapi:"attr,name"` + + // True if the agent pool is organization scoped, false otherwise. + OrganizationScoped *bool `jsonapi:"attr,organization-scoped,omitempty"` + + // A new list of workspaces that are associated with an agent pool. + AllowedWorkspaces []*Workspace `jsonapi:"relation,allowed-workspaces"` } // Update an agent pool by its ID. diff --git a/agent_pool_integration_test.go b/agent_pool_integration_test.go index a054ff4f9..ee2ddf5d2 100644 --- a/agent_pool_integration_test.go +++ b/agent_pool_integration_test.go @@ -200,6 +200,41 @@ func TestAgentPoolsUpdate(t *testing.T) { assert.Nil(t, w) assert.EqualError(t, err, ErrInvalidAgentPoolID.Error()) }) + + t.Run("when updating allowed-workspaces", func(t *testing.T) { + kBefore, kTestCleanup := createAgentPool(t, client, orgTest) + defer kTestCleanup() + + workspaceTest, workspaceTestCleanup := createWorkspace(t, client, orgTest) + defer workspaceTestCleanup() + + kAfter, err := client.AgentPools.Update(ctx, kBefore.ID, AgentPoolUpdateOptions{ + Name: String(kBefore.Name), + AllowedWorkspaces: []*Workspace{ + workspaceTest, + }, + }) + require.NoError(t, err) + + assert.NotEqual(t, kBefore.AllowedWorkspaces, kAfter.AllowedWorkspaces) + assert.Equal(t, 1, len(kAfter.AllowedWorkspaces)) + assert.Equal(t, workspaceTest.ID, kAfter.AllowedWorkspaces[0].ID) + }) + + t.Run("when updating organization scope", func(t *testing.T) { + kBefore, kTestCleanup := createAgentPool(t, client, orgTest) + defer kTestCleanup() + + organizationScoped := false + kAfter, err := client.AgentPools.Update(ctx, kBefore.ID, AgentPoolUpdateOptions{ + Name: String(kBefore.Name), + OrganizationScoped: &organizationScoped, + }) + require.NoError(t, err) + + assert.NotEqual(t, kBefore.OrganizationScoped, kAfter.OrganizationScoped) + assert.Equal(t, organizationScoped, kAfter.OrganizationScoped) + }) } func TestAgentPoolsDelete(t *testing.T) {