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.formatters import table |
||
19 | from st2client.commands import resource |
||
20 | from st2client.models.rbac import Role |
||
21 | from st2client.models.rbac import UserRoleAssignment |
||
22 | |||
23 | __all__ = [ |
||
24 | 'RoleBranch', |
||
25 | 'RoleAssignmentBranch' |
||
26 | ] |
||
27 | |||
28 | ROLE_ATTRIBUTE_DISPLAY_ORDER = ['id', 'name', 'system', 'permission_grants'] |
||
29 | ROLE_ASSIGNMENT_ATTRIBUTE_DISPLAY_ORDER = ['id', 'role', 'user', 'is_remote', 'description'] |
||
30 | |||
31 | |||
32 | class RoleBranch(resource.ResourceBranch): |
||
33 | def __init__(self, description, app, subparsers, parent_parser=None): |
||
34 | super(RoleBranch, self).__init__( |
||
35 | Role, description, app, subparsers, |
||
36 | parent_parser=parent_parser, |
||
37 | read_only=True, |
||
38 | commands={ |
||
39 | 'list': RoleListCommand, |
||
40 | 'get': RoleGetCommand |
||
41 | }) |
||
42 | |||
43 | |||
44 | class RoleListCommand(resource.ResourceCommand): |
||
45 | display_attributes = ['id', 'name', 'system', 'description'] |
||
46 | attribute_display_order = ROLE_ATTRIBUTE_DISPLAY_ORDER |
||
47 | |||
48 | def __init__(self, resource, *args, **kwargs): |
||
0 ignored issues
–
show
|
|||
49 | super(RoleListCommand, self).__init__( |
||
50 | resource, 'list', 'Get the list of the %s.' % |
||
51 | resource.get_plural_display_name().lower(), |
||
52 | *args, **kwargs) |
||
53 | |||
54 | self.group = self.parser.add_mutually_exclusive_group() |
||
55 | |||
56 | # Filter options |
||
57 | self.group.add_argument('-s', '--system', action='store_true', |
||
58 | help='Only display system roles.') |
||
59 | |||
60 | # Display options |
||
61 | self.parser.add_argument('-a', '--attr', nargs='+', |
||
62 | default=self.display_attributes, |
||
63 | help=('List of attributes to include in the ' |
||
64 | 'output. "all" will return all ' |
||
65 | 'attributes.')) |
||
66 | self.parser.add_argument('-w', '--width', nargs='+', type=int, |
||
67 | default=None, |
||
68 | help=('Set the width of columns in output.')) |
||
69 | |||
70 | @resource.add_auth_token_to_kwargs_from_cli |
||
71 | def run(self, args, **kwargs): |
||
72 | # Filtering options |
||
73 | if args.system: |
||
74 | kwargs['system'] = args.system |
||
75 | |||
76 | if args.system: |
||
77 | result = self.manager.query(**kwargs) |
||
78 | else: |
||
79 | result = self.manager.get_all(**kwargs) |
||
80 | |||
81 | return result |
||
82 | |||
83 | def run_and_print(self, args, **kwargs): |
||
84 | instances = self.run(args, **kwargs) |
||
85 | self.print_output(instances, table.MultiColumnTable, |
||
86 | attributes=args.attr, widths=args.width, |
||
87 | json=args.json, yaml=args.yaml) |
||
88 | |||
89 | |||
90 | class RoleGetCommand(resource.ResourceGetCommand): |
||
91 | display_attributes = ['all'] |
||
92 | attribute_display_order = ROLE_ATTRIBUTE_DISPLAY_ORDER |
||
93 | pk_argument_name = 'id' |
||
94 | |||
95 | |||
96 | class RoleAssignmentBranch(resource.ResourceBranch): |
||
97 | def __init__(self, description, app, subparsers, parent_parser=None): |
||
98 | super(RoleAssignmentBranch, self).__init__( |
||
99 | UserRoleAssignment, description, app, subparsers, |
||
100 | parent_parser=parent_parser, |
||
101 | read_only=True, |
||
102 | commands={ |
||
103 | 'list': RoleAssignmentListCommand, |
||
104 | 'get': RoleAssignmentGetCommand |
||
105 | }) |
||
106 | |||
107 | |||
108 | class RoleAssignmentListCommand(resource.ResourceCommand): |
||
109 | display_attributes = ['id', 'role', 'user', 'is_remote', 'source', 'description'] |
||
110 | attribute_display_order = ROLE_ASSIGNMENT_ATTRIBUTE_DISPLAY_ORDER |
||
111 | |||
112 | def __init__(self, resource, *args, **kwargs): |
||
113 | super(RoleAssignmentListCommand, self).__init__( |
||
114 | resource, 'list', 'Get the list of the %s.' % |
||
115 | resource.get_plural_display_name().lower(), |
||
116 | *args, **kwargs) |
||
117 | |||
118 | # Filter options |
||
119 | self.parser.add_argument('-r', '--role', help='Role to filter on.') |
||
120 | self.parser.add_argument('-u', '--user', help='User to filter on.') |
||
121 | self.parser.add_argument('-s', '--source', help='Source to filter on.') |
||
122 | self.parser.add_argument('--remote', action='store_true', |
||
123 | help='Only display remote role assignments.') |
||
124 | |||
125 | # Display options |
||
126 | self.parser.add_argument('-a', '--attr', nargs='+', |
||
127 | default=self.display_attributes, |
||
128 | help=('List of attributes to include in the ' |
||
129 | 'output. "all" will return all ' |
||
130 | 'attributes.')) |
||
131 | self.parser.add_argument('-w', '--width', nargs='+', type=int, |
||
132 | default=None, |
||
133 | help=('Set the width of columns in output.')) |
||
134 | |||
135 | @resource.add_auth_token_to_kwargs_from_cli |
||
136 | def run(self, args, **kwargs): |
||
137 | # Filtering options |
||
138 | if args.role: |
||
139 | kwargs['role'] = args.role |
||
140 | if args.user: |
||
141 | kwargs['user'] = args.user |
||
142 | if args.source: |
||
143 | kwargs['source'] = args.source |
||
144 | if args.remote: |
||
145 | kwargs['remote'] = args.remote |
||
146 | |||
147 | if args.role or args.user or args.remote or args.source: |
||
148 | result = self.manager.query(**kwargs) |
||
149 | else: |
||
150 | result = self.manager.get_all(**kwargs) |
||
151 | |||
152 | return result |
||
153 | |||
154 | def run_and_print(self, args, **kwargs): |
||
155 | instances = self.run(args, **kwargs) |
||
156 | self.print_output(instances, table.MultiColumnTable, |
||
157 | attributes=args.attr, widths=args.width, |
||
158 | json=args.json, yaml=args.yaml) |
||
159 | |||
160 | |||
161 | class RoleAssignmentGetCommand(resource.ResourceGetCommand): |
||
162 | display_attributes = ['all'] |
||
163 | attribute_display_order = ROLE_ASSIGNMENT_ATTRIBUTE_DISPLAY_ORDER |
||
164 | pk_argument_name = 'id' |
||
165 |
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: