Skip to content

Commit

Permalink
Add UpdateAllowedWorkspaces method to Agent Pool
Browse files Browse the repository at this point in the history
  • Loading branch information
hs26gill committed May 11, 2023
1 parent db09c87 commit bc218da
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
* Adds support for a new variable field `version-id` by @arybolovlev [#697](https://github.com/hashicorp/go-tfe/pull/697)
* Adds `ExpiredAt` field to `OrganizationToken`, `TeamToken`, and `UserToken`. This feature will be available in TFE release, v202305-1. @JuliannaTetreault [#672](https://github.com/hashicorp/go-tfe/pull/672)

## Bug Fixes
* AgentPool `Update` was previously not able to remove all allowed workspaces from an agent pool. `AllowedWorkspaces` has been removed from `AgentPoolUpdateOptions` and are now handled by a separate `UpdateAllowedWorkspaces` method using `AgentPoolAllowedWorkspacesUpdateOptions`.


# v1.23.0

## Features
Expand Down
36 changes: 34 additions & 2 deletions agent_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ type AgentPools interface {
// Update an agent pool by its ID.
Update(ctx context.Context, agentPool string, options AgentPoolUpdateOptions) (*AgentPool, error)

// UpdateAllowedWorkspaces updates the list of allowed workspaces associated with an agent pool.
UpdateAllowedWorkspaces(ctx context.Context, agentPool string, options AgentPoolUpdateAllowedWorkspacesOptions) (*AgentPool, error)

// Delete an agent pool by its ID.
Delete(ctx context.Context, agentPoolID string) error
}
Expand Down Expand Up @@ -189,13 +192,22 @@ type AgentPoolUpdateOptions struct {
Type string `jsonapi:"primary,agent-pools"`

// A new name to identify the agent pool.
Name *string `jsonapi:"attr,name"`
Name *string `jsonapi:"attr,name,omitempty"`

// True if the agent pool is organization scoped, false otherwise.
OrganizationScoped *bool `jsonapi:"attr,organization-scoped,omitempty"`
}

// AgentPoolUpdateAllowedWorkspacesOptions represents the options for updating allowed workspace on an agent pool
type AgentPoolUpdateAllowedWorkspacesOptions struct {
// Type is a public field utilized by JSON:API to
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
// https://jsonapi.org/format/#crud-creating
Type string `jsonapi:"primary,agent-pools"`

// A new list of workspaces that are associated with an agent pool.
AllowedWorkspaces []*Workspace `jsonapi:"relation,allowed-workspaces,omitempty"`
AllowedWorkspaces []*Workspace `jsonapi:"relation,allowed-workspaces"`
}

// Update an agent pool by its ID.
Expand Down Expand Up @@ -223,6 +235,26 @@ func (s *agentPools) Update(ctx context.Context, agentPoolID string, options Age
return k, nil
}

func (s *agentPools) UpdateAllowedWorkspaces(ctx context.Context, agentPoolID string, options AgentPoolUpdateAllowedWorkspacesOptions) (*AgentPool, error) {
if !validStringID(&agentPoolID) {
return nil, ErrInvalidAgentPoolID
}

u := fmt.Sprintf("agent-pools/%s", url.QueryEscape(agentPoolID))
req, err := s.client.NewRequest("PATCH", u, &options)
if err != nil {
return nil, err
}

k := &AgentPool{}
err = req.Do(ctx, k)
if err != nil {
return nil, err
}

return k, nil
}

// Delete an agent pool by its ID.
func (s *agentPools) Delete(ctx context.Context, agentPoolID string) error {
if !validStringID(&agentPoolID) {
Expand Down
73 changes: 61 additions & 12 deletions agent_pool_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,20 @@ func TestAgentPoolsUpdate(t *testing.T) {
assert.NotEqual(t, kBefore.Name, kAfter.Name)
})

t.Run("when updating the name", func(t *testing.T) {
kBefore, kTestCleanup := createAgentPool(t, client, orgTest)
defer kTestCleanup()
t.Run("when updating only the name", func(t *testing.T) {
workspaceTest, workspaceTestCleanup := createWorkspace(t, client, orgTest)
defer workspaceTestCleanup()

organizationScoped := false
options := AgentPoolCreateOptions{
Name: String("a-pool"),
OrganizationScoped: &organizationScoped,
AllowedWorkspaces: []*Workspace{
workspaceTest,
},
}
kBefore, err := client.AgentPools.Create(ctx, orgTest.Name, options)
require.NoError(t, err)

kAfter, err := client.AgentPools.Update(ctx, kBefore.ID, AgentPoolUpdateOptions{
Name: String("updated-key-name"),
Expand All @@ -281,6 +292,8 @@ func TestAgentPoolsUpdate(t *testing.T) {

assert.Equal(t, kBefore.ID, kAfter.ID)
assert.Equal(t, "updated-key-name", kAfter.Name)
assert.Equal(t, 1, len(kAfter.AllowedWorkspaces))
assert.Equal(t, workspaceTest.ID, kAfter.AllowedWorkspaces[0].ID)
})

t.Run("without a valid agent pool ID", func(t *testing.T) {
Expand All @@ -289,39 +302,75 @@ func TestAgentPoolsUpdate(t *testing.T) {
assert.EqualError(t, err, ErrInvalidAgentPoolID.Error())
})

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 TestAgentPoolsUpdateAllowedWorkspaces(t *testing.T) {
client := testClient(t)
ctx := context.Background()

orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()

upgradeOrganizationSubscription(t, client, orgTest)

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),
kAfter, err := client.AgentPools.UpdateAllowedWorkspaces(ctx, kBefore.ID, AgentPoolUpdateAllowedWorkspacesOptions{
AllowedWorkspaces: []*Workspace{
workspaceTest,
},
})
require.NoError(t, err)

assert.Equal(t, kBefore.Name, kAfter.Name)
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()
t.Run("when removing all the allowed-workspaces", func(t *testing.T) {
workspaceTest, workspaceTestCleanup := createWorkspace(t, client, orgTest)
defer workspaceTestCleanup()

organizationScoped := false
kAfter, err := client.AgentPools.Update(ctx, kBefore.ID, AgentPoolUpdateOptions{
Name: String(kBefore.Name),
options := AgentPoolCreateOptions{
Name: String("a-pool"),
OrganizationScoped: &organizationScoped,
AllowedWorkspaces: []*Workspace{
workspaceTest,
},
}

kBefore, kTestCleanup := createAgentPoolWithOptions(t, client, orgTest, options)
defer kTestCleanup()

kAfter, err := client.AgentPools.UpdateAllowedWorkspaces(ctx, kBefore.ID, AgentPoolUpdateAllowedWorkspacesOptions{
AllowedWorkspaces: []*Workspace{},
})
require.NoError(t, err)

assert.NotEqual(t, kBefore.OrganizationScoped, kAfter.OrganizationScoped)
assert.Equal(t, organizationScoped, kAfter.OrganizationScoped)
assert.Equal(t, kBefore.ID, kAfter.ID)
assert.Equal(t, "a-pool", kAfter.Name)
assert.Empty(t, kAfter.AllowedWorkspaces)
})
}

Expand Down
15 changes: 15 additions & 0 deletions mocks/agent_pool_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions mocks/registry_no_code_module_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bc218da

Please sign in to comment.