Skip to content

Commit 1d3b758

Browse files
committed
SCALRCORE-21783 Add AccessTokens.Read method
1 parent cb33e3e commit 1d3b758

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

access_token.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var _ AccessTokens = (*accessTokens)(nil)
1414
// AccessTokens describes all the access token related methods that the
1515
// Scalr IACP API supports.
1616
type AccessTokens interface {
17+
Read(ctx context.Context, accessTokenID string) (*AccessToken, error)
1718
Update(ctx context.Context, accessTokenID string, options AccessTokenUpdateOptions) (*AccessToken, error)
1819
Delete(ctx context.Context, accessTokenID string) error
1920
}
@@ -58,6 +59,27 @@ type AccessTokenUpdateOptions struct {
5859
Description *string `jsonapi:"attr,description,omitempty"`
5960
}
6061

62+
// Read access token by its ID
63+
func (s *accessTokens) Read(ctx context.Context, accessTokenID string) (*AccessToken, error) {
64+
if !validStringID(&accessTokenID) {
65+
return nil, errors.New("invalid value for access token ID")
66+
}
67+
68+
u := fmt.Sprintf("access-tokens/%s", url.QueryEscape(accessTokenID))
69+
req, err := s.client.newRequest("GET", u, nil)
70+
if err != nil {
71+
return nil, err
72+
}
73+
74+
at := &AccessToken{}
75+
err = s.client.do(ctx, req, at)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
return at, nil
81+
}
82+
6183
// Update is used to update an AccessToken.
6284
func (s *accessTokens) Update(ctx context.Context, accessTokenID string, options AccessTokenUpdateOptions) (*AccessToken, error) {
6385

access_token_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,33 @@ import (
99
"github.com/stretchr/testify/require"
1010
)
1111

12+
func TestAccessTokenRead(t *testing.T) {
13+
client := testClient(t)
14+
ctx := context.Background()
15+
16+
ap, apCleanup := createAgentPool(t, client)
17+
defer apCleanup()
18+
19+
atTest, atTestCleanup := createAgentPoolToken(t, client, ap.ID)
20+
defer atTestCleanup()
21+
22+
t.Run("when the token exists", func(t *testing.T) {
23+
at, err := client.AccessTokens.Read(ctx, atTest.ID)
24+
require.NoError(t, err)
25+
assert.Equal(t, atTest.ID, at.ID)
26+
})
27+
28+
t.Run("when the token does not exist", func(t *testing.T) {
29+
_, err := client.AccessTokens.Read(ctx, "at-nonexisting")
30+
assert.Error(t, err)
31+
})
32+
33+
t.Run("with invalid token ID", func(t *testing.T) {
34+
_, err := client.AccessTokens.Read(ctx, badIdentifier)
35+
assert.EqualError(t, err, "invalid value for access token ID")
36+
})
37+
}
38+
1239
func TestAccessTokenUpdate(t *testing.T) {
1340
client := testClient(t)
1441
ctx := context.Background()

0 commit comments

Comments
 (0)