Merge "add support for webhooks"

This commit is contained in:
Zuul 2018-01-04 04:02:51 +00:00 committed by Gerrit Code Review
commit 0c398e3704
4 changed files with 159 additions and 1 deletions

View File

@ -40,6 +40,7 @@ from vitrageclient.v1.cli import rca
from vitrageclient.v1.cli import resource
from vitrageclient.v1.cli import template
from vitrageclient.v1.cli import topology
from vitrageclient.v1.cli import webhook
profiler = importutils.try_import('osprofiler.profiler')
@ -58,7 +59,11 @@ class VitrageCommandManager(commandmanager.CommandManager):
'template list': template.TemplateList,
'template show': template.TemplateShow,
'event post': event.EventPost,
'healthcheck': healthcheck.HealthCheck
'healthcheck': healthcheck.HealthCheck,
'webhook delete': webhook.WebhookDelete,
'webhook add': webhook.WebhookAdd,
'webhook list': webhook.WebhookList,
'webhook show': webhook.WebhookShow
}
def load_commands(self, namespace):

View File

@ -0,0 +1,91 @@
# 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 cliff import lister
from cliff import show
from vitrageclient.common import utils
# noinspection PyAbstractClass
class WebhookShow(show.ShowOne):
"""Show a webhook destination with "id" """
def get_parser(self, prog_name):
parser = super(WebhookShow, self).get_parser(prog_name)
parser.add_argument('id', help='id of webhook to show')
return parser
def take_action(self, parsed_args):
id = parsed_args.id
post_registration = utils.get_client(self).webhook.show(id=id)
return self.dict2columns(post_registration)
class WebhookList(lister.Lister):
"""List all webhook destinations in DB"""
POST_PROPS = \
('created_at', 'id', 'url', 'headers', 'regex_filter')
def get_parser(self, prog_name):
parser = super(WebhookList, self).get_parser(prog_name)
return parser
def take_action(self, parsed_args):
post_registrations = utils.get_client(self).webhook.list()
return utils.list2cols(self.POST_PROPS, post_registrations)
class WebhookAdd(show.ShowOne):
"""Add a new webhook registration to DB"""
def get_parser(self, prog_name):
parser = super(WebhookAdd, self).get_parser(prog_name)
parser.add_argument('--url',
required=True,
help='url to post to'
)
parser.add_argument('--regex_filter',
required=False,
help='a regular expression json specifying alarm '
'filters'
)
parser.add_argument('--headers',
required=False,
help='json to be included in the request header'
)
return parser
def take_action(self, parsed_args):
result = utils.get_client(self).webhook.add(
url=parsed_args.url,
regex_filter=parsed_args.regex_filter,
headers=parsed_args.headers)
return self.dict2columns(result)
class WebhookDelete(show.ShowOne):
"""Delete a webhook destination with "id" """
def get_parser(self, prog_name):
parser = super(WebhookDelete, self).get_parser(prog_name)
parser.add_argument('id', help='id of webhook to delete')
return parser
def take_action(self, parsed_args):
id = parsed_args.id
result = utils.get_client(self).webhook.delete(id=id)
return self.dict2columns(result)

View File

@ -19,6 +19,7 @@ from vitrageclient.v1 import rca
from vitrageclient.v1 import resource
from vitrageclient.v1 import template
from vitrageclient.v1 import topology
from vitrageclient.v1 import webhook
class Client(object):
@ -33,3 +34,4 @@ class Client(object):
self.template = template.Template(self._api)
self.event = event.Event(self._api)
self.healthcheck = healthcheck.HealthCheck(self._api)
self.webhook = webhook.Webhook(self._api)

View File

@ -0,0 +1,60 @@
# Copyright 2017 - ZTE Corporation
#
# 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.
class Webhook(object):
url = 'v1/webhook/'
def __init__(self, api):
self.api = api
def list(self):
"""Get webhook list"""
return self.api.get(self.url).json()
def show(self, id):
"""Show specific webhook
:param id: id of registration to show
"""
url = self.url + id
return self.api.get(url).json()
def add(self, url, regex_filter=None, headers=None):
"""Add a webhook to the DB
:param url: url to register in the DB
:param regex_filter: a optional regular expression dict to filter
alarms
:param headers: optional headers to attach to requests
"""
params = dict(url=url, regex_filter=regex_filter,
headers=headers)
return self.api.post(self.url, json=params).json()
def delete(self, id):
"""delete a webhook from the DB
:param id: id of webhook to delete
"""
url = self.url + id
return self.api.delete(url).json()