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 st2client import models |
||
17 | from st2client.commands import resource |
||
18 | from st2client.formatters import table |
||
19 | |||
20 | |||
21 | class RuleBranch(resource.ResourceBranch): |
||
22 | |||
23 | def __init__(self, description, app, subparsers, parent_parser=None): |
||
24 | super(RuleBranch, self).__init__( |
||
25 | models.Rule, description, app, subparsers, |
||
26 | parent_parser=parent_parser, |
||
27 | commands={ |
||
28 | 'list': RuleListCommand, |
||
29 | 'get': RuleGetCommand, |
||
30 | 'update': RuleUpdateCommand, |
||
31 | 'delete': RuleDeleteCommand |
||
32 | }) |
||
33 | |||
34 | self.commands['enable'] = RuleEnableCommand(self.resource, self.app, self.subparsers) |
||
35 | self.commands['disable'] = RuleDisableCommand(self.resource, self.app, self.subparsers) |
||
36 | |||
37 | |||
38 | class RuleListCommand(resource.ResourceTableCommand): |
||
39 | display_attributes = ['ref', 'pack', 'description', 'enabled'] |
||
40 | display_attributes_iftt = ['ref', 'trigger.ref', 'action.ref', 'enabled'] |
||
41 | |||
42 | def __init__(self, resource, *args, **kwargs): |
||
0 ignored issues
–
show
|
|||
43 | super(RuleListCommand, self).__init__(resource, 'list', |
||
44 | 'Get the list of the 50 most recent %s.' % |
||
45 | resource.get_plural_display_name().lower(), |
||
46 | *args, **kwargs) |
||
47 | self.default_limit = 50 |
||
48 | self.resource_name = resource.get_plural_display_name().lower() |
||
49 | self.group = self.parser.add_argument_group() |
||
50 | self.parser.add_argument('-n', '--last', type=int, dest='last', |
||
51 | default=self.default_limit, |
||
52 | help=('List N most recent %s. Default is 50.' % |
||
53 | self.resource_name)) |
||
54 | self.parser.add_argument('--iftt', action='store_true', |
||
55 | help='Show trigger and action in display list.') |
||
56 | self.parser.add_argument('-p', '--pack', type=str, |
||
57 | help=('Only return resources belonging to the' |
||
58 | ' provided pack')) |
||
59 | self.group.add_argument('-c', '--action', |
||
60 | help='Action reference to filter the list.') |
||
61 | self.group.add_argument('-g', '--trigger', |
||
62 | help='Trigger type reference to filter the list.') |
||
63 | self.enabled_filter_group = self.parser.add_mutually_exclusive_group() |
||
64 | self.enabled_filter_group.add_argument('--enabled', action='store_true', |
||
65 | help='Show rules that are enabled.') |
||
66 | self.enabled_filter_group.add_argument('--disabled', action='store_true', |
||
67 | help='Show rules that are disabled.') |
||
68 | |||
69 | @resource.add_auth_token_to_kwargs_from_cli |
||
70 | def run(self, args, **kwargs): |
||
71 | # Filtering options |
||
72 | if args.pack: |
||
73 | kwargs['pack'] = args.pack |
||
74 | if args.action: |
||
75 | kwargs['action'] = args.action |
||
76 | if args.trigger: |
||
77 | kwargs['trigger'] = args.trigger |
||
78 | if args.enabled: |
||
79 | kwargs['enabled'] = True |
||
80 | if args.disabled: |
||
81 | kwargs['enabled'] = False |
||
82 | if args.iftt: |
||
83 | # switch attr to display the trigger and action |
||
84 | args.attr = self.display_attributes_iftt |
||
85 | |||
86 | return self.manager.query_with_count(limit=args.last, **kwargs) |
||
87 | |||
88 | def run_and_print(self, args, **kwargs): |
||
89 | instances, count = self.run(args, **kwargs) |
||
90 | if args.json or args.yaml: |
||
91 | self.print_output(instances, table.MultiColumnTable, |
||
92 | attributes=args.attr, widths=args.width, |
||
93 | json=args.json, yaml=args.yaml) |
||
94 | else: |
||
95 | self.print_output(instances, table.MultiColumnTable, |
||
96 | attributes=args.attr, widths=args.width) |
||
97 | |||
98 | if args.last and count and count > args.last: |
||
99 | table.SingleRowTable.note_box(self.resource_name, args.last) |
||
100 | |||
101 | |||
102 | class RuleGetCommand(resource.ContentPackResourceGetCommand): |
||
103 | display_attributes = ['all'] |
||
104 | attribute_display_order = ['id', 'uid', 'ref', 'pack', 'name', 'description', |
||
105 | 'enabled'] |
||
106 | |||
107 | |||
108 | class RuleUpdateCommand(resource.ContentPackResourceUpdateCommand): |
||
109 | pass |
||
110 | |||
111 | |||
112 | class RuleEnableCommand(resource.ContentPackResourceEnableCommand): |
||
113 | display_attributes = ['all'] |
||
114 | attribute_display_order = ['id', 'ref', 'pack', 'name', 'enabled', 'description', |
||
115 | 'enabled'] |
||
116 | |||
117 | |||
118 | class RuleDisableCommand(resource.ContentPackResourceDisableCommand): |
||
119 | display_attributes = ['all'] |
||
120 | attribute_display_order = ['id', 'ref', 'pack', 'name', 'enabled', 'description', |
||
121 | 'enabled'] |
||
122 | |||
123 | |||
124 | class RuleDeleteCommand(resource.ContentPackResourceDeleteCommand): |
||
125 | pass |
||
126 |
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: