Skip to content

Commit

Permalink
Merge pull request #75 from datadrivers/feat/list-blobstores
Browse files Browse the repository at this point in the history
feat(blobstore): Add List function for Blobstores
  • Loading branch information
anmoel authored Dec 29, 2021
2 parents e78be33 + e07e4e4 commit b621b19
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
23 changes: 23 additions & 0 deletions nexus3/pkg/blobstore/service.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package blobstore

import (
"encoding/json"
"fmt"
"net/http"

"github.com/datadrivers/go-nexus-client/nexus3/pkg/client"
"github.com/datadrivers/go-nexus-client/nexus3/schema/blobstore"
)

const (
Expand Down Expand Up @@ -38,6 +40,27 @@ func (s *BlobStoreService) Delete(name string) error {
return deleteBlobstore(s.Client, name)
}

func (s *BlobStoreService) List() ([]blobstore.Generic, error) {
return listBlobstores(s.Client)
}

func listBlobstores(c *client.Client) ([]blobstore.Generic, error) {
body, resp, err := c.Get(blobstoreAPIEndpoint, nil)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not list blobstores: HTTP: %d, %s", resp.StatusCode, string(body))
}

var genericBlobstores []blobstore.Generic
if err := json.Unmarshal(body, &genericBlobstores); err != nil {
return nil, fmt.Errorf("could not unmarshal list of generic blobstores: %v", err)
}
return genericBlobstores, nil
}

func deleteBlobstore(c *client.Client, name string) error {
body, resp, err := c.Delete(fmt.Sprintf("%s/%s", blobstoreAPIEndpoint, name))
if err != nil {
Expand Down
15 changes: 13 additions & 2 deletions nexus3/pkg/blobstore/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ func getDefaultConfig() client.Config {
}

func TestNewBlobStoreService(t *testing.T) {
c := client.NewClient(getDefaultConfig())
service := getTestService()

assert.NotNil(t, c, "NewClient() must not return nil")
assert.NotNil(t, service, "NewBlobStoreService() must not return nil")
}

func TestListBlobstores(t *testing.T) {
service := getTestService()
blobstores, err := service.List()

assert.Nil(t, err)
assert.Equal(t, "default", blobstores[0].Name)
assert.Equal(t, "File", blobstores[0].Type)
assert.Equal(t, false, blobstores[0].Unavailable)
assert.Equal(t, 0, blobstores[0].BlobCount)
}
14 changes: 14 additions & 0 deletions nexus3/schema/blobstore/generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package blobstore

// Generic data
type Generic struct {
Name string `json:"name"`
Type string `json:"type"`

Unavailable bool `json:"unavailable"`
AvailableSpaceInBytes int `json:"availableSpaceInBytes"`
BlobCount int `json:"blobCount"`
TotalSizeInBytes int `json:"totalSizeInBytes"`

*SoftQuota `json:"softQuota,omitempty"`
}

0 comments on commit b621b19

Please sign in to comment.