From 3deddd8ce61edb5c690fa197e2933245cf65dfbd Mon Sep 17 00:00:00 2001 From: Idan Hefetz Date: Tue, 25 Dec 2018 13:12:13 +0000 Subject: [PATCH] client support for resource api changes - added support for query in resource list - changed call to resource list from GET to POST - added support for resource count new api Story: 2004669 Task: 28652 Task: 28655 Depends-On: https://review.openstack.org/627309 Change-Id: Ib75b67c15c0a175e804fd020958fb7e50ec4df07 --- doc/source/contributor/cli.rst | 22 ++++++++ .../resource_count-52fc0ec86b983197.yaml | 4 ++ .../resource_list_query-5f5763fbff70bdcb.yaml | 3 ++ setup.cfg | 1 + tools/vitrage.bash_completion | 3 +- vitrageclient/shell.py | 1 + vitrageclient/v1/cli/resource.py | 50 ++++++++++++++++++- vitrageclient/v1/resource.py | 22 ++++++-- 8 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 releasenotes/notes/resource_count-52fc0ec86b983197.yaml create mode 100644 releasenotes/notes/resource_list_query-5f5763fbff70bdcb.yaml diff --git a/doc/source/contributor/cli.rst b/doc/source/contributor/cli.rst index 40dfc25..8de97c9 100644 --- a/doc/source/contributor/cli.rst +++ b/doc/source/contributor/cli.rst @@ -165,6 +165,7 @@ You'll find complete documentation on the shell by running healthcheck Check api health status help print detailed help for another command (cliff) rca show Show the Root Cause Analysis for a certain alarm + resource count Show a count of all resources resource list List resources resource show Show a resource template add Add a template @@ -718,6 +719,27 @@ resource show:: | vitrage_type | nova.instance | +---------------------------+--------------------------------------+ +resource count:: + + vitrage resource count + { + "nova.instance": 394, + "openstack.cluster": 1, + "cinder.volume": 405, + "nova.host": 16, + "neutron.network": 7, + "neutron.port": 1127, + "nova.zone": 3, + "tripleo.controller": 3 + } + + vitrage resource count --type nova.instance --group-by state + { + "ACTIVE": 359, + "ERROR": 27, + "SUBOPTIMAL": 8 + } + Alarms Examples --------------- Note: To see complete usage: 'vitrage help' and 'vitrage help ' diff --git a/releasenotes/notes/resource_count-52fc0ec86b983197.yaml b/releasenotes/notes/resource_count-52fc0ec86b983197.yaml new file mode 100644 index 0000000..5825a57 --- /dev/null +++ b/releasenotes/notes/resource_count-52fc0ec86b983197.yaml @@ -0,0 +1,4 @@ +--- +features: + - Resource count new API with support for queries and group-by. + Allows retrieving quick summaries of graph nodes. diff --git a/releasenotes/notes/resource_list_query-5f5763fbff70bdcb.yaml b/releasenotes/notes/resource_list_query-5f5763fbff70bdcb.yaml new file mode 100644 index 0000000..6d58825 --- /dev/null +++ b/releasenotes/notes/resource_list_query-5f5763fbff70bdcb.yaml @@ -0,0 +1,3 @@ +--- +features: + - Resource list API now supports using a query diff --git a/setup.cfg b/setup.cfg index 5b5f8b5..ec545d2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -49,6 +49,7 @@ openstack.rca.v1 = rca_alarm_count = vitrageclient.v1.cli.alarm:AlarmCount rca_resource_list = vitrageclient.v1.cli.resource:ResourceList rca_resource_show = vitrageclient.v1.cli.resource:ResourceShow + rca_resource_count = vitrageclient.v1.cli.resource:ResourceCount rca_template_list = vitrageclient.v1.cli.template:TemplateList rca_template_show = vitrageclient.v1.cli.template:TemplateShow rca_template_validate = vitrageclient.v1.cli.template:TemplateValidate diff --git a/tools/vitrage.bash_completion b/tools/vitrage.bash_completion index afd681a..b59d463 100755 --- a/tools/vitrage.bash_completion +++ b/tools/vitrage.bash_completion @@ -19,7 +19,8 @@ _vitrage() cmds_rca='show' cmds_rca_show='-h --help -f --format -c --column --max-width --fit-width --print-empty --noindent --variable --prefix --all-tenants' cmds_resource='list show' - cmds_resource_list='-h --help -f --format -c --column --max-width --fit-width --print-empty --quote --noindent --sort-column --type --all-tenants' + cmds_resource_count='-h --help --type --all-tenants --filter --group-by' + cmds_resource_list='-h --help -f --format -c --column --max-width --fit-width --print-empty --quote --noindent --sort-column --type --all-tenants --filter' cmds_resource_show='-h --help -f --format -c --column --max-width --fit-width --print-empty --noindent --variable --prefix' cmds_template='add delete list show validate' cmds_template_add='-h --help -f --format -c --column --max-width --fit-width --print-empty --quote --noindent --sort-column --path --type' diff --git a/vitrageclient/shell.py b/vitrageclient/shell.py index 5da0ebb..d665fc7 100755 --- a/vitrageclient/shell.py +++ b/vitrageclient/shell.py @@ -51,6 +51,7 @@ class VitrageCommandManager(commandmanager.CommandManager): 'topology show': topology.TopologyShow, 'resource show': resource.ResourceShow, 'resource list': resource.ResourceList, + 'resource count': resource.ResourceCount, 'alarm list': alarm.AlarmList, 'alarm history': alarm.AlarmHistory, 'alarm show': alarm.AlarmShow, diff --git a/vitrageclient/v1/cli/resource.py b/vitrageclient/v1/cli/resource.py index 13f58be..44f0253 100644 --- a/vitrageclient/v1/cli/resource.py +++ b/vitrageclient/v1/cli/resource.py @@ -47,15 +47,20 @@ class ResourceList(lister.Lister): dest='all_tenants', action='store_true', help='Shows resources of all the tenants') + parser.add_argument('--filter', + metavar='', + help='resource query') return parser def take_action(self, parsed_args): resource_type = parsed_args.resource_type all_tenants = parsed_args.all_tenants + query = parsed_args.filter resources = utils.get_client(self).resource.list( resource_type=resource_type, - all_tenants=all_tenants) + all_tenants=all_tenants, + query=query) # cluster, zone and host don't have "project_id" property # neutron.port don't have "name" property # cluster don't have "update_timestamp" @@ -72,3 +77,46 @@ class ResourceList(lister.Lister): ('State', 'vitrage_operational_state'), ('Metadata', 'metadata'), ), resources) + + +class ResourceCount(show.ShowOne): + """Show a count of all resources""" + + def get_parser(self, prog_name): + parser = super(ResourceCount, self).get_parser(prog_name) + parser.add_argument('--type', + dest='resource_type', + metavar='', + help='Type of resource') + parser.add_argument('--all-tenants', + default=False, + dest='all_tenants', + action='store_true', + help='Shows resources of all the tenants') + parser.add_argument('--filter', + metavar='', + help='resource query'), + parser.add_argument( + '--group-by', + dest='group_by', + metavar='', + default='vitrage_type', + help='A resource data field, to group by it\'s values'), + return parser + + @property + def formatter_default(self): + return 'json' + + def take_action(self, parsed_args): + resource_type = parsed_args.resource_type + all_tenants = parsed_args.all_tenants + query = parsed_args.filter + group_by = parsed_args.group_by + resource_count = utils.get_client(self).resource.count( + resource_type=resource_type, + all_tenants=all_tenants, + query=query, + group_by=group_by) + + return self.dict2columns(resource_count) diff --git a/vitrageclient/v1/resource.py b/vitrageclient/v1/resource.py index 7c22be9..15cb8bc 100644 --- a/vitrageclient/v1/resource.py +++ b/vitrageclient/v1/resource.py @@ -17,15 +17,16 @@ class Resource(object): def __init__(self, api): self.api = api - def list(self, resource_type=None, all_tenants=False): + def list(self, resource_type=None, all_tenants=False, query=None): """Get a all resources :param all_tenants: should return all tenants resources :param resource_type: the type for the resources + :param query: the query filter for the vertices """ - params = dict(resource_type=resource_type, - all_tenants=all_tenants) - return self.api.get(self.url, params=params).json() + params = dict(resource_type=resource_type, all_tenants=all_tenants, + query=query) + return self.api.post(self.url, json=params).json() def get(self, vitrage_id): """Get a resource @@ -34,3 +35,16 @@ class Resource(object): """ url = self.url + vitrage_id return self.api.get(url).json() + + def count(self, resource_type=None, all_tenants=False, query=None, + group_by=None): + """Get a count of all resources + + :param all_tenants: should return all tenants resources + :param resource_type: the type for the resources + :param query: the query filter for the vertices + :param group_by: a property name to group by it's values + """ + params = dict(resource_type=resource_type, all_tenants=all_tenants, + query=query, group_by=group_by) + return self.api.post(self.url + 'count/', json=params).json()