diff --git a/cinderclient/v1/contrib/list_extensions.py b/cinderclient/v1/contrib/list_extensions.py new file mode 100644 index 000000000..91fa0405e --- /dev/null +++ b/cinderclient/v1/contrib/list_extensions.py @@ -0,0 +1,47 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from cinderclient import base +from cinderclient import utils + + +class ListExtResource(base.Resource): + @property + def summary(self): + descr = self.description.strip() + if not descr: + return '??' + lines = descr.split("\n") + if len(lines) == 1: + return lines[0] + else: + return lines[0] + "..." + + +class ListExtManager(base.Manager): + resource_class = ListExtResource + + def show_all(self): + return self._list("/extensions", 'extensions') + + +@utils.service_type('volume') +def do_list_extensions(client, _args): + """ + List all the os-api extensions that are available. + """ + extensions = client.list_extensions.show_all() + fields = ["Name", "Summary", "Alias", "Updated"] + utils.print_list(extensions, fields) diff --git a/tests/v1/contrib/__init__.py b/tests/v1/contrib/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/v1/contrib/test_list_extensions.py b/tests/v1/contrib/test_list_extensions.py new file mode 100644 index 000000000..faf10bb83 --- /dev/null +++ b/tests/v1/contrib/test_list_extensions.py @@ -0,0 +1,21 @@ +from cinderclient import extension +from cinderclient.v1.contrib import list_extensions + +from tests import utils +from tests.v1 import fakes + + +extensions = [ + extension.Extension(list_extensions.__name__.split(".")[-1], + list_extensions), +] +cs = fakes.FakeClient(extensions=extensions) + + +class ListExtensionsTests(utils.TestCase): + def test_list_extensions(self): + all_exts = cs.list_extensions.show_all() + cs.assert_called('GET', '/extensions') + self.assertTrue(len(all_exts) > 0) + for r in all_exts: + self.assertTrue(len(r.summary) > 0) diff --git a/tests/v1/fakes.py b/tests/v1/fakes.py index 9c708a78c..4f3be3a09 100644 --- a/tests/v1/fakes.py +++ b/tests/v1/fakes.py @@ -61,7 +61,8 @@ class FakeClient(fakes.FakeClient, client.Client): def __init__(self, *args, **kwargs): client.Client.__init__(self, 'username', 'password', - 'project_id', 'auth_url') + 'project_id', 'auth_url', + extensions=kwargs.get('extensions')) self.client = FakeHTTPClient(**kwargs) @@ -96,7 +97,6 @@ class FakeHTTPClient(base_client.HTTPClient): # Note the call self.callstack.append((method, url, kwargs.get('body', None))) - status, headers, body = getattr(self, callback)(**kwargs) r = utils.TestResponse({ "status_code": status, @@ -267,3 +267,29 @@ class FakeHTTPClient(base_client.HTTPClient): def delete_types_1(self, **kw): return (202, {}, None) + + # + # List all extensions + # + def get_extensions(self, **kw): + exts = [ + { + "alias": "FAKE-1", + "description": "Fake extension number 1", + "links": [], + "name": "Fake1", + "namespace": ("http://docs.openstack.org/" + "/ext/fake1/api/v1.1"), + "updated": "2011-06-09T00:00:00+00:00" + }, + { + "alias": "FAKE-2", + "description": "Fake extension number 2", + "links": [], + "name": "Fake2", + "namespace": ("http://docs.openstack.org/" + "/ext/fake1/api/v1.1"), + "updated": "2011-06-09T00:00:00+00:00" + }, + ] + return (200, {}, {"extensions": exts, })