diff --git a/vitrageclient/shell.py b/vitrageclient/shell.py index d1e88fd..0f4387c 100755 --- a/vitrageclient/shell.py +++ b/vitrageclient/shell.py @@ -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') @@ -57,7 +58,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): diff --git a/vitrageclient/v1/cli/webhook.py b/vitrageclient/v1/cli/webhook.py new file mode 100644 index 0000000..d2186b7 --- /dev/null +++ b/vitrageclient/v1/cli/webhook.py @@ -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) diff --git a/vitrageclient/v1/client.py b/vitrageclient/v1/client.py index 38c6608..4b85ea1 100644 --- a/vitrageclient/v1/client.py +++ b/vitrageclient/v1/client.py @@ -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) diff --git a/vitrageclient/v1/webhook.py b/vitrageclient/v1/webhook.py new file mode 100644 index 0000000..48d115c --- /dev/null +++ b/vitrageclient/v1/webhook.py @@ -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()