-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathservice_account_token.go
77 lines (64 loc) · 1.88 KB
/
service_account_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
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
)
// Compile-time proof of interface implementation.
var _ ServiceAccountTokens = (*serviceAccountTokens)(nil)
// ServiceAccountTokens describes all the access token related methods that the
// Scalr IACP API supports.
type ServiceAccountTokens interface {
// List service account's access tokens
List(ctx context.Context, serviceAccountID string, options AccessTokenListOptions) (*AccessTokenList, error)
// Create new access token for service account
Create(ctx context.Context, serviceAccountID string, options AccessTokenCreateOptions) (*AccessToken, error)
}
// serviceAccountTokens implements ServiceAccountTokens.
type serviceAccountTokens struct {
client *Client
}
// List the access tokens of ServiceAccount.
func (s *serviceAccountTokens) List(
ctx context.Context, serviceAccountID string, options AccessTokenListOptions,
) (*AccessTokenList, error) {
req, err := s.client.newRequest(
"GET",
fmt.Sprintf("service-accounts/%s/access-tokens", url.QueryEscape(serviceAccountID)),
&options,
)
if err != nil {
return nil, err
}
atl := &AccessTokenList{}
err = s.client.do(ctx, req, atl)
if err != nil {
return nil, err
}
return atl, nil
}
// Create is used to create a new AccessToken for ServiceAccount.
func (s *serviceAccountTokens) Create(
ctx context.Context, serviceAccountID string, options AccessTokenCreateOptions,
) (*AccessToken, error) {
// Make sure we don't send a user provided ID.
options.ID = ""
if !validStringID(&serviceAccountID) {
return nil, errors.New("invalid value for service account ID")
}
req, err := s.client.newRequest(
"POST",
fmt.Sprintf("service-accounts/%s/access-tokens", url.QueryEscape(serviceAccountID)),
&options,
)
if err != nil {
return nil, err
}
at := &AccessToken{}
err = s.client.do(ctx, req, at)
if err != nil {
return nil, err
}
return at, nil
}