Revision 264a13f7 kamaki/cli/commands/network.py

b/kamaki/cli/commands/network.py
63 63
    def _run(self, service='network'):
64 64
        if getattr(self, 'cloud', None):
65 65
            base_url = self._custom_url(service) or self._custom_url(
66
                'compute')
66
                'network')
67 67
            if base_url:
68 68
                token = self._custom_token(service) or self._custom_token(
69
                    'compute') or self.config.get_cloud('token')
69
                    'network') or self.config.get_cloud('token')
70 70
                self.client = CycladesNetworkClient(
71 71
                  base_url=base_url, token=token)
72 72
                return
......
74 74
            self.cloud = 'default'
75 75
        if getattr(self, 'auth_base', False):
76 76
            cyclades_endpoints = self.auth_base.get_service_endpoints(
77
                self._custom_type('compute') or 'compute',
78
                self._custom_version('compute') or '')
77
                self._custom_type('network') or 'network',
78
                self._custom_version('network') or '')
79 79
            base_url = cyclades_endpoints['publicURL']
80 80
            token = self.auth_base.token
81 81
            self.client = CycladesNetworkClient(base_url=base_url, token=token)
......
108 108
    @errors.generic.all
109 109
    @errors.cyclades.connection
110 110
    def _run(self):
111
        detail = self['detail'] or self['user_id']
112
        nets = self.client.list_networks(detail=detail)
111
        nets = self.client.list_networks()
113 112
        nets = self._filter_by_user_id(nets)
114 113
        nets = self._filter_by_name(nets)
115 114
        nets = self._filter_by_id(nets)
116
        if detail and not self['detail']:
115
        if not self['detail']:
117 116
            nets = [dict(
118 117
                id=n['id'], name=n['name'], links=n['links']) for n in nets]
119 118
        kwargs = dict()
......
145 144
        self._run(network_id=network_id)
146 145

  
147 146

  
147
class NetworkTypeArgument(ValueArgument):
148

  
149
    types = ('CUSTOM', 'MAC_FILTERED', 'IP_LESS_ROUTED', 'PHYSICAL_VLAN')
150

  
151
    @property
152
    def value(self):
153
        return getattr(self, '_value', None)
154

  
155
    @value.setter
156
    def value(self, new_value):
157
        if new_value and new_value.upper() in self.types:
158
            self._value = new_value.upper()
159
        elif new_value:
160
            raise CLIInvalidArgument(
161
                'Invalid network type %s' % new_value, details=[
162
                    'Valid types: %s' % ', '.join(self.types), ])
163

  
164

  
148 165
@command(network_cmds)
149 166
class network_create(_init_network, _optional_json):
150
    """Create a new network
151
    Valid network types: CUSTOM MAC_FILTERED IP_LESS_ROUTED PHYSICAL_VLAN
152
    """
167
    """Create a new network"""
153 168

  
154 169
    arguments = dict(
155 170
        name=ValueArgument('Network name', '--name'),
156 171
        shared=FlagArgument(
157
            'Make network shared (special privileges required)', '--shared')
172
            'Make network shared (special privileges required)', '--shared'),
173
        network_type=NetworkTypeArgument(
174
            'Valid network types: %s' % (', '.join(NetworkTypeArgument.types)),
175
            '--type')
158 176
    )
177
    required = ('network_type', )
159 178

  
160 179
    @errors.generic.all
161 180
    @errors.cyclades.connection
......
165 184
            network_type, name=self['name'], shared=self['shared'])
166 185
        self._print(net, self.print_dict)
167 186

  
168
    def main(self, network_type):
187
    def main(self):
169 188
        super(self.__class__, self)._run()
170
        self._run(network_type=network_type)
189
        self._run(network_type=self['network_type'])
171 190

  
172 191

  
173 192
@command(network_cmds)
......
187 206

  
188 207

  
189 208
@command(network_cmds)
190
class network_set(_init_network, _optional_json):
191
    """Set an attribute of a network, leave the rest untouched (update)
192
    Only "--name" is supported for now
193
    """
209
class network_modify(_init_network, _optional_json):
210
    """Modify network attributes"""
