diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cad7ecd2..694a9b1f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/agent.go b/agent.go index 379b67a65..1a61393d4 100644 --- a/agent.go +++ b/agent.go @@ -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 diff --git a/agent_integration_test.go b/agent_integration_test.go index 3b4689e62..48514316e 100644 --- a/agent_integration_test.go +++ b/agent_integration_test.go @@ -5,6 +5,7 @@ package tfe import ( "context" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -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) { @@ -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) @@ -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) diff --git a/agent_pool.go b/agent_pool.go index 47778820b..f33efcd0c 100644 --- a/agent_pool.go +++ b/agent_pool.go @@ -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. diff --git a/agent_pool_integration_test.go b/agent_pool_integration_test.go index 2bad0c15e..787e9e047 100644 --- a/agent_pool_integration_test.go +++ b/agent_pool_integration_test.go @@ -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) diff --git a/helper_test.go b/helper_test.go index 2ec5cfd6d..dcf5cadba 100644 --- a/helper_test.go +++ b/helper_test.go @@ -261,9 +261,10 @@ 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 @@ -271,7 +272,15 @@ func createAgent(t *testing.T, client *Client, org *Organization) (*Agent, *Agen 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) @@ -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 @@ -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, ) @@ -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") })