Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3d771ca
add sorting to agent list and agent pool list
twitnithegirl Aug 21, 2025
9680a10
update changelog
twitnithegirl Aug 21, 2025
4831aa5
update integration test
twitnithegirl Aug 21, 2025
f850519
appease linting gods
twitnithegirl Aug 21, 2025
7a3534e
correct agent pool integration test
twitnithegirl Aug 25, 2025
c1f1eab
correct agent integration test
twitnithegirl Aug 26, 2025
1b28d83
Merge branch 'main' into twitnithegirl/TFDN-1107-add-sorting-to-agent…
twitnithegirl Aug 26, 2025
83883d9
correct linting in helper_test.go
twitnithegirl Aug 26, 2025
853d272
debugging output
twitnithegirl Aug 26, 2025
9b4922b
debuggering
twitnithegirl Aug 26, 2025
1d453b6
return correct agent in test helper
twitnithegirl Aug 27, 2025
1c5a25a
Merge branch 'main' into twitnithegirl/TFDN-1107-add-sorting-to-agent…
twitnithegirl Aug 27, 2025
13f9121
correct number of calls
twitnithegirl Aug 27, 2025
56c8b6b
Merge branch 'main' into twitnithegirl/TFDN-1107-add-sorting-to-agent…
twitnithegirl Aug 28, 2025
25872a6
attempt string pointer success
twitnithegirl Sep 2, 2025
f3bfc54
Merge branch 'twitnithegirl/TFDN-1107-add-sorting-to-agent-list' of g…
twitnithegirl Sep 2, 2025
4ed2a67
Merge branch 'main' into twitnithegirl/TFDN-1107-add-sorting-to-agent…
twitnithegirl Sep 2, 2025
f15d957
sanity check
twitnithegirl Sep 2, 2025
cc2f610
sanity check
twitnithegirl Sep 3, 2025
6ea0fe9
sanity check
twitnithegirl Sep 3, 2025
1aff19e
please work
twitnithegirl Sep 3, 2025
148b96f
maybe this
twitnithegirl Sep 3, 2025
2e315c5
plz give answers
twitnithegirl Sep 3, 2025
98314bd
puts debuggerer
twitnithegirl Sep 3, 2025
5c6d249
help
twitnithegirl Sep 3, 2025
f8edf89
omg
twitnithegirl Sep 3, 2025
487231d
yay
twitnithegirl Sep 3, 2025
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Unreleased

