Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions planetscale/service_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func (s *serviceTokenService) DeleteAccess(ctx context.Context, delReq *DeleteSe
type CreateServiceTokenRequest struct {
Organization string `json:"-"`
Name *string `json:"name,omitempty"`
TTL *int `json:"ttl,omitempty"`
}

type ListServiceTokenGrantsRequest struct {
Expand Down Expand Up @@ -157,6 +158,7 @@ type ServiceToken struct {
Name *string `json:"name"`
CreatedAt time.Time `json:"created_at"`
LastUsedAt *time.Time `json:"last_used_at"`
ExpiresAt *time.Time `json:"expires_at"`
}

type ServiceTokenGrant struct {
Expand Down
44 changes: 44 additions & 0 deletions planetscale/service_tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,50 @@ func TestServiceTokens_Create(t *testing.T) {
c.Assert(snapshot, qt.DeepEquals, want)
}

func TestServiceTokens_CreateWithTTL(t *testing.T) {
c := qt.New(t)

wantBody := []byte("{\"name\":\"my-token\",\"ttl\":3600}\n")

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
data, err := io.ReadAll(r.Body)
c.Assert(err, qt.IsNil)
c.Assert(data, qt.DeepEquals, wantBody)

out := `{"id":"test-id","type":"ServiceToken","token":"d2980bbd91a4ab878601ef0573a7af7b1b15e705","name":"my-token","created_at":"2021-01-14T10:19:23.000Z","last_used_at":"2021-01-15T12:30:00.000Z","expires_at":"2021-01-14T11:19:23.000Z"}`
_, err = w.Write([]byte(out))
c.Assert(err, qt.IsNil)
}))

client, err := NewClient(WithBaseURL(ts.URL))
c.Assert(err, qt.IsNil)

ctx := context.Background()
tokenName := "my-token"
ttl := 3600

snapshot, err := client.ServiceTokens.Create(ctx, &CreateServiceTokenRequest{
Organization: testOrg,
Name: &tokenName,
TTL: &ttl,
})
lastUsedAt := time.Date(2021, 1, 15, 12, 30, 0, 0, time.UTC)
expiresAt := time.Date(2021, 1, 14, 11, 19, 23, 0, time.UTC)
want := &ServiceToken{
ID: "test-id",
Type: "ServiceToken",
Token: "d2980bbd91a4ab878601ef0573a7af7b1b15e705",
Name: &tokenName,
CreatedAt: time.Date(2021, 1, 14, 10, 19, 23, 0, time.UTC),
LastUsedAt: &lastUsedAt,
ExpiresAt: &expiresAt,
}

c.Assert(err, qt.IsNil)
c.Assert(snapshot, qt.DeepEquals, want)
}

func TestServiceTokens_ListGrants(t *testing.T) {
c := qt.New(t)

Expand Down