Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for creating and filtering workspace scoped agent pools #682

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
* `ApplyToProjects` and `RemoveFromProjects` to `VariableSets` endpoints now generally available.
* `ListForProject` to `VariableSets` endpoints now generally available.

## Enhancements
* Adds `OrganizationScoped` and `AllowedWorkspaces` fields for creating workspace scoped agent pools and adds `AllowedWorkspacesName` for filtering agents pools associated with a given workspace by @hs26gill [#682](https://github.com/hashicorp/go-tfe/pull/682/files)

## Bug Fixes


# v1.22.0

## Beta API Changes
Expand Down
11 changes: 10 additions & 1 deletion agent_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ type AgentPoolListOptions struct {

// Optional: A search query string used to filter agent pool. Agent pools are searchable by name
Query string `url:"q,omitempty"`

// Optional: String (workspace name) used to filter the results.
AllowedWorkspacesName string `url:"filter[allowed_workspaces][name],omitempty"`
}

// AgentPoolCreateOptions represents the options for creating an agent pool.
Expand All @@ -91,6 +94,12 @@ type AgentPoolCreateOptions struct {

// Required: A 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"`

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

// List all the agent pools of the given organization.
Expand Down Expand Up @@ -186,7 +195,7 @@ type AgentPoolUpdateOptions struct {
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"`
AllowedWorkspaces []*Workspace `jsonapi:"relation,allowed-workspaces,omitempty"`
}

// Update an agent pool by its ID.
Expand Down
72 changes: 72 additions & 0 deletions agent_pool_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,47 @@ func TestAgentPoolsList(t *testing.T) {
require.NoError(t, err)
assert.Empty(t, pools.Items)
})

t.Run("with allowed workspace name filter", func(t *testing.T) {
ws1, ws1TestCleanup := createWorkspace(t, client, orgTest)
defer ws1TestCleanup()

ws2, ws2TestCleanup := createWorkspace(t, client, orgTest)
defer ws2TestCleanup()

organizationScoped := false
ap, apCleanup := createAgentPoolWithOptions(t, client, orgTest, AgentPoolCreateOptions{
Name: String("a-pool"),
OrganizationScoped: &organizationScoped,
AllowedWorkspaces: []*Workspace{ws1},
})
defer apCleanup()

ap2, ap2Cleanup := createAgentPoolWithOptions(t, client, orgTest, AgentPoolCreateOptions{
Name: String("b-pool"),
OrganizationScoped: &organizationScoped,
AllowedWorkspaces: []*Workspace{ws2},
})
defer ap2Cleanup()

pools, err := client.AgentPools.List(ctx, orgTest.Name, &AgentPoolListOptions{
AllowedWorkspacesName: ws1.Name,
})
require.NoError(t, err)
assert.NotEmpty(t, pools.Items)
assert.Contains(t, pools.Items, ap)
assert.Contains(t, pools.Items, agentPool)
assert.Equal(t, 2, pools.TotalCount)

pools, err = client.AgentPools.List(ctx, orgTest.Name, &AgentPoolListOptions{
AllowedWorkspacesName: ws2.Name,
})
require.NoError(t, err)
assert.NotEmpty(t, pools.Items)
assert.Contains(t, pools.Items, agentPool)
assert.Contains(t, pools.Items, ap2)
assert.Equal(t, 2, pools.TotalCount)
})
}

func TestAgentPoolsCreate(t *testing.T) {
Expand Down Expand Up @@ -128,6 +169,37 @@ func TestAgentPoolsCreate(t *testing.T) {
assert.Nil(t, pool)
assert.EqualError(t, err, ErrInvalidOrg.Error())
})

t.Run("with allowed-workspaces options", 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,
},
}

pool, err := client.AgentPools.Create(ctx, orgTest.Name, options)
require.NoError(t, err)

assert.Equal(t, 1, len(pool.AllowedWorkspaces))
assert.Equal(t, workspaceTest.ID, pool.AllowedWorkspaces[0].ID)

// Get a refreshed view from the API.
refreshed, err := client.AgentPools.Read(ctx, pool.ID)
require.NoError(t, err)

for _, item := range []*AgentPool{
pool,
refreshed,
} {
assert.NotEmpty(t, item.ID)
}
})
}

func TestAgentPoolsRead(t *testing.T) {
Expand Down
26 changes: 26 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,32 @@ func createAgentPool(t *testing.T, client *Client, org *Organization) (*AgentPoo
}
}

func createAgentPoolWithOptions(t *testing.T, client *Client, org *Organization, opts AgentPoolCreateOptions) (*AgentPool, func()) {
var orgCleanup func()

if org == nil {
org, orgCleanup = createOrganization(t, client)
}

ctx := context.Background()
pool, err := client.AgentPools.Create(ctx, org.Name, opts)
if err != nil {
t.Fatal(err)
}

return pool, func() {
if err := client.AgentPools.Delete(ctx, pool.ID); err != nil {
t.Logf("Error destroying agent pool! WARNING: Dangling resources "+
"may exist! The full error is shown below.\n\n"+
"Agent pool ID: %s\nError: %s", pool.ID, err)
}

if orgCleanup != nil {
orgCleanup()
}
}
}

func createAgentToken(t *testing.T, client *Client, ap *AgentPool) (*AgentToken, func()) {
var apCleanup func()

Expand Down
2 changes: 0 additions & 2 deletions variable_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ func (s *variableSets) RemoveFromWorkspaces(ctx context.Context, variableSetID s

// ApplyToProjects applies the variable set to projects in the supplied list.
// This method will return an error if the variable set has global = true.
// **Note: This feature is still in BETA and subject to change.**
func (s variableSets) ApplyToProjects(ctx context.Context, variableSetID string, options VariableSetApplyToProjectsOptions) error {
if !validStringID(&variableSetID) {
return ErrInvalidVariableSetID
Expand All @@ -401,7 +400,6 @@ func (s variableSets) ApplyToProjects(ctx context.Context, variableSetID string,

// RemoveFromProjects removes the variable set from projects in the supplied list.
// This method will return an error if the variable set has global = true.
// **Note: This feature is still in BETA and subject to change.**
func (s variableSets) RemoveFromProjects(ctx context.Context, variableSetID string, options VariableSetRemoveFromProjectsOptions) error {
if !validStringID(&variableSetID) {
return ErrInvalidVariableSetID
Expand Down