Merge "Allow deletion of multiple resources for some manila commands"

This commit is contained in:
Jenkins 2016-09-08 13:17:51 +00:00 committed by Gerrit Code Review
commit c67f7d56d4
7 changed files with 273 additions and 34 deletions

View File

@ -101,7 +101,8 @@ class FakeHTTPClient(client.HTTPClient):
raise AssertionError('%r != %r' %
(self.callstack[pos][3], body))
def assert_called_anytime(self, method, url, body=None):
def assert_called_anytime(self, method, url, body=None,
clear_callstack=True):
"""Assert than an API method was called anytime in the test.
"""
expected = (method, url)
@ -121,7 +122,8 @@ class FakeHTTPClient(client.HTTPClient):
if body is not None:
assert entry[3] == body, "%s != %s" % (entry[3], body)
self.callstack = []
if clear_callstack:
self.callstack = []
def clear_callstack(self):
self.callstack = []

View File

@ -59,7 +59,8 @@ class FakeClient(object):
'a': actual
}
def assert_called_anytime(self, method, url, body=None):
def assert_called_anytime(self, method, url, body=None,
clear_callstack=True):
"""Assert than an API method was called anytime in the test."""
expected = (method, url)
@ -83,8 +84,8 @@ class FakeClient(object):
print("!=")
print(body)
raise
self.client.callstack = []
if clear_callstack:
self.client.callstack = []
def clear_callstack(self):
self.client.callstack = []

View File

@ -150,6 +150,15 @@ class FakeHTTPClient(fakes.FakeHTTPClient):
share = {'share': {'id': 1234, 'name': 'sharename'}}
return (200, {}, share)
def get_share_servers_1234(self, **kw):
share_servers = {
'share_servers': {
'id': 1234,
'share_network_id': 'fake_network_id',
},
}
return (200, {}, share_servers)
def get_shares_1111(self, **kw):
share = {'share': {'id': 1111, 'name': 'share1111'}}
return (200, {}, share)
@ -428,6 +437,42 @@ class FakeHTTPClient(fakes.FakeHTTPClient):
def delete_share_servers_1234(self, **kwargs):
return (202, {}, None)
def delete_security_services_fake_security_service1(self, **kwargs):
return (202, {}, None)
def delete_security_services_fake_security_service2(self, **kwargs):
return (202, {}, None)
def delete_share_networks_fake_share_network1(self, **kwargs):
return (202, {}, None)
def delete_share_networks_fake_share_network2(self, **kwargs):
return (202, {}, None)
def delete_snapshots_fake_snapshot1(self, **kwargs):
return (202, {}, None)
def delete_snapshots_fake_snapshot2(self, **kwargs):
return (202, {}, None)
def post_snapshots_fake_snapshot_force1_action(self, **kwargs):
return (202, {}, None)
def post_snapshots_fake_snapshot_force2_action(self, **kwargs):
return (202, {}, None)
def delete_types_fake_type1(self, **kwargs):
return (202, {}, None)
def delete_types_fake_type2(self, **kwargs):
return (202, {}, None)
def delete_share_servers_fake_share_server1(self, **kwargs):
return (202, {}, None)
def delete_share_servers_fake_share_server2(self, **kwargs):
return (202, {}, None)
def put_share_networks_1111(self, **kwargs):
share_network = {'share_network': {'id': 1111}}
return (200, {}, share_network)

View File

