Support CLI for /health and /products

Change-Id: Ic9e6136e29dde21f9dbf3219ed89bd39e763cd1b
This commit is contained in:
Feilong Wang 2017-09-11 15:13:40 +12:00
parent 4159748b34
commit c8066c6115
4 changed files with 159 additions and 2 deletions

68
distilclient/cli.py Normal file
View File

@ -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='<os-rating-api-version>',
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

80
distilclient/v2/cli.py Normal file
View File

@ -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="<regions>",
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)

View File

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

View File

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