1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
2 | # contributor license agreements. See the NOTICE file distributed with |
||
3 | # this work for additional information regarding copyright ownership. |
||
4 | # The ASF licenses this file to You under the Apache License, Version 2.0 |
||
5 | # (the "License"); you may not use this file except in compliance with |
||
6 | # the License. You may obtain a copy of the License at |
||
7 | # |
||
8 | # http://www.apache.org/licenses/LICENSE-2.0 |
||
9 | # |
||
10 | # Unless required by applicable law or agreed to in writing, software |
||
11 | # distributed under the License is distributed on an "AS IS" BASIS, |
||
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
13 | # See the License for the specific language governing permissions and |
||
14 | # limitations under the License. |
||
15 | |||
16 | from __future__ import absolute_import |
||
17 | |||
18 | from st2client import models |
||
19 | from st2client.commands import resource |
||
20 | from st2client.formatters import table |
||
21 | from st2client.utils.date import format_isodate_for_user_timezone |
||
22 | |||
23 | |||
24 | class RuleEnforcementBranch(resource.ResourceBranch): |
||
25 | |||
26 | def __init__(self, description, app, subparsers, parent_parser=None): |
||
27 | super(RuleEnforcementBranch, self).__init__( |
||
28 | models.RuleEnforcement, description, app, subparsers, |
||
29 | parent_parser=parent_parser, |
||
30 | commands={ |
||
31 | 'list': RuleEnforcementListCommand, |
||
32 | 'get': RuleEnforcementGetCommand, |
||
33 | }) |
||
34 | |||
35 | |||
36 | class RuleEnforcementGetCommand(resource.ResourceGetCommand): |
||
37 | display_attributes = ['id', 'rule.ref', 'trigger_instance_id', |
||
38 | 'execution_id', 'failure_reason', 'enforced_at'] |
||
39 | attribute_display_order = ['id', 'rule.ref', 'trigger_instance_id', |
||
40 | 'execution_id', 'failure_reason', 'enforced_at'] |
||
41 | |||
42 | pk_argument_name = 'id' |
||
43 | |||
44 | @resource.add_auth_token_to_kwargs_from_cli |
||
45 | def run(self, args, **kwargs): |
||
46 | resource_id = getattr(args, self.pk_argument_name, None) |
||
47 | return self.get_resource_by_id(resource_id, **kwargs) |
||
48 | |||
49 | |||
50 | class RuleEnforcementListCommand(resource.ResourceCommand): |
||
51 | display_attributes = ['id', 'rule.ref', 'trigger_instance_id', |
||
52 | 'execution_id', 'enforced_at'] |
||
53 | attribute_display_order = ['id', 'rule.ref', 'trigger_instance_id', |
||
54 | 'execution_id', 'enforced_at'] |
||
55 | |||
56 | attribute_transform_functions = { |
||
57 | 'enforced_at': format_isodate_for_user_timezone |
||
58 | } |
||
59 | |||
60 | def __init__(self, resource, *args, **kwargs): |
||
0 ignored issues
–
show
|
|||
61 | |||
62 | self.default_limit = 50 |
||
63 | |||
64 | super(RuleEnforcementListCommand, self).__init__( |
||
65 | resource, 'list', 'Get the list of the %s most recent %s.' % |
||
66 | (self.default_limit, resource.get_plural_display_name().lower()), |
||
67 | *args, **kwargs) |
||
68 | self.resource_name = resource.get_plural_display_name().lower() |
||
69 | self.group = self.parser.add_argument_group() |
||
70 | self.parser.add_argument('-n', '--last', type=int, dest='last', |
||
71 | default=self.default_limit, |
||
72 | help=('List N most recent %s. Use -n -1 to fetch the full result \ |
||
73 | set.' % self.resource_name)) |
||
74 | |||
75 | # Filter options |
||
76 | self.group.add_argument('--trigger-instance', |
||
77 | help='Trigger instance id to filter the list.') |
||
78 | |||
79 | self.group.add_argument('--execution', |
||
80 | help='Execution id to filter the list.') |
||
81 | self.group.add_argument('--rule', |
||
82 | help='Rule ref to filter the list.') |
||
83 | |||
84 | self.parser.add_argument('-tg', '--timestamp-gt', type=str, dest='timestamp_gt', |
||
85 | default=None, |
||
86 | help=('Only return enforcements with enforced_at ' |
||
87 | 'greater than the one provided. ' |
||
88 | 'Use time in the format 2000-01-01T12:00:00.000Z')) |
||
89 | self.parser.add_argument('-tl', '--timestamp-lt', type=str, dest='timestamp_lt', |
||
90 | default=None, |
||
91 | help=('Only return enforcements with enforced_at ' |
||
92 | 'lower than the one provided. ' |
||
93 | 'Use time in the format 2000-01-01T12:00:00.000Z')) |
||
94 | # Display options |
||
95 | self.parser.add_argument('-a', '--attr', nargs='+', |
||
96 | default=self.display_attributes, |
||
97 | help=('List of attributes to include in the ' |
||
98 | 'output. "all" will return all ' |
||
99 | 'attributes.')) |
||
100 | self.parser.add_argument('-w', '--width', nargs='+', type=int, |
||
101 | default=None, |
||
102 | help=('Set the width of columns in output.')) |
||
103 | |||
104 | @resource.add_auth_token_to_kwargs_from_cli |
||
105 | def run(self, args, **kwargs): |
||
106 | # Filtering options |
||
107 | if args.trigger_instance: |
||
108 | kwargs['trigger_instance'] = args.trigger_instance |
||
109 | if args.execution: |
||
110 | kwargs['execution'] = args.execution |
||
111 | if args.rule: |
||
112 | kwargs['rule_ref'] = args.rule |
||
113 | if args.timestamp_gt: |
||
114 | kwargs['enforced_at_gt'] = args.timestamp_gt |
||
115 | if args.timestamp_lt: |
||
116 | kwargs['enforced_at_lt'] = args.timestamp_lt |
||
117 | |||
118 | return self.manager.query_with_count(limit=args.last, **kwargs) |
||
119 | |||
120 | def run_and_print(self, args, **kwargs): |
||
121 | instances, count = self.run(args, **kwargs) |
||
122 | if args.json or args.yaml: |
||
123 | self.print_output(reversed(instances), table.MultiColumnTable, |
||
124 | attributes=args.attr, widths=args.width, |
||
125 | json=args.json, yaml=args.yaml, |
||
126 | attribute_transform_functions=self.attribute_transform_functions) |
||
127 | else: |
||
128 | self.print_output(instances, table.MultiColumnTable, |
||
129 | attributes=args.attr, widths=args.width) |
||
130 | if args.last and count and count > args.last: |
||
131 | table.SingleRowTable.note_box(self.resource_name, args.last) |
||
132 |
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: