forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_token.go
135 lines (110 loc) · 3.38 KB
/
user_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
package tfe
import (
"context"
"errors"
"fmt"
"net/url"
"time"
)
// Compile-time proof of interface implementation.
var _ UserTokens = (*userTokens)(nil)
// UserTokens describes all the user token related methods that the
// Terraform Cloud/Enterprise API supports.
//
// TFE API docs:
// https://www.terraform.io/docs/enterprise/api/user-tokens.html
type UserTokens interface {
// List all the tokens of the given user ID.
List(ctx context.Context, userID string) (*UserTokenList, error)
// Generate a new user token
Generate(ctx context.Context, userID string, options UserTokenGenerateOptions) (*UserToken, error)
// Read a user token by its ID.
Read(ctx context.Context, tokenID string) (*UserToken, error)
// Delete a user token by its ID.
Delete(ctx context.Context, tokenID string) error
}
// userTokens implements UserTokens.
type userTokens struct {
client *Client
}
// UserTokenList is a list of tokens for the given user ID.
type UserTokenList struct {
*Pagination
Items []*UserToken
}
// UserToken represents a Terraform Enterprise user token.
type UserToken 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"`
}
// UserTokenGenerateOptions the options for creating a user token.
type UserTokenGenerateOptions struct {
// Description of the token
Description string `jsonapi:"attr,description,omitempty"`
}
// Generate a new user token
func (s *userTokens) Generate(ctx context.Context, userID string, options UserTokenGenerateOptions) (*UserToken, error) {
if !validStringID(&userID) {
return nil, errors.New("invalid value for user ID")
}
u := fmt.Sprintf("users/%s/authentication-tokens", url.QueryEscape(userID))
req, err := s.client.newRequest("POST", u, &options)
if err != nil {
return nil, err
}
ut := &UserToken{}
err = s.client.do(ctx, req, ut)
if err != nil {
return nil, err
}
return ut, err
}
// List shows existing user tokens
func (s *userTokens) List(ctx context.Context, userID string) (*UserTokenList, error) {
if !validStringID(&userID) {
return nil, errors.New("invalid value for user ID")
}
u := fmt.Sprintf("users/%s/authentication-tokens", url.QueryEscape(userID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
tl := &UserTokenList{}
err = s.client.do(ctx, req, tl)
if err != nil {
return nil, err
}
return tl, err
}
// Read a user token by its ID.
func (s *userTokens) Read(ctx context.Context, tokenID string) (*UserToken, error) {
if !validStringID(&tokenID) {
return nil, errors.New("invalid value for token ID")
}
u := fmt.Sprintf("authentication-tokens/%s", url.QueryEscape(tokenID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
tt := &UserToken{}
err = s.client.do(ctx, req, tt)
if err != nil {
return nil, err
}
return tt, err
}
// Delete a user token by its ID.
func (s *userTokens) Delete(ctx context.Context, tokenID string) error {
if !validStringID(&tokenID) {
return errors.New("invalid value for token ID")
}
u := fmt.Sprintf("authentication-tokens/%s", url.QueryEscape(tokenID))
req, err := s.client.newRequest("DELETE", u, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}