in curator/helpers/date_ops.py [0:0]
def get_datemath(client, datemath, random_element=None):
"""
:param client: A client connection object
:param datemath: An elasticsearch datemath string
:param random_element: This allows external randomization of the name and is only
useful for tests so that you can guarantee the output because you provided the
random portion.
:type client: :py:class:`~.elasticsearch.Elasticsearch`
:type datemath: :py:class:`~.datemath.datemath`
:type random_element: str
:returns: the parsed index name from ``datemath``
:rtype: str
"""
logger = logging.getLogger(__name__)
if random_element is None:
random_prefix = 'curator_get_datemath_function_' + ''.join(
random.choice(string.ascii_lowercase) for _ in range(32)
)
else:
random_prefix = 'curator_get_datemath_function_' + random_element
datemath_dummy = f'<{random_prefix}-{datemath}>'
# We both want and expect a 404 here (NotFoundError), since we have
# created a 32 character random string to definitely be an unknown
# index name.
logger.debug('Random datemath string for extraction: %s', datemath_dummy)
faux_index = ''
try:
client.indices.get(index=datemath_dummy)
except NotFoundError as err:
# This is the magic. Elasticsearch still gave us the formatted
# index name in the error results.
faux_index = err.body['error']['index']
logger.debug('Response index name for extraction: %s', faux_index)
# Now we strip the random index prefix back out again
# pylint: disable=consider-using-f-string
pattern = r'^{0}-(.*)$'.format(random_prefix)
regex = re.compile(pattern)
try:
# And return only the now-parsed date string
return regex.match(faux_index).group(1)
except AttributeError as exc:
raise ConfigurationError(
f'The rendered index "{faux_index}" does not contain a valid date pattern '
f'or has invalid index name characters.'
) from exc