File: webhook.py

package info (click to toggle)
python-vitrageclient 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 648 kB
  • sloc: python: 1,807; makefile: 26; sh: 2
file content (103 lines) | stat: -rw-r--r-- 3,521 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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 """

    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
        webhook = utils.get_client(self).webhook.show(id=id)
        return self.dict2columns(webhook)


class WebhookList(lister.Lister):
    """List all webhooks in the database"""

    POST_PROPS = \
        (
            ('ID', 'id'),
            ('Created At', 'created_at'),
            ('Project ID', 'project_id'),
            ('URL', 'url'),
            ('Headers', 'headers'),
            ('Filter', 'regex_filter')
        )

    def get_parser(self, prog_name):
        parser = super(WebhookList, self).get_parser(prog_name)
        parser.add_argument('--all-tenants',
                            default=False,
                            dest='all_tenants',
                            action='store_true',
                            help='Shows webhooks of all the tenants')
        return parser

    def take_action(self, parsed_args):
        all_tenants = parsed_args.all_tenants
        webhooks = utils.get_client(self).webhook.list(all_tenants=all_tenants)

        return utils.list2cols_with_rename(self.POST_PROPS, webhooks)


class WebhookAdd(show.ShowOne):
    """Add a new webhook to the database"""
    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 """

    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)