Revision de73876b kamaki/cli/commands/errors.py

b/kamaki/cli/commands/errors.py
82 82
                    if not client:
83 83
                        raise
84 84
                    url = getattr(client, 'base_url', '<empty>')
85
                    raiseCLIError(ce,
86
                        'Invalid service url %s' % url,
87
                        details=[
85
                    msg = 'Invalid service url %s' % url
86
                    raiseCLIError(ce, msg, details=[
88 87
                        'Please, check if service url is correctly set',
89 88
                        '* to get current url: /config get compute.url',
90 89
                        '* to set url: /config set compute.url <URL>'])
......
112 111
                kloger.warning(
113 112
                    'No permanent token (try: kamaki config set token <tkn>)')
114 113
            if not getattr(client, 'base_url', False):
115
                raise CLIError('Missing astakos server URL',
116
                    importance=3,
117
                    details=['Check if astakos.url is set correctly',
114
                msg = 'Missing astakos server URL'
115
                raise CLIError(msg, importance=3, details=[
116
                    'Check if astakos.url is set correctly',
118 117
                    'To get astakos url:   /config get astakos.url',
119 118
                    'To set astakos url:   /config set astakos.url <URL>'])
120 119
            return r
......
128 127
            except ClientError as ce:
129 128
                if ce.status == 401:
130 129
                    token = kwargs.get('custom_token', 0) or self.client.token
131
                    raiseCLIError(ce,
132
                        'Authorization failed for token %s' % token if token\
133
                            else 'No token provided',
134
                        details=[] if token else this._token_details)
130
                    msg = (
131
                        'Authorization failed for token %s' % token
132
                    ) if token else 'No token provided',
133
                    details = [] if token else this._token_details
134
                    raiseCLIError(ce, msg, details=details)
135 135
            self._raise = foo
136 136
            return r
137 137
        return _raise
......
151 151
    def _get_cmd_ids(this, foo):
152 152
        def _raise(self, cmd_ids, *args, **kwargs):
153 153
            if not cmd_ids:
154
                raise CLISyntaxError('Usage: <id1|id1-id2> [id3|id3-id4] ...',
154
                raise CLISyntaxError(
155
                    'Usage: <id1|id1-id2> [id3|id3-id4] ...',
155 156
                    details=self.__doc__.split('\n'))
156 157
            return foo(self, cmd_ids, *args, **kwargs)
157 158
        return _raise
......
193 194
                network_id = int(network_id)
194 195
                return foo(self, *args, **kwargs)
195 196
            except ValueError as ve:
196
                raiseCLIError(ve, 'Invalid network id %s ' % network_id,
197
                    details='network id must be a positive integer',
198
                    importance=1)
197
                msg = 'Invalid network id %s ' % network_id
198
                details = ['network id must be a positive integer']
199
                raiseCLIError(ve, msg, details=details, importance=1)
199 200
            except ClientError as ce:
200
                if network_id and ce.status == 404 and\
201
                    'network' in ('%s' % ce).lower():
202
                        raiseCLIError(ce,
203
                            'No network with id %s found' % network_id,
204
                            details=this.about_network_id)
201
                if network_id and ce.status == 404 and (
202
                    'network' in ('%s' % ce).lower()
203
                ):
204
                    msg = 'No network with id %s found' % network_id,
205
                    raiseCLIError(ce, msg, details=this.about_network_id)
205 206
                raise
206 207
        return _raise
207 208

  
......
212 213
                return foo(self, *args, **kwargs)
213 214
            except ClientError as ce:
214 215
                if ce.status == 413:
215
                    raiseCLIError(ce,
216
                        'Cannot create another network',
217
                        details=['Maximum number of networks reached',
218
                            '* to get a list of networks: /network list',
219
                            '* to delete a network: /network delete <net id>'])
216
                    msg = 'Cannot create another network',
217
                    details = ['Maximum number of networks reached',
218
                        '* to get a list of networks: /network list',
219
                        '* to delete a network: /network delete <net id>']
220
                    raiseCLIError(ce, msg, details=details)
220 221
                raise
221 222
        return _raise
222 223

  
......
228 229
                return foo(self, *args, **kwargs)
229 230
            except ClientError as ce:
230 231
                if network_id and ce.status == 400:
231
                    raiseCLIError(ce,
232
                        'Network with id %s does not exist' % network_id,
233
                        details=self.about_network_id)
232
                    msg = 'Network with id %s does not exist' % network_id,
233
                    raiseCLIError(ce, msg, details=self.about_network_id)
234 234
                elif network_id or ce.status == 421:
235
                    raiseCLIError(ce,
236
                        'Network with id %s is in use' % network_id,
237
                        details=[
238
                            'Disconnect all nics/VMs of this network first',
239
                            '* to get nics: /network info %s' % network_id,
240
                            '.  (under "attachments" section)',
241
                            '* to disconnect: /network disconnect <nic id>'])
235
                    msg = 'Network with id %s is in use' % network_id,
236
                    raiseCLIError(ce, msg, details=[
237
                        'Disconnect all nics/VMs of this network first',
238
                        '* to get nics: /network info %s' % network_id,
239
                        '.  (under "attachments" section)',
240
                        '* to disconnect: /network disconnect <nic id>'])
242 241
                raise
243 242
        return _raise
244 243

  
......
250 249
                flavor_id = int(flavor_id)
251 250
                return foo(self, *args, **kwargs)
252 251
            except ValueError as ve:
253
                raiseCLIError(ve, 'Invalid flavor id %s ' % flavor_id,
254
                    details='Flavor id must be a positive integer',
255
                    importance=1)
252
                msg = 'Invalid flavor id %s ' % flavor_id,
253
                details = 'Flavor id must be a positive integer',
254
                raiseCLIError(ve, msg, details=details, importance=1)
256 255
            except ClientError as ce:
257
                if flavor_id and ce.status == 404 and\
258
                    'flavor' in ('%s' % ce).lower():
259
                        raiseCLIError(ce,
260
                            'No flavor with id %s found' % flavor_id,
261
                            details=this.about_flavor_id)
256
                if flavor_id and ce.status == 404 and (
257
                    'flavor' in ('%s' % ce).lower()
258
                ):
259
                        msg = 'No flavor with id %s found' % flavor_id,
260
                        raiseCLIError(ce, msg, details=this.about_flavor_id)
262 261
                raise
263 262
        return _raise
264 263

  
......
270 269
                server_id = int(server_id)
271 270
                return foo(self, *args, **kwargs)
272 271
            except ValueError as ve:
273
                raiseCLIError(ve,
274
                    'Invalid server(VM) id %s' % server_id,
275
                    details=['id must be a positive integer'],
276
                    importance=1)
272
                msg = 'Invalid server(VM) id %s' % server_id,
273
                details = ['id must be a positive integer'],
274
                raiseCLIError(ve, msg, details=details, importance=1)
277 275
            except ClientError as ce:
278 276
                err_msg = ('%s' % ce).lower()
279
                if (ce.status == 404 and 'server' in err_msg)\
280
                or (ce.status == 400 and 'not found' in err_msg):
281
                    raiseCLIError(ce,
282
                        'server(VM) with id %s not found' % server_id,
283
                        details=[
284
                            '* to get existing VM ids: /server list',
285
                            '* to get VM details: /server info <VM id>'])
277
                if (
278
                    ce.status == 404 and 'server' in err_msg
279
                ) or (
280
                    ce.status == 400 and 'not found' in err_msg
281
                ):
282
                    msg = 'server(VM) with id %s not found' % server_id,
283
                    raiseCLIError(ce, msg, details=[
284
                        '* to get existing VM ids: /server list',
285
                        '* to get VM details: /server info <VM id>'])
286 286
                raise
287 287
        return _raise
288 288

  
......
293 293
            try:
294 294
                return foo(self, *args, **kwargs)
295 295
            except ClientError as ce:
296
                if ce.status == 400 and profile\
297
                and 'firewall' in ('%s' % ce).lower():
298
                    raiseCLIError(ce,
299
                        '%s is an invalid firewall profile term' % profile,
300
                        details=['Try one of the following:',
301
                            '* DISABLED: Shutdown firewall',
302
                            '* ENABLED: Firewall in normal mode',
303
                            '* PROTECTED: Firewall in secure mode'])
296
                if ce.status == 400 and profile and (
297
                    'firewall' in ('%s' % ce).lower()
298
                ):
299
                    msg = '%s is an invalid firewall profile term' % profile
300
                    raiseCLIError(ce, msg, details=[
301
                        'Try one of the following:',
302
                        '* DISABLED: Shutdown firewall',
303
                        '* ENABLED: Firewall in normal mode',
304
                        '* PROTECTED: Firewall in secure mode'])
304 305
                raise
305 306
        return _raise
306 307

  
......
311 312
                return foo(self, *args, **kwargs)
312 313
            except ClientError as ce:
313 314
                nic_id = kwargs.get('nic_id', None)
314
                if nic_id and ce.status == 404\
315
                and 'network interface' in ('%s' % ce).lower():
315
                if nic_id and ce.status == 404 and (
316
                    'network interface' in ('%s' % ce).lower()
317
                ):
316 318
                    server_id = kwargs.get('server_id', '<no server>')
317 319
                    err_msg = 'No nic %s on server(VM) with id %s' % (
318 320
                        nic_id,
......
333 335
                return foo(self, *args, **kwargs)
334 336
            except IndexError as ie:
335 337
                nic_id = kwargs.get('nic_id', None)
336
                raiseCLIError(ie,
337
                    'Invalid format for network interface (nic) %s' % nic_id,
338
                    importance=1,
339
                    details=[
340
                        'nid_id format: nic-<server id>-<nic id>',
341
                        '* get nics of a network: /network info <net id>',
342
                        '    (listed the "attachments" section)'])
338
                msg = 'Invalid format for network interface (nic) %s' % nic_id
339
                raiseCLIError(ie, msg, importance=1, details=[
340
                    'nid_id format: nic-<server id>-<nic id>',
341
                    '* get nics of a network: /network info <net id>',
342
                    '    (listed the "attachments" section)'])
343 343
        return _raise
344 344

  
345 345
    @classmethod
......
349 349
            try:
350 350
                foo(self, *args, **kwargs)
351 351
            except ClientError as ce:
352
                if key and ce.status == 404\
353
                    and 'metadata' in ('%s' % ce).lower():
352
                if key and ce.status == 404 and (
353
                    'metadata' in ('%s' % ce).lower()
354
                ):
354 355
                        raiseCLIError(ce, 'No VM metadata with key %s' % key)
355 356
                raise
356 357
        return _raise
......
358 359

  
359 360
class plankton(object):
360 361

  
361
    about_image_id = ['How to pick a suitable image:',
362
    about_image_id = [
363
        'How to pick a suitable image:',
362 364
        '* get a list of image ids: /image list',
363 365
        '* details of image: /flavor info <image id>']
364 366

  
......
373 375
            try:
374 376
                foo(self, *args, **kwargs)
375 377
            except ClientError as ce:
376
                if image_id and (ce.status == 404\
377
                    or (ce.status == 400 and
378
                        'image not found' in ('%s' % ce).lower())\
379
                    or ce.status == 411):
380
                        raiseCLIError(ce,
381
                            'No image with id %s found' % image_id,
382
                            details=this.about_image_id)
378
                if image_id and (
379
                    ce.status == 404
380
                    or (
381
                        ce.status == 400
382
                        and 'image not found' in ('%s' % ce).lower())
383
                    or ce.status == 411
384
                ):
385
                        msg = 'No image with id %s found' % image_id
386
                        raiseCLIError(ce, msg, details=this.about_image_id)
383 387
                raise
384 388
        return _raise
385 389

  
......
390 394
            try:
391 395
                foo(self, *args, **kwargs)
392 396
            except ClientError as ce:
393
                if ce.status == 404 or ((ce.status == 400\
394
                    and 'metadata' in ('%s' % ce).lower())):
395
                        raiseCLIError(ce,
396
                            'No properties with key %s in this image' % key)
397
                if ce.status == 404 or (
398
                    (ce.status == 400 and 'metadata' in ('%s' % ce).lower())):
399
                        msg = 'No properties with key %s in this image' % key
400
                        raiseCLIError(ce, msg)
397 401
                raise
398 402
        return _raise
399 403

  
400 404

  
401 405
class pithos(object):
402
    container_howto = ['To specify a container:',
403
    '  1. Set store.container variable (permanent)',
404
    '     /config set store.container <container>',
405
    '  2. --container=<container> (temporary, overrides 1)',
406
    '  3. Use the container:path format (temporary, overrides all)',
407
    'For a list of containers: /store list']
406
    container_howto = [
407
       "" 'To specify a container:',
408
        '  1. Set store.container variable (permanent)',
409
        '     /config set store.container <container>',
410
        '  2. --container=<container> (temporary, overrides 1)',
411
        '  3. Use the container:path format (temporary, overrides all)',
412
        'For a list of containers: /store list']
408 413

  
409 414
    @classmethod
410 415
    def connection(this, foo):
......
435 440
                return foo(self, *args, **kwargs)
436 441
            except ClientError as ce:
437 442
                if ce.status == 404 and 'container' in ('%s' % ce).lower():
438
                        cont = '%s or %s' % (self.container, dst_cont)\
439
                        if dst_cont else self.container
440
                        raiseCLIError(ce,
441
                            'Is container %s in current account?' % (cont),
442
                            details=this.container_howto)
443
                        cont = ('%s or %s' % (
444
                            self.container,
445
                            dst_cont)) if dst_cont else self.container
446
                        msg = 'Is container %s in current account?' % (cont),
447
                        raiseCLIError(ce, msg, details=this.container_howto)
443 448
                raise
444 449
        return _raise
445 450

  
......
450 455
            try:
451 456
                return foo(self, *args, **kwargs)
452 457
            except IOError as ioe:
453
                raiseCLIError(ioe,
454
                    'Failed to access file %s' % local_path,
455
                    importance=2)
458
                msg = 'Failed to access file %s' % local_path,
459
                raiseCLIError(ioe, msg, importance=2)
456 460
        return _raise
457 461

  
458 462
    @classmethod
......
462 466
                return foo(self, *args, **kwargs)
463 467
            except ClientError as ce:
464 468
                err_msg = ('%s' % ce).lower()
465
                if (ce.status == 404 or ce.status == 500)\
466
                and 'object' in err_msg and 'not' in err_msg:
467
                    raiseCLIError(ce,
468
                        'No object %s in container %s'\
469
                        % (self.path, self.container),
470
                        details=this.container_howto)
469
                if (
470
                    ce.status == 404 or ce.status == 500
471
                ) and 'object' in err_msg and 'not' in err_msg:
472
                    msg = 'No object %s in container %s' % (
473
                        self.path,
474
                        self.container)
475
                    raiseCLIError(ce, msg, details=this.container_howto)
471 476
                raise
472 477
        return _raise
473 478

  
......
481 486
                try:
482 487
                    size = int(size)
483 488
                except ValueError as ve:
484
                    raiseCLIError(ve,
485
                        'Invalid file size %s ' % size,
486
                        details=['size must be a positive integer'],
487
                        importance=1)
489
                    msg = 'Invalid file size %s ' % size
490
                    details = ['size must be a positive integer']
491
                    raiseCLIError(ve, msg, details=details, importance=1)
488 492
            else:
489 493
                try:
490 494
                    start = int(start)
491 495
                except ValueError as e:
492
                    raiseCLIError(e,
493
                        'Invalid start value %s in range' % start,
494
                        details=['size must be a positive integer'],
495
                        importance=1)
496
                    msg = 'Invalid start value %s in range' % start,
497
                    details = ['size must be a positive integer'],
498
                    raiseCLIError(e, msg, details=details, importance=1)
496 499
                try:
497 500
                    end = int(end)
498 501
                except ValueError as e:
499
                    raiseCLIError(e,
500
                        'Invalid end value %s in range' % end,
501
                        details=['size must be a positive integer'],
502
                        importance=1)
502
                    msg = 'Invalid end value %s in range' % end
503
                    details = ['size must be a positive integer']
504
                    raiseCLIError(e, msg, details=details, importance=1)
503 505
                if start > end:
504 506
                    raiseCLIError(
505 507
                        'Invalid range %s-%s' % (start, end),
......
511 513
            except ClientError as ce:
512 514
                err_msg = ('%s' % ce).lower()
513 515
                if size and (ce.status == 416 or
514
                (ce.status == 400 and\
516
                (ce.status == 400 and
515 517
                    'object length is smaller than range length' in err_msg)):
516
                    raiseCLIError(ce,
517
                        'Remote object %s:%s <= %s %s' % (
518
                    raiseCLIError(ce, 'Remote object %s:%s <= %s %s' % (
518 519
                            self.container,
519 520
                            self.path,
520 521
                            format_size(size),

Also available in: Unified diff