diff --git a/daemon/core/api/grpc/client.py b/daemon/core/api/grpc/client.py index b904d9f4..01a7b1f6 100644 --- a/daemon/core/api/grpc/client.py +++ b/daemon/core/api/grpc/client.py @@ -270,6 +270,33 @@ class CoreGrpcClient: ) return self.stub.SetSessionOptions(request) + def get_session_metadata(self, session_id): + """ + Retrieve session metadata as a dict with id mapping. + + :param int session_id: id of session + :return: response with metadata dict + :rtype: core_pb2.GetSessionMetadataResponse + :raises grpc.RpcError: when session doesn't exist + """ + request = core_pb2.GetSessionMetadataRequest(session_id=session_id) + return self.stub.GetSessionMetadata(request) + + def set_session_metadata(self, session_id, config): + """ + Set metadata for a session. + + :param int session_id: id of session + :param dict[str, str] config: configuration values to set + :return: response with result of success or failure + :rtype: core_pb2.SetSessionMetadataResponse + :raises grpc.RpcError: when session doesn't exist + """ + request = core_pb2.SetSessionMetadataRequest( + session_id=session_id, config=config + ) + return self.stub.SetSessionMetadata(request) + def get_session_location(self, session_id): """ Get session location. diff --git a/daemon/core/api/grpc/server.py b/daemon/core/api/grpc/server.py index 3a3fd2f2..33d1ebfd 100644 --- a/daemon/core/api/grpc/server.py +++ b/daemon/core/api/grpc/server.py @@ -331,6 +331,35 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): config.update(request.config) return core_pb2.SetSessionOptionsResponse(result=True) + def GetSessionMetadata(self, request, context): + """ + Retrieve session metadata. + + :param core.api.grpc.core_pb2.GetSessionMetadata request: get session metadata + request + :param grpc.ServicerContext context: context object + :return: get session metadata response + :rtype: core.api.grpc.core_pb2.GetSessionMetadata + """ + logging.debug("get session metadata: %s", request) + session = self.get_session(request.session_id, context) + config = session.metadata.get_configs() + return core_pb2.GetSessionMetadataResponse(config=config) + + def SetSessionMetadata(self, request, context): + """ + Update a session's metadata. + + :param core.api.grpc.core_pb2.SetSessionMetadata request: set metadata request + :param grpc.ServicerContext context: context object + :return: set metadata response + :rtype: core.api.grpc.core_pb2.SetSessionMetadataResponse + """ + logging.debug("set session metadata: %s", request) + session = self.get_session(request.session_id, context) + session.metadata.set_configs(request.config) + return core_pb2.SetSessionMetadataResponse(result=True) + def GetSession(self, request, context): """ Retrieve requested session diff --git a/daemon/proto/core/api/grpc/core.proto b/daemon/proto/core/api/grpc/core.proto index 03843efa..19a1ff97 100644 --- a/daemon/proto/core/api/grpc/core.proto +++ b/daemon/proto/core/api/grpc/core.proto @@ -23,6 +23,10 @@ service CoreApi { } rpc SetSessionOptions (SetSessionOptionsRequest) returns (SetSessionOptionsResponse) { } + rpc SetSessionMetadata (SetSessionMetadataRequest) returns (SetSessionMetadataResponse) { + } + rpc GetSessionMetadata (GetSessionMetadataRequest) returns (GetSessionMetadataResponse) { + } rpc GetSessionLocation (GetSessionLocationRequest) returns (GetSessionLocationResponse) { } rpc SetSessionLocation (SetSessionLocationRequest) returns (SetSessionLocationResponse) { @@ -203,6 +207,23 @@ message SetSessionOptionsResponse { bool result = 1; } +message SetSessionMetadataRequest { + int32 session_id = 1; + map config = 2; +} + +message SetSessionMetadataResponse { + bool result = 1; +} + +message GetSessionMetadataRequest { + int32 session_id = 1; +} + +message GetSessionMetadataResponse { + map config = 1; +} + message GetSessionLocationRequest { int32 session_id = 1; } diff --git a/daemon/tests/test_grpc.py b/daemon/tests/test_grpc.py index 63125eb5..c091691e 100644 --- a/daemon/tests/test_grpc.py +++ b/daemon/tests/test_grpc.py @@ -283,6 +283,38 @@ class TestGrpc: config = session.options.get_configs() assert len(config) > 0 + def test_set_session_metadata(self, grpc_server): + # given + client = CoreGrpcClient() + session = grpc_server.coreemu.create_session() + + # then + key = "meta1" + value = "value1" + with client.context_connect(): + response = client.set_session_metadata(session.id, {key: value}) + + # then + assert response.result is True + assert session.metadata.get_config(key) == value + config = session.metadata.get_configs() + assert len(config) > 0 + + def test_get_session_metadata(self, grpc_server): + # given + client = CoreGrpcClient() + session = grpc_server.coreemu.create_session() + key = "meta1" + value = "value1" + session.metadata.set_config(key, value) + + # then + with client.context_connect(): + response = client.get_session_metadata(session.id) + + # then + assert response.config[key] == value + def test_set_session_state(self, grpc_server): # given client = CoreGrpcClient()