Skip to content

Commit

Permalink
Merge pull request #6 from datadrivers/feature/blobstores
Browse files Browse the repository at this point in the history
Support Blobstores
  • Loading branch information
anmoel authored Feb 4, 2020
2 parents ab334ce + 852d00c commit d889fb7
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 0 deletions.
105 changes: 105 additions & 0 deletions blobstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package client

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

const (
blobstoreAPIEndpoint = "service/rest/beta/blobstores"

BlobstoreTypeFile = "File"
BlobstoreTypeS3 = "S3"
)

// Blobstore data
type Blobstore struct {
AvailableSpaceInBytes int `json:"availableSpaceInBytes"`
BlobCount int `json:"blobCount"`
Name string `json:"name"`
Path string `json:"path"`
TotalSizeInBytes int `json:"totalSizeInBytes"`
Type string `json:"type"`

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

// BlobstoreSoftQuota data
type BlobstoreSoftQuota struct {
Limit int `json:"limit"`
Type string `json:"type"`
}

func (c client) BlobstoreCreate(bs Blobstore) error {
ioReader, err := jsonMarshalInterfaceToIOReader(bs)
if err != nil {
return err
}

body, resp, err := c.Post(fmt.Sprintf("%s/%s", blobstoreAPIEndpoint, strings.ToLower(bs.Type)), ioReader)
if err != nil {
return err
}

if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("could not create blobstore \"%s\": HTTP: %d, %s", bs.Name, resp.StatusCode, string(body))
}

return nil
}

func (c client) BlobstoreRead(id string) (*Blobstore, 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 read blobstores: HTTP: %d, %s", resp.StatusCode, string(body))
}

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

for _, bs := range blobstores {
if bs.Name == id {
return &bs, nil
}
}

return nil, nil
}

func (c client) BlobstoreUpdate(id string, bs Blobstore) error {
ioReader, err := jsonMarshalInterfaceToIOReader(bs)
if err != nil {
return err
}

body, resp, err := c.Put(fmt.Sprintf("%s/%s/%s", blobstoreAPIEndpoint, strings.ToLower(bs.Type), id), ioReader)
if err != nil {
return err
}

if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("could not update blobstore \"%s\": HTTP %d, %s", id, resp.StatusCode, string(body))
}

return nil
}

func (c client) BlobstoreDelete(id string) error {
body, resp, err := c.Delete(fmt.Sprintf("%s/%s", blobstoreAPIEndpoint, id))
if err != nil {
return err
}

if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("could not delete blobstore \"%s\": HTTP: %d, %s", id, resp.StatusCode, string(body))
}
return nil
}
64 changes: 64 additions & 0 deletions blobstore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package client

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBlobstoreFile(t *testing.T) {
client := NewClient(getDefaultConfig())

bsName := "test-blobstore-name"
bsPath := "test-blobstore-path"
bsType := BlobstoreTypeFile

bs := Blobstore{
Name: bsName,
Path: bsPath,
Type: bsType,
}

createErr := client.BlobstoreCreate(bs)
assert.Nil(t, createErr)

bsCreated, err := client.BlobstoreRead(bs.Name)
assert.Nil(t, err)
assert.NotNil(t, bsCreated)
// Path not returned by API, not possible to test :-/
// assert.Equal(t, bsPath, bsCreated.Path)
assert.Equal(t, bsType, bsCreated.Type)
assert.Equal(t, 0, bsCreated.BlobCount)
assert.Nil(t, bsCreated.BlobstoreSoftQuota)

bsCreated.BlobstoreSoftQuota = &BlobstoreSoftQuota{
Type: "spaceRemainingQuota",
Limit: 100000000,
}
err = client.BlobstoreUpdate(bsCreated.Name, *bsCreated)
assert.Nil(t, err)

bsUpdated, err := client.BlobstoreRead(bsCreated.Name)
assert.Nil(t, err)
assert.NotNil(t, bsUpdated)
assert.NotNil(t, bsUpdated.BlobstoreSoftQuota)

if createErr == nil {
err := client.BlobstoreDelete(bs.Name)
assert.Nil(t, err)
}
}

func TestBlobstoreRead(t *testing.T) {
client := NewClient(getDefaultConfig())

bsName := "default"

bs, err := client.BlobstoreRead(bsName)
assert.Nil(t, err)
assert.NotNil(t, bs)

if bs != nil {
assert.Equal(t, bsName, bs.Name)
}
}
4 changes: 4 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type Client interface {
ContentType() string
ContentTypeTextPlain()
ContentTypeJSON()
BlobstoreCreate(Blobstore) error
BlobstoreRead(string) (*Blobstore, error)
BlobstoreUpdate(string, Blobstore) error
BlobstoreDelete(string) error
RepositoryCreate(Repository, string, string) error
RepositoryUpdate(string, Repository, string, string) error
RepositoryRead(string) (*Repository, error)
Expand Down

0 comments on commit d889fb7

Please sign in to comment.