194 211

  
195
    arguments = dict(name=ValueArgument('New name of the network', '--name'))
212
    arguments = dict(new_name=ValueArgument('Rename the network', '--name'))
213
    required = ['new_name', ]
196 214

  
197 215
    @errors.generic.all
198 216
    @errors.cyclades.connection
199 217
    @errors.cyclades.network_id
200 218
    def _run(self, network_id):
201
        if self['name'] in (None, ):
202
            raise CLISyntaxError(
203
                'Missing network attributes to update',
204
                details=[
205
                    'At least one if the following is expected:',
206
                    '  --name=<new name>'])
207
        r = self.client.update_network(network_id, name=self['name'])
219
        r = self.client.update_network(network_id, name=self['new_name'])
208 220
        self._print(r, self.print_dict)
209 221

  
210 222
    def main(self, network_id):
......
225 237
            '--more')
226 238
    )
227 239

  
228
    def _filter_by_user_id(self, nets):
229
        return filter_dicts_by_dict(nets, dict(user_id=self['user_id'])) if (
230
            self['user_id']) else nets
231

  
232 240
    @errors.generic.all
233 241
    @errors.cyclades.connection
234 242
    def _run(self):
235 243
        nets = self.client.list_subnets()
236
        nets = self._filter_by_user_id(nets)
237 244
        nets = self._filter_by_name(nets)
238 245
        nets = self._filter_by_id(nets)
239 246
        if not self['detail']:
......
289 296

  
290 297
@command(subnet_cmds)
291 298
class subnet_create(_init_network, _optional_json):
292
    """Create a new subnet
293
    """
299
    """Create a new subnet"""
294 300

  
295 301
    arguments = dict(
296 302
        name=ValueArgument('Subnet name', '--name'),
......
301 307
        gateway=ValueArgument('Gateway IP', '--gateway'),
302 308
        subnet_id=ValueArgument('The id for the subnet', '--id'),
303 309
        ipv6=FlagArgument('If set, IP version is set to 6, else 4', '--ipv6'),
304
        enable_dhcp=FlagArgument('Enable dhcp (default: off)', '--with-dhcp')
310
        enable_dhcp=FlagArgument('Enable dhcp (default: off)', '--with-dhcp'),
311
        network_id=ValueArgument('Set the network ID', '--network-id'),
312
        cidr=ValueArgument('Set the CIDR', '--cidr')
305 313
    )
314
    required = ('network_id', 'cidr')
306 315

  
307 316
    @errors.generic.all
308 317
    @errors.cyclades.connection
......
314 323
            self['subnet_id'], self['ipv6'], self['enable_dhcp'])
315 324
        self._print(net, self.print_dict)
316 325

  
317
    def main(self, network_id, cidr):
326
    def main(self):
318 327
        super(self.__class__, self)._run()
319
        self._run(network_id=network_id, cidr=cidr)
328
        self._run(network_id=self['network_id'], cidr=self['cidr'])
320 329

  
321 330

  
322 331
# @command(subnet_cmds)
......
335 344

  
336 345

  
337 346
@command(subnet_cmds)
338
class subnet_set(_init_network, _optional_json):
339
    """Set an attribute of a subnet, leave the rest untouched (update)
340
    Only "--name" is supported for now
341
    """
347
class subnet_modify(_init_network, _optional_json):
348
    """Modify the attributes of a subnet"""
342 349

  
343
    arguments = dict(name=ValueArgument('New name of the subnet', '--name'))
350
    arguments = dict(
351
        new_name=ValueArgument('New name of the subnet', '--name')
352
    )
353
    required = ['new_name']
344 354

  
345 355
    @errors.generic.all
346 356
    @errors.cyclades.connection
347 357
    def _run(self, subnet_id):
348
        if self['name'] in (None, ):
349
            raise CLISyntaxError(
350
                'Missing subnet attributes to update',
351
                details=[
352
                    'At least one if the following is expected:',
353
                    '  --name=<new name>'])
354 358
        r = self.client.get_subnet_details(subnet_id)
355 359
        r = self.client.update_subnet(
356
            subnet_id, r['network_id'], name=self['name'])
