Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ES index settings methods #53737

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions salt/modules/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,60 @@ def index_close(index, allow_no_indices=True, expand_wildcards='open', ignore_un
raise CommandExecutionError("Cannot close index {0}, server returned code {1} with message {2}".format(index, e.status_code, e.error))


def index_settings_get(index, hosts=None, profile=None):
'''
Check for the existence of an index and if it exists, return its settings

index
Index name

CLI example::

salt myminion elasticsearch.index_settings_get testindex
'''
es = _get_instance(hosts, profile)

try:
return es.indices.get_settings(index=index)
except elasticsearch.exceptions.NotFoundError:
return None
except elasticsearch.TransportError as e:
raise CommandExecutionError("Cannot retrieve index settings {0}, server returned code {1} with message {2}".format(index, e.status_code, e.error))


def index_settings_put(index, body=None, hosts=None, profile=None, source=None):
'''
Update existing index settings

index
Index name
body
Index definition, such as settings and mappings as defined in https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
source
URL to file specifying index definition. Cannot be used in combination with ``body``.

CLI example::

salt myminion elasticsearch.index_settings_put testindex '{"settings" : {"index" : {"number_of_replicas" : 2}}}'
'''
es = _get_instance(hosts, profile)
if source and body:
message = 'Either body or source should be specified but not both.'
raise SaltInvocationError(message)
if source:
body = __salt__['cp.get_file_str'](
source,
saltenv=__opts__.get('saltenv', 'base'))

try:
result = es.indices.put_settings(index=index, body=body)
return result.get('acknowledged', False)
except elasticsearch.exceptions.NotFoundError:
return None
except elasticsearch.TransportError as e:
raise CommandExecutionError("Cannot update index settings {0}, server returned code {1} with message {2}".format(index, e.status_code, e.error))


def mapping_create(index, doc_type, body=None, hosts=None, profile=None, source=None):
'''
Create a mapping in a given index
Expand Down
144 changes: 144 additions & 0 deletions tests/unit/modules/test_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,150 @@ class MockElastic(object):
MagicMock(return_value=MockElastic())):
self.assertRaises(CommandExecutionError, elasticsearch.index_exists, "foo", "bar")

def test_settings_get(self):
'''
Test if settings can be obtained
'''
class MockElasticIndices(object):
'''
Mock of Elasticsearch IndicesClient
'''
def get_settings(self, index=None):
'''
Mock of get_settings method
'''
return {"test": "key"}

class MockElastic(object):
'''
Mock of Elasticsearch client
'''
indices = MockElasticIndices()

with patch.object(elasticsearch, '_get_instance',
MagicMock(return_value=MockElastic())):
self.assertDictEqual(elasticsearch.index_settings_get("foo", "bar"), {"test": "key"})

def test_settings_get_not(self):
'''
Test if index doesn't exist
'''
class MockElasticIndices(object):
'''
Mock of Elasticsearch IndicesClient
'''
def get_settings(self, index=None):
'''
Mock of get_settings method
'''
raise NotFoundError

class MockElastic(object):
'''
Mock of Elasticsearch client
'''
indices = MockElasticIndices()

with patch.object(elasticsearch, '_get_instance',
MagicMock(return_value=MockElastic())):
self.assertIs(elasticsearch.index_settings_get("foo", "bar"), None)

def test_settings_get_failure(self):
'''
Test if index obtain fails with CommandExecutionError
'''
class MockElasticIndices(object):
'''
Mock of Elasticsearch IndicesClient
'''
def get_settings(self, index=None):
'''
Mock of get_settings method
'''
raise TransportError("custom message", 123)

class MockElastic(object):
'''
Mock of Elasticsearch client
'''
indices = MockElasticIndices()

with patch.object(elasticsearch, '_get_instance',
MagicMock(return_value=MockElastic())):
self.assertRaises(CommandExecutionError, elasticsearch.index_settings_get, "foo", "bar")

def test_index_settings_put(self):
'''
Test if we can put settings for the index
'''
class MockElasticIndices(object):
'''
Mock of Elasticsearch IndicesClient
'''
def put_settings(self, index=None, body=None):
'''
Mock of put_settings method
'''
return {"acknowledged": True}

class MockElastic(object):
'''
Mock of Elasticsearch client
'''
indices = MockElasticIndices()

with patch.object(elasticsearch, '_get_instance',
MagicMock(return_value=MockElastic())):
self.assertTrue(elasticsearch.index_settings_put("foo", "bar"))

def test_index_settings_put_not(self):
'''
Test if settings put returned False
'''
class MockElasticIndices(object):
'''
Mock of Elasticsearch IndicesClient
'''
def put_settings(self, index=None, body=None):
'''
Mock of put_settings method
'''
return {"acknowledged": False}

class MockElastic(object):
'''
Mock of Elasticsearch client
'''
indices = MockElasticIndices()

with patch.object(elasticsearch, '_get_instance',
MagicMock(return_value=MockElastic())):
self.assertFalse(elasticsearch.index_settings_put("foo", "bar"))

def test_index_settings_put_failure(self):
'''
Test if settings put failed with CommandExecutionError
'''
class MockElasticIndices(object):
'''
Mock of Elasticsearch IndicesClient
'''
def put_settings(self, index=None, body=None):
'''
Mock of put_settings method
'''
raise TransportError("custom message", 123)

class MockElastic(object):
'''
Mock of Elasticsearch client
'''
indices = MockElasticIndices()

with patch.object(elasticsearch, '_get_instance',
MagicMock(return_value=MockElastic())):
self.assertRaises(CommandExecutionError, elasticsearch.index_settings_put, "foo", "bar")

# 'index_get' function tests: 3

def test_index_get(self):
Expand Down