Coverage for manila/rpc.py: 83%

88 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2026-02-18 22:19 +0000

1# Copyright 2013 Red Hat, Inc. 

2# 

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

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

5# 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, WITHOUT 

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

12# License for the specific language governing permissions and limitations 

13# under the License. 

14 

15__all__ = [ 

16 'init', 

17 'cleanup', 

18 'set_defaults', 

19 'add_extra_exmods', 

20 'clear_extra_exmods', 

21 'get_allowed_exmods', 

22 'RequestContextSerializer', 

23 'get_client', 

24 'get_server', 

25 'get_notifier', 

26] 

27 

28from oslo_config import cfg 

29import oslo_messaging as messaging 

30from oslo_messaging.rpc import dispatcher 

31from oslo_serialization import jsonutils 

32from oslo_utils import importutils 

33profiler = importutils.try_import('osprofiler.profiler') 

34 

35import manila.context 

36import manila.exception 

37from manila import utils 

38 

39CONF = cfg.CONF 

40TRANSPORT = None 

41NOTIFICATION_TRANSPORT = None 

42NOTIFIER = None 

43 

44ALLOWED_EXMODS = [ 

45 manila.exception.__name__, 

46] 

47EXTRA_EXMODS = [] 

48 

49 

50def init(conf): 

51 global TRANSPORT, NOTIFICATION_TRANSPORT, NOTIFIER 

52 exmods = get_allowed_exmods() 

53 TRANSPORT = messaging.get_rpc_transport(conf, 

54 allowed_remote_exmods=exmods) 

55 NOTIFICATION_TRANSPORT = messaging.get_notification_transport( 

56 conf, 

57 allowed_remote_exmods=exmods) 

58 

59 if utils.notifications_enabled(conf): 

60 json_serializer = messaging.JsonPayloadSerializer() 

61 serializer = RequestContextSerializer(json_serializer) 

62 NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT, 

63 serializer=serializer) 

64 else: 

65 NOTIFIER = utils.DO_NOTHING 

66 

67 

68def initialized(): 

69 return None not in [TRANSPORT, NOTIFIER] 

70 

71 

72def cleanup(): 

73 global TRANSPORT, NOTIFICATION_TRANSPORT, NOTIFIER 

74 assert TRANSPORT is not None 

75 assert NOTIFICATION_TRANSPORT is not None 

76 assert NOTIFIER is not None 

77 TRANSPORT.cleanup() 

78 NOTIFICATION_TRANSPORT.cleanup() 

79 TRANSPORT = NOTIFIER = NOTIFICATION_TRANSPORT = None 

80 

81 

82def set_defaults(control_exchange): 

83 messaging.set_transport_defaults(control_exchange) 

84 

85 

86def add_extra_exmods(*args): 

87 EXTRA_EXMODS.extend(args) 

88 

89 

90def clear_extra_exmods(): 

91 del EXTRA_EXMODS[:] 

92 

93 

94def get_allowed_exmods(): 

95 return ALLOWED_EXMODS + EXTRA_EXMODS 

96 

97 

98class JsonPayloadSerializer(messaging.NoOpSerializer): 

99 @staticmethod 

100 def serialize_entity(context, entity): 

101 return jsonutils.to_primitive(entity, convert_instances=True) 

102 

103 

104class RequestContextSerializer(messaging.Serializer): 

105 

106 def __init__(self, base): 

107 self._base = base 

108 

109 def serialize_entity(self, context, entity): 

110 if not self._base: 

111 return entity 

112 return self._base.serialize_entity(context, entity) 

113 

114 def deserialize_entity(self, context, entity): 

115 if not self._base: 

116 return entity 

117 return self._base.deserialize_entity(context, entity) 

118 

119 def serialize_context(self, context): 

120 _context = context.to_dict() 

121 if profiler is not None: 121 ↛ 130line 121 didn't jump to line 130 because the condition on line 121 was always true

122 prof = profiler.get() 

123 if prof: 123 ↛ 124line 123 didn't jump to line 124 because the condition on line 123 was never true

124 trace_info = { 

125 "hmac_key": prof.hmac_key, 

126 "base_id": prof.get_base_id(), 

127 "parent_id": prof.get_id() 

128 } 

129 _context.update({"trace_info": trace_info}) 

130 return _context 

131 

132 def deserialize_context(self, context): 

133 trace_info = context.pop("trace_info", None) 

134 if trace_info: 134 ↛ 135line 134 didn't jump to line 135 because the condition on line 134 was never true

135 if profiler is not None: 

136 profiler.init(**trace_info) 

137 

138 return manila.context.RequestContext.from_dict(context) 

139 

140 

141def get_transport_url(url_str=None): 

142 return messaging.TransportURL.parse(CONF, url_str) 

143 

144 

145def get_client(target, version_cap=None, serializer=None): 

146 assert TRANSPORT is not None 

147 serializer = RequestContextSerializer(serializer) 

148 return messaging.get_rpc_client( 

149 TRANSPORT, target, version_cap=version_cap, 

150 serializer=serializer) 

151 

152 

153def get_server(target, endpoints, serializer=None): 

154 assert TRANSPORT is not None 

155 access_policy = dispatcher.DefaultRPCAccessPolicy 

156 serializer = RequestContextSerializer(serializer) 

157 return messaging.get_rpc_server(TRANSPORT, 

158 target, 

159 endpoints, 

160 serializer=serializer, 

161 access_policy=access_policy) 

162 

163 

164@utils.if_notifications_enabled 

165def get_notifier(service=None, host=None, publisher_id=None): 

166 assert NOTIFIER is not None 

167 if not publisher_id: 167 ↛ 169line 167 didn't jump to line 169 because the condition on line 167 was always true

168 publisher_id = "%s.%s" % (service, host or CONF.host) 

169 return NOTIFIER.prepare(publisher_id=publisher_id)