Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
439 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package tfe | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"time" | ||
) | ||
|
||
// Compile-time proof of interface implementation. | ||
var _ Agents = (*agents)(nil) | ||
|
||
// Agents describes all the agent-related methods that the | ||
// Terraform Cloud API supports. | ||
// TFE API docs: https://www.terraform.io/docs/cloud/api/agents.html | ||
type Agents interface { | ||
// Read an agent by its ID. | ||
Read(ctx context.Context, agentID string) (*Agent, error) | ||
|
||
// List all the agents of the given pool. | ||
List(ctx context.Context, agentPoolID string, options *AgentListOptions) (*AgentList, error) | ||
} | ||
|
||
// agents implements Agents. | ||
type agents struct { | ||
client *Client | ||
} | ||
|
||
// AgentList represents a list of agents. | ||
type AgentList struct { | ||
*Pagination | ||
Items []*Agent | ||
} | ||
|
||
// Agent represents a Terraform Cloud agent. | ||
type Agent struct { | ||
ID string `jsonapi:"primary,agents"` | ||
Name string `jsonapi:"attr,name"` | ||
IP string `jsonapi:"attr,ip-address"` | ||
Status string `jsonapi:"attr,status"` | ||
LastPingAt string `jsonapi:"attr,last-ping-at"` | ||
} | ||
|
||
type AgentListOptions struct { | ||
ListOptions | ||
|
||
//Optional: | ||
LastPingSince time.Time `url:"filter[last-ping-since],omitempty,iso8601"` | ||
} | ||
|
||
// Read a single agent by its ID | ||
func (s *agents) Read(ctx context.Context, agentID string) (*Agent, error) { | ||
if !validStringID(&agentID) { | ||
return nil, ErrInvalidAgentID | ||
} | ||
|
||
u := fmt.Sprintf("agents/%s", url.QueryEscape(agentID)) | ||
req, err := s.client.NewRequest("GET", u, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
agent := &Agent{} | ||
err = req.Do(ctx, agent) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return agent, nil | ||
} | ||
|
||
// List all the agents of the given organization. | ||
func (s *agents) List(ctx context.Context, agentPoolID string, options *AgentListOptions) (*AgentList, error) { | ||
if !validStringID(&agentPoolID) { | ||
return nil, ErrInvalidOrg | ||
} | ||
|
||
u := fmt.Sprintf("agent-pools/%s/agents", url.QueryEscape(agentPoolID)) | ||
req, err := s.client.NewRequest("GET", u, options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
agentList := &AgentList{} | ||
err = req.Do(ctx, agentList) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return agentList, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package tfe | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAgentsRead(t *testing.T) { | ||
skipIfNotLinuxAmd64(t) | ||
|
||
client := testClient(t) | ||
ctx := context.Background() | ||
|
||
org, orgCleanup := createOrganization(t, client) | ||
t.Cleanup(orgCleanup) | ||
|
||
upgradeOrganizationSubscription(t, client, org) | ||
|
||
agent, _, agentCleanup := createAgent(t, client, org) | ||
|
||
t.Cleanup(agentCleanup) | ||
|
||
t.Run("when the agent exists", func(t *testing.T) { | ||
k, err := client.Agents.Read(ctx, agent.ID) | ||
require.NoError(t, err) | ||
assert.Equal(t, agent, k) | ||
}) | ||
|
||
t.Run("when the agent does not exist", func(t *testing.T) { | ||
k, err := client.Agents.Read(ctx, "nonexistent") | ||
assert.Nil(t, k) | ||
assert.Equal(t, err, ErrResourceNotFound) | ||
}) | ||
|
||
t.Run("without a valid agent ID", func(t *testing.T) { | ||
k, err := client.Agents.Read(ctx, badIdentifier) | ||
assert.Nil(t, k) | ||
assert.EqualError(t, err, ErrInvalidAgentID.Error()) | ||
}) | ||
} | ||
|
||
func TestAgentsList(t *testing.T) { | ||
skipIfNotLinuxAmd64(t) | ||
|
||
client := testClient(t) | ||
ctx := context.Background() | ||
|
||
org, orgCleanup := createOrganization(t, client) | ||
t.Cleanup(orgCleanup) | ||
|
||
upgradeOrganizationSubscription(t, client, org) | ||
|
||
_, agentPool, agentCleanup := createAgent(t, client, org) | ||
t.Cleanup(agentCleanup) | ||
|
||
t.Run("expect an agent to exist", func(t *testing.T) { | ||
agent, err := client.Agents.List(ctx, agentPool.ID, nil) | ||
|
||
require.NoError(t, err) | ||
require.NotEmpty(t, agent.Items) | ||
assert.NotEmpty(t, agent.Items[0].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) | ||
assert.EqualError(t, err, ErrInvalidOrg.Error()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.