360
            subnet_id, r['network_id'], name=self['new_name'])
357 361
        self._print(r, self.print_dict)
358 362

  
359 363
    def main(self, subnet_id):
......
393 397

  
394 398
@command(port_cmds)
395 399
class port_delete(_init_network, _optional_output_cmd):
396
    """Delete a port"""
400
    """Delete a port (== disconnect server from network)"""
397 401

  
398 402
    @errors.generic.all
399 403
    @errors.cyclades.connection
......
407 411

  
408 412

  
409 413
@command(port_cmds)
410
class port_set(_init_network, _optional_json):
411
    """Set an attribute of a port, leave the rest untouched (update)
412
    Only "--name" is supported for now
413
    """
414
class port_modify(_init_network, _optional_json):
415
    """Modify the attributes of a port"""
414 416

  
415
    arguments = dict(name=ValueArgument('New name of the port', '--name'))
417
    arguments = dict(new_name=ValueArgument('New name of the port', '--name'))
418
    required = ['new_name', ]
416 419

  
417 420
    @errors.generic.all
418 421
    @errors.cyclades.connection
419 422
    def _run(self, port_id):
420
        if self['name'] in (None, ):
421
            raise CLISyntaxError(
422
                'Missing port attributes to update',
423
                details=[
424
                    'At least one if the following is expected:',
425
                    '  --name=<new name>'])
426 423
        r = self.client.get_port_details(port_id)
427 424
        r = self.client.update_port(
428
            port_id, r['network_id'], name=self['name'])
425
            port_id, r['network_id'], name=self['new_name'])
429 426
        self._print(r, self.print_dict)
430 427

  
431 428
    def main(self, port_id):
......
435 432

  
436 433
@command(port_cmds)
437 434
class port_create(_init_network, _optional_json):
438
    """Create a new port"""
435
    """Create a new port (== connect server to network)"""
439 436

  
440 437
    arguments = dict(
441 438
        name=ValueArgument('A human readable name', '--name'),
......
446 443
            'Subnet id for fixed ips (used with --ip-address)',
447 444
            '--subnet-id'),
448 445
        ip_address=ValueArgument(
449
            'IP address for subnet id (used with --subnet-id', '--ip-address')
446
            'IP address for subnet id (used with --subnet-id', '--ip-address'),
447
        network_id=ValueArgument('Set the network ID', '--network-id'),
448
        device_id=ValueArgument(
449
            'The device is either a virtual server or a virtual router',
450
            '--device-id')
450 451
    )
452
    retuired = ('network_id', 'device_id')
451 453

  
452 454
    @errors.generic.all
453 455
    @errors.cyclades.connection
454 456
    @errors.cyclades.network_id
455 457
    def _run(self, network_id, device_id):
456
        if bool(self['subnet_id']) != bool(self['ip_address']):
457
            raise CLIInvalidArgument('Invalid use of arguments', details=[
458
                '--subnet-id and --ip-address should be used together'])
459 458
        fixed_ips = [dict(
460 459
            subnet_id=self['subnet_id'], ip_address=self['ip_address'])] if (
461 460
                self['subnet_id']) else None
......
466 465
            fixed_ips=fixed_ips)
467 466
        self._print(r, self.print_dict)
468 467

  
469
    def main(self, network_id, device_id):
468
    def main(self):
470 469
        super(self.__class__, self)._run()
471
        self._run(network_id=network_id, device_id=device_id)
470
        self._run(network_id=self['network_id'], device_id=self['device_id'])
471

  
472

  
473
#  Warn users for some importand changes
474

  
475

  
476
@command(network_cmds)
477
class network_connect(_init_network, _optional_output_cmd):
478
    """DEPRECATED, use /port create"""
479

  
480
    def main(self):
481
        raise CLISyntaxError('DEPRECATED, replaced by kamaki port create')
482

  
483

  
484
@command(network_cmds)
485
class network_disconnect(_init_network):
486
    """DEPRECATED, /use port delete"""
487

  
488
    def main(self):
489
        raise CLISyntaxError('DEPRECATED, replaced by kamaki port delete')

Also available in: Unified diff