From 2c5e8abccf12f1c3b9685e8d6fb293501df4eb4c Mon Sep 17 00:00:00 2001 From: liusheng Date: Tue, 14 Feb 2017 16:15:56 +0800 Subject: [PATCH] Add support for event commands Change-Id: I67af7921ea53dd97b176b87263dcb2b941cf819a --- doc/source/index.rst | 2 +- doc/source/shell.rst | 26 ++------- pankoclient/common/utils.py | 8 +++ pankoclient/osc/v2/capabilities.py | 4 +- pankoclient/osc/v2/events.py | 87 ++++++++++++++++++++++++++++++ pankoclient/v2/client.py | 2 + pankoclient/v2/events.py | 54 +++++++++++++++++++ setup.cfg | 2 + 8 files changed, 159 insertions(+), 26 deletions(-) create mode 100644 pankoclient/osc/v2/events.py create mode 100644 pankoclient/v2/events.py diff --git a/doc/source/index.rst b/doc/source/index.rst index 4573a43..75bc31a 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -15,7 +15,7 @@ Panko API. This is a new client to interact with Panko API. There may be differences in functionality, syntax, and command line output when compared with the - alarm functionality provided by ceilometerclient. + event functionality provided by ceilometerclient. .. seealso:: diff --git a/doc/source/shell.rst b/doc/source/shell.rst index 6913f88..7323988 100644 --- a/doc/source/shell.rst +++ b/doc/source/shell.rst @@ -1,12 +1,11 @@ The :program:`panko` shell utility -========================================= +================================== .. program:: panko .. highlight:: bash The :program:`panko` shell utility interacts with Panko API -from the command line. It supports the entirety of the Panko API excluding -deprecated combination alarms. +from the command line. You'll need to provide :program:`panko` with your OpenStack credentials. You can do this with the :option:`--os-username`, :option:`--os-password`, @@ -65,23 +64,4 @@ command. Examples -------- -Create an alarm:: - - panko alarm create -t threshold --name alarm1 -m cpu_util --threshold 5 - -List alarms:: - - panko alarm list - -List alarm with query parameters:: - - panko alarm list --query "state=alarm and type=threshold" - -Show an alarm's history:: - - panko alarm-history show - -Search alarm history data:: - - panko --debug alarm-history search --query 'timestamp>"2016-03-09T01:22:35"' - +#TODO diff --git a/pankoclient/common/utils.py b/pankoclient/common/utils.py index fbad573..055b068 100644 --- a/pankoclient/common/utils.py +++ b/pankoclient/common/utils.py @@ -48,3 +48,11 @@ def get_pagination_options(limit=None, marker=None, sorts=None): for sort in sorts or []: options.append("sort=%s" % urllib_parse.quote(sort)) return "&".join(options) + + +def filtersdict_to_url(filters): + urls = [] + for k, v in sorted(filters.items()): + url = "q.field=%s&q.op=eq&q.value=%s" % (k, v) + urls.append(url) + return '&'.join(urls) diff --git a/pankoclient/osc/v2/capabilities.py b/pankoclient/osc/v2/capabilities.py index 1e75a1a..cb86a0b 100644 --- a/pankoclient/osc/v2/capabilities.py +++ b/pankoclient/osc/v2/capabilities.py @@ -15,10 +15,10 @@ """Panko v2 Capabilities action implementations""" -from cliff import show +from osc_lib.command import command -class CliCapabilitiesList(show.ShowOne): +class CliCapabilitiesList(command.ShowOne): """List capabilities for event service""" def take_action(self, parsed_args): diff --git a/pankoclient/osc/v2/events.py b/pankoclient/osc/v2/events.py new file mode 100644 index 0000000..36a07cb --- /dev/null +++ b/pankoclient/osc/v2/events.py @@ -0,0 +1,87 @@ +# Copyright 2016 Huawei, Inc. 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. +# + +"""Panko v2 event action implementations""" +import copy + +import json + +from osc_lib.command import command +from osc_lib import utils + + +class EventList(command.Lister): + """List events""" + + @staticmethod + def split_filter_param(param): + key, eq_op, value = param.partition('=') + if not eq_op: + msg = 'Malformed parameter(%s). Use the key=value format.' % param + raise ValueError(msg) + return key, value + + def get_parser(self, prog_name): + parser = super(EventList, self).get_parser(prog_name) + parser.add_argument('--filter', dest='filter', + metavar='', + type=self.split_filter_param, + action='append', + help='Filter parameters to apply on' + ' returned events.') + parser.add_argument("--limit", type=int, metavar="", + help="Number of events to return " + "(Default is server default)") + parser.add_argument("--marker", metavar="", + help="Last item of the previous listing. " + "Return the next results after this value," + "the supported marker is message_id.") + parser.add_argument("--sort", action="append", + metavar="", + help="Sort of events attribute, " + "e.g. name:asc") + return parser + + def take_action(self, parsed_args): + ac = self.app.client_manager.event + filters = dict(parsed_args.filter) if parsed_args.filter else None + events = ac.event.list( + filters=filters, sorts=parsed_args.sort, + limit=parsed_args.limit, marker=parsed_args.marker) + columns = ('event_type', 'generated', 'message_id', 'traits') + formatters = {'traits': lambda s: json.dumps(s, indent=4)} + return (columns, + (utils.get_item_properties( + s, columns, formatters=formatters) for s in events)) + + +class EventShow(command.ShowOne): + """List events""" + + def get_parser(self, prog_name): + parser = super(EventShow, self).get_parser(prog_name) + parser.add_argument( + 'message_id', + metavar='', + help="event of specified message_id to display" + ) + return parser + + def take_action(self, parsed_args): + ac = self.app.client_manager.event + event = ac.event.get(message_id=parsed_args.message_id) + data = copy.deepcopy(event._info) + data.update({'traits': json.dumps(data['traits'], indent=4)}) + return self.dict2columns(data) diff --git a/pankoclient/v2/client.py b/pankoclient/v2/client.py index 34fb0b1..22baf40 100644 --- a/pankoclient/v2/client.py +++ b/pankoclient/v2/client.py @@ -14,6 +14,7 @@ # from pankoclient.v2 import capabilities +from pankoclient.v2 import events from pankoclient.common import http @@ -24,3 +25,4 @@ class Client(object): """Initialize a new client for the Panko v1 API.""" self.http_client = http._construct_http_client(*args, **kwargs) self.capabilities = capabilities.CapabilitiesManager(self.http_client) + self.event = events.EventManager(self.http_client) diff --git a/pankoclient/v2/events.py b/pankoclient/v2/events.py new file mode 100644 index 0000000..cb16168 --- /dev/null +++ b/pankoclient/v2/events.py @@ -0,0 +1,54 @@ +# +# 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 pankoclient.common import base +from pankoclient.common import utils + + +class Event(base.Resource): + pass + + +class EventManager(base.ManagerWithFind): + resource_class = Event + + def list(self, filters=None, limit=None, marker=None, sorts=None): + """Return all events matching the query filters. + + :param query: Filter arguments for which Events to return + :param limit: Maximum number of samples to be returned. + :param sorts: A pair of sort key and sort direction combined with ":" + :param marker: The pagination query marker, message id of the last + item viewed + """ + pagination = utils.get_pagination_options(limit, marker, sorts) + filter_string = (utils.filtersdict_to_url(filters) if + filters else "") + url = "v2/events" + options = [] + if filter_string: + options.append(filter_string) + if pagination: + options.append(pagination) + if options: + url += "?" + "&".join(options) + return self._list(url) + + def get(self, message_id): + """Return a single event with the given message id. + + :param message_id: Message ID of the Event to be returned + """ + path = '/v2/events/%s' + return self._get(path % message_id) + diff --git a/setup.cfg b/setup.cfg index a41d431..17e5261 100644 --- a/setup.cfg +++ b/setup.cfg @@ -31,6 +31,8 @@ openstack.cli.extension = openstack.event.v2 = event capabilities list = pankoclient.osc.v2.capabilities:CliCapabilitiesList + event list = pankoclient.osc.v2.events:EventList + event show = pankoclient.osc.v2.events:EventShow [build_sphinx] source-dir = doc/source