Revision 545c6c29 kamaki/cli/commands/image.py

b/kamaki/cli/commands/image.py
38 38
from kamaki.cli.argument import FlagArgument, ValueArgument, KeyValueArgument
39 39
from kamaki.cli.argument import IntArgument
40 40
from kamaki.cli.commands.cyclades import _init_cyclades
41
from kamaki.cli.commands import _command_init, errors, _optional_output_cmd
41
from kamaki.cli.commands import _command_init, errors
42
from kamaki.cli.commands import _optional_output_cmd, _optional_json
42 43

  
43 44

  
44 45
image_cmds = CommandTree(
......
73 74

  
74 75

  
75 76
@command(image_cmds)
76
class image_list(_init_image):
77
class image_list(_init_image, _optional_json):
77 78
    """List images accessible by user"""
78 79

  
79 80
    arguments = dict(
......
104 105
        more=FlagArgument(
105 106
            'output results in pages (-n to set items per page, default 10)',
106 107
            '--more'),
107
        enum=FlagArgument('Enumerate results', '--enumerate'),
108
        json_output=FlagArgument('Show results in json', ('-j', '--json'))
108
        enum=FlagArgument('Enumerate results', '--enumerate')
109 109
    )
110 110

  
111 111
    def _filtered_by_owner(self, detail, *list_params):
......
148 148
        else:
149 149
            images = self.client.list_public(detail, filters, order)
150 150

  
151
        if self['json_output']:
152
            print_json(images)
153
            return
154 151
        images = self._filtered_by_name(images)
152
        kwargs = dict(with_enumeration=self['enum'])
155 153
        if self['more']:
156
            print_items(
157
                images,
158
                with_enumeration=self['enum'], page_size=self['limit'] or 10)
154
            kwargs['page_size'] = self['limit'] or 10
159 155
        elif self['limit']:
160
            print_items(images[:self['limit']], with_enumeration=self['enum'])
161
        else:
162
            print_items(images, with_enumeration=self['enum'])
156
            images = images[:self['limit']]
157
        self._print(images, **kwargs)
163 158

  
164 159
    def main(self):
165 160
        super(self.__class__, self)._run()
......
167 162

  
168 163

  
169 164
@command(image_cmds)
170
class image_meta(_init_image):
165
class image_meta(_init_image, _optional_json):
171 166
    """Get image metadata
172 167
    Image metadata include:
173 168
    - image file information (location, size, etc.)
......
175 170
    - image os properties (os, fs, etc.)
176 171
    """
177 172

  
178
    arguments = dict(
179
        json_output=FlagArgument('Show results in json', ('-j', '--json'))
180
    )
181

  
182 173
    @errors.generic.all
183 174
    @errors.plankton.connection
184 175
    @errors.plankton.id
185 176
    def _run(self, image_id):
186
        printer = print_json if self['json_output'] else print_dict
187
        printer(self.client.get_meta(image_id))
177
        self._print([self.client.get_meta(image_id)])
188 178

  
189 179
    def main(self, image_id):
190 180
        super(self.__class__, self)._run()
......
192 182

  
193 183

  
194 184
@command(image_cmds)
195
class image_register(_init_image):
185
class image_register(_init_image, _optional_json):
196 186
    """(Re)Register an image"""
197 187

  
198 188
    arguments = dict(
......
207 197
            'add property in key=value form (can be repeated)',
208 198
            ('-p', '--property')),
209 199
        is_public=FlagArgument('mark image as public', '--public'),
210
        size=IntArgument('set image size', '--size'),
211
        #update=FlagArgument(
212
        #    'update existing image properties',
213
        #    ('-u', '--update')),
214
        json_output=FlagArgument('Show results in json', ('-j', '--json'))
200
        size=IntArgument('set image size', '--size')
215 201
    )
216 202

  
217 203
    @errors.generic.all
......
239 225
                'size',
240 226
                'is_public']).intersection(self.arguments):
241 227
            params[key] = self[key]
228
        properties = self['properties']
242 229

  
243
            properties = self['properties']
244

  
245
        printer = print_json if self['json_output'] else print_dict
