Coverage for /Users/buh/.pyenv/versions/3.12.2/envs/es-testbed/lib/python3.12/site-packages/es_testbed/mgrs/templatemgr.py: 86%
30 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-27 23:17 -0600
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-27 23:17 -0600
1"""Index Template Entity Manager Class"""
3import typing as t
4from .entitymgr import EntityMgr
5from ..exceptions import ResultNotExpected
6from ..helpers.es_api import exists, put_idx_tmpl
7from ..helpers.utils import getlogger
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 self.logger = getlogger('es_testbed.TemplateMgr')
29 super().__init__(client=client, plan=plan, autobuild=autobuild)
31 @property
32 def patterns(self) -> t.Sequence[str]:
33 """Return the list of index patterns associated with this template"""
34 _ = []
35 _.append(f"{self.get_pattern('index')}*")
36 _.append(f"{self.get_pattern('data_stream')}*")
37 return _
39 def get_pattern(self, kind: str) -> str:
40 """Return the a formatted index search pattern string"""
41 return f'{self.plan.prefix}-{self.ident(dkey=kind)}-{self.plan.uniq}'
43 def setup(self) -> None:
44 """Setup the entity manager"""
45 ds = {} if self.plan.type == 'data_stream' else None
46 put_idx_tmpl(
47 self.client,
48 self.name,
49 self.patterns,
50 components=self.plan.component_templates,
51 data_stream=ds,
52 )
53 if not exists(self.client, self.kind, self.name): 53 ↛ 54line 53 didn't jump to line 54, because the condition on line 53 was never true
54 raise ResultNotExpected(
55 f'Unable to verify creation of index template {self.name}'
56 )
57 self.appender(self.name)
58 self.logger.info('Successfully created index template: %s', self.last)
59 self.success = True