## Enhancements
* Add `Sort` option to `Agents` and `AgentPools`, by @twitnithegirl [#1193](https://github.com/hashicorp/go-tfe/pull/1193)

# v1.91.1

## Bug Fixes
Expand Down
3 changes: 3 additions & 0 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type AgentListOptions struct {

//Optional:
LastPingSince time.Time `url:"filter[last-ping-since],omitempty,iso8601"`

// Optional: Allows sorting the agents by "created-by"
Sort string `url:"sort,omitempty"`
}

// Read a single agent by its ID
Expand Down
40 changes: 37 additions & 3 deletions agent_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package tfe

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -22,8 +23,7 @@ func TestAgentsRead(t *testing.T) {

upgradeOrganizationSubscription(t, client, org)

agent, _, agentCleanup := createAgent(t, client, org)

agent, _, agentCleanup := createAgent(t, client, org, nil, "")
t.Cleanup(agentCleanup)

t.Run("when the agent exists", func(t *testing.T) {
Expand Down Expand Up @@ -56,8 +56,13 @@ func TestAgentsList(t *testing.T) {

upgradeOrganizationSubscription(t, client, org)

_, agentPool, agentCleanup := createAgent(t, client, org)
agent1, agentPool, agentCleanup := createAgent(t, client, org, nil, "agent1")
t.Cleanup(agentCleanup)
agent2, agentPool2, agentCleanup2 := createAgent(t, client, org, agentPool, "agent2")
fmt.Println("agent pool stuff")
fmt.Println(agentPool2.ID)
fmt.Println(agentPool.ID)
t.Cleanup(agentCleanup2)

t.Run("expect an agent to exist", func(t *testing.T) {
agent, err := client.Agents.List(ctx, agentPool.ID, nil)
Expand All @@ -67,6 +72,35 @@ func TestAgentsList(t *testing.T) {
assert.NotEmpty(t, agent.Items[0].ID)
})

t.Run("with sorting", func(t *testing.T) {
agents, err := client.Agents.List(ctx, agentPool.ID, &AgentListOptions{
Sort: "created-at",
})
fmt.Println("line 78")
fmt.Println(agents.Items[0].Name)
fmt.Println(agents.Items[0].ID)
fmt.Println(agents.Items[1].Name)
fmt.Println(agents.Items[1].ID)
require.NoError(t, err)
require.NotNil(t, agents)
require.Len(t, agents.Items, 2)
fmt.Println(agent1)
fmt.Println(agent1.Name)
fmt.Println(agent2)
fmt.Println(agent2.Name)
fmt.Println(agents)

require.Equal(t, []string{agent1.ID, agent2.ID}, []string{agents.Items[0].ID, agents.Items[1].ID})

agents, err = client.Agents.List(ctx, agentPool.ID, &AgentListOptions{
Sort: "-created-at",
})
require.NoError(t, err)
require.NotNil(t, agents)
require.Len(t, agents.Items, 2)
require.Equal(t, []string{agent2.ID, agent1.ID}, []string{agents.Items[0].ID, agents.Items[1].ID})
})

t.Run("without a valid agent pool ID", func(t *testing.T) {
agent, err := client.Agents.List(ctx, badIdentifier, nil)
assert.Nil(t, agent)
Expand Down
3 changes: 3 additions & 0 deletions agent_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ type AgentPoolListOptions struct {

// Optional: String (project name) used to filter the results.
AllowedProjectsName string `url:"filter[allowed_projects][name],omitempty"`

// Optional: Allows sorting the agent pools by "created-by" or "name"
Sort string `url:"sort,omitempty"`
}

// AgentPoolCreateOptions represents the options for creating an agent pool.
Expand Down
20 changes: 20 additions & 0 deletions agent_pool_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ func TestAgentPoolsList(t *testing.T) {
assert.Equal(t, 999, pools.CurrentPage)
assert.Equal(t, 1, pools.TotalCount)
})
t.Run("with sorting", func(t *testing.T) {
agentPool2, agentPoolCleanup2 := createAgentPool(t, client, orgTest)
t.Cleanup(agentPoolCleanup2)

pools, err := client.AgentPools.List(ctx, orgTest.Name, &AgentPoolListOptions{
Sort: "created-at",
})
require.NoError(t, err)
require.NotNil(t, pools)
require.Len(t, pools.Items, 2)
require.Equal(t, []string{agentPool.ID, agentPool2.ID}, []string{pools.Items[0].ID, pools.Items[1].ID})

pools, err = client.AgentPools.List(ctx, orgTest.Name, &AgentPoolListOptions{
Sort: "-created-at",
})
require.NoError(t, err)
require.NotNil(t, pools)
require.Len(t, pools.Items, 2)
require.Equal(t, []string{agentPool2.ID, agentPool.ID}, []string{pools.Items[0].ID, pools.Items[1].ID})
})

t.Run("without a valid organization", func(t *testing.T) {
pools, err := client.AgentPools.List(ctx, badIdentifier, nil)
Expand Down
41 changes: 37 additions & 4 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,26 @@ func downloadTFCAgent(t *testing.T) (string, error) {
return fmt.Sprintf("%s/tfc-agent", tmpDir), nil
}

func createAgent(t *testing.T, client *Client, org *Organization) (*Agent, *AgentPool, func()) {
func createAgent(t *testing.T, client *Client, org *Organization, agentPool *AgentPool, name string) (*Agent, *AgentPool, func()) {
var orgCleanup func()
var agentPoolTokenCleanup func()
var agentPoolCleanup func()
var agent *Agent
var ok bool

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

agentPool, agentPoolCleanup := createAgentPool(t, client, org)
if agentPool == nil {
agentPool, agentPoolCleanup = createAgentPool(t, client, org)
}

if name == "" {
name = "test-agent"
}
fmt.Println("the name passed into createAgent")
fmt.Println(name)

upgradeOrganizationSubscription(t, client, org)

Expand All @@ -289,6 +298,21 @@ func createAgent(t *testing.T, client *Client, org *Organization) (*Agent, *Agen
}
}

xctx := context.Background()

agentList, err := client.Agents.List(xctx, agentPool.ID, nil)
if err != nil {
fmt.Println("OMG!")
}

if agentList != nil && len(agentList.Items) > 0 {
for _, value := range agentList.Items {
fmt.Println("the before times")
fmt.Println(value.Name)
fmt.Println(value.ID)
}
}

agentPath, err := downloadTFCAgent(t)
if err != nil {
return agent, agentPool, cleanup
Expand All @@ -300,7 +324,7 @@ func createAgent(t *testing.T, client *Client, org *Organization) (*Agent, *Agen
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env,
"TFC_AGENT_TOKEN="+agentPoolToken.Token,
"TFC_AGENT_NAME="+"test-agent",
"TFC_AGENT_NAME="+name,
"TFC_ADDRESS="+DefaultConfig().Address,
)

Expand All @@ -324,7 +348,16 @@ func createAgent(t *testing.T, client *Client, org *Organization) (*Agent, *Agen
}

if agentList != nil && len(agentList.Items) > 0 {
return agentList.Items[0], nil
var result *Agent
for _, value := range agentList.Items {
fmt.Println("value.Name for agent in list")
fmt.Println(value.Name)
fmt.Println(value.ID)
if value.Name == name {
result = value
}
}
return result, nil
}
return nil, errors.New("no agent found")
})
Expand Down
Loading