Coverage for curator/actions/delete_indices.py: 100%

46 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-20 21:00 -0600

1"""Delete index action class""" 

2import logging 

3# pylint: disable=import-error 

4from curator.helpers.getters import get_indices 

5from curator.helpers.testers import verify_index_list 

6from curator.helpers.utils import chunk_index_list, report_failure, show_dry_run, to_csv 

7 

8class DeleteIndices: 

9 """Delete Indices Action Class""" 

10 def __init__(self, ilo, master_timeout=30): 

11 """ 

12 :param ilo: An IndexList Object 

13 :param master_timeout: Number of seconds to wait for master node response 

14 

15 :type ilo: :py:class:`~.curator.indexlist.IndexList` 

16 :type master_timeout: int 

17 """ 

18 verify_index_list(ilo) 

19 if not isinstance(master_timeout, int): 

20 raise TypeError( 

21 f'Incorrect type for "master_timeout": {type(master_timeout)}. ' 

22 f'Should be integer value.' 

23 ) 

24 #: The :py:class:`~.curator.indexlist.IndexList` object passed from param ``ilo`` 

25 self.index_list = ilo 

26 #: The :py:class:`~.elasticsearch.Elasticsearch` client object derived from 

27 #: :py:attr:`index_list` 

28 self.client = ilo.client 

29 #: String value of param ``master_timeout`` + ``s``, for seconds. 

30 self.master_timeout = str(master_timeout) + 's' 

31 self.loggit = logging.getLogger('curator.actions.delete_indices') 

32 self.loggit.debug('master_timeout value: %s', self.master_timeout) 

33 

34 def _verify_result(self, result, count): 

35 """ 

36 Breakout method to aid readability 

37 

38 :param result: A list of indices from :py:meth:`__chunk_loop` 

39 :param count: The number of tries that have occurred 

40 

41 :type result: list 

42 :type count: int 

43 

44 :returns: ``True`` if result is verified successful, else ``False`` 

45 :rtype: bool 

46 """ 

47 if isinstance(result, list) and result: 

48 self.loggit.error('The following indices failed to delete on try #%s:', count) 

49 for idx in result: 

50 self.loggit.error("---%s",idx) 

51 retval = False 

52 else: 

53 self.loggit.debug('Successfully deleted all indices on try #%s', count) 

54 retval = True 

55 return retval 

56 

57 def __chunk_loop(self, chunk_list): 

58 """ 

59 Loop through deletes 3 times to ensure they complete 

60 

61 :param chunk_list: A list of indices pre-chunked so it won't overload the URL size limit. 

62 :type chunk_list: list 

63 """ 

64 working_list = chunk_list 

65 for count in range(1, 4): # Try 3 times 

66 for i in working_list: 

67 self.loggit.info("---deleting index %s", i) 

68 self.client.indices.delete( 

69 index=to_csv(working_list), master_timeout=self.master_timeout) 

70 result = [i for i in working_list if i in get_indices(self.client)] 

71 if self._verify_result(result, count): 

72 return 

73 working_list = result 

74 self.loggit.error('Unable to delete the following indices after 3 attempts: %s', result) 

75 

76 def do_dry_run(self): 

77 """Log what the output would be, but take no action.""" 

78 show_dry_run(self.index_list, 'delete_indices') 

79 

80 def do_action(self): 

81 """ 

82 :py:meth:`~.elasticsearch.client.IndicesClient.delete` indices in :py:attr:`index_list` 

83 """ 

84 self.index_list.empty_list_check() 

85 msg = ( 

86 f'Deleting {len(self.index_list.indices)} selected indices: {self.index_list.indices}') 

87 self.loggit.info(msg) 

88 try: 

89 index_lists = chunk_index_list(self.index_list.indices) 

90 for lst in index_lists: 

91 self.__chunk_loop(lst) 

92 # pylint: disable=broad-except 

93 except Exception as err: 

94 report_failure(err)