From 40f6822bb84d441ea00a5f0f5bc64321b8435e1f Mon Sep 17 00:00:00 2001 From: Netra2104 Date: Mon, 4 Jul 2022 10:49:40 -0400 Subject: [PATCH 1/9] added tests for agent pools --- agent_pool.go | 16 ++++++++++++---- agent_pool_integration_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/agent_pool.go b/agent_pool.go index a5db842a6..c41c23dc1 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"` + + // 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..1aa6f0918 100644 --- a/agent_pool_integration_test.go +++ b/agent_pool_integration_test.go @@ -200,6 +200,40 @@ 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() + + kAfter, err := client.AgentPools.Update(ctx, kBefore.ID, AgentPoolUpdateOptions{ + Name: String(kBefore.Name), + OrganizationScoped: false, + }) + require.NoError(t, err) + + assert.NotEqual(t, kBefore.OrganizationScoped, kAfter.OrganizationScoped) + assert.Equal(t, false, kAfter.OrganizationScoped) + }) } func TestAgentPoolsDelete(t *testing.T) { From bded44c44ecc10e2233523c6df9ced12b983e2e1 Mon Sep 17 00:00:00 2001 From: Netra2104 Date: Thu, 7 Jul 2022 10:35:21 -0400 Subject: [PATCH 2/9] pointer fix --- agent_pool.go | 4 ++-- agent_pool_integration_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/agent_pool.go b/agent_pool.go index c41c23dc1..60d19a5b3 100644 --- a/agent_pool.go +++ b/agent_pool.go @@ -48,7 +48,7 @@ type AgentPoolList struct { type AgentPool struct { ID string `jsonapi:"primary,agent-pools"` Name string `jsonapi:"attr,name"` - OrganizationScoped bool `jsonapi:"attr,organization-scoped"` + OrganizationScoped *bool `jsonapi:"attr,organization-scoped"` // Relations Organization *Organization `jsonapi:"relation,organization"` @@ -176,7 +176,7 @@ type AgentPoolUpdateOptions struct { Name *string `jsonapi:"attr,name"` // True if the agent pool is organization scoped, false otherwise. - OrganizationScoped bool `jsonapi:"attr,organization-scoped"` + OrganizationScoped *bool `jsonapi:"attr,organization-scoped"` // A new list of workspaces that are associated with an agent pool. AllowedWorkspaces []*Workspace `jsonapi:"relation,allowed-workspaces"` diff --git a/agent_pool_integration_test.go b/agent_pool_integration_test.go index 1aa6f0918..eab121e5a 100644 --- a/agent_pool_integration_test.go +++ b/agent_pool_integration_test.go @@ -227,7 +227,7 @@ func TestAgentPoolsUpdate(t *testing.T) { kAfter, err := client.AgentPools.Update(ctx, kBefore.ID, AgentPoolUpdateOptions{ Name: String(kBefore.Name), - OrganizationScoped: false, + OrganizationScoped: func(b bool) *bool { return &b }(false), }) require.NoError(t, err) From 019949bb436422aa386c8ce80c3590a67c3d64c2 Mon Sep 17 00:00:00 2001 From: Netra2104 Date: Thu, 7 Jul 2022 11:30:23 -0400 Subject: [PATCH 3/9] test fix --- agent_pool_integration_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/agent_pool_integration_test.go b/agent_pool_integration_test.go index eab121e5a..7a528675c 100644 --- a/agent_pool_integration_test.go +++ b/agent_pool_integration_test.go @@ -224,10 +224,10 @@ func TestAgentPoolsUpdate(t *testing.T) { 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: func(b bool) *bool { return &b }(false), + OrganizationScoped: &organizationScoped, }) require.NoError(t, err) From 4456725b24a1137c4d4dbe39206ebe8bf3a022a0 Mon Sep 17 00:00:00 2001 From: Netra2104 Date: Thu, 7 Jul 2022 13:06:39 -0400 Subject: [PATCH 4/9] more fixes --- agent_pool_integration_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/agent_pool_integration_test.go b/agent_pool_integration_test.go index 7a528675c..499f8505f 100644 --- a/agent_pool_integration_test.go +++ b/agent_pool_integration_test.go @@ -163,7 +163,7 @@ func TestAgentPoolsRead(t *testing.T) { func TestAgentPoolsUpdate(t *testing.T) { client := testClient(t) ctx := context.Background() - + organizationScoped := false orgTest, orgTestCleanup := createOrganization(t, client) defer orgTestCleanup() @@ -175,6 +175,7 @@ func TestAgentPoolsUpdate(t *testing.T) { kAfter, err := client.AgentPools.Update(ctx, kBefore.ID, AgentPoolUpdateOptions{ Name: String(randomString(t)), + OrganizationScoped: &organizationScoped, }) require.NoError(t, err) @@ -188,6 +189,7 @@ func TestAgentPoolsUpdate(t *testing.T) { kAfter, err := client.AgentPools.Update(ctx, kBefore.ID, AgentPoolUpdateOptions{ Name: String("updated-key-name"), + OrganizationScoped: &organizationScoped, }) require.NoError(t, err) @@ -210,6 +212,7 @@ func TestAgentPoolsUpdate(t *testing.T) { kAfter, err := client.AgentPools.Update(ctx, kBefore.ID, AgentPoolUpdateOptions{ Name: String(kBefore.Name), + OrganizationScoped: &organizationScoped, AllowedWorkspaces: []*Workspace{ workspaceTest, }, @@ -224,7 +227,7 @@ func TestAgentPoolsUpdate(t *testing.T) { 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, From 1e03a9a49dbe56d9abe4cc9261a88a055a057338 Mon Sep 17 00:00:00 2001 From: Netra2104 Date: Mon, 11 Jul 2022 12:16:46 -0400 Subject: [PATCH 5/9] added omit attribute --- agent_pool.go | 4 ++-- agent_pool_integration_test.go | 7 ++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/agent_pool.go b/agent_pool.go index 60d19a5b3..25e87c8f1 100644 --- a/agent_pool.go +++ b/agent_pool.go @@ -48,7 +48,7 @@ type AgentPoolList struct { type AgentPool struct { ID string `jsonapi:"primary,agent-pools"` Name string `jsonapi:"attr,name"` - OrganizationScoped *bool `jsonapi:"attr,organization-scoped"` + OrganizationScoped *bool `jsonapi:"attr,organization-scoped,omitempty"` // Relations Organization *Organization `jsonapi:"relation,organization"` @@ -176,7 +176,7 @@ type AgentPoolUpdateOptions struct { Name *string `jsonapi:"attr,name"` // True if the agent pool is organization scoped, false otherwise. - OrganizationScoped *bool `jsonapi:"attr,organization-scoped"` + 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"` diff --git a/agent_pool_integration_test.go b/agent_pool_integration_test.go index 499f8505f..2de5fa082 100644 --- a/agent_pool_integration_test.go +++ b/agent_pool_integration_test.go @@ -163,7 +163,6 @@ func TestAgentPoolsRead(t *testing.T) { func TestAgentPoolsUpdate(t *testing.T) { client := testClient(t) ctx := context.Background() - organizationScoped := false orgTest, orgTestCleanup := createOrganization(t, client) defer orgTestCleanup() @@ -175,7 +174,6 @@ func TestAgentPoolsUpdate(t *testing.T) { kAfter, err := client.AgentPools.Update(ctx, kBefore.ID, AgentPoolUpdateOptions{ Name: String(randomString(t)), - OrganizationScoped: &organizationScoped, }) require.NoError(t, err) @@ -189,7 +187,6 @@ func TestAgentPoolsUpdate(t *testing.T) { kAfter, err := client.AgentPools.Update(ctx, kBefore.ID, AgentPoolUpdateOptions{ Name: String("updated-key-name"), - OrganizationScoped: &organizationScoped, }) require.NoError(t, err) @@ -212,7 +209,6 @@ func TestAgentPoolsUpdate(t *testing.T) { kAfter, err := client.AgentPools.Update(ctx, kBefore.ID, AgentPoolUpdateOptions{ Name: String(kBefore.Name), - OrganizationScoped: &organizationScoped, AllowedWorkspaces: []*Workspace{ workspaceTest, }, @@ -228,6 +224,7 @@ func TestAgentPoolsUpdate(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, @@ -235,7 +232,7 @@ func TestAgentPoolsUpdate(t *testing.T) { require.NoError(t, err) assert.NotEqual(t, kBefore.OrganizationScoped, kAfter.OrganizationScoped) - assert.Equal(t, false, kAfter.OrganizationScoped) + assert.Equal(t, organizationScoped, kAfter.OrganizationScoped) }) } From 457b25302206177d2ff6530d989455352f9e4331 Mon Sep 17 00:00:00 2001 From: Netra2104 Date: Mon, 11 Jul 2022 12:51:39 -0400 Subject: [PATCH 6/9] removed pointer from AgentPool --- agent_pool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agent_pool.go b/agent_pool.go index 25e87c8f1..8905cc4b7 100644 --- a/agent_pool.go +++ b/agent_pool.go @@ -48,7 +48,7 @@ type AgentPoolList struct { type AgentPool struct { ID string `jsonapi:"primary,agent-pools"` Name string `jsonapi:"attr,name"` - OrganizationScoped *bool `jsonapi:"attr,organization-scoped,omitempty"` + OrganizationScoped bool `jsonapi:"attr,organization-scoped"` // Relations Organization *Organization `jsonapi:"relation,organization"` From 232da5d05029786bdfed2968ac3e29de6663f46b Mon Sep 17 00:00:00 2001 From: Netra2104 Date: Mon, 11 Jul 2022 17:40:35 -0400 Subject: [PATCH 7/9] added change logs --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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) From f4b47af7624ee42c29cf1f6dca2fa168cb7aa5ef Mon Sep 17 00:00:00 2001 From: Netra2104 Date: Mon, 11 Jul 2022 17:50:09 -0400 Subject: [PATCH 8/9] unnecessary change removed --- agent_pool_integration_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/agent_pool_integration_test.go b/agent_pool_integration_test.go index 2de5fa082..69e6325fb 100644 --- a/agent_pool_integration_test.go +++ b/agent_pool_integration_test.go @@ -163,6 +163,7 @@ func TestAgentPoolsRead(t *testing.T) { func TestAgentPoolsUpdate(t *testing.T) { client := testClient(t) ctx := context.Background() + orgTest, orgTestCleanup := createOrganization(t, client) defer orgTestCleanup() From 8559840792c87be5cc9e7c345834e4f43ce7c07a Mon Sep 17 00:00:00 2001 From: Netra2104 Date: Tue, 12 Jul 2022 11:01:03 -0400 Subject: [PATCH 9/9] ran go fmt --- agent_pool_integration_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/agent_pool_integration_test.go b/agent_pool_integration_test.go index 69e6325fb..ee2ddf5d2 100644 --- a/agent_pool_integration_test.go +++ b/agent_pool_integration_test.go @@ -227,13 +227,13 @@ func TestAgentPoolsUpdate(t *testing.T) { organizationScoped := false kAfter, err := client.AgentPools.Update(ctx, kBefore.ID, AgentPoolUpdateOptions{ - Name: String(kBefore.Name), + Name: String(kBefore.Name), OrganizationScoped: &organizationScoped, }) require.NoError(t, err) - assert.NotEqual(t, kBefore.OrganizationScoped, kAfter.OrganizationScoped) - assert.Equal(t, organizationScoped, kAfter.OrganizationScoped) + assert.NotEqual(t, kBefore.OrganizationScoped, kAfter.OrganizationScoped) + assert.Equal(t, organizationScoped, kAfter.OrganizationScoped) }) }