Coverage for /Users/buh/.pyenv/versions/3.12.2/envs/es-testbed/lib/python3.12/site-packages/es_testbed/classes/entitymgrs/templatemgr.py: 85%
33 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-27 20:59 -0600
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-27 20:59 -0600
1"""Index Template Entity Manager Class"""
3import typing as t
4from es_testbed.exceptions import ResultNotExpected
5from es_testbed.helpers import es_api
6from es_testbed.helpers.utils import getlogger
7from .entitymgr import EntityMgr
9if t.TYPE_CHECKING: 9 ↛ 10line 9 didn't jump to line 10, because the condition on line 9 was never true
10 from elasticsearch8 import Elasticsearch
11 from dotmap import DotMap
13# pylint: disable=missing-docstring,too-many-arguments
16class TemplateMgr(EntityMgr):
17 """Index Template entity manager"""
19 kind = 'template'
20 listname = 'index_templates'
22 def __init__(
23 self,
24 client: t.Union['Elasticsearch', None] = None,
25 plan: t.Union['DotMap', None] = None,
26 autobuild: t.Optional[bool] = True,
27 ):
28 super().__init__(client=client, plan=plan, autobuild=autobuild)
29 self.logger = getlogger('es_testbed.TemplateMgr')
31 @property
32 def logdisplay(self) -> str:
33 """Return a human readable representation of what is managed"""
34 return 'index template'
36 @property
37 def patterns(self) -> t.Sequence[str]:
38 """Return the list of index patterns associated with this template"""
39 _ = []
40 _.append(f"{self.get_pattern('index')}*")
41 _.append(f"{self.get_pattern('data_stream')}*")
42 return _
44 def get_pattern(self, kind: str) -> str:
45 """Return the a formatted index search pattern string"""
46 return f'{self.plan.prefix}-{self.ident(dkey=kind)}-{self.plan.uniq}'
48 def setup(self) -> None:
49 """Setup the entity manager"""
50 ds = {} if self.plan.type == 'data_stream' else None
51 es_api.put_idx_tmpl(
52 self.client,
53 self.name,
54 self.patterns,
55 components=self.plan.component_templates,
56 data_stream=ds,
57 )
58 if not es_api.exists(self.client, self.kind, self.name): 58 ↛ 59line 58 didn't jump to line 59, because the condition on line 58 was never true
59 raise ResultNotExpected(
60 f'Unable to verify creation of index template {self.name}'
61 )
62 self.appender(self.name)
63 self.logger.debug('Successfully created index template: %s', self.last)
64 self.success = True