Add --all-tenants option to 'nova delete'

Currently, the all_tenants=1 search option is being passed all the
time for 'nova delete' commands in order to enable 'nova delete' by
name to work across tenants, for those that have all_tenants access
in the nova policy.json. This however breaks all 'nova delete' for
non-admins when policy has been configured to allow only admin to
list servers across all_tenants.

This patch changes 'nova delete' to take an option --all-tenants to
get the functionality to delete by name across tenants. This is
similar to how 'nova list --all-tenants' works.

Closes-Bug: #1439381

Change-Id: I204daaf5c0f4dab7c93ef0bd85ffab3529ca352a
This commit is contained in:
melanie witt 2015-04-02 00:03:30 +00:00
parent 3681c186f7
commit 14cada7d0d
2 changed files with 21 additions and 3 deletions
novaclient
tests/unit/v2
v2

@ -1119,15 +1119,27 @@ class ShellTest(utils.TestCase):
self.assert_called('DELETE', '/servers/5678', pos=-1)
self.run_command('delete sample-server sample-server2')
self.assert_called('GET',
'/servers?all_tenants=1&name=sample-server', pos=-6)
'/servers?name=sample-server', pos=-6)
self.assert_called('GET', '/servers/1234', pos=-5)
self.assert_called('DELETE', '/servers/1234', pos=-4)
self.assert_called('GET',
'/servers?all_tenants=1&name=sample-server2',
'/servers?name=sample-server2',
pos=-3)
self.assert_called('GET', '/servers/5678', pos=-2)
self.assert_called('DELETE', '/servers/5678', pos=-1)
def test_delete_two_with_two_existent_all_tenants(self):
self.run_command('delete sample-server sample-server2 --all-tenants')
self.assert_called('GET',
'/servers?all_tenants=1&name=sample-server', pos=0)
self.assert_called('GET', '/servers/1234', pos=1)
self.assert_called('DELETE', '/servers/1234', pos=2)
self.assert_called('GET',
'/servers?all_tenants=1&name=sample-server2',
pos=3)
self.assert_called('GET', '/servers/5678', pos=4)
self.assert_called('DELETE', '/servers/5678', pos=5)
def test_delete_two_with_one_nonexistent(self):
cmd = 'delete 1234 123456789'
self.assertRaises(exceptions.CommandError, self.run_command, cmd)

@ -1872,12 +1872,18 @@ def do_show(cs, args):
_print_server(cs, args)
@cliutils.arg(
'--all-tenants',
action='store_const',
const=1,
default=0,
help=_('Delete server(s) in another tenant by name (Admin only).'))
@cliutils.arg(
'server', metavar='<server>', nargs='+',
help=_('Name or ID of server(s).'))
def do_delete(cs, args):
"""Immediately shut down and delete specified server(s)."""
find_args = {'all_tenants': '1'}
find_args = {'all_tenants': args.all_tenants}
utils.do_action_on_many(
lambda s: _find_server(cs, s, **find_args).delete(),
args.server,