@ -95,8 +95,10 @@ class ShellTest(test_utils.TestCase):
def assert_called(self, method, url, body=None, **kwargs):
return self.shell.cs.assert_called(method, url, body, **kwargs)
def assert_called_anytime(self, method, url, body=None):
return self.shell.cs.assert_called_anytime(method, url, body)
def assert_called_anytime(self, method, url, body=None,
clear_callstack=True):
return self.shell.cs.assert_called_anytime(
method, url, body, clear_callstack=clear_callstack)
def test_service_list(self):
self.run_command('service-list')
@ -1314,10 +1316,6 @@ class ShellTest(test_utils.TestCase):
mock.ANY,
fields=['Id', 'Host', 'Status'])
def test_share_server_delete(self):
self.run_command('share-server-delete 1234')
self.assert_called('DELETE', '/share-servers/1234')
def test_create_share(self):
# Use only required fields
self.run_command("create nfs 1")
@ -1964,3 +1962,94 @@ class ShellTest(test_utils.TestCase):
self.run_command(command)
expected = {'reset_task_state': {'task_state': param}}
self.assert_called('POST', '/shares/1234/action', body=expected)
@ddt.data('fake-security-service1',
'fake-security-service1 fake-security-service2')
@mock.patch.object(shell_v2, '_find_security_service', mock.Mock())
def test_security_service_delete(self, args):
args_split = args.split()
shell_v2._find_security_service.side_effect = args_split
self.run_command('security-service-delete %s' % args)
self.assert_called_anytime(
'DELETE', '/security-services/%s' % args_split[0],
clear_callstack=False)
self.assert_called_anytime(
'DELETE', '/security-services/%s' % args_split[-1])
@ddt.data('fake-share-network1',
'fake-share-network1 fake-share-network2')
@mock.patch.object(shell_v2, '_find_share_network', mock.Mock())
def test_share_network_delete(self, args):
args_split = args.split()
shell_v2._find_share_network.side_effect = args_split
self.run_command('share-network-delete %s' % args)
self.assert_called_anytime(
'DELETE', '/share-networks/%s' % args_split[0],
clear_callstack=False)
self.assert_called_anytime(
'DELETE', '/share-networks/%s' % args_split[-1])
@ddt.data('fake-snapshot1',
'fake-snapshot1 fake-snapshot2')
@mock.patch.object(shell_v2, '_find_share_snapshot', mock.Mock())
def test_snapshot_delete(self, args):
args_split = args.split()
shell_v2._find_share_snapshot.side_effect = args_split
self.run_command('snapshot-delete %s' % args)
self.assert_called_anytime(
'DELETE', '/snapshots/%s' % args_split[0],
clear_callstack=False)
self.assert_called_anytime(
'DELETE', '/snapshots/%s' % args_split[-1])
@ddt.data('fake-snapshot-force1',
'fake-snapshot-force1 fake-snapshot-force2')
@mock.patch.object(shell_v2, '_find_share_snapshot', mock.Mock())
def test_snapshot_delete_force(self, args):
args_split = args.split()
shell_v2._find_share_snapshot.side_effect = args_split
self.run_command('snapshot-force-delete %s' % args)
self.assert_called_anytime(
'POST', '/snapshots/%s/action' % args_split[0],
{'force_delete': None}, clear_callstack=False)
self.assert_called_anytime(
'POST', '/snapshots/%s/action' % args_split[-1],
{'force_delete': None})
@ddt.data('fake-type1',
'fake-type1 fake-type2')
@mock.patch.object(shell_v2, '_find_share_type', mock.Mock())
def test_share_type_delete(self, args):
args_split = args.split()
shell_v2._find_share_type.side_effect = args_split
self.run_command('type-delete %s' % args)
self.assert_called_anytime(
'DELETE', '/types/%s' % args_split[0],
clear_callstack=False)
self.assert_called_anytime(
'DELETE', '/types/%s' % args_split[-1])
@ddt.data('fake-share-server1',
'fake-share-server1 fake-share-server2')
@mock.patch.object(shell_v2, '_find_share_server', mock.Mock())
def test_share_server_delete(self, args):
args_split = args.split()
shell_v2._find_share_server.side_effect = args_split
self.run_command('share-server-delete %s' % args)
self.assert_called_anytime(
'DELETE', '/share-servers/%s' % args_split[0],
clear_callstack=False)
self.assert_called_anytime(
'DELETE', '/share-servers/%s' % args_split[-1])

View File

@ -39,7 +39,7 @@ class ShareServer(common_base.Resource):
return super(ShareServer, self).__getattr__(attr)
class ShareServerManager(base.Manager):
class ShareServerManager(base.ManagerWithFind):
"""Manage :class:`ShareServer` resources."""
resource_class = ShareServer

View File

