Coverage for manila/api/v2/messages.py: 95%

72 statements  

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

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

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

3# a copy of the License at 

4# 

5# http://www.apache.org/licenses/LICENSE-2.0 

6# 

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

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

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

10# License for the specific language governing permissions and limitations 

11# under the License. 

12 

13"""The messages API controller module. 

14 

15This module handles the following requests: 

16GET /messages 

17GET /messages/<message_id> 

18DELETE /messages/<message_id> 

19""" 

20 

21from http import client as http_client 

22 

23from oslo_utils import timeutils 

24import webob 

25from webob import exc 

26 

27from manila.api import common 

28from manila.api.openstack import api_version_request as api_version 

29from manila.api.openstack import wsgi 

30from manila.api.schemas import messages as schema 

31from manila.api import validation 

32from manila.api.views import messages as messages_view 

33from manila import exception 

34from manila.i18n import _ 

35from manila.message import api as message_api 

36 

37MESSAGES_BASE_MICRO_VERSION = '2.37' 

38MESSAGES_QUERY_BY_TIMESTAMP = '2.52' 

39 

40 

41@validation.validated 

42class MessagesController(wsgi.Controller): 

43 """The User Messages API controller for the OpenStack API.""" 

44 _view_builder_class = messages_view.ViewBuilder 

45 resource_name = 'message' 

46 

47 def __init__(self): 

48 self.message_api = message_api.API() 

49 super(MessagesController, self).__init__() 

50 

51 @wsgi.Controller.api_version(MESSAGES_BASE_MICRO_VERSION) 

52 @wsgi.Controller.authorize('get') 

53 @validation.request_query_schema(schema.show_request_query) 

54 @validation.response_body_schema(schema.show_response_body) 

55 def show(self, req, id): 

56 """Return the given message.""" 

57 context = req.environ['manila.context'] 

58 

59 try: 

60 message = self.message_api.get(context, id) 

61 except exception.MessageNotFound as error: 

62 raise exc.HTTPNotFound(explanation=error.msg) 

63 

64 return self._view_builder.detail(req, message) 

65 

66 @wsgi.Controller.api_version(MESSAGES_BASE_MICRO_VERSION) 

67 @wsgi.Controller.authorize 

68 @wsgi.action("delete") 

69 @validation.response_body_schema(schema.delete_response_body) 

70 def delete(self, req, id): 

71 """Delete a message.""" 

72 context = req.environ['manila.context'] 

73 

74 try: 

75 message = self.message_api.get(context, id) 

76 self.message_api.delete(context, message) 

77 except exception.MessageNotFound as error: 

78 raise exc.HTTPNotFound(explanation=error.msg) 

79 

80 return webob.Response(status_int=http_client.NO_CONTENT) 

81 

82 @wsgi.Controller.api_version(MESSAGES_BASE_MICRO_VERSION) 

83 @wsgi.Controller.authorize('get_all') 

84 @validation.request_query_schema( 

85 schema.index_request_query, min_version='2.37', max_version='2.51' 

86 ) 

87 @validation.request_query_schema( 

88 schema.index_request_query_v252, min_version='2.52' 

89 ) 

90 @validation.response_body_schema(schema.index_response_body) 

91 def index(self, req): 

92 """Returns a list of messages, transformed through view builder.""" 

93 context = req.environ['manila.context'] 

94 filters = req.params.copy() 

95 

96 params = common.get_pagination_params(req) 

97 limit, offset = [params.get('limit'), params.get('offset')] 

98 sort_key, sort_dir = common.get_sort_params(filters) 

99 

100 if req.api_version_request < api_version.APIVersionRequest('2.52'): 

101 filters.pop('created_since', None) 

102 filters.pop('created_before', None) 

103 else: 

104 for time_comparison_filter in ['created_since', 'created_before']: 

105 if time_comparison_filter in filters: 105 ↛ 104line 105 didn't jump to line 104 because the condition on line 105 was always true

106 time_str = filters.get(time_comparison_filter) 

107 try: 

108 parsed_time = timeutils.parse_isotime(time_str) 

109 except ValueError: 

110 msg = _('Invalid value specified for the query ' 

111 'key: %s') % time_comparison_filter 

112 raise exc.HTTPBadRequest(explanation=msg) 

113 

114 filters[time_comparison_filter] = parsed_time 

115 

116 messages = self.message_api.get_all(context, search_opts=filters, 

117 limit=limit, 

118 offset=offset, 

119 sort_key=sort_key, 

120 sort_dir=sort_dir) 

121 

122 return self._view_builder.index(req, messages) 

123 

124 

125def create_resource(): 

126 return wsgi.Resource(MessagesController())