From 88bb8e2df962949ef558ebf85ff4ea1f5ac3e16a Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Mon, 27 Dec 2021 19:32:25 +0300 Subject: [PATCH] [#1059] control: Add `SetShardMode` implementation to ctrl svc Signed-off-by: Pavel Karpy --- pkg/services/control/server/set_shard_mode.go | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 pkg/services/control/server/set_shard_mode.go diff --git a/pkg/services/control/server/set_shard_mode.go b/pkg/services/control/server/set_shard_mode.go new file mode 100644 index 000000000..86e9c7dce --- /dev/null +++ b/pkg/services/control/server/set_shard_mode.go @@ -0,0 +1,54 @@ +package control + +import ( + "context" + "fmt" + + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" + "github.com/nspcc-dev/neofs-node/pkg/services/control" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +func (s *Server) SetShardMode(_ context.Context, req *control.SetShardModeRequest) (*control.SetShardModeResponse, error) { + // verify request + err := s.isValidRequest(req) + if err != nil { + return nil, status.Error(codes.PermissionDenied, err.Error()) + } + + var ( + mode shard.Mode + + requestedMode = req.GetBody().GetMode() + requestedShard = shard.NewIDFromBytes(req.Body.GetShard_ID()) + ) + + switch requestedMode { + case control.ShardMode_READ_WRITE: + mode = shard.ModeReadWrite + case control.ShardMode_READ_ONLY: + mode = shard.ModeReadOnly + default: + return nil, status.Error(codes.Internal, fmt.Sprintf("unknown shard mode: %s", requestedMode)) + } + + err = s.s.SetShardMode(requestedShard, mode) + if err != nil { + return nil, status.Error(codes.Internal, err.Error()) + } + + // create and fill response + resp := new(control.SetShardModeResponse) + + body := new(control.SetShardModeResponse_Body) + resp.SetBody(body) + + // sign the response + err = SignMessage(s.key, resp) + if err != nil { + return nil, status.Error(codes.Internal, err.Error()) + } + + return resp, nil +}