Coverage for manila/api/views/messages.py: 100%

16 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 

14from manila.api import common 

15from manila.message import message_field 

16 

17 

18class ViewBuilder(common.ViewBuilder): 

19 """Model a server API response as a python dictionary.""" 

20 

21 _collection_name = "messages" 

22 

23 def index(self, request, messages): 

24 """Show a list of messages.""" 

25 return self._list_view(self.detail, request, messages) 

26 

27 def detail(self, request, message): 

28 """Detailed view of a single message.""" 

29 message_ref = { 

30 'id': message.get('id'), 

31 'project_id': message.get('project_id'), 

32 'action_id': message.get('action_id'), 

33 'detail_id': message.get('detail_id'), 

34 'message_level': message.get('message_level'), 

35 'created_at': message.get('created_at'), 

36 'expires_at': message.get('expires_at'), 

37 'request_id': message.get('request_id'), 

38 'links': self._get_links(request, message['id']), 

39 'resource_type': message.get('resource_type'), 

40 'resource_id': message.get('resource_id'), 

41 'user_message': "%s: %s" % ( 

42 message_field.translate_action(message.get('action_id')), 

43 message_field.translate_detail(message.get('detail_id'))), 

44 } 

45 

46 return {'message': message_ref} 

47 

48 def _list_view(self, func, request, messages, coll_name=_collection_name): 

49 """Provide a view for a list of messages. 

50 

51 :param func: Function used to format the message data 

52 :param request: API request 

53 :param messages: List of messages in dictionary format 

54 :param coll_name: Name of collection, used to generate the next link 

55 for a pagination query 

56 :returns: message data in dictionary format 

57 """ 

58 messages_list = [func(request, message)['message'] 

59 for message in messages] 

60 messages_links = self._get_collection_links(request, 

61 messages, 

62 coll_name) 

63 messages_dict = dict({"messages": messages_list}) 

64 

65 if messages_links: 

66 messages_dict['messages_links'] = messages_links 

67 

68 return messages_dict