From 1ca3e6e04b249b3fcb3b62fbb94fdb6e8de12327 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Mon, 27 Jul 2020 09:49:23 +0300 Subject: [PATCH] NFSSVC-27 Simplify ListBuckets handler --- neofs/api/handler/container.go | 83 ++++++++++++++++------------------ 1 file changed, 38 insertions(+), 45 deletions(-) diff --git a/neofs/api/handler/container.go b/neofs/api/handler/container.go index 3f3fe04c..f87cc156 100644 --- a/neofs/api/handler/container.go +++ b/neofs/api/handler/container.go @@ -43,7 +43,6 @@ type ( cnrInfoParams struct { cid refs.CID - con *grpc.ClientConn tkn *service.BearerTokenMsg } ) @@ -51,6 +50,7 @@ type ( func (h *handler) getContainerInfo(ctx context.Context, p cnrInfoParams) (*Bucket, error) { var ( err error + con *grpc.ClientConn res *container.GetResponse ) @@ -59,9 +59,11 @@ func (h *handler) getContainerInfo(ctx context.Context, p cnrInfoParams) (*Bucke req.SetTTL(service.SingleForwardingTTL) req.SetBearer(p.tkn) - if err = service.SignRequestData(h.key, req); err != nil { + if con, err = h.cli.GetConnection(ctx); err != nil { + return nil, errors.Wrap(err, "could not fetch connection") + } else if err = service.SignRequestData(h.key, req); err != nil { return nil, errors.Wrap(err, "could not sign container info request") - } else if res, err = container.NewServiceClient(p.con).Get(ctx, req); err != nil { + } else if res, err = container.NewServiceClient(con).Get(ctx, req); err != nil { return nil, errors.Wrap(err, "could not fetch container info") } @@ -75,13 +77,34 @@ func (h *handler) getContainerInfo(ctx context.Context, p cnrInfoParams) (*Bucke }, nil } +func (h *handler) getContainerList(ctx context.Context, tkn *service.BearerTokenMsg) ([]refs.CID, error) { + var ( + err error + con *grpc.ClientConn + res *container.ListResponse + ) + + req := new(container.ListRequest) + req.OwnerID = tkn.OwnerID + req.SetTTL(service.SingleForwardingTTL) + req.SetBearer(tkn) + + if con, err = h.cli.GetConnection(ctx); err != nil { + return nil, errors.Wrap(err, "could not fetch connection") + } else if err = service.SignRequestData(h.key, req); err != nil { + return nil, errors.Wrap(err, "could not sign request") + } else if res, err = container.NewServiceClient(con).List(ctx, req); err != nil { + return nil, errors.Wrap(err, "could not fetch list containers") + } + + return res.CID, nil +} + func (h *handler) ListBucketsHandler(w http.ResponseWriter, r *http.Request) { var ( err error - uid = h.uid inf *Bucket - con *grpc.ClientConn - res *container.ListResponse + lst []refs.CID tkn *service.BearerTokenMsg ) @@ -102,43 +125,11 @@ func (h *handler) ListBucketsHandler(w http.ResponseWriter, r *http.Request) { }, r.URL) return - } - - req := new(container.ListRequest) - req.OwnerID = uid - req.SetTTL(service.SingleForwardingTTL) - req.SetBearer(tkn) - // req.SetVersion(APIVersion) ?? - - if con, err = h.cli.GetConnection(ctx); err != nil { - h.log.Error("could not get connection", + } else if lst, err = h.getContainerList(ctx, tkn); err != nil { + h.log.Error("could not fetch bearer token", zap.Error(err)) - e := api.GetAPIError(api.ErrInternalError) - - api.WriteErrorResponse(ctx, w, api.Error{ - Code: e.Code, - Description: err.Error(), - HTTPStatusCode: e.HTTPStatusCode, - }, r.URL) - - return - } else if err = service.SignRequestData(h.key, req); err != nil { - h.log.Error("could not prepare request", - zap.Error(err)) - - e := api.GetAPIError(api.ErrInternalError) - - api.WriteErrorResponse(ctx, w, api.Error{ - Code: e.Code, - Description: err.Error(), - HTTPStatusCode: e.HTTPStatusCode, - }, r.URL) - - return - } else if res, err = container.NewServiceClient(con).List(ctx, req); err != nil { - h.log.Error("could not list buckets", - zap.Error(err)) + // TODO check that error isn't gRPC error e := api.GetAPIError(api.ErrInternalError) @@ -152,13 +143,13 @@ func (h *handler) ListBucketsHandler(w http.ResponseWriter, r *http.Request) { } result := &ListBucketsResponse{Owner: Owner{ - ID: uid.String(), - DisplayName: uid.String(), + ID: tkn.OwnerID.String(), + DisplayName: tkn.OwnerID.String(), }} - params := cnrInfoParams{con: con, tkn: tkn} + params := cnrInfoParams{tkn: tkn} - for _, cid := range res.CID { + for _, cid := range lst { // should receive each container info (??): params.cid = cid @@ -166,6 +157,8 @@ func (h *handler) ListBucketsHandler(w http.ResponseWriter, r *http.Request) { h.log.Error("could not fetch bucket info", zap.Error(err)) + // TODO check that error isn't gRPC error + e := api.GetAPIError(api.ErrInternalError) api.WriteErrorResponse(ctx, w, api.Error{