From ccd481e4e24c7f5778239fc2b0789c69d064f92e Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Tue, 11 Aug 2020 17:36:52 +0300 Subject: [PATCH] Prepare list-buckets handler Signed-off-by: Evgeniy Kulikov --- api/handler/list-buckets.go | 73 +++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 api/handler/list-buckets.go diff --git a/api/handler/list-buckets.go b/api/handler/list-buckets.go new file mode 100644 index 00000000..2002ec27 --- /dev/null +++ b/api/handler/list-buckets.go @@ -0,0 +1,73 @@ +package handler + +import ( + "net/http" + "time" + + "github.com/nspcc-dev/neofs-s3-gate/api" + "github.com/nspcc-dev/neofs-s3-gate/auth" + "go.uber.org/zap" +) + +func (h *handler) ListBucketsHandler(w http.ResponseWriter, r *http.Request) { + var ( + res *ListBucketsResponse + rid = api.GetRequestID(r.Context()) + ) + + tkn, err := auth.GetBearerToken(r.Context()) + if err != nil { + h.log.Error("something went wrong", + zap.String("request_id", rid), + zap.Error(err)) + + api.WriteErrorResponse(r.Context(), w, api.Error{ + Code: api.GetAPIError(api.ErrInternalError).Code, + Description: err.Error(), + HTTPStatusCode: http.StatusInternalServerError, + }, r.URL) + + return + } + + list, err := h.obj.ListBuckets(r.Context()) + if err != nil { + h.log.Error("something went wrong", + zap.String("request_id", rid), + zap.Error(err)) + + api.WriteErrorResponse(r.Context(), w, api.Error{ + Code: api.GetAPIError(api.ErrInternalError).Code, + Description: err.Error(), + HTTPStatusCode: http.StatusInternalServerError, + }, r.URL) + + return + } + + res = &ListBucketsResponse{ + Owner: Owner{ + ID: tkn.OwnerID.String(), + DisplayName: tkn.OwnerID.String(), + }, + } + + for _, item := range list { + res.Buckets.Buckets = append(res.Buckets.Buckets, Bucket{ + Name: item.Name, + CreationDate: item.Created.Format(time.RFC3339), + }) + } + + if err = api.EncodeToResponse(w, res); err != nil { + h.log.Error("something went wrong", + zap.String("request_id", rid), + zap.Error(err)) + + api.WriteErrorResponse(r.Context(), w, api.Error{ + Code: api.GetAPIError(api.ErrInternalError).Code, + Description: err.Error(), + HTTPStatusCode: http.StatusInternalServerError, + }, r.URL) + } +}