-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from datadrivers/feature/blobstores
Support Blobstores
- Loading branch information
Showing
3 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters