2020-08-24 14:07:08 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-08-24 14:07:08 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/container"
|
2021-05-25 15:46:50 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
2020-08-24 14:07:08 +00:00
|
|
|
)
|
|
|
|
|
2021-05-25 15:46:50 +00:00
|
|
|
// FIXME: (temp solution) we need to pass session token from header
|
2021-05-25 16:23:11 +00:00
|
|
|
type ContextWithToken struct {
|
2021-05-25 15:46:50 +00:00
|
|
|
context.Context
|
|
|
|
|
|
|
|
SessionToken *session.SessionToken
|
|
|
|
}
|
|
|
|
|
2020-08-24 14:07:08 +00:00
|
|
|
type ServiceExecutor interface {
|
2021-05-25 16:23:11 +00:00
|
|
|
Put(ContextWithToken, *container.PutRequestBody) (*container.PutResponseBody, error)
|
2021-05-26 11:08:24 +00:00
|
|
|
Delete(ContextWithToken, *container.DeleteRequestBody) (*container.DeleteResponseBody, error)
|
2020-08-24 14:07:08 +00:00
|
|
|
Get(context.Context, *container.GetRequestBody) (*container.GetResponseBody, error)
|
|
|
|
List(context.Context, *container.ListRequestBody) (*container.ListResponseBody, error)
|
2021-05-25 16:25:14 +00:00
|
|
|
SetExtendedACL(ContextWithToken, *container.SetExtendedACLRequestBody) (*container.SetExtendedACLResponseBody, error)
|
2020-08-24 14:07:08 +00:00
|
|
|
GetExtendedACL(context.Context, *container.GetExtendedACLRequestBody) (*container.GetExtendedACLResponseBody, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type executorSvc struct {
|
2021-03-15 10:53:08 +00:00
|
|
|
Server
|
2021-02-01 12:43:09 +00:00
|
|
|
|
2020-08-24 14:07:08 +00:00
|
|
|
exec ServiceExecutor
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewExecutionService wraps ServiceExecutor and returns Container Service interface.
|
2021-03-15 10:53:08 +00:00
|
|
|
func NewExecutionService(exec ServiceExecutor) Server {
|
2020-08-24 14:07:08 +00:00
|
|
|
return &executorSvc{
|
2020-10-22 11:12:35 +00:00
|
|
|
exec: exec,
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 16:23:11 +00:00
|
|
|
func contextWithTokenFromRequest(ctx context.Context, req interface {
|
|
|
|
GetMetaHeader() *session.RequestMetaHeader
|
|
|
|
}) ContextWithToken {
|
2021-05-25 15:46:50 +00:00
|
|
|
var tok *session.SessionToken
|
|
|
|
|
|
|
|
for meta := req.GetMetaHeader(); meta != nil; meta = meta.GetOrigin() {
|
|
|
|
tok = meta.GetSessionToken()
|
|
|
|
}
|
|
|
|
|
2021-05-25 16:23:11 +00:00
|
|
|
return ContextWithToken{
|
2021-05-25 15:46:50 +00:00
|
|
|
Context: ctx,
|
|
|
|
SessionToken: tok,
|
2021-05-25 16:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *executorSvc) Put(ctx context.Context, req *container.PutRequest) (*container.PutResponse, error) {
|
|
|
|
respBody, err := s.exec.Put(contextWithTokenFromRequest(ctx, req), req.GetBody())
|
2020-08-24 14:07:08 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not execute Put request: %w", err)
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp := new(container.PutResponse)
|
|
|
|
resp.SetBody(respBody)
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *executorSvc) Delete(ctx context.Context, req *container.DeleteRequest) (*container.DeleteResponse, error) {
|
2021-05-26 11:08:24 +00:00
|
|
|
respBody, err := s.exec.Delete(contextWithTokenFromRequest(ctx, req), req.GetBody())
|
2020-08-24 14:07:08 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not execute Delete request: %w", err)
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp := new(container.DeleteResponse)
|
|
|
|
resp.SetBody(respBody)
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *executorSvc) Get(ctx context.Context, req *container.GetRequest) (*container.GetResponse, error) {
|
|
|
|
respBody, err := s.exec.Get(ctx, req.GetBody())
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not execute Get request: %w", err)
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp := new(container.GetResponse)
|
|
|
|
resp.SetBody(respBody)
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *executorSvc) List(ctx context.Context, req *container.ListRequest) (*container.ListResponse, error) {
|
|
|
|
respBody, err := s.exec.List(ctx, req.GetBody())
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not execute List request: %w", err)
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp := new(container.ListResponse)
|
|
|
|
resp.SetBody(respBody)
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *executorSvc) SetExtendedACL(ctx context.Context, req *container.SetExtendedACLRequest) (*container.SetExtendedACLResponse, error) {
|
2021-05-25 16:25:14 +00:00
|
|
|
respBody, err := s.exec.SetExtendedACL(contextWithTokenFromRequest(ctx, req), req.GetBody())
|
2020-08-24 14:07:08 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not execute SetEACL request: %w", err)
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp := new(container.SetExtendedACLResponse)
|
|
|
|
resp.SetBody(respBody)
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *executorSvc) GetExtendedACL(ctx context.Context, req *container.GetExtendedACLRequest) (*container.GetExtendedACLResponse, error) {
|
|
|
|
respBody, err := s.exec.GetExtendedACL(ctx, req.GetBody())
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not execute GetEACL request: %w", err)
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp := new(container.GetExtendedACLResponse)
|
|
|
|
resp.SetBody(respBody)
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|