Coverage for /builds/BuildGrid/buildgrid/buildgrid/server/referencestorage/service.py: 65.28%

72 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-22 21:04 +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 

17 

18import grpc 

19 

20from buildgrid._exceptions import InvalidArgumentError, NotFoundError 

21from buildgrid._protos.buildstream.v2 import buildstream_pb2 

22from buildgrid._protos.buildstream.v2 import buildstream_pb2_grpc 

23from buildgrid.server._authentication import AuthContext, authorize 

24 

25 

26class ReferenceStorageService(buildstream_pb2_grpc.ReferenceStorageServicer): 

27 

28 def __init__(self, server): 

29 self.__logger = logging.getLogger(__name__) 

30 

31 self._instances = {} 

32 

33 buildstream_pb2_grpc.add_ReferenceStorageServicer_to_server(self, server) 

34 

35 # --- Public API --- 

36 

37 def add_instance(self, name, instance): 

38 self._instances[name] = instance 

39 

40 # --- Public API: Servicer --- 

41 

42 @authorize(AuthContext) 

43 def GetReference(self, request, context): 

44 self.__logger.debug(f"GetReference request from [{context.peer()}]") 

45 

46 try: 

47 instance = self._get_instance(request.instance_name) 

48 digest = instance.get_digest_reference(request.key) 

49 response = buildstream_pb2.GetReferenceResponse() 

50 response.digest.CopyFrom(digest) 

51 return response 

52 

53 except InvalidArgumentError as e: 

54 self.__logger.info(e) 

55 context.set_details(str(e)) 

56 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

57 

58 except NotFoundError as e: 

59 self.__logger.debug(e) 

60 context.set_code(grpc.StatusCode.NOT_FOUND) 

61 

62 except Exception: 

63 self.__logger.exception( 

64 f"Unexpected error in GetReference; request=[{request}]" 

65 ) 

66 context.set_details(str(e)) 

67 context.set_code(grpc.StatusCode.INTERNAL) 

68 

69 return buildstream_pb2.GetReferenceResponse() 

70 

71 @authorize(AuthContext) 

72 def UpdateReference(self, request, context): 

73 self.__logger.debug(f"UpdateReference request from [{context.peer()}]") 

74 

75 try: 

76 instance = self._get_instance(request.instance_name) 

77 digest = request.digest 

78 

79 for key in request.keys: 

80 instance.update_reference(key, digest) 

81 

82 except InvalidArgumentError as e: 

83 self.__logger.info(e) 

84 context.set_details(str(e)) 

85 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

86 

87 except NotImplementedError as e: 

88 self.__logger.info(e) 

89 context.set_code(grpc.StatusCode.UNIMPLEMENTED) 

90 

91 except Exception: 

92 self.__logger.exception( 

93 f"Unexpected error in UpdateReference; request=[{request}]" 

94 ) 

95 context.set_details(str(e)) 

96 context.set_code(grpc.StatusCode.INTERNAL) 

97 

98 return buildstream_pb2.UpdateReferenceResponse() 

99 

100 @authorize(AuthContext) 

101 def Status(self, request, context): 

102 self.__logger.debug(f"Status request from [{context.peer()}]") 

103 

104 try: 

105 instance = self._get_instance(request.instance_name) 

106 allow_updates = instance.allow_updates 

107 return buildstream_pb2.StatusResponse(allow_updates=allow_updates) 

108 

109 except InvalidArgumentError as e: 

110 self.__logger.info(e) 

111 context.set_details(str(e)) 

112 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

113 

114 except Exception: 

115 self.__logger.exception( 

116 f"Unexpected error in Status; request=[{request}]" 

117 ) 

118 context.set_details(str(e)) 

119 context.set_code(grpc.StatusCode.INTERNAL) 

120 

121 return buildstream_pb2.StatusResponse() 

122 

123 # --- Private API --- 

124 

125 def _get_instance(self, instance_name): 

126 try: 

127 return self._instances[instance_name] 

128 

129 except KeyError: 

130 raise InvalidArgumentError(f"Invalid instance name: [{instance_name}]")