@ -236,6 +236,11 @@ def _find_security_service(cs, security_service):
security_service)
def _find_share_server(cs, share_server):
"""Get a share server by ID."""
return apiclient_utils.find_resource(cs.share_servers, share_server)
def _translate_keys(collection, convert):
for item in collection:
keys = item.__dict__
@ -1776,21 +1781,52 @@ def do_snapshot_rename(cs, args):
@cliutils.arg(
'snapshot',
metavar='<snapshot>',
help='Name or ID of the snapshot to delete.')
nargs='+',
help='Name or ID of the snapshot(s) to delete.')
def do_snapshot_delete(cs, args):
"""Remove a snapshot."""
snapshot = _find_share_snapshot(cs, args.snapshot)
snapshot.delete()
"""Remove one or more snapshots."""
failure_count = 0
for snapshot in args.snapshot:
try:
snapshot_ref = _find_share_snapshot(
cs, snapshot)
cs.share_snapshots.delete(snapshot_ref)
except Exception as e:
failure_count += 1
print("Delete for snapshot %s failed: %s" % (
snapshot, e), file=sys.stderr)
if failure_count == len(args.snapshot):
raise exceptions.CommandError("Unable to delete any of the specified "
"snapshots.")
@cliutils.arg(
'snapshot',
metavar='<snapshot>',
help='Name or ID of the snapshot to force delete.')
nargs='+',
help='Name or ID of the snapshot(s) to force delete.')
def do_snapshot_force_delete(cs, args):
"""Attempt force-delete of snapshot, regardless of state (Admin only)."""
snapshot = _find_share_snapshot(cs, args.snapshot)
snapshot.force_delete()
"""Attempt force-deletion of one or more snapshots.
Regardless of the state (Admin only).
"""
failure_count = 0
for snapshot in args.snapshot:
try:
snapshot_ref = _find_share_snapshot(
cs, snapshot)
cs.share_snapshots.force_delete(snapshot_ref)
except Exception as e:
failure_count += 1
print("Delete for snapshot %s failed: %s" % (
snapshot, e), file=sys.stderr)
if failure_count == len(args.snapshot):
raise exceptions.CommandError("Unable to force delete any of the "
"specified snapshots.")
@cliutils.arg(
@ -2207,10 +2243,25 @@ def do_share_network_security_service_list(cs, args):
@cliutils.arg(
'share_network',
metavar='<share-network>',
help='Name or ID of share network to be deleted.')
nargs='+',
help='Name or ID of share network(s) to be deleted.')
def do_share_network_delete(cs, args):
"""Delete share network."""
_find_share_network(cs, args.share_network).delete()
"""Delete one or more share networks."""
failure_count = 0
for share_network in args.share_network:
try:
share_ref = _find_share_network(
cs, share_network)
cs.share_networks.delete(share_ref)
except Exception as e:
failure_count += 1
print("Delete for share network %s failed: %s" % (
share_network, e), file=sys.stderr)
if failure_count == len(args.share_network):
raise exceptions.CommandError("Unable to delete any of the specified "
"share networks.")
@cliutils.arg(
@ -2445,11 +2496,25 @@ def do_security_service_list(cs, args):
@cliutils.arg(
'security_service',
metavar='<security-service>',
help='Security service name or ID to delete.')
nargs='+',
help='Name or ID of the security service(s) to delete')
def do_security_service_delete(cs, args):
"""Delete security service."""
security_service = _find_security_service(cs, args.security_service)
security_service.delete()
"""Delete one or more security services."""
failure_count = 0
for security_service in args.security_service:
try:
security_ref = _find_security_service(
cs, security_service)
cs.security_services.delete(security_ref)
except Exception as e:
failure_count += 1
print("Delete for security service %s failed: %s" % (
security_service, e), file=sys.stderr)
if failure_count == len(args.security_service):
raise exceptions.CommandError("Unable to delete any of the specified "
"security services.")
@cliutils.arg(
@ -2532,11 +2597,27 @@ def do_share_server_details(cs, args):
@cliutils.arg(
'id',
metavar='<id>',
nargs='+',
type=str,
help='ID of share server.')
help='ID of the share server(s) to delete.')
def do_share_server_delete(cs, args):
"""Delete share server (Admin only)."""
cs.share_servers.delete(args.id)
"""Delete one or more share servers (Admin only)."""
failure_count = 0
for id in args.id:
try:
id_ref = _find_share_server(
cs, id)
cs.share_servers.delete(id_ref)
except Exception as e:
failure_count += 1
print("Delete for share server %s failed: %s" % (
id, e), file=sys.stderr)
if failure_count == len(args.id):
raise exceptions.CommandError("Unable to delete any of the specified "
"share servers.")
@cliutils.arg(
@ -2843,11 +2924,26 @@ def do_type_create(cs, args):
@cliutils.arg(
'id',
metavar='<id>',
help="Name or ID of the share type to delete.")
nargs='+',
help="Name or ID of the share type(s) to delete.")
def do_type_delete(cs, args):
"""Delete a specific share type (Admin only)."""
share_type = _find_share_type(cs, args.id)
cs.share_types.delete(share_type)
"""Delete one or more specific share types (Admin only)."""
failure_count = 0
for id in args.id:
try:
id_ref = _find_share_type(
cs, id)
cs.share_types.delete(id_ref)
except Exception as e:
failure_count += 1
print("Delete for share type %s failed: %s" % (
id, e), file=sys.stderr)
if failure_count == len(args.id):
raise exceptions.CommandError("Unable to delete any of the specified "
"share types.")
@cliutils.arg(

View File

@ -0,0 +1,6 @@
---
fixes:
- Fixed 6 delete commands namely security-service-delete,
share-network-delete, share-server-delete, snapshot-delete,
snapshot-force-delete, and type-delete to allow deletion of
multiple resources together.