def rollable_alias()

in curator/helpers/testers.py [0:0]


def rollable_alias(client, alias):
    """
    Calls :py:meth:`~.elasticsearch.client.IndicesClient.get_alias`

    :param client: A client connection object
    :param alias: An Elasticsearch alias

    :type client: :py:class:`~.elasticsearch.Elasticsearch`
    :type alias: str


    :returns: ``True`` or ``False`` depending on whether ``alias`` is an alias that
        points to an index that can be used by the ``_rollover`` API.
    :rtype: bool
    """
    logger = logging.getLogger(__name__)
    try:
        response = client.indices.get_alias(name=alias)
    except NotFoundError:
        logger.error('Alias "%s" not found.', alias)
        return False
    # Response should be like:
    # {'there_should_be_only_one': {'aliases': {'value of "alias" here': {}}}}
    # where 'there_should_be_only_one' is a single index name that ends in a number,
    # and 'value of "alias" here' reflects the value of the passed parameter, except
    # where the ``is_write_index`` setting makes it possible to have more than one
    # index associated with a rollover index
    for idx in response:
        if 'is_write_index' in response[idx]['aliases'][alias]:
            if response[idx]['aliases'][alias]['is_write_index']:
                return True
    # implied ``else``: If not ``is_write_index``, it has to fit the following criteria:
    if len(response) > 1:
        logger.error(
            '"alias" must only reference one index, but points to %s', response
        )
        return False
    index = list(response.keys())[0]
    rollable = False
    # In order for `rollable` to be True, the last 2 digits of the index
    # must be digits, or a hyphen followed by a digit.
    # NOTE: This is not a guarantee that the rest of the index name is
    # necessarily correctly formatted.
    if index[-2:][1].isdigit():
        if index[-2:][0].isdigit():
            rollable = True
        elif index[-2:][0] == '-':
            rollable = True
    return rollable