Coverage for /builds/BuildGrid/buildgrid/buildgrid/server/capabilities/service.py: 100.00%

27 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-06-11 15:37 +0000

1# Copyright (C) 2018 Bloomberg LP 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# <http://www.apache.org/licenses/LICENSE-2.0> 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15 

16import logging 

17from typing import cast 

18 

19import grpc 

20 

21from buildgrid._protos.build.bazel.remote.execution.v2.remote_execution_pb2 import DESCRIPTOR as RE_DESCRIPTOR 

22from buildgrid._protos.build.bazel.remote.execution.v2.remote_execution_pb2 import ( 

23 GetCapabilitiesRequest, 

24 ServerCapabilities, 

25) 

26from buildgrid._protos.build.bazel.remote.execution.v2.remote_execution_pb2_grpc import ( 

27 CapabilitiesServicer, 

28 add_CapabilitiesServicer_to_server, 

29) 

30from buildgrid.server.auth.manager import authorize 

31from buildgrid.server.capabilities.instance import CapabilitiesInstance 

32from buildgrid.server.instance import instanced 

33from buildgrid.server.request_metadata_utils import printable_request_metadata 

34from buildgrid.server.servicer import InstancedServicer 

35from buildgrid.server.utils.decorators import handle_errors_unary_unary, track_request_id 

36 

37LOGGER = logging.getLogger(__name__) 

38 

39 

40class CapabilitiesService(CapabilitiesServicer, InstancedServicer[CapabilitiesInstance]): 

41 REGISTER_METHOD = add_CapabilitiesServicer_to_server 

42 FULL_NAME = RE_DESCRIPTOR.services_by_name["Capabilities"].full_name 

43 

44 @property 

45 def enabled(self) -> bool: 

46 # We always want a capabilities service 

47 return True 

48 

49 @instanced(lambda r: cast(str, r.instance_name)) 

50 @authorize 

51 @track_request_id 

52 @handle_errors_unary_unary(ServerCapabilities) 

53 def GetCapabilities(self, request: GetCapabilitiesRequest, context: grpc.ServicerContext) -> ServerCapabilities: 

54 """Handles GetCapabilitiesRequest messages. 

55 

56 Args: 

57 request (GetCapabilitiesRequest): The incoming RPC request. 

58 context (grpc.ServicerContext): Context for the RPC call. 

59 """ 

60 LOGGER.info( 

61 f"GetCapabilities request from [{context.peer()}] " 

62 f"([{printable_request_metadata(context.invocation_metadata())}])" 

63 ) 

64 

65 instance = self.get_instance(request.instance_name) 

66 return instance.get_capabilities()