Coverage for manila/message/api.py: 100%

34 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""" 

13Handles all requests related to user facing messages. 

14""" 

15import datetime 

16 

17from oslo_config import cfg 

18from oslo_log import log as logging 

19from oslo_utils import timeutils 

20 

21from manila.db import base 

22from manila.message import message_field 

23from manila.message import message_levels 

24 

25 

26messages_opts = [ 

27 cfg.IntOpt('message_ttl', default=2592000, 

28 help='Message minimum life in seconds.'), 

29 cfg.IntOpt('message_reap_interval', default=86400, 

30 help='Interval between periodic task runs to clean expired ' 

31 'messages in seconds.'), 

32] 

33 

34CONF = cfg.CONF 

35CONF.register_opts(messages_opts) 

36 

37LOG = logging.getLogger(__name__) 

38 

39 

40class API(base.Base): 

41 """API for handling user messages.""" 

42 

43 def create(self, context, action, project_id, resource_type=None, 

44 resource_id=None, exception=None, detail=None, 

45 level=message_levels.ERROR): 

46 """Create a message with the specified information.""" 

47 LOG.info("Creating message record for request_id = %s", 

48 context.request_id) 

49 

50 # Updates expiry time for message as per message_ttl config. 

51 expires_at = (timeutils.utcnow() + datetime.timedelta( 

52 seconds=CONF.message_ttl)) 

53 detail_id = message_field.translate_detail_id(exception, detail) 

54 

55 message_record = { 

56 'project_id': project_id, 

57 'request_id': context.request_id, 

58 'resource_type': resource_type, 

59 'resource_id': resource_id, 

60 'action_id': action[0], 

61 'detail_id': detail_id, 

62 'message_level': level, 

63 'expires_at': expires_at, 

64 } 

65 try: 

66 self.db.message_create(context, message_record) 

67 except Exception: 

68 LOG.exception(("Failed to create message record " 

69 "for request_id %s"), context.request_id) 

70 

71 def get(self, context, id): 

72 """Return message with the specified message id.""" 

73 return self.db.message_get(context, id) 

74 

75 def get_all(self, context, search_opts=None, limit=None, 

76 offset=None, sort_key=None, sort_dir=None): 

77 """Return messages for the given context.""" 

78 LOG.debug("Searching for messages by: %s", search_opts) 

79 

80 search_opts = search_opts or {} 

81 messages = self.db.message_get_all(context, filters=search_opts, 

82 limit=limit, offset=offset, 

83 sort_key=sort_key, 

84 sort_dir=sort_dir) 

85 

86 return messages 

87 

88 def delete(self, context, id): 

89 """Delete message with the specified message id.""" 

90 return self.db.message_destroy(context, id) 

91 

92 def cleanup_expired_messages(self, context): 

93 ctx = context.elevated() 

94 count = self.db.cleanup_expired_messages(ctx) 

95 LOG.info("Deleted %s expired messages.", count)