Skip to content

Commit 3d771ca

Browse files
committed
add sorting to agent list and agent pool list
1 parent 8444912 commit 3d771ca

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

agent.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ type AgentListOptions struct {
4949

5050
//Optional:
5151
LastPingSince time.Time `url:"filter[last-ping-since],omitempty,iso8601"`
52+
53+
// Optional: Allows sorting the agents by "created-by"
54+
Sort string `url:"sort,omitempty"`
5255
}
5356

5457
// Read a single agent by its ID

agent_integration_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestAgentsRead(t *testing.T) {
2323
upgradeOrganizationSubscription(t, client, org)
2424

2525
agent, _, agentCleanup := createAgent(t, client, org)
26+
agent2, _, agentCleanup := createAgent(t, client, org)
2627

2728
t.Cleanup(agentCleanup)
2829

@@ -67,6 +68,24 @@ func TestAgentsList(t *testing.T) {
6768
assert.NotEmpty(t, agent.Items[0].ID)
6869
})
6970

71+
t.Run("with sorting", func(t *testing.T) {
72+
agents, err := client.Agents.List(ctx, agentPool.ID, &AgentListOptions{
73+
Sort: "created-at",
74+
})
75+
require.NoError(t, err)
76+
require.NotNil(t, agents)
77+
require.Len(t, agents.Items, 2)
78+
require.Equal(t, []string{agent1.ID, agent2.ID}, []string{agents.Items[0].ID, agents.Items[1].ID})
79+
80+
agents, err = client.Agents.List(ctx, agentPool.ID, &AgentListOptions{
81+
Sort: "-created-at",
82+
})
83+
require.NoError(t, err)
84+
require.NotNil(t, agents)
85+
require.Len(t, agents.Items, 2)
86+
require.Equal(t, []string{agent2.ID, agent1.ID}, []string{agents.Items[0].ID, agents.Items[1].ID})
87+
})
88+
7089
t.Run("without a valid agent pool ID", func(t *testing.T) {
7190
agent, err := client.Agents.List(ctx, badIdentifier, nil)
7291
assert.Nil(t, agent)

agent_pool.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ type AgentPoolListOptions struct {
9898

9999
// Optional: String (project name) used to filter the results.
100100
AllowedProjectsName string `url:"filter[allowed_projects][name],omitempty"`
101+
102+
// Optional: Allows sorting the agent pools by "created-by" or "name"
103+
Sort string `url:"sort,omitempty"`
101104
}
102105

103106
// AgentPoolCreateOptions represents the options for creating an agent pool.

agent_pool_integration_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ func TestAgentPoolsList(t *testing.T) {
2020

2121
agentPool, agentPoolCleanup := createAgentPool(t, client, orgTest)
2222
t.Cleanup(agentPoolCleanup)
23+
agentPool2, agentPoolCleanup2 := createAgentPool(t, client, orgTest)
24+
defer agentPoolCleanup2()
2325

2426
t.Run("without list options", func(t *testing.T) {
2527
pools, err := client.AgentPools.List(ctx, orgTest.Name, nil)
@@ -62,6 +64,23 @@ func TestAgentPoolsList(t *testing.T) {
6264
assert.Equal(t, 999, pools.CurrentPage)
6365
assert.Equal(t, 1, pools.TotalCount)
6466
})
67+
t.Run("with sorting", func(t *testing.T) {
68+
pools, err := client.AgentPools.List(ctx, orgTest.Name, &AgentPoolListOptions{
69+
Sort: "created-at",
70+
})
71+
require.NoError(t, err)
72+
require.NotNil(t, pools)
73+
require.Len(t, pools.Items, 2)
74+
require.Equal(t, []string{agentPool.ID, agentPool2.ID}, []string{pools.Items[0].ID, pools.Items[1].ID})
75+
76+
pools, err = client.AgentPools.List(ctx, orgTest.Name, &AgentPoolListOptions{
77+
Sort: "-created-at",
78+
})
79+
require.NoError(t, err)
80+
require.NotNil(t, pools)
81+
require.Len(t, pools.Items, 2)
82+
require.Equal(t, []string{agentPool2.ID, agentPool.ID}, []string{pools.Items[0].ID, pools.Items[1].ID})
83+
})
6584

6685
t.Run("without a valid organization", func(t *testing.T) {
6786
pools, err := client.AgentPools.List(ctx, badIdentifier, nil)

0 commit comments

Comments
 (0)