Skip to content

Commit

Permalink
Check for index/alias association before deleting
Browse files Browse the repository at this point in the history
fixes #887
  • Loading branch information
untergeek committed Feb 24, 2017
1 parent 4826670 commit b6860cc
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 24 deletions.
21 changes: 17 additions & 4 deletions curator/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,24 @@ def remove(self, ilo, warn_if_no_indices=False):
else:
# Re-raise the NoIndices so it will behave as before
raise NoIndices
aliases = self.client.indices.get_aliases()
for index in ilo.working_list():
self.loggit.debug(
'Removing index {0} from alias {1}'.format(index, self.name))
self.actions.append(
{ 'remove' : { 'index' : index, 'alias': self.name } })
if index in aliases:
self.loggit.debug(
'Index {0} in get_aliases output'.format(index))
# Only remove if the index is associated with the alias
if self.name in aliases[index]['aliases']:
self.loggit.debug(
'Removing index {0} from alias '
'{1}'.format(index, self.name)
)
self.actions.append(
{ 'remove' : { 'index' : index, 'alias': self.name } })
else:
self.loggit.debug(
'Can not remove: Index {0} is not associated with alias'
' {1}'.format(index, self.name)
)

def body(self):
"""
Expand Down
5 changes: 5 additions & 0 deletions docs/Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Changelog
with a logged warning, even if the filters result in a NoIndices condition.
Use with care.

**Bug Fixes**

* Check if an index is in an alias before attempting to delete it from the
alias. Issue raised in #887. (untergeek)

**Documentation**

* Add missing repository arg to auto-gen API docs. Reported in #888
Expand Down
22 changes: 22 additions & 0 deletions test/integration/test_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,28 @@ def test_remove_with_empty_list(self):
{'dummy': {'aliases': {alias: {}}}, 'my_index': {'aliases': {}}},
self.client.indices.get_alias()
)
def test_remove_index_not_in_alias(self):
alias = 'testalias'
self.write_config(
self.args['configfile'], testvars.client_config.format(host, port))
self.write_config(self.args['actionfile'],
testvars.alias_remove_index_not_there.format(alias,'my'))
self.create_index('my_index1')
self.create_index('my_index2')
self.client.indices.put_alias(index='my_index1', name=alias)
test = clicktest.CliRunner()
result = test.invoke(
curator.cli,
[
'--config', self.args['configfile'],
self.args['actionfile']
],
)
self.assertEqual(
{'my_index1': {'aliases': {}}, 'my_index2': {'aliases': {}}},
self.client.indices.get_aliases()
)
self.assertEqual(0, result.exit_code)
def test_no_add_remove(self):
alias = 'testalias'
self.write_config(
Expand Down
15 changes: 15 additions & 0 deletions test/integration/testvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@
' kind: prefix\n'
' value: my\n')

alias_remove_index_not_there = ('---\n'
'actions:\n'
' 1:\n'
' description: "Add/remove specified indices from designated alias"\n'
' action: alias\n'
' options:\n'
' name: {0}\n'
' continue_if_exception: False\n'
' disable_action: False\n'
' remove:\n'
' filters:\n'
' - filtertype: pattern\n'
' kind: prefix\n'
' value: {1}\n')

alias_add_with_empty_remove = ('---\n'
'actions:\n'
' 1:\n'
Expand Down
21 changes: 4 additions & 17 deletions test/unit/test_action_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ def test_remove_single(self):
client.indices.get_settings.return_value = testvars.settings_one
client.cluster.state.return_value = testvars.clu_state_one
client.indices.stats.return_value = testvars.stats_one
client.indices.get_aliases.return_value = testvars.settings_1_get_aliases
ilo = curator.IndexList(client)
ao = curator.Alias(name='alias')
ao = curator.Alias(name='my_alias')
ao.remove(ilo)
self.assertEqual(testvars.alias_one_rm, ao.actions)
def test_add_multiple(self):
Expand All @@ -76,26 +77,12 @@ def test_remove_multiple(self):
client.indices.get_settings.return_value = testvars.settings_two
client.cluster.state.return_value = testvars.clu_state_two
client.indices.stats.return_value = testvars.stats_two
client.indices.get_aliases.return_value = testvars.settings_2_get_aliases
ilo = curator.IndexList(client)
ao = curator.Alias(name='alias')
ao = curator.Alias(name='my_alias')
ao.remove(ilo)
cmp = sorted(ao.actions, key=lambda k: k['remove']['index'])
self.assertEqual(testvars.alias_two_rm, cmp)
def test_show_body(self):
client = Mock()
client.info.return_value = {'version': {'number': '2.4.1'} }
client.indices.get_settings.return_value = testvars.settings_one
client.cluster.state.return_value = testvars.clu_state_one
client.indices.stats.return_value = testvars.stats_one
ilo = curator.IndexList(client)
ao = curator.Alias(name='alias')
ao.remove(ilo)
ao.add(ilo)
body = ao.body()
self.assertEqual(
testvars.alias_one_body['actions'][0], body['actions'][0])
self.assertEqual(
testvars.alias_one_body['actions'][1], body['actions'][1])
def test_raise_on_empty_body(self):
client = Mock()
client.info.return_value = {'version': {'number': '2.4.1'} }
Expand Down
8 changes: 5 additions & 3 deletions test/unit/testvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'filter' : { 'term' : { 'user' : 'kimchy' }}
}
}]
alias_one_rm = [{'remove': {'alias': 'alias', 'index': 'index_name'}}]
alias_one_rm = [{'remove': {'alias': 'my_alias', 'index': named_index}}]
alias_one_body = { "actions" : [
{'remove': {'alias': 'alias', 'index': 'index_name'}},
{'add': {'alias': 'alias', 'index': 'index_name'}}
Expand All @@ -38,8 +38,8 @@
{'add': {'alias': 'alias', 'index': 'index-2016.03.04'}},
]
alias_two_rm = [
{'remove': {'alias': 'alias', 'index': 'index-2016.03.03'}},
{'remove': {'alias': 'alias', 'index': 'index-2016.03.04'}},
{'remove': {'alias': 'my_alias', 'index': 'index-2016.03.03'}},
{'remove': {'alias': 'my_alias', 'index': 'index-2016.03.04'}},
]
alias_success = { "acknowledged": True }
allocation_in = {named_index: {'settings': {'index': {'routing': {'allocation': {'require': {'foo': 'bar'}}}}}}}
Expand Down Expand Up @@ -183,6 +183,8 @@
}
}

settings_1_get_aliases = { named_index: { "aliases" : { 'my_alias' : { } } } }

settings_two = {
u'index-2016.03.03': {
u'state': u'open',
Expand Down

0 comments on commit b6860cc

Please sign in to comment.