Skip to content

Commit

Permalink
Update func names and permission type
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-aaron committed Jan 10, 2025
1 parent bf31705 commit 079c1c0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 39 deletions.
53 changes: 27 additions & 26 deletions spaces_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const spacesKeysBasePath = "v2/spaces/keys"

// SpacesKeysService is an interface for managing Spaces keys with the DigitalOcean API.
type SpacesKeysService interface {
ListSpacesKeys(context.Context, *ListOptions) ([]*SpacesKey, *Response, error)
UpdateSpacesKey(context.Context, string, *SpacesKeyUpdateRequest) (*SpacesKey, *Response, error)
CreateSpacesKey(context.Context, *SpacesKeyCreateRequest) (*SpacesKey, *Response, error)
DeleteSpacesKey(context.Context, string) (*Response, error)
List(context.Context, *ListOptions) ([]*SpacesKey, *Response, error)
Update(context.Context, string, *SpacesKeyUpdateRequest) (*SpacesKey, *Response, error)
Create(context.Context, *SpacesKeyCreateRequest) (*SpacesKey, *Response, error)
Delete(context.Context, string) (*Response, error)
}

// SpacesKeysServiceOp handles communication with the Spaces key related methods of the
Expand All @@ -24,30 +24,31 @@ type SpacesKeysServiceOp struct {

var _ SpacesKeysService = &SpacesKeysServiceOp{}

// Permission represents a permission for a Spaces grant
type Permission string
// SpacesKeyPermission represents a permission for a Spaces grant
type SpacesKeyPermission string

const (
// PermissionRead grants read-only access to the Spaces bucket
PermissionRead Permission = "read"
// PermissionReadWrite grants read and write access to the Spaces bucket
PermissionReadWrite Permission = "readwrite"
// PermissionFullAccess grants full access to the Spaces bucket
PermissionFullAccess Permission = "fullaccess"
// SpacesKeyRead grants read-only access to the Spaces bucket
SpacesKeyRead SpacesKeyPermission = "read"
// SpacesKeyReadWrite grants read and write access to the Spaces bucket
SpacesKeyReadWrite SpacesKeyPermission = "readwrite"
// SpacesKeyFullAccess grants full access to the Spaces bucket
SpacesKeyFullAccess SpacesKeyPermission = "fullaccess"
)

// Grant represents a Grant for a Spaces key
type Grant struct {
Bucket string `json:"bucket"`
Permission Permission `json:"permission"`
Bucket string `json:"bucket"`
Permission SpacesKeyPermission `json:"permission"`
}

// SpacesKey represents a DigitalOcean Spaces key
type SpacesKey struct {
Name string `json:"name"`
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
CreatedAt string `json:"created_at"`
Name string `json:"name"`
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Grants []*Grant `json:"grants"`
CreatedAt string `json:"created_at"`
}

// SpacesKeyRoot represents a response from the DigitalOcean API
Expand All @@ -74,8 +75,8 @@ type spacesListKeysRoot struct {
Meta *Meta `json:"meta"`
}

// CreateSpacesKey implements SpacesKeysService.
func (s *SpacesKeysServiceOp) CreateSpacesKey(ctx context.Context, createRequest *SpacesKeyCreateRequest) (*SpacesKey, *Response, error) {
// Create creates a new Spaces key.
func (s *SpacesKeysServiceOp) Create(ctx context.Context, createRequest *SpacesKeyCreateRequest) (*SpacesKey, *Response, error) {
if createRequest == nil {
return nil, nil, NewArgError("createRequest", "cannot be nil")
}
Expand All @@ -94,8 +95,8 @@ func (s *SpacesKeysServiceOp) CreateSpacesKey(ctx context.Context, createRequest
return root.Key, resp, nil
}

// DeleteSpacesKey implements SpacesKeysService.
func (s *SpacesKeysServiceOp) DeleteSpacesKey(ctx context.Context, accessKey string) (*Response, error) {
// Delete deletes a Spaces key.
func (s *SpacesKeysServiceOp) Delete(ctx context.Context, accessKey string) (*Response, error) {
if accessKey == "" {
return nil, NewArgError("accessKey", "cannot be empty")
}
Expand All @@ -113,8 +114,8 @@ func (s *SpacesKeysServiceOp) DeleteSpacesKey(ctx context.Context, accessKey str
return resp, nil
}

// UpdateSpacesKey
func (s *SpacesKeysServiceOp) UpdateSpacesKey(ctx context.Context, accessKey string, updateRequest *SpacesKeyUpdateRequest) (*SpacesKey, *Response, error) {
// Update updates a Spaces key.
func (s *SpacesKeysServiceOp) Update(ctx context.Context, accessKey string, updateRequest *SpacesKeyUpdateRequest) (*SpacesKey, *Response, error) {
if accessKey == "" {
return nil, nil, NewArgError("accessKey", "cannot be empty")
}
Expand All @@ -136,8 +137,8 @@ func (s *SpacesKeysServiceOp) UpdateSpacesKey(ctx context.Context, accessKey str
return root.Key, resp, nil
}

// ListSpacesKeys returns a list of Spaces keys.
func (s *SpacesKeysServiceOp) ListSpacesKeys(ctx context.Context, opts *ListOptions) ([]*SpacesKey, *Response, error) {
// List returns a list of Spaces keys.
func (s *SpacesKeysServiceOp) List(ctx context.Context, opts *ListOptions) ([]*SpacesKey, *Response, error) {
path, err := addOptions(spacesKeysBasePath, opts)
if err != nil {
return nil, nil, err
Expand Down
41 changes: 28 additions & 13 deletions spaces_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,26 @@ func TestSpacesKeyCreate(t *testing.T) {
Grants: []*Grant{
{
Bucket: "test-bucket",
Permission: PermissionRead,
Permission: SpacesKeyRead,
},
},
}

mux.HandleFunc("/v2/spaces/keys", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, `{"key":{"name":"test-key","access_key":"test-access-key","secret_key":"test-secret-key","created_at":"2023-10-01T00:00:00Z"}}`)
fmt.Fprint(w, `{"key":{"name":"test-key","access_key":"test-access-key","secret_key":"test-secret-key","created_at":"2023-10-01T00:00:00Z","grants":[{"bucket":"test-bucket","permission":"read"}]}}`)
})

key, resp, err := client.SpacesKeys.CreateSpacesKey(context.Background(), createRequest)
key, resp, err := client.SpacesKeys.Create(context.Background(), createRequest)
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, "test-key", key.Name)
assert.Equal(t, "test-access-key", key.AccessKey)
assert.Equal(t, "test-secret-key", key.SecretKey)
assert.Len(t, key.Grants, 1)
assert.Equal(t, "test-bucket", key.Grants[0].Bucket)
assert.Equal(t, SpacesKeyRead, key.Grants[0].Permission)
}

func TestSpacesKeyDelete(t *testing.T) {
Expand All @@ -46,7 +49,7 @@ func TestSpacesKeyDelete(t *testing.T) {
w.WriteHeader(http.StatusNoContent)
})

resp, err := client.SpacesKeys.DeleteSpacesKey(context.Background(), "test-access-key")
resp, err := client.SpacesKeys.Delete(context.Background(), "test-access-key")
assert.NoError(t, err)
assert.NotNil(t, resp)
}
Expand All @@ -60,22 +63,25 @@ func TestSpacesKeyUpdate(t *testing.T) {
Grants: []*Grant{
{
Bucket: "updated-bucket",
Permission: PermissionReadWrite,
Permission: SpacesKeyReadWrite,
},
},
}

mux.HandleFunc("/v2/spaces/keys/test-access-key", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method)
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"key":{"name":"updated-key","access_key":"test-access-key","created_at":"2023-10-01T00:00:00Z"}}`)
fmt.Fprint(w, `{"key":{"name":"updated-key","access_key":"test-access-key","created_at":"2023-10-01T00:00:00Z","grants":[{"bucket":"updated-bucket","permission":"readwrite"}]}}`)
})

key, resp, err := client.SpacesKeys.UpdateSpacesKey(context.Background(), "test-access-key", updateRequest)
key, resp, err := client.SpacesKeys.Update(context.Background(), "test-access-key", updateRequest)
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, "updated-key", key.Name)
assert.Equal(t, "test-access-key", key.AccessKey)
assert.Len(t, key.Grants, 1)
assert.Equal(t, "updated-bucket", key.Grants[0].Bucket)
assert.Equal(t, SpacesKeyReadWrite, key.Grants[0].Permission)
}

func TestSpacesKeyList(t *testing.T) {
Expand All @@ -85,15 +91,18 @@ func TestSpacesKeyList(t *testing.T) {
mux.HandleFunc("/v2/spaces/keys", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"keys":[{"name":"test-key","access_key":"test-access-key","created_at":"2023-10-01T00:00:00Z"}]}`)
fmt.Fprint(w, `{"keys":[{"name":"test-key","access_key":"test-access-key","created_at":"2023-10-01T00:00:00Z","grants":[{"bucket":"test-bucket","permission":"read"}]}]}`)
})

keys, resp, err := client.SpacesKeys.ListSpacesKeys(context.Background(), nil)
keys, resp, err := client.SpacesKeys.List(context.Background(), nil)
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Len(t, keys, 1)
assert.Equal(t, "test-key", keys[0].Name)
assert.Equal(t, "test-access-key", keys[0].AccessKey)
assert.Len(t, keys[0].Grants, 1)
assert.Equal(t, "test-bucket", keys[0].Grants[0].Bucket)
assert.Equal(t, SpacesKeyRead, keys[0].Grants[0].Permission)
}

