Debian Codegraph @

mistralclient/commands/v2/members.py

raw symbols SCIP
1 # Copyright 2016 - Catalyst IT Limited
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14 #
15
16 from osc_lib.command import command
17
18 from mistralclient.commands.v2 import base
19 from mistralclient import exceptions
20
21
22 class MemberFormatter(base.MistralFormatter):
23 COLUMNS = [
24 ('resource_id', 'Resource ID'),
25 ('resource_type', 'Resource Type'),
26 ('project_id', 'Resource Owner'),
27 ('member_id', 'Member ID'),
28 ('status', 'Status'),
29 ('created_at', 'Created at'),
30 ('updated_at', 'Updated at')
31 ]
32
33 @staticmethod
34 def format(member=None, lister=False):
35 if member:
36 data = (
37 member.resource_id,
38 member.resource_type,
39 member.project_id,
40 member.member_id,
41 member.status,
42 member.created_at,
43 )
44
45 if hasattr(member, 'updated_at'):
46 data += (member.updated_at,)
47 else:
48 data += (None,)
49 else:
50 data = (tuple('' for _ in range(len(MemberFormatter.COLUMNS))),)
51
52 return MemberFormatter.headings(), data
53
54
55 class List(base.MistralLister):
56 """List all members."""
57
58 def _get_format_function(self):
59 return MemberFormatter.format_list
60
61 def get_parser(self, parsed_args):
62 parser = super(List, self).get_parser(parsed_args)
63
64 parser.add_argument(
65 'resource_id',
66 help='Resource id to be shared.'
67 )
68 parser.add_argument(
69 'resource_type',
70 help='Resource type.'
71 )
72
73 return parser
74
75 def _get_resources(self, parsed_args):
76 mistral_client = self.app.client_manager.workflow_engine
77 return mistral_client.members.list(
78 parsed_args.resource_id,
79 parsed_args.resource_type
80 )
81
82
83 class Get(command.ShowOne):
84 """Show specific member information."""
85
86 def get_parser(self, prog_name):
87 parser = super(Get, self).get_parser(prog_name)
88
89 parser.add_argument(
90 'resource',
91 help='Resource ID to be shared.'
92 )
93 parser.add_argument(
94 'resource_type',
95 help='Resource type.'
96 )
97 parser.add_argument(
98 '-m',
99 '--member-id',
100 default='',
101 help='Project ID to whom the resource is shared to. No need to '
102 'provide this param if you are the resource member.'
103 )
104
105 return parser
106
107 def take_action(self, parsed_args):
108 mistral_client = self.app.client_manager.workflow_engine
109 member = mistral_client.members.get(
110 parsed_args.resource,
111 parsed_args.resource_type,
112 parsed_args.member_id,
113 )
114
115 return MemberFormatter.format(member)
116
117
118 class Create(command.ShowOne):
119 """Shares a resource to another tenant."""
120
121 def get_parser(self, prog_name):
122 parser = super(Create, self).get_parser(prog_name)
123
124 parser.add_argument(
125 'resource_id',
126 help='Resource ID to be shared.'
127 )
128 parser.add_argument(
129 'resource_type',
130 help='Resource type.'
131 )
132 parser.add_argument(
133 'member_id',
134 help='Project ID to whom the resource is shared to.'
135 )
136
137 return parser
138
139 def take_action(self, parsed_args):
140 mistral_client = self.app.client_manager.workflow_engine
141 member = mistral_client.members.create(
142 parsed_args.resource_id,
143 parsed_args.resource_type,
144 parsed_args.member_id,
145 )
146
147 return MemberFormatter.format(member)
148
149
150 class Delete(command.Command):
151 """Delete a resource sharing relationship."""
152
153 def get_parser(self, prog_name):
154 parser = super(Delete, self).get_parser(prog_name)
155
156 parser.add_argument(
157 'resource',
158 help='Resource ID to be shared.'
159 )
160 parser.add_argument(
161 'resource_type',
162 help='Resource type.'
163 )
164 parser.add_argument(
165 'member_id',
166 help='Project ID to whom the resource is shared to.'
167 )
168
169 return parser
170
171 def take_action(self, parsed_args):
172 mistral_client = self.app.client_manager.workflow_engine
173
174 try:
175 mistral_client.members.delete(
176 parsed_args.resource,
177 parsed_args.resource_type,
178 parsed_args.member_id,
179 )
180
181 print(
182 "Request to delete %s member %s has been accepted." %
183 (parsed_args.resource_type, parsed_args.member_id)
184 )
185 except Exception as e:
186 print(e)
187
188 error_msg = "Unable to delete the specified member."
189 raise exceptions.MistralClientException(error_msg)
190
191
192 class Update(command.ShowOne):
193 """Update resource sharing status."""
194
195 def get_parser(self, prog_name):
196 parser = super(Update, self).get_parser(prog_name)
197
198 parser.add_argument(
199 'resource_id',
200 help='Resource ID to be shared.'
201 )
202 parser.add_argument(
203 'resource_type',
204 help='Resource type.'
205 )
206 parser.add_argument(
207 '-m',
208 '--member-id',
209 default='',
210 help='Project ID to whom the resource is shared to. No need to '
211 'provide this param if you are the resource member.'
212 )
213 parser.add_argument(
214 '-s',
215 '--status',
216 default='accepted',
217 choices=['pending', 'accepted', 'rejected'],
218 help='status of the sharing.'
219 )
220
221 return parser
222
223 def take_action(self, parsed_args):
224 mistral_client = self.app.client_manager.workflow_engine
225
226 member = mistral_client.members.update(
227 parsed_args.resource_id,
228 parsed_args.resource_type,
229 parsed_args.member_id,
230 status=parsed_args.status
231 )
232
233 return MemberFormatter.format(member)