-
Notifications
You must be signed in to change notification settings - Fork 102
/
agent_token.go
147 lines (119 loc) · 3.85 KB
/
agent_token.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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 _ AgentTokens = (*agentTokens)(nil)
// AgentTokens describes all the agent token related methods that the
// HCP Terraform API supports.
//
// TFE API docs:
// https://developer.hashicorp.com/terraform/cloud-docs/api-docs/agent-tokens
type AgentTokens interface {
// List all the agent tokens of the given agent pool.
List(ctx context.Context, agentPoolID string) (*AgentTokenList, error)
// Create a new agent token with the given options.
Create(ctx context.Context, agentPoolID string, options AgentTokenCreateOptions) (*AgentToken, error)
// Read an agent token by its ID.
Read(ctx context.Context, agentTokenID string) (*AgentToken, error)
// Delete an agent token by its ID.
Delete(ctx context.Context, agentTokenID string) error
}
// agentTokens implements AgentTokens.
type agentTokens struct {
client *Client
}
// AgentToken represents a HCP Terraform agent token.
type AgentToken struct {
ID string `jsonapi:"primary,authentication-tokens"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
Description string `jsonapi:"attr,description"`
LastUsedAt time.Time `jsonapi:"attr,last-used-at,iso8601"`
Token string `jsonapi:"attr,token"`
}
// AgentTokenList represents a list of agent tokens.
type AgentTokenList struct {
*Pagination
Items []*AgentToken
}
// AgentTokenCreateOptions represents the options for creating an agent token.
type AgentTokenCreateOptions struct {
// Type is a public field utilized by JSON:API to
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
// https://jsonapi.org/format/#crud-creating
Type string `jsonapi:"primary,agent-tokens"`
// Description of the token
Description *string `jsonapi:"attr,description"`
}
// List all the agent tokens of the given agent pool.
func (s *agentTokens) List(ctx context.Context, agentPoolID string) (*AgentTokenList, error) {
if !validStringID(&agentPoolID) {
return nil, ErrInvalidAgentPoolID
}
u := fmt.Sprintf("agent-pools/%s/authentication-tokens", url.PathEscape(agentPoolID))
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
tokenList := &AgentTokenList{}
err = req.Do(ctx, tokenList)
if err != nil {
return nil, err
}
return tokenList, nil
}
// Create a new agent token with the given options.
func (s *agentTokens) Create(ctx context.Context, agentPoolID string, options AgentTokenCreateOptions) (*AgentToken, error) {
if !validStringID(&agentPoolID) {
return nil, ErrInvalidAgentPoolID
}
if !validString(options.Description) {
return nil, ErrAgentTokenDescription
}
u := fmt.Sprintf("agent-pools/%s/authentication-tokens", url.PathEscape(agentPoolID))
req, err := s.client.NewRequest("POST", u, &options)
if err != nil {
return nil, err
}
at := &AgentToken{}
err = req.Do(ctx, at)
if err != nil {
return nil, err
}
return at, err
}
// Read an agent token by its ID.
func (s *agentTokens) Read(ctx context.Context, agentTokenID string) (*AgentToken, error) {
if !validStringID(&agentTokenID) {
return nil, ErrInvalidAgentTokenID
}
u := fmt.Sprintf("authentication-tokens/%s", url.PathEscape(agentTokenID))
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
at := &AgentToken{}
err = req.Do(ctx, at)
if err != nil {
return nil, err
}
return at, err
}
// Delete an agent token by its ID.
func (s *agentTokens) Delete(ctx context.Context, agentTokenID string) error {
if !validStringID(&agentTokenID) {
return ErrInvalidAgentTokenID
}
u := fmt.Sprintf("authentication-tokens/%s", url.PathEscape(agentTokenID))
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return err
}
return req.Do(ctx, nil)
}