func TestSpacesKeyList_Pagination(t *testing.T) {
Expand All @@ -105,26 +114,32 @@ func TestSpacesKeyList_Pagination(t *testing.T) {
page := r.URL.Query().Get("page")
if page == "2" {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"keys":[{"name":"test-key-2","access_key":"test-access-key-2","created_at":"2023-10-02T00:00:00Z"}]}`)
fmt.Fprint(w, `{"keys":[{"name":"test-key-2","access_key":"test-access-key-2","created_at":"2023-10-02T00:00:00Z","grants":[{"bucket":"test-bucket-2","permission":"readwrite"}]}]}`)
} else {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"keys":[{"name":"test-key-1","access_key":"test-access-key-1","created_at":"2023-10-01T00:00:00Z"}]}`)
fmt.Fprint(w, `{"keys":[{"name":"test-key-1","access_key":"test-access-key-1","created_at":"2023-10-01T00:00:00Z","grants":[{"bucket":"test-bucket-1","permission":"read"}]}]}`)
}
})

// Test first page
keys, resp, err := client.SpacesKeys.ListSpacesKeys(context.Background(), &ListOptions{Page: 1})
keys, resp, err := client.SpacesKeys.List(context.Background(), &ListOptions{Page: 1})
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Len(t, keys, 1)
assert.Equal(t, "test-key-1", keys[0].Name)
assert.Equal(t, "test-access-key-1", keys[0].AccessKey)
assert.Len(t, keys[0].Grants, 1)
assert.Equal(t, "test-bucket-1", keys[0].Grants[0].Bucket)
assert.Equal(t, SpacesKeyRead, keys[0].Grants[0].Permission)

// Test second page
keys, resp, err = client.SpacesKeys.ListSpacesKeys(context.Background(), &ListOptions{Page: 2})
keys, resp, err = client.SpacesKeys.List(context.Background(), &ListOptions{Page: 2})
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Len(t, keys, 1)
assert.Equal(t, "test-key-2", keys[0].Name)
assert.Equal(t, "test-access-key-2", keys[0].AccessKey)
assert.Len(t, keys[0].Grants, 1)
assert.Equal(t, "test-bucket-2", keys[0].Grants[0].Bucket)
assert.Equal(t, SpacesKeyReadWrite, keys[0].Grants[0].Permission)
}

0 comments on commit 079c1c0

Please sign in to comment.