Coverage for /Users/buh/.pyenv/versions/3.12.9/envs/es-testbed/lib/python3.12/site-packages/es_testbed/mgrs/ilm.py: 76%

38 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-31 12:31 -0600

1"""ILM Policy Entity Manager Class""" 

2 

3import typing as t 

4import logging 

5import tiered_debug as debug 

6from es_testbed.exceptions import ResultNotExpected 

7from es_testbed.helpers.es_api import exists, put_ilm 

8from es_testbed.helpers.utils import build_ilm_policy 

9from es_testbed.mgrs.entity import EntityMgr 

10 

11if t.TYPE_CHECKING: 

12 from elasticsearch8 import Elasticsearch 

13 from dotmap import DotMap 

14 

15logger = logging.getLogger(__name__) 

16 

17 

18class IlmMgr(EntityMgr): 

19 """Index Lifecycle Policy Entity Manager""" 

20 

21 kind = 'ilm' 

22 listname = 'ilm_policies' 

23 

24 def __init__( 

25 self, 

26 client: t.Union['Elasticsearch', None] = None, 

27 plan: t.Union['DotMap', None] = None, 

28 ): 

29 """Initialize the ILM policy manager""" 

30 debug.lv2('Initializing IlmMgr object...') 

31 super().__init__(client=client, plan=plan) 

32 debug.lv3('IlmMgr object initialized') 

33 

34 def get_policy(self) -> t.Dict: 

35 """Return the configured ILM policy""" 

36 debug.lv2('Starting method...') 

37 d = self.plan.ilm 

38 kwargs = { 

39 'phases': d.phases, 

40 'forcemerge': d.forcemerge, 

41 'max_num_segments': d.max_num_segments, 

42 'readonly': d.readonly, 

43 'repository': self.plan.repository, 

44 } 

45 retval = build_ilm_policy(**kwargs) 

46 debug.lv3('Exiting method, returning value') 

47 debug.lv5(f'Value = {retval}') 

48 return retval 

49 

50 def setup(self) -> None: 

51 """Setup the entity manager""" 

52 debug.lv2('Starting method...') 

53 if self.plan.ilm.enabled: 

54 if not self.plan.ilm.policy: # If you've put a full policy there... 

55 self.plan.ilm.policy = self.get_policy() 

56 put_ilm(self.client, self.name, policy=self.plan.ilm.policy) 

57 # Verify existence 

58 if not exists(self.client, 'ilm', self.name): 

59 raise ResultNotExpected( 

60 f'Unable to verify creation of ilm policy {self.name}' 

61 ) 

62 # This goes first because the length of entity_list determines the suffix 

63 self.appender(self.name) 

64 logger.info(f'Successfully created ILM policy: {self.last}') 

65 debug.lv5(self.client.ilm.get_lifecycle(name=self.last)) 

66 else: 

67 self.appender(None) # This covers self.plan.ilm_policies[-1] 

68 logger.info('No ILM policy created.') 

69 debug.lv3('Exiting method')