From fdc12bd65027ecf28a78734b3046f20e5f90893f Mon Sep 17 00:00:00 2001 From: Yong Sheng Gong Date: Sun, 12 Aug 2012 07:33:10 +0800 Subject: [PATCH] add ext list and show commands. Change-Id: I3bdf1a3b066ee12572468b8d7abee96eb07f9257 --- quantumclient/quantum/v2_0/extension.py | 95 +++++++++++++++++++++++++ quantumclient/shell.py | 4 ++ quantumclient/v2_0/client.py | 12 ++++ 3 files changed, 111 insertions(+) create mode 100644 quantumclient/quantum/v2_0/extension.py diff --git a/quantumclient/quantum/v2_0/extension.py b/quantumclient/quantum/v2_0/extension.py new file mode 100644 index 000000000..00fa1fba7 --- /dev/null +++ b/quantumclient/quantum/v2_0/extension.py @@ -0,0 +1,95 @@ +# Copyright 2012 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. +# +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +import logging + +from cliff import lister +from cliff import show + +from quantumclient.common import utils +from quantumclient.quantum.v2_0 import QuantumCommand + + +class ListExt(QuantumCommand, lister.Lister): + """List all exts.""" + + api = 'network' + resource = 'extension' + log = logging.getLogger(__name__ + '.ListExt') + _formatters = None + + def get_parser(self, prog_name): + parser = super(ListExt, self).get_parser(prog_name) + return parser + + def get_data(self, parsed_args): + self.log.debug('get_data(%s)' % parsed_args) + quantum_client = self.get_client() + search_opts = {} + quantum_client.format = parsed_args.request_format + obj_lister = getattr(quantum_client, + "list_%ss" % self.resource) + data = obj_lister(**search_opts) + info = [] + collection = self.resource + "s" + if collection in data: + info = data[collection] + _columns = len(info) > 0 and sorted(info[0].keys()) or [] + return (_columns, (utils.get_item_properties(s, _columns) + for s in info)) + + +class ShowExt(QuantumCommand, show.ShowOne): + """Show information of a given resource + + """ + api = 'network' + resource = "extension" + log = logging.getLogger(__name__ + '.ShowExt') + + def get_parser(self, prog_name): + parser = super(ShowExt, self).get_parser(prog_name) + parser.add_argument( + 'ext_alias', metavar='ext_alias', + help='the extension alias') + return parser + + def get_data(self, parsed_args): + self.log.debug('get_data(%s)' % parsed_args) + quantum_client = self.get_client() + quantum_client.format = parsed_args.request_format + params = {} + obj_shower = getattr(quantum_client, + "show_%s" % self.resource) + data = obj_shower(parsed_args.ext_alias, **params) + if self.resource in data: + for k, v in data[self.resource].iteritems(): + if isinstance(v, list): + value = "" + for _item in v: + if value: + value += "\n" + if isinstance(_item, dict): + value += utils.dumps(_item) + else: + value += str(_item) + data[self.resource][k] = value + elif v is None: + data[self.resource][k] = '' + return zip(*sorted(data[self.resource].iteritems())) + else: + return None diff --git a/quantumclient/shell.py b/quantumclient/shell.py index a80784014..1790e81c2 100644 --- a/quantumclient/shell.py +++ b/quantumclient/shell.py @@ -91,6 +91,10 @@ COMMAND_V2 = { 'quantumclient.quantum.v2_0.quota.DeleteQuota'), 'quota-update': utils.import_class( 'quantumclient.quantum.v2_0.quota.UpdateQuota'), + 'ext-list': utils.import_class( + 'quantumclient.quantum.v2_0.extension.ListExt'), + 'ext-show': utils.import_class( + 'quantumclient.quantum.v2_0.extension.ShowExt'), } COMMANDS = {'2.0': COMMAND_V2} diff --git a/quantumclient/v2_0/client.py b/quantumclient/v2_0/client.py index 8cc78d211..23766daf2 100644 --- a/quantumclient/v2_0/client.py +++ b/quantumclient/v2_0/client.py @@ -156,6 +156,8 @@ class Client(object): subnet_path = "/subnets/%s" quotas_path = "/quotas" quota_path = "/quotas/%s" + exts_path = "/extensions" + ext_path = "/extensions/%s" @APIParamsCall def get_quotas_tenant(self, **_params): @@ -183,6 +185,16 @@ class Client(object): """Delete the specified tenant's quota values.""" return self.delete(self.quota_path % (tenant_id)) + @APIParamsCall + def list_extensions(self, **_params): + """Fetch a list of all exts on server side.""" + return self.get(self.exts_path, params=_params) + + @APIParamsCall + def show_extension(self, ext_alias, **_params): + """Fetch a list of all exts on server side.""" + return self.get(self.ext_path % ext_alias, params=_params) + @APIParamsCall def list_ports(self, **_params): """