From c8066c61158fdab721bcd577f549c6e5c728ec6f Mon Sep 17 00:00:00 2001 From: Feilong Wang Date: Mon, 11 Sep 2017 15:13:40 +1200 Subject: [PATCH] Support CLI for /health and /products Change-Id: Ic9e6136e29dde21f9dbf3219ed89bd39e763cd1b --- distilclient/cli.py | 68 +++++++++++++++++++++++++++++++++++ distilclient/v2/cli.py | 80 ++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 ++ setup.cfg | 10 ++++-- 4 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 distilclient/cli.py create mode 100644 distilclient/v2/cli.py diff --git a/distilclient/cli.py b/distilclient/cli.py new file mode 100644 index 0000000..007b385 --- /dev/null +++ b/distilclient/cli.py @@ -0,0 +1,68 @@ +# Copyright 2017 Catalyst IT Ltd. +# +# 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. + +import logging + +from osc_lib import utils + +LOG = logging.getLogger(__name__) + +DEFAULT_OS_RATING_API_VERSION = '2' +API_VERSION_OPTION = 'os_rating_api_version' +API_NAME = "rating" +API_VERSIONS = { + "2": "distilclient.v2.client.Client", +} + + +def make_client(instance): + """Returns an distil service client""" + distil_client = utils.get_client_class( + API_NAME, + instance._api_version[API_NAME], + API_VERSIONS) + + LOG.debug("Instantiating distil client: {0}".format( + distil_client)) + + kwargs = { + 'session': instance.session, + 'service_type': 'ratingv2', + 'region_name': instance._region_name + } + + distil_endpoint = instance.get_configuration().get('distil_url') + if not distil_endpoint: + distil_endpoint = instance.get_endpoint_for_service_type( + 'ratingv2', + region_name=instance._region_name, + interface=instance._interface + ) + + client = distil_client(distil_endpoint, **kwargs) + return client + + +def build_option_parser(parser): + """Hook to add global options.""" + parser.add_argument( + '--os-rating-api-version', + metavar='', + default=utils.env( + 'OS_RATING_API_VERSION', + default=DEFAULT_OS_RATING_API_VERSION), + help=('Client version, default=' + + DEFAULT_OS_RATING_API_VERSION + + ' (Env: OS_RATING_API_VERSION)')) + return parser diff --git a/distilclient/v2/cli.py b/distilclient/v2/cli.py new file mode 100644 index 0000000..51fb970 --- /dev/null +++ b/distilclient/v2/cli.py @@ -0,0 +1,80 @@ +# Copyright 2017 Catalyst IT Ltd. +# +# 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 collections import namedtuple + + +from osc_lib.command import command +from osc_lib import utils +from oslo_log import log as logging + +from distilclient.i18n import _ + + +class Health(command.Lister): + """Display detailed health status of Distil server""" + + _description = _("Display detailed health status of Distil server") + log = logging.getLogger(__name__ + ".Health") + + def take_action(self, parsed_args): + health = self.app.client_manager.rating.health.get() + columns = ("Indicator", "Status", "Message") + indicators = [] + for k in health["health"].keys(): + indicators.append({"indicator": k, + "status": health["health"][k]["status"], + "message": health["health"][k]["msg"]}) + rows = (utils.get_item_properties(namedtuple('GenericDict', + i.keys())(**i), columns) + for i in indicators) + + return (columns, rows) + + +class ListProducts(command.Lister): + """List available products""" + + _description = _("List available products") + log = logging.getLogger(__name__ + ".ListProducts") + + def get_parser(self, prog_name): + parser = super(ListProducts, self).get_parser(prog_name) + parser.add_argument( + "--regions", + metavar="", + help="Region list separated with commas.") + + return parser + + def take_action(self, parsed_args): + kwargs = {} + columns = ("Region", "Category", "Name", "Rate", "Unit") + if parsed_args.regions is not None: + kwargs["regions"] = parsed_args.regions.split(',') + + data = self.app.client_manager.rating.products.list(**kwargs) + products = [] + for region in data["products"].keys(): + for category in data["products"][region].keys(): + for product in data["products"][region][category]: + formated_product = product.copy() + formated_product["region"] = region + formated_product["category"] = category + products.append(formated_product) + + rows = (utils.get_item_properties(namedtuple('GenericDict', + p.keys())(**p), columns) + for p in products) + return (columns, rows) diff --git a/requirements.txt b/requirements.txt index a966c89..f092407 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,8 @@ six>=1.9.0 # MIT pbr>=1.6 # Apache-2.0 python-keystoneclient>=1.7.0,!=1.8.0,!=2.1.0 # Apache-2.0 +osc-lib>=1.7.0 # Apache-2.0 +python-openstackclient>=3.12.0 # Apache-2.0 +oslo.log>=3.30.0 # Apache-2.0 debtcollector>=1.2.0 # Apache-2.0 \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 69ce3c5..4363927 100644 --- a/setup.cfg +++ b/setup.cfg @@ -24,8 +24,14 @@ setup-hooks = pbr.hooks.setup_hook [entry_points] -console_scripts = - distil = distilclient.shell:main +openstack.cli.extension = + rating = distilclient.cli + +openstack.rating.v2 = + rating_health_get = distilclient.v2.cli:Health + rating_product_list = distilclient.v2.cli:ListProducts + + [files] packages =