From 9ba6ed5a90fcdb8f5d8eb84ebbeab32bd73b5cec Mon Sep 17 00:00:00 2001 From: Clay Gerrard <clay.gerrard@gmail.com> Date: Wed, 5 Sep 2012 14:51:57 +0000 Subject: [PATCH] Add filter options to list and snapshot-list Change-Id: I9a8b538ad53960700ae7a57d1ceb09b05e3dc468 --- cinderclient/v1/shell.py | 38 +++++++++++++++++++++++++++-- cinderclient/v1/volume_snapshots.py | 1 + tests/fakes.py | 18 +++++++------- tests/v1/fakes.py | 23 +++++++++++++++++ tests/v1/test_shell.py | 21 ++++++++++++++++ 5 files changed, 90 insertions(+), 11 deletions(-) diff --git a/cinderclient/v1/shell.py b/cinderclient/v1/shell.py index 69bed6c48..b8bf4333a 100644 --- a/cinderclient/v1/shell.py +++ b/cinderclient/v1/shell.py @@ -117,11 +117,25 @@ def _extract_metadata(arg_list): type=int, const=1, help=argparse.SUPPRESS) +@utils.arg( + '--display-name', + metavar='<display-name>', + default=None, + help='Filter results by display-name') +@utils.arg( + '--status', + metavar='<status>', + default=None, + help='Filter results by status') @utils.service_type('volume') def do_list(cs, args): """List all the volumes.""" all_tenants = int(os.environ.get("ALL_TENANTS", args.all_tenants)) - search_opts = {'all_tenants': all_tenants} + search_opts = { + 'all_tenants': all_tenants, + 'display_name': args.display_name, + 'status': args.status, + } volumes = cs.volumes.list(search_opts=search_opts) _translate_volume_keys(volumes) @@ -240,11 +254,31 @@ def do_delete(cs, args): type=int, const=1, help=argparse.SUPPRESS) +@utils.arg( + '--display-name', + metavar='<display-name>', + default=None, + help='Filter results by display-name') +@utils.arg( + '--status', + metavar='<status>', + default=None, + help='Filter results by status') +@utils.arg( + '--volume-id', + metavar='<volume-id>', + default=None, + help='Filter results by volume-id') @utils.service_type('volume') def do_snapshot_list(cs, args): """List all the snapshots.""" all_tenants = int(os.environ.get("ALL_TENANTS", args.all_tenants)) - search_opts = {'all_tenants': all_tenants} + search_opts = { + 'all_tenants': all_tenants, + 'display_name': args.display_name, + 'status': args.status, + 'volume_id': args.volume_id, + } snapshots = cs.volume_snapshots.list(search_opts=search_opts) _translate_volume_snapshot_keys(snapshots) diff --git a/cinderclient/v1/volume_snapshots.py b/cinderclient/v1/volume_snapshots.py index f283fdc9e..59725503b 100644 --- a/cinderclient/v1/volume_snapshots.py +++ b/cinderclient/v1/volume_snapshots.py @@ -17,6 +17,7 @@ Volume snapshot interface (1.1 extension). """ +import urllib from cinderclient import base diff --git a/tests/fakes.py b/tests/fakes.py index 58f632839..04b40a4b9 100644 --- a/tests/fakes.py +++ b/tests/fakes.py @@ -20,18 +20,18 @@ def assert_has_keys(dict, required=[], optional=[]): class FakeClient(object): - def assert_called(self, method, url, body=None, pos=-1): + def assert_called(self, method, url, body=None, pos=-1, **kwargs): """ Assert than an API method was just called. """ expected = (method, url) called = self.client.callstack[pos][0:2] - assert(self.client.callstack, - "Expected %s %s but no calls were made." % expected) + assert self.client.callstack, ("Expected %s %s but no calls " + "were made." % expected) - assert (expected == called, 'Expected %s %s; got %s %s' % - (expected + called)) + assert expected == called, 'Expected %s %s; got %s %s' % ( + expected + called) if body is not None: assert self.client.callstack[pos][2] == body @@ -42,8 +42,8 @@ class FakeClient(object): """ expected = (method, url) - assert(self.client.callstack, - "Expected %s %s but no calls were made." % expected) + assert self.client.callstack, ("Expected %s %s but no calls " + "were made." % expected) found = False for entry in self.client.callstack: @@ -51,8 +51,8 @@ class FakeClient(object): found = True break - assert(found, 'Expected %s %s; got %s' % - (expected, self.client.callstack)) + assert found, 'Expected %s %s; got %s' % ( + expected, self.client.callstack) if body is not None: try: diff --git a/tests/v1/fakes.py b/tests/v1/fakes.py index d93c9a46f..61741b125 100644 --- a/tests/v1/fakes.py +++ b/tests/v1/fakes.py @@ -21,6 +21,20 @@ from cinderclient.v1 import client from tests import fakes +def _stub_snapshot(**kwargs): + snapshot = { + "created_at": "2012-08-28T16:30:31.000000", + "display_description": None, + "display_name": None, + "id": '11111111-1111-1111-1111-111111111111', + "size": 1, + "status": "available", + "volume_id": '00000000-0000-0000-0000-000000000000', + } + snapshot.update(kwargs) + return snapshot + + class FakeClient(fakes.FakeClient, client.Client): def __init__(self, *args, **kwargs): @@ -122,6 +136,15 @@ class FakeHTTPClient(base_client.HTTPClient): "maxPersonality": 5, "maxPersonalitySize": 10240}, }, }) + # + # Snapshots + # + + def get_snapshots_detail(self, **kw): + return (200, {'snapshots': [ + _stub_snapshot(), + ]}) + # # Servers # diff --git a/tests/v1/test_shell.py b/tests/v1/test_shell.py index 7efad0e60..007afc73c 100644 --- a/tests/v1/test_shell.py +++ b/tests/v1/test_shell.py @@ -69,9 +69,30 @@ class ShellTest(utils.TestCase): # NOTE(jdg): we default to detail currently self.assert_called('GET', '/volumes/detail') + def test_list_filter_status(self): + self.run_command('list --status=available') + self.assert_called('GET', '/volumes/detail?status=available') + + def test_list_filter_display_name(self): + self.run_command('list --display-name=1234') + self.assert_called('GET', '/volumes/detail?display_name=1234') + + def test_list_all_tenants(self): + self.run_command('list --all-tenants=1') + self.assert_called('GET', '/volumes/detail?all_tenants=1') + def test_show(self): self.run_command('show 1234') self.assert_called('GET', '/volumes/1234') def test_delete(self): self.run_command('delete 1234') + + def test_snapshot_list_filter_volume_id(self): + self.run_command('snapshot-list --volume-id=1234') + self.assert_called('GET', '/snapshots/detail?volume_id=1234') + + def test_snapshot_list_filter_status_and_volume_id(self): + self.run_command('snapshot-list --status=available --volume-id=1234') + self.assert_called('GET', '/snapshots/detail?' + 'status=available&volume_id=1234')