Coverage for /Users/buh/.pyenv/versions/3.12.2/envs/es-testbed/lib/python3.12/site-packages/es_testbed/mgrs/ilm.py: 97%
29 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-05-02 23:00 -0600
« prev ^ index » next coverage.py v7.4.4, created at 2024-05-02 23:00 -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, prettystr
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 'tiers': d.tiers,
35 'forcemerge': d.forcemerge,
36 'max_num_segments': d.max_num_segments,
37 'repository': self.plan.repository,
38 }
39 return build_ilm_policy(**kwargs)
41 def setup(self) -> None:
42 """Setup the entity manager"""
43 logger.debug('Starting IlmMgr setup...')
44 if self.plan.ilm.enabled:
45 self.plan.ilm.policy = self.get_policy()
46 put_ilm(self.client, self.name, policy=self.plan.ilm.policy)
47 # Verify existence
48 if not exists(self.client, 'ilm', self.name):
49 raise ResultNotExpected(
50 f'Unable to verify creation of ilm policy {self.name}'
51 )
52 # This goes first because the length of entity_list determines the suffix
53 self.appender(self.name)
54 logger.info('Successfully created ILM policy: %s', self.last)
55 logger.debug(self.client.ilm.get_lifecycle(name=self.last))
56 else:
57 self.appender(None) # This covers self.plan.ilm_policies[-1]
58 logger.info('No ILM policy created.')