-
Notifications
You must be signed in to change notification settings - Fork 3
/
access_token.go
120 lines (96 loc) · 3.19 KB
/
access_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
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
"time"
)
// Compile-time proof of interface implementation.
var _ AccessTokens = (*accessTokens)(nil)
// AccessTokens describes all the access token related methods that the
// Scalr IACP API supports.
type AccessTokens interface {
Read(ctx context.Context, accessTokenID string) (*AccessToken, error)
Update(ctx context.Context, accessTokenID string, options AccessTokenUpdateOptions) (*AccessToken, error)
Delete(ctx context.Context, accessTokenID string) error
}
// accessTokens implements AccessTokens.
type accessTokens struct {
client *Client
}
// AccessTokenList represents a list of access tokens.
type AccessTokenList struct {
*Pagination
Items []*AccessToken
}
// AccessToken represents a Scalr access token.
type AccessToken struct {
ID string `jsonapi:"primary,access-tokens"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
Description string `jsonapi:"attr,description"`
Token string `jsonapi:"attr,token"`
}
// AccessTokenListOptions represents the options for listing access tokens.
type AccessTokenListOptions struct {
ListOptions
}
// AccessTokenCreateOptions represents the options for creating a new AccessToken.
type AccessTokenCreateOptions struct {
// For internal use only!
ID string `jsonapi:"primary,access-tokens"`
Description *string `jsonapi:"attr,description,omitempty"`
}
// AccessTokenUpdateOptions represents the options for updating an AccessToken.
type AccessTokenUpdateOptions struct {
// For internal use only!
ID string `jsonapi:"primary,access-tokens"`
Description *string `jsonapi:"attr,description,omitempty"`
}
// Read access token by its ID
func (s *accessTokens) Read(ctx context.Context, accessTokenID string) (*AccessToken, error) {
if !validStringID(&accessTokenID) {
return nil, errors.New("invalid value for access token ID")
}
u := fmt.Sprintf("access-tokens/%s", url.QueryEscape(accessTokenID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
at := &AccessToken{}
err = s.client.do(ctx, req, at)
if err != nil {
return nil, err
}
return at, nil
}
// Update is used to update an AccessToken.
func (s *accessTokens) Update(ctx context.Context, accessTokenID string, options AccessTokenUpdateOptions) (*AccessToken, error) {
// Make sure we don't send a user provided ID.
options.ID = ""
if !validStringID(&accessTokenID) {
return nil, fmt.Errorf("invalid value for access token ID: '%s'", accessTokenID)
}
req, err := s.client.newRequest("PATCH", fmt.Sprintf("access-tokens/%s", url.QueryEscape(accessTokenID)), &options)
if err != nil {
return nil, err
}
accessToken := &AccessToken{}
err = s.client.do(ctx, req, accessToken)
if err != nil {
return nil, err
}
return accessToken, nil
}
// Delete an access token by its ID.
func (s *accessTokens) Delete(ctx context.Context, accessTokenID string) error {
if !validStringID(&accessTokenID) {
return fmt.Errorf("invalid value for access token ID: '%s'", accessTokenID)
}
t := fmt.Sprintf("access-tokens/%s", url.QueryEscape(accessTokenID))
req, err := s.client.newRequest("DELETE", t, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}