Revision 62c6652f
b/kamaki/cli/commands/network.py | ||
---|---|---|
36 | 36 |
|
37 | 37 |
from kamaki.cli import command |
38 | 38 |
from kamaki.cli.command_tree import CommandTree |
39 |
from kamaki.cli.errors import CLISyntaxError, CLIBaseUrlError |
|
39 |
from kamaki.cli.errors import ( |
|
40 |
CLISyntaxError, CLIBaseUrlError, CLIInvalidArgument) |
|
40 | 41 |
from kamaki.clients.cyclades import CycladesNetworkClient |
41 |
from kamaki.cli.argument import FlagArgument, ValueArgument |
|
42 |
from kamaki.cli.argument import FlagArgument, ValueArgument, RepeatableArgument
|
|
42 | 43 |
from kamaki.cli.commands import _command_init, errors, addLogSettings |
43 | 44 |
from kamaki.cli.commands import ( |
44 | 45 |
_optional_output_cmd, _optional_json, _name_filter, _id_filter) |
... | ... | |
209 | 210 |
def main(self, network_id): |
210 | 211 |
super(self.__class__, self)._run() |
211 | 212 |
self._run(network_id=network_id) |
213 |
|
|
214 |
|
|
215 |
@command(subnet_cmds) |
|
216 |
class subnet_list(_init_network, _optional_json, _name_filter, _id_filter): |
|
217 |
"""List subnets |
|
218 |
Use filtering arguments (e.g., --name-like) to manage long server lists |
|
219 |
""" |
|
220 |
|
|
221 |
arguments = dict( |
|
222 |
detail=FlagArgument('show detailed output', ('-l', '--details')), |
|
223 |
more=FlagArgument( |
|
224 |
'output results in pages (-n to set items per page, default 10)', |
|
225 |
'--more'), |
|
226 |
user_id=ValueArgument( |
|
227 |
'show only subnets belonging to user with this id', '--user-id') |
|
228 |
) |
|
229 |
|
|
230 |
def _filter_by_user_id(self, nets): |
|
231 |
return filter_dicts_by_dict(nets, dict(user_id=self['user_id'])) if ( |
|
232 |
self['user_id']) else nets |
|
233 |
|
|
234 |
@errors.generic.all |
|
235 |
@errors.cyclades.connection |
|
236 |
def _run(self): |
|
237 |
detail = self['detail'] or self['user_id'] |
|
238 |
nets = self.client.list_subnets() |
|
239 |
nets = self._filter_by_user_id(nets) |
|
240 |
nets = self._filter_by_name(nets) |
|
241 |
nets = self._filter_by_id(nets) |
|
242 |
if detail and not self['detail']: |
|
243 |
nets = [dict( |
|
244 |
id=n['id'], name=n['name'], links=n['links']) for n in nets] |
|
245 |
kwargs = dict() |
|
246 |
if self['more']: |
|
247 |
kwargs['out'] = StringIO() |
|
248 |
kwargs['title'] = () |
|
249 |
self._print(nets, **kwargs) |
|
250 |
if self['more']: |
|
251 |
pager(kwargs['out'].getvalue()) |
|
252 |
|
|
253 |
def main(self): |
|
254 |
super(self.__class__, self)._run() |
|
255 |
self._run() |
|
256 |
|
|
257 |
|
|
258 |
@command(subnet_cmds) |
|
259 |
class subnet_info(_init_network, _optional_json): |
|
260 |
"""Get details about a subnet""" |
|
261 |
|
|
262 |
@errors.generic.all |
|
263 |
@errors.cyclades.connection |
|
264 |
def _run(self, subnet_id): |
|
265 |
net = self.client.get_subnet_details(subnet_id) |
|
266 |
self._print(net, self.print_dict) |
|
267 |
|
|
268 |
def main(self, subnet_id): |
|
269 |
super(self.__class__, self)._run() |
|
270 |
self._run(subnet_id=subnet_id) |
|
271 |
|
|
272 |
|
|
273 |
class AllocationPoolArgument(RepeatableArgument): |
|
274 |
|
|
275 |
@property |
|
276 |
def value(self): |
|
277 |
return super(AllocationPoolArgument, self).value or [] |
|
278 |
|
|
279 |
@value.setter |
|
280 |
def value(self, new_pools): |
|
281 |
new_list = [] |
|
282 |
for pool in new_pools: |
|
283 |
start, comma, end = pool.partition(',') |
|
284 |
if not (start and comma and end): |
|
285 |
raise CLIInvalidArgument( |
|
286 |
'Invalid allocation pool argument %s' % pool, details=[ |
|
287 |
'Allocation values must be of the form:', |
|
288 |
' <start address>,<end address>']) |
|
289 |
new_list.append(dict(start=start, end=end)) |
|
290 |
self._value = new_list |
|
291 |
|
|
292 |
|
|
293 |
@command(subnet_cmds) |
|
294 |
class subnet_create(_init_network, _optional_json): |
|
295 |
"""Create a new subnet |
|
296 |
""" |
|
297 |
|
|
298 |
arguments = dict( |
|
299 |
name=ValueArgument('Subnet name', '--name'), |
|
300 |
allocation_pools=AllocationPoolArgument( |
|
301 |
'start_address,end_address of allocation pool (can be repeated)' |
|
302 |
' e.g., --alloc-pool=123.45.67.1,123.45.67.8', |
|
303 |
'--alloc-pool'), |
|
304 |
gateway=ValueArgument('Gateway IP', '--gateway'), |
|
305 |
subnet_id=ValueArgument('The id for the subnet', '--id'), |
|
306 |
ipv6=FlagArgument('If set, IP version is set to 6, else 4', '--ipv6'), |
|
307 |
enable_dhcp=FlagArgument('Enable dhcp (default: off)', '--with-dhcp') |
|
308 |
) |
|
309 |
|
|
310 |
@errors.generic.all |
|
311 |
@errors.cyclades.connection |
|
312 |
@errors.cyclades.network_id |
|
313 |
def _run(self, network_id, cidr): |
|
314 |
net = self.client.create_subnet( |
|
315 |
network_id, cidr, |
|
316 |
self['name'], self['allocation_pools'], self['gateway'], |
|
317 |
self['subnet_id'], self['ipv6'], self['enable_dhcp']) |
|
318 |
self._print(net, self.print_dict) |
|
319 |
|
|
320 |
def main(self, network_id, cidr): |
|
321 |
super(self.__class__, self)._run() |
|
322 |
self._run(network_id=network_id, cidr=cidr) |
|
323 |
|
|
324 |
|
|
325 |
# @command(subnet_cmds) |
|
326 |
# class subnet_delete(_init_network, _optional_output_cmd): |
|
327 |
# """Delete a subnet""" |
|
328 |
|
|
329 |
# @errors.generic.all |
|
330 |
# @errors.cyclades.connection |
|
331 |
# def _run(self, subnet_id): |
|
332 |
# r = self.client.delete_subnet(subnet_id) |
|
333 |
# self._optional_output(r) |
|
334 |
|
|
335 |
# def main(self, subnet_id): |
|
336 |
# super(self.__class__, self)._run() |
|
337 |
# self._run(subnet_id=subnet_id) |
|
338 |
|
|
339 |
|
|
340 |
@command(subnet_cmds) |
|
341 |
class subnet_set(_init_network, _optional_json): |
|
342 |
"""Set an attribute of a subnet, leave the rest untouched (update) |
|
343 |
Only "--name" is supported for now |
|
344 |
""" |
|
345 |
|
|
346 |
arguments = dict(name=ValueArgument('New name of the subnet', '--name')) |
|
347 |
|
|
348 |
@errors.generic.all |
|
349 |
@errors.cyclades.connection |
|
350 |
def _run(self, subnet_id): |
|
351 |
if self['name'] in (None, ): |
|
352 |
raise CLISyntaxError( |
|
353 |
'Missing subnet attributes to update', |
|
354 |
details=[ |
|
355 |
'At least one if the following is expected:', |
|
356 |
' --name=<new name>']) |
|
357 |
r = self.client.get_subnet_details(subnet_id) |
|
358 |
r = self.client.update_subnet( |
|
359 |
subnet_id, r['network_id'], name=self['name']) |
|
360 |
self._print(r, self.print_dict) |
|
361 |
|
|
362 |
def main(self, subnet_id): |
|
363 |
super(self.__class__, self)._run() |
|
364 |
self._run(subnet_id=subnet_id) |
Also available in: Unified diff