File: list.py

package info (click to toggle)
python-softlayer 6.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,508 kB
  • sloc: python: 57,195; makefile: 133; xml: 97; sh: 59
file content (64 lines) | stat: -rw-r--r-- 2,729 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""List images."""
# :license: MIT, see LICENSE for more details.

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer.CLI import image as image_mod
from SoftLayer import utils


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.option('--name', default=None, help='Filter on image name')
@click.option('--public/--private', is_flag=True, default=None,
              help='Display only public or private images')
@click.option('--limit', '-l',
              help='How many results to get in one api call',
              default=100,
              show_default=True)
@environment.pass_env
def cli(env, name, public, limit):
    """List images."""

    image_mgr = SoftLayer.ImageManager(env.client)

    images = []
    if public in [False, None]:
        for image in image_mgr.list_private_images(name=name, limit=limit, mask=image_mod.MASK):
            images.append(image)

    if public in [True, None]:
        for image in image_mgr.list_public_images(name=name, limit=limit, mask=image_mod.MASK):
            images.append(image)

    table = formatting.Table(['Id', 'Name', 'Type', 'Visibility', 'Account', 'OS', 'Created', 'Notes'])
    table.align['OS'] = 'l'
    table.align['Notes'] = 'l'

    images = [image for image in images if not image['parentId']]
    for image in images:
        operative_system = '-'
        if image.get('children') and len(image.get('children')) != 0:
            if image.get('children')[0].get('blockDevices') and len(image.get('children')[0].get('blockDevices')) != 0:
                for block_device in image.get('children')[0].get('blockDevices'):
                    if block_device.get('diskImage').get('softwareReferences') and \
                            len(block_device.get('diskImage').get('softwareReferences')) != 0:
                        operative_system = block_device.get('diskImage').get('softwareReferences')[0].\
                            get('softwareDescription').get('longDescription')
        visibility = (image_mod.PUBLIC_TYPE if image['publicFlag'] else image_mod.PRIVATE_TYPE)
        table.add_row([
            image.get('id', formatting.blank()),
            formatting.FormattedItem(image['name'], click.wrap_text(image['name'], width=50)),
            formatting.FormattedItem(
                utils.lookup(image, 'imageType', 'keyName'),
                utils.lookup(image, 'imageType', 'name')),
            visibility,
            image.get('accountId', formatting.blank()),
            operative_system,
            utils.clean_time(image.get('createDate', formatting.blank())),
            image.get('note', formatting.blank()),
        ])

    env.fout(table)