From 2368e7c936c4fb833d1689d5734239690387c2bb Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Sun, 25 Jan 2015 15:35:08 -0800 Subject: [PATCH] Adding error support to all commands in storage interface --- pkg/storage/inmemory/inmemory.go | 10 +++++----- pkg/storage/storage.go | 6 +++--- pkg/storage/storage_api_suite.go | 9 ++++++--- pkg/webapi/minioapi/minioapi.go | 14 ++++++++++++-- pkg/webapi/minioapi/minioapi_test.go | 14 ++++++++++---- 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/pkg/storage/inmemory/inmemory.go b/pkg/storage/inmemory/inmemory.go index ba45f027f..2577a4146 100644 --- a/pkg/storage/inmemory/inmemory.go +++ b/pkg/storage/inmemory/inmemory.go @@ -86,12 +86,12 @@ func (storage *storage) StoreBucket(bucketName string) error { return nil } -func (storage *storage) ListObjects(bucket, prefix string, count int) ([]mstorage.ObjectMetadata, bool) { +func (storage *storage) ListObjects(bucket, prefix string, count int) ([]mstorage.ObjectMetadata, bool, error) { // TODO prefix and count handling var results []mstorage.ObjectMetadata for key, object := range storage.objectdata { if len(results) == count { - return results, true + return results, true, nil } if bucket == object.metadata.Bucket { if strings.HasPrefix(key, bucket+":") { @@ -99,16 +99,16 @@ func (storage *storage) ListObjects(bucket, prefix string, count int) ([]mstorag } } } - return results, false + return results, false, nil } -func (storage *storage) ListBuckets(prefix string) []mstorage.BucketMetadata { +func (storage *storage) ListBuckets(prefix string) ([]mstorage.BucketMetadata, error) { // TODO prefix handling var results []mstorage.BucketMetadata for _, bucket := range storage.bucketdata { results = append(results, bucket.metadata) } - return results + return results, nil } func Start() (chan<- string, <-chan error, *storage) { diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index b3a143556..c9743cca3 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -24,14 +24,14 @@ import ( type Storage interface { // Bucket Operations + ListBuckets(prefix string) ([]BucketMetadata, error) StoreBucket(bucket string) error - ListBuckets(prefix string) []BucketMetadata // Object Operations - GetObjectMetadata(bucket string, object string) (ObjectMetadata, error) CopyObjectToWriter(w io.Writer, bucket string, object string) (int64, error) + GetObjectMetadata(bucket string, object string) (ObjectMetadata, error) + ListObjects(bucket, prefix string, count int) ([]ObjectMetadata, bool, error) StoreObject(bucket string, key string, data io.Reader) error - ListObjects(bucket, prefix string, count int) ([]ObjectMetadata, bool) } type BucketMetadata struct { diff --git a/pkg/storage/storage_api_suite.go b/pkg/storage/storage_api_suite.go index d4e871fa2..546f47a0b 100644 --- a/pkg/storage/storage_api_suite.go +++ b/pkg/storage/storage_api_suite.go @@ -61,22 +61,25 @@ func testPaging(c *C, create func() Storage) { storage := create() storage.StoreBucket("bucket") storage.ListObjects("bucket", "", 1000) - objects, isTruncated := storage.ListObjects("bucket", "", 1000) + objects, isTruncated, err := storage.ListObjects("bucket", "", 1000) c.Assert(len(objects), Equals, 0) c.Assert(isTruncated, Equals, false) + c.Assert(err, IsNil) for i := 1; i <= 1000; i++ { key := "obj" + strconv.Itoa(i) storage.StoreObject("bucket", key, bytes.NewBufferString(key)) - objects, isTruncated = storage.ListObjects("bucket", "", 1000) + objects, isTruncated, err = storage.ListObjects("bucket", "", 1000) c.Assert(len(objects), Equals, i) c.Assert(isTruncated, Equals, false) + c.Assert(err, IsNil) } for i := 1001; i <= 2000; i++ { key := "obj" + strconv.Itoa(i) storage.StoreObject("bucket", key, bytes.NewBufferString(key)) - objects, isTruncated = storage.ListObjects("bucket", "", 1000) + objects, isTruncated, err = storage.ListObjects("bucket", "", 1000) c.Assert(len(objects), Equals, 1000) c.Assert(isTruncated, Equals, true) + c.Assert(err, IsNil) } } diff --git a/pkg/webapi/minioapi/minioapi.go b/pkg/webapi/minioapi/minioapi.go index 07fdb6976..5c7e8d7e5 100644 --- a/pkg/webapi/minioapi/minioapi.go +++ b/pkg/webapi/minioapi/minioapi.go @@ -153,7 +153,12 @@ func (server *minioApi) listBucketsHandler(w http.ResponseWriter, req *http.Requ contentType = jsonType } } - buckets := server.storage.ListBuckets(prefix) + buckets, err := server.storage.ListBuckets(prefix) + if err != nil { + log.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } response := generateBucketsListResult(buckets) var bytesBuffer bytes.Buffer @@ -189,7 +194,12 @@ func (server *minioApi) listObjectsHandler(w http.ResponseWriter, req *http.Requ } } - objects, isTruncated := server.storage.ListObjects(bucket, prefix, 1000) + objects, isTruncated, err := server.storage.ListObjects(bucket, prefix, 1000) + if err != nil { + log.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } response := generateObjectsListResult(bucket, objects, isTruncated) var bytesBuffer bytes.Buffer diff --git a/pkg/webapi/minioapi/minioapi_test.go b/pkg/webapi/minioapi/minioapi_test.go index 7fa8b7af8..debe2900f 100644 --- a/pkg/webapi/minioapi/minioapi_test.go +++ b/pkg/webapi/minioapi/minioapi_test.go @@ -214,8 +214,9 @@ func (s *MySuite) TestPutBucket(c *C) { testServer := httptest.NewServer(httpHandler) defer testServer.Close() - buckets := storage.ListBuckets("bucket") + buckets, err := storage.ListBuckets("bucket") c.Assert(len(buckets), Equals, 0) + c.Assert(err, IsNil) request, err := http.NewRequest("PUT", testServer.URL+"/bucket/", bytes.NewBufferString("")) c.Assert(err, IsNil) @@ -226,8 +227,9 @@ func (s *MySuite) TestPutBucket(c *C) { c.Assert(response.StatusCode, Equals, http.StatusOK) // check bucket exists - buckets = storage.ListBuckets("bucket") + buckets, err = storage.ListBuckets("bucket") c.Assert(len(buckets), Equals, 1) + c.Assert(err, IsNil) c.Assert(buckets[0].Name, Equals, "bucket") } @@ -237,8 +239,10 @@ func (s *MySuite) TestPutObject(c *C) { testServer := httptest.NewServer(httpHandler) defer testServer.Close() - objects, _ := storage.ListObjects("bucket", "", 1000) + objects, isTruncated, err := storage.ListObjects("bucket", "", 1000) c.Assert(len(objects), Equals, 0) + c.Assert(isTruncated, Equals, false) + c.Assert(err, IsNil) date1 := time.Now() @@ -260,8 +264,10 @@ func (s *MySuite) TestPutObject(c *C) { date2 := time.Now() - objects, _ = storage.ListObjects("bucket", "", 1000) + objects, isTruncated, err = storage.ListObjects("bucket", "", 1000) c.Assert(len(objects), Equals, 1) + c.Assert(isTruncated, Equals, false) + c.Assert(err, IsNil) var writer bytes.Buffer