<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> from salt . exceptions import CommandExecutionError <EOL> import logging <EOL> log = logging . getLogger ( __name__ ) <EOL> try : <EOL> import elasticsearch <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( logging . CRITICAL ) <EOL> HAS_ELASTICSEARCH = True <EOL> except ImportError : <EOL> HAS_ELASTICSEARCH = False <EOL> from salt . ext . six import string_types <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if not HAS_ELASTICSEARCH : <EOL> return ( False , '<STR_LIT>' ) <EOL> return True <EOL> def _get_instance ( hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = None <EOL> if profile is None : <EOL> profile = '<STR_LIT>' <EOL> if isinstance ( profile , string_types ) : <EOL> _profile = __salt__ [ '<STR_LIT>' ] ( profile , None ) <EOL> elif isinstance ( profile , dict ) : <EOL> _profile = profile <EOL> if _profile : <EOL> hosts = _profile . get ( '<STR_LIT:host>' , None ) <EOL> if not hosts : <EOL> hosts = _profile . get ( '<STR_LIT>' , None ) <EOL> if not hosts : <EOL> hosts = [ '<STR_LIT>' ] <EOL> if isinstance ( hosts , string_types ) : <EOL> hosts = [ hosts ] <EOL> try : <EOL> es = elasticsearch . Elasticsearch ( hosts ) <EOL> if not es . ping ( ) : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( hosts ) ) <EOL> except elasticsearch . exceptions . ConnectionError : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( hosts ) ) <EOL> return es <EOL> def alias_create ( indices , alias , hosts = None , body = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> result = es . indices . put_alias ( index = indices , name = alias , body = body ) <EOL> return True <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def alias_delete ( indices , aliases , hosts = None , body = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> result = es . indices . delete_alias ( index = indices , name = aliases ) <EOL> if result . get ( '<STR_LIT>' , False ) : <EOL> return True <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def alias_exists ( aliases , indices = None , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> if es . indices . exists_alias ( name = aliases , index = indices ) : <EOL> return True <EOL> else : <EOL> return False <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> except elasticsearch . exceptions . ConnectionError : <EOL> return None <EOL> return None <EOL> def alias_get ( indices = None , aliases = None , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> ret = es . indices . get_alias ( index = indices , name = aliases ) <EOL> return ret <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def document_create ( index , doc_type , body = None , id = None , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> result = es . index ( index = index , doc_type = doc_type , body = body , id = id ) <EOL> return True <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def document_delete ( index , doc_type , id , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> if not index_exists ( index = index ) : <EOL> return True <EOL> else : <EOL> result = es . delete ( index = index , doc_type = doc_type , id = id ) <EOL> if result . get ( '<STR_LIT>' , False ) : <EOL> return True <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def document_exists ( index , id , doc_type = '<STR_LIT>' , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> if es . exists ( index = index , id = id , doc_type = doc_type ) : <EOL> return True <EOL> else : <EOL> return False <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> except elasticsearch . exceptions . ConnectionError : <EOL> return None <EOL> return None <EOL> def document_get ( index , id , doc_type = '<STR_LIT>' , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> ret = es . get ( index = index , id = id , doc_type = doc_type ) <EOL> return ret <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def index_create ( index , body = None , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> if index_exists ( index ) : <EOL> return True <EOL> else : <EOL> result = es . indices . create ( index = index , body = body ) <EOL> return True <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def index_delete ( index , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> if not index_exists ( index = index ) : <EOL> return True <EOL> else : <EOL> result = es . indices . delete ( index = index ) <EOL> if result . get ( '<STR_LIT>' , False ) : <EOL> return True <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def index_exists ( index , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> if not isinstance ( index , list ) : <EOL> index = [ index ] <EOL> if es . indices . exists ( index = index ) : <EOL> return True <EOL> else : <EOL> return False <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> except elasticsearch . exceptions . ConnectionError : <EOL> return None <EOL> return None <EOL> def index_get ( index , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> if index_exists ( index ) : <EOL> ret = es . indices . get ( index = index ) <EOL> return ret <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def mapping_create ( index , doc_type , body , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> result = es . indices . put_mapping ( index = index , doc_type = doc_type , body = body ) <EOL> return mapping_get ( index , doc_type ) <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def mapping_delete ( index , doc_type , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> result = es . indices . delete_mapping ( index = index , doc_type = doc_type ) <EOL> if result . get ( '<STR_LIT>' , False ) : <EOL> return True <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def mapping_get ( index , doc_type , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> ret = es . indices . get_mapping ( index = index , doc_type = doc_type ) <EOL> return ret <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def index_template_create ( name , body , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> result = es . indices . put_template ( name = name , body = body ) <EOL> return True <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def index_template_delete ( name , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> result = es . indices . delete_template ( name = name ) <EOL> if result . get ( '<STR_LIT>' , False ) : <EOL> return True <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def index_template_exists ( name , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> if es . indices . exists_template ( name = name ) : <EOL> return True <EOL> else : <EOL> return False <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None <EOL> def index_template_get ( name , hosts = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> es = _get_instance ( hosts , profile ) <EOL> try : <EOL> ret = es . indices . get_template ( name = name ) <EOL> return ret <EOL> except elasticsearch . exceptions . NotFoundError : <EOL> return None <EOL> return None </s>
