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
This commit is contained in:
Joshua Harlow 2012-09-18 16:36:17 -07:00
parent bfb0f70f40
commit ccc4291c52
4 changed files with 116 additions and 1 deletions

View File

@ -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)

View File

View File

@ -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)

View File

@ -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
#