246
        printer(self.client.register(name, location, params, properties))
230
        self._print([self.client.register(name, location, params, properties)])
247 231

  
248 232
    def main(self, name, location):
249 233
        super(self.__class__, self)._run()
......
266 250

  
267 251

  
268 252
@command(image_cmds)
269
class image_shared(_init_image):
253
class image_shared(_init_image, _optional_json):
270 254
    """List images shared by a member"""
271 255

  
272
    arguments = dict(
273
        json_output=FlagArgument('Show results in json', ('-j', '--json'))
274
    )
275

  
276 256
    @errors.generic.all
277 257
    @errors.plankton.connection
278 258
    def _run(self, member):
279
        r = self.client.list_shared(member)
280
        if self['json_output']:
281
            print_json(r)
282
        else:
283
            print_items(r, title=('image_id',))
259
        self._print(self.client.list_shared(member), title=('image_id',))
284 260

  
285 261
    def main(self, member):
286 262
        super(self.__class__, self)._run()
......
293 269

  
294 270

  
295 271
@command(image_cmds)
296
class image_members_list(_init_image):
272
class image_members_list(_init_image, _optional_json):
297 273
    """List members of an image"""
298 274

  
299
    arguments = dict(
300
        json_output=FlagArgument('Show results in json', ('-j', '--json'))
301
    )
302

  
303 275
    @errors.generic.all
304 276
    @errors.plankton.connection
305 277
    @errors.plankton.id
306 278
    def _run(self, image_id):
307
        members = self.client.list_members(image_id)
308
        if self['json_output']:
309
            print_json(members)
310
        else:
311
            print_items(members, title=('member_id',), with_redundancy=True)
279
        self._print(self.client.list_members(image_id), title=('member_id',))
312 280

  
313 281
    def main(self, image_id):
314 282
        super(self.__class__, self)._run()
......
369 337

  
370 338

  
371 339
@command(image_cmds)
372
class image_compute_list(_init_cyclades):
340
class image_compute_list(_init_cyclades, _optional_json):
373 341
    """List images"""
374 342

  
375 343
    arguments = dict(
......
378 346
        more=FlagArgument(
379 347
            'output results in pages (-n to set items per page, default 10)',
380 348
            '--more'),
381
        enum=FlagArgument('Enumerate results', '--enumerate'),
382
        json_output=FlagArgument('Show results in json', ('-j', '--json'))
349
        enum=FlagArgument('Enumerate results', '--enumerate')
383 350
    )
384 351

  
385 352
    def _make_results_pretty(self, images):
......
391 358
    @errors.cyclades.connection
392 359
    def _run(self):
393 360
        images = self.client.list_images(self['detail'])
394
        if self['json_output']:
395
            print_json(images)
396
            return
397
        if self['detail']:
361
        if self['detail'] and not self['json_output']:
398 362
            self._make_results_pretty(images)
363
        kwargs = dict(with_enumeration=self['enum'])
399 364
        if self['more']:
400
            print_items(
401
                images,
402
                page_size=self['limit'] or 10, with_enumeration=self['enum'])
365
            kwargs['page_size'] = self['limit'] or 10
403 366
        else:
404
            print_items(images[:self['limit']], with_enumeration=self['enum'])
367
            images = images[:self['limit']]
368
        self._print(images, **kwargs)
405 369

  
406 370
    def main(self):
407 371
        super(self.__class__, self)._run()
......
409 373

  
410 374

  
411 375
@command(image_cmds)
412
class image_compute_info(_init_cyclades):
376
class image_compute_info(_init_cyclades, _optional_json):
413 377
    """Get detailed information on an image"""
414 378

  
415
    arguments = dict(
416
        json_output=FlagArgument('Show results in json', ('-j', '--json'))
417
    )
418

  
419 379
    @errors.generic.all
420 380
    @errors.cyclades.connection
421 381
    @errors.plankton.id
422 382
    def _run(self, image_id):
423 383
        image = self.client.get_image_details(image_id)
424
        if self['json_output']:
425
            print_json(image)
426
            return
427
        if 'metadata' in image:
384
        if (not self['json_output']) and 'metadata' in image:
428 385
            image['metadata'] = image['metadata']['values']
429
        print_dict(image)
386
        self._print([image])
430 387

  
431 388
    def main(self, image_id):
432 389
        super(self.__class__, self)._run()
......
454 411

  
455 412

  
456 413
@command(image_cmds)
457
class image_compute_properties_list(_init_cyclades):
414
class image_compute_properties_list(_init_cyclades, _optional_json):
458 415
    """List all image properties"""
459 416

  
460
    arguments = dict(
461
        json_output=FlagArgument('Show results in json', ('-j', '--json'))
462
    )
463

  
464 417
    @errors.generic.all
465 418
    @errors.cyclades.connection
466 419
    @errors.plankton.id
467 420
    def _run(self, image_id):
468
        printer = print_json if self['json_output'] else print_dict
469
        printer(self.client.get_image_metadata(image_id))
421
        self._print(self.client.get_image_metadata(image_id), print_dict)
470 422

  
471 423
    def main(self, image_id):
472 424
        super(self.__class__, self)._run()
......
474 426

  
475 427

  
476 428
@command(image_cmds)
477
class image_compute_properties_get(_init_cyclades):
429
class image_compute_properties_get(_init_cyclades, _optional_json):
478 430
    """Get an image property"""
479 431

  
480
    arguments = dict(
481
        json_output=FlagArgument('Show results in json', ('-j', '--json'))
482
    )
483

  
484 432
    @errors.generic.all
485 433
    @errors.cyclades.connection
486 434
    @errors.plankton.id
487 435
    @errors.plankton.metadata
488 436
    def _run(self, image_id, key):
489
        printer = print_json if self['json_output'] else print_dict
490
        printer(self.client.get_image_metadata(image_id, key))
437
        self._print(self.client.get_image_metadata(image_id, key), print_dict)
491 438

  
492 439
    def main(self, image_id, key):
493 440
        super(self.__class__, self)._run()
......
495 442

  
496 443

  
497 444
@command(image_cmds)
498
class image_compute_properties_add(_init_cyclades):
445
class image_compute_properties_add(_init_cyclades, _optional_json):
499 446
    """Add a property to an image"""
500 447

  
501
    arguments = dict(
502
        json_output=FlagArgument('Show results in json', ('-j', '--json'))
503
    )
504

  
505 448
    @errors.generic.all
506 449
    @errors.cyclades.connection
507 450
    @errors.plankton.id
508 451
    @errors.plankton.metadata
509 452
    def _run(self, image_id, key, val):
510
        printer = print_json if self['json_output'] else print_dict
511
        printer(self.client.create_image_metadata(image_id, key, val))
453
        self._print(
454
            self.client.create_image_metadata(image_id, key, val), print_dict)
512 455

  
513 456
    def main(self, image_id, key, val):
514 457
        super(self.__class__, self)._run()
......
516 459

  
517 460

  
518 461
@command(image_cmds)
519
class image_compute_properties_set(_init_cyclades):
462
class image_compute_properties_set(_init_cyclades, _optional_json):
520 463
    """Add / update a set of properties for an image
521 464
    proeprties must be given in the form key=value, e.v.
522 465
    /image compute properties set <image-id> key1=val1 key2=val2
523 466
    """
524
    arguments = dict(
525
        json_output=FlagArgument('Show results in json', ('-j', '--json'))
526
    )
527 467

  
528 468
    @errors.generic.all
529 469
    @errors.cyclades.connection
530 470
    @errors.plankton.id
531 471
    def _run(self, image_id, keyvals):
532
        metadata = dict()
472
        meta = dict()
533 473
        for keyval in keyvals:
534 474
            key, val = keyval.split('=')
535
            metadata[key] = val
536
        printer = print_json if self['json_output'] else print_dict
537
        printer(self.client.update_image_metadata(image_id, **metadata))
475
            meta[key] = val
476
        self._print(
477
            self.client.update_image_metadata(image_id, **meta), print_dict)
538 478

  
539 479
    def main(self, image_id, *key_equals_value):
540 480
        super(self.__class__, self)._run()

Also available in: Unified diff