From ccc4291c52eb4f68e37691bbe7df34450e89df7a Mon Sep 17 00:00:00 2001 From: Joshua Harlow <harlowja@yahoo-inc.com> Date: Tue, 18 Sep 2012 16:36:17 -0700 Subject: [PATCH] Add simple os-api extension cli extension Add a useful extension that will show you what openstack api extensions are available for usage and print out the result into a nice little table. Useful as a example for others to base contrib/ extensions off of. Example @ http://paste.openstack.org/show/20989/ Change-Id: I5b72f5ea73c00f1c1a0f09f670d744c820e05837 --- novaclient/v1_1/contrib/list_extensions.py | 46 ++++++++++++++++++++ tests/v1_1/contrib/__init__.py | 0 tests/v1_1/contrib/test_list_extensions.py | 21 +++++++++ tests/v1_1/fakes.py | 50 +++++++++++++++++++++- 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 novaclient/v1_1/contrib/list_extensions.py create mode 100644 tests/v1_1/contrib/__init__.py create mode 100644 tests/v1_1/contrib/test_list_extensions.py diff --git a/novaclient/v1_1/contrib/list_extensions.py b/novaclient/v1_1/contrib/list_extensions.py new file mode 100644 index 000000000..f184238dc --- /dev/null +++ b/novaclient/v1_1/contrib/list_extensions.py @@ -0,0 +1,46 @@ +# 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 novaclient import base +from novaclient 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') + + +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_1/contrib/__init__.py b/tests/v1_1/contrib/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/v1_1/contrib/test_list_extensions.py b/tests/v1_1/contrib/test_list_extensions.py new file mode 100644 index 000000000..8b0651158 --- /dev/null +++ b/tests/v1_1/contrib/test_list_extensions.py @@ -0,0 +1,21 @@ +from novaclient import extension +from novaclient.v1_1.contrib import list_extensions + +from tests import utils +from tests.v1_1 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_1/fakes.py b/tests/v1_1/fakes.py index 30af70f3b..fddd44416 100644 --- a/tests/v1_1/fakes.py +++ b/tests/v1_1/fakes.py @@ -25,7 +25,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) @@ -67,6 +68,53 @@ class FakeHTTPClient(base_client.HTTPClient): else: return httplib2.Response({"status": status}), body + # + # List all extensions + # + + def get_extensions(self, **kw): + exts = [ + { + "alias": "NMN", + "description": "Multiple network support", + "links": [], + "name": "Multinic", + "namespace": ("http://docs.openstack.org/" + "compute/ext/multinic/api/v1.1"), + "updated": "2011-06-09T00:00:00+00:00" + }, + { + "alias": "OS-DCF", + "description": "Disk Management Extension", + "links": [], + "name": "DiskConfig", + "namespace": ("http://docs.openstack.org/" + "compute/ext/disk_config/api/v1.1"), + "updated": "2011-09-27T00:00:00+00:00" + }, + { + "alias": "OS-EXT-SRV-ATTR", + "description": "Extended Server Attributes support.", + "links": [], + "name": "ExtendedServerAttributes", + "namespace": ("http://docs.openstack.org/" + "compute/ext/extended_status/api/v1.1"), + "updated": "2011-11-03T00:00:00+00:00" + }, + { + "alias": "OS-EXT-STS", + "description": "Extended Status support", + "links": [], + "name": "ExtendedStatus", + "namespace": ("http://docs.openstack.org/" + "compute/ext/extended_status/api/v1.1"), + "updated": "2011-11-03T00:00:00+00:00" + }, + ] + return (200, { + "extensions": exts, + }) + # # Limits #