File: events_cli.py

package info (click to toggle)
python-pankoclient 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 320 kB
  • sloc: python: 1,228; makefile: 20
file content (153 lines) | stat: -rw-r--r-- 5,876 bytes parent folder | download | duplicates (2)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#   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

from osc_lib.command import command
from osc_lib import utils
from oslo_serialization import jsonutils


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='<KEY>=<VALUE>',
                            type=self.split_filter_param,
                            action='append',
                            help='Filter parameters to apply on returned '
                                 'events. (can be applied multiple times)')
        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:name',
                   'traits:value', 'traits:type')
        rows = []
        for event in events:
            traits_type = [t['type'] for t in event.traits]
            traits_name = [t['name'] for t in event.traits]
            traits_value = [t['value'] for t in event.traits]
            [getattr(event, item, '') for item in columns]
            row = (getattr(event, 'event_type', ''),
                   getattr(event, 'generated', ''),
                   getattr(event, 'message_id', ''),
                   '\n'.join(traits_name),
                   '\n'.join(traits_value),
                   '\n'.join(traits_type),)
            rows.append(row)
        return columns, tuple(rows)


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': jsonutils.dumps(data['traits'], indent=4)})
        return self.dict2columns(data)


class EventTypeList(command.Lister):
    """List event types"""

    def take_action(self, parsed_args):
        ac = self.app.client_manager.event
        event_types = ac.event_type.list()
        return ('Event Type',), ((t,)for t in event_types)


class EventTraitList(command.Lister):
    """List event traits of a specified event type and trait name"""

    def get_parser(self, prog_name):
        parser = super(EventTraitList, self).get_parser(prog_name)
        parser.add_argument(
            'type_name',
            metavar='<EVENT_TYPE>',
            help='Type of the event for which traits will listed.'
        )
        parser.add_argument(
            'trait_name',
            metavar='<TRAIT_NAME>',
            help='The name of the trait to list.'
        )
        return parser

    def take_action(self, parsed_args):
        ac = self.app.client_manager.event
        event_traits = ac.event_trait.list(
            parsed_args.type_name, parsed_args.trait_name)
        columns = ('name', 'value', 'type')
        return (columns,
                (utils.get_item_properties(t, columns) for t in event_traits))


class EventTraitDescription(command.Lister):
    """List trait info for an event type."""

    def get_parser(self, prog_name):
        parser = super(EventTraitDescription, self).get_parser(prog_name)
        parser.add_argument(
            'type_name',
            metavar='<EVENT_TYPE>',
            help='Type of the event for which traits definitions will be '
                 'shown.'
        )
        return parser

    def take_action(self, parsed_args):
        ac = self.app.client_manager.event
        event_traits = ac.event_trait_description.list(parsed_args.type_name)
        columns = ('name', 'type')
        return (columns,
                (utils.get_item_properties(t, columns) for t in event_traits))