Add support for event commands

Change-Id: I67af7921ea53dd97b176b87263dcb2b941cf819a
This commit is contained in:
liusheng 2017-02-14 16:15:56 +08:00
parent 3d9eddfac9
commit 2c5e8abccf
8 changed files with 159 additions and 26 deletions

View File

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

View File

@ -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 <ALARM_ID>
Search alarm history data::
panko --debug alarm-history search --query 'timestamp>"2016-03-09T01:22:35"'
#TODO

View File

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

View File

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

View File

@ -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='<KEY1=VALUE1;KEY2=VALUE2...>',
type=self.split_filter_param,
action='append',
help='Filter parameters to apply on'
' returned events.')
parser.add_argument("--limit", type=int, metavar="<LIMIT>",
help="Number of events to return "
"(Default is server default)")
parser.add_argument("--marker", metavar="<MARKER>",
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="<SORT_KEY:SORT_DIR>",
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='<message_id>',
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)

View File

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

54
pankoclient/v2/events.py Normal file
View File

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

View File

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