Coverage for /Users/buh/.pyenv/versions/3.12.9/envs/es-testbed/lib/python3.12/site-packages/es_testbed/mgrs/ilm.py: 83%
30 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-17 19:30 -0600
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-17 19:30 -0600
1"""ILM Policy Entity Manager Class"""
3import typing as t
4import logging
5from es_testbed.exceptions import ResultNotExpected
6from es_testbed.helpers.es_api import exists, put_ilm
7from es_testbed.helpers.utils import build_ilm_policy
8from es_testbed.mgrs.entity import EntityMgr
10if t.TYPE_CHECKING:
11 from elasticsearch8 import Elasticsearch
12 from dotmap import DotMap
14logger = logging.getLogger(__name__)
17class IlmMgr(EntityMgr):
18 """Index Lifecycle Policy Entity Manager"""
20 kind = 'ilm'
21 listname = 'ilm_policies'
23 def __init__(
24 self,
25 client: t.Union['Elasticsearch', None] = None,
26 plan: t.Union['DotMap', None] = None,
27 ):
28 super().__init__(client=client, plan=plan)
30 def get_policy(self) -> t.Dict:
31 """Return the configured ILM policy"""
32 d = self.plan.ilm
33 kwargs = {
34 'phases': d.phases,
35 'forcemerge': d.forcemerge,
36 'max_num_segments': d.max_num_segments,
37 'readonly': d.readonly,
38 'repository': self.plan.repository,
39 }
40 return build_ilm_policy(**kwargs)
42 def setup(self) -> None:
43 """Setup the entity manager"""
44 logger.debug('Starting IlmMgr setup...')
45 if self.plan.ilm.enabled:
46 if not self.plan.ilm.policy: # If you've put a full policy there...
47 self.plan.ilm.policy = self.get_policy()
48 put_ilm(self.client, self.name, policy=self.plan.ilm.policy)
49 # Verify existence
50 if not exists(self.client, 'ilm', self.name):
51 raise ResultNotExpected(
52 f'Unable to verify creation of ilm policy {self.name}'
53 )
54 # This goes first because the length of entity_list determines the suffix
55 self.appender(self.name)
56 logger.info(f'Successfully created ILM policy: {self.last}')
57 logger.debug(self.client.ilm.get_lifecycle(name=self.last))
58 else:
59 self.appender(None) # This covers self.plan.ilm_policies[-1]
60 logger.info('No ILM policy created.')