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