Skip to content

Commit

Permalink
API Key CRUD (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
SolarFactories authored Dec 4, 2024
1 parent 50c5c32 commit b56de7d
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
7 changes: 7 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ func withBody(body interface{}) requestOption {
return err
}
contentType = "application/x-www-form-urlencoded"
case string:
bodyBuf = new(bytes.Buffer)
_, err := io.WriteString(bodyBuf, body)
contentType = "text/plain"
if err != nil {
return err
}
default:
bodyBuf = new(bytes.Buffer)
if err := json.NewEncoder(bodyBuf).Encode(body); err != nil {
Expand Down
41 changes: 40 additions & 1 deletion team.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ type Team struct {
}

type APIKey struct {
Key string `json:"key"`
Key string `json:"key"`
Comment string `json:"comment"`
Created int `json:"created"`
LastUsed int `json:"lastUsed"`
MaskedKey string `json:"maskedKey"`
}

type TeamService struct {
Expand Down Expand Up @@ -61,6 +65,41 @@ func (ts TeamService) GenerateAPIKey(ctx context.Context, teamUUID uuid.UUID) (k
return
}

func (ts TeamService) DeleteAPIKey(ctx context.Context, key string) (err error) {
req, err := ts.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/team/key/%s", key))
if err != nil {
return
}
_, err = ts.client.doRequest(req, nil)
return
}

func (ts TeamService) UpdateAPIKeyComment(ctx context.Context, key, comment string) (commentOut string, err error) {
req, err := ts.client.newRequest(ctx, http.MethodPost, fmt.Sprintf("/api/v1/team/key/%s/comment", key), withBody(comment))
if err != nil {
return
}
var apiKey APIKey
_, err = ts.client.doRequest(req, &apiKey)
commentOut = apiKey.Comment
return
}

func (ts TeamService) GetAPIKeys(ctx context.Context, teamUUID uuid.UUID) (keys []APIKey, err error) {
keys = []APIKey{}
err = ForEach(
func(po PageOptions) (Page[Team], error) { return ts.GetAll(ctx, po) },
func(item Team) error {
if item.UUID != teamUUID {
return nil
}
keys = append(keys, item.APIKeys...)
return nil
},
)
return keys, err
}

func (ts TeamService) Create(ctx context.Context, team Team) (t Team, err error) {
req, err := ts.client.newRequest(ctx, http.MethodPut, "/api/v1/team", withBody(team))
if err != nil {
Expand Down
77 changes: 77 additions & 0 deletions team_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package dtrack

import (
"context"
"github.com/stretchr/testify/require"
"testing"
)

func TestGenerateAPIKey(t *testing.T) {
client := setUpContainer(t, testContainerOptions{
APIPermissions: []string{
PermissionAccessManagement,
},
})

team, err := client.Team.Create(context.Background(), Team{
Name: "GenerateAPIKey",
})
require.NoError(t, err)

key, err := client.Team.GenerateAPIKey(context.Background(), team.UUID)
require.NoError(t, err)

keys, err := client.Team.GetAPIKeys(context.Background(), team.UUID)
require.NoError(t, err)
require.Equal(t, len(keys), 1)
require.Equal(t, keys[0].Key, key)
}

func TestDeleteAPIKey(t *testing.T) {
client := setUpContainer(t, testContainerOptions{
APIPermissions: []string{
PermissionAccessManagement,
},
})

team, err := client.Team.Create(context.Background(), Team{
Name: "DeleteAPIKey",
})
require.NoError(t, err)

key, err := client.Team.GenerateAPIKey(context.Background(), team.UUID)
require.NoError(t, err)

err = client.Team.DeleteAPIKey(context.Background(), key)
require.NoError(t, err)

keys, err := client.Team.GetAPIKeys(context.Background(), team.UUID)
require.NoError(t, err)
require.Empty(t, keys)
}

func TestUpdateAPIKeyComment(t *testing.T) {
client := setUpContainer(t, testContainerOptions{
APIPermissions: []string{
PermissionAccessManagement,
},
})

team, err := client.Team.Create(context.Background(), Team{
Name: "UpdateAPIKeyComment",
})
require.NoError(t, err)

key, err := client.Team.GenerateAPIKey(context.Background(), team.UUID)
require.NoError(t, err)

comment, err := client.Team.UpdateAPIKeyComment(context.Background(), key, "test-comment")
require.NoError(t, err)
require.Equal(t, comment, "test-comment")

keys, err := client.Team.GetAPIKeys(context.Background(), team.UUID)
require.NoError(t, err)
require.Equal(t, len(keys), 1)
require.Equal(t, keys[0].Key, key)
require.Equal(t, keys[0].Comment, "test-comment")
}

0 comments on commit b56de7d

Please sign in to comment.