-
Notifications
You must be signed in to change notification settings - Fork 102
/
agent.go
94 lines (76 loc) · 2.13 KB
/
agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
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
// HCP Terraform API supports.
// TFE API docs: https://developer.hashicorp.com/terraform/cloud-docs/api-docs/agents
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 HCP Terraform 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.PathEscape(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.PathEscape(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
}