File: detail.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 (137 lines) | stat: -rw-r--r-- 4,974 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""Get details for an image."""
# :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 helpers
from SoftLayer.CLI import image as image_mod
from SoftLayer import utils


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@environment.pass_env
def cli(env, identifier):
    """Get details for an image."""

    image_mgr = SoftLayer.ImageManager(env.client)
    image_id = helpers.resolve_id(image_mgr.resolve_ids, identifier, 'image')
    image = image_mgr.get_image(image_id, mask=image_mod.DETAIL_MASK)

    children_images = image.get('children')
    total_size = utils.lookup(image, 'firstChild', 'blockDevicesDiskSpaceTotal') or 0

    table = formatting.KeyValueTable(['name', 'value'])
    table.align['name'] = 'r'
    table.align['value'] = 'l'

    table.add_row(['Id', image['id']])
    table.add_row(['Global Identifier',
                   image.get('globalIdentifier', formatting.blank())])
    table.add_row(['Name', image['name'].strip()])
    table.add_row(['Status', formatting.FormattedItem(
        utils.lookup(image, 'status', 'keyname'),
        utils.lookup(image, 'status', 'name'),
    )])

    table.add_row([
        'Active Transaction',
        formatting.listing(_get_transaction_groups(children_images), separator=','),
    ])
    table.add_row(['Account', image.get('accountId', formatting.blank())])
    table.add_row(['Created', image.get('createDate')])
    table.add_row(['Total Size', formatting.b_to_gb(total_size)])
    table.add_row(['Visibility',
                   image_mod.PUBLIC_TYPE if image['publicFlag']
                   else image_mod.PRIVATE_TYPE])
    table.add_row(['Type',
                   formatting.FormattedItem(
                       utils.lookup(image, 'imageType', 'keyName'),
                       utils.lookup(image, 'imageType', 'name'),
                   )])
    table.add_row(['Flex', image.get('flexImageFlag')])
    table.add_row(['Note', image.get('note')])
    table.add_row(['Datacenters', _get_datacenter_table(children_images)])
    table.add_row(['Virtual Disks', _get_virtual_disks(children_images)])
    table.add_row(['Share Image', _get_share_image(image)])

    env.fout(table)


def _get_datacenter_table(children_images):
    """Returns image details as datacenter, size, and transaction within a formatting table.

      :param children_images: A list of images.
      """
    table_datacenter = formatting.Table(['DC', 'Size', 'Transaction'])
    for child in children_images:
        table_datacenter.add_row([
            utils.lookup(child, 'datacenter', 'name'),
            formatting.b_to_gb(child.get('blockDevicesDiskSpaceTotal', 0)),
            formatting.transaction_status(child.get('transaction'))
        ])

    return table_datacenter


def _get_transaction_groups(children_images):
    """Returns a Set of transaction groups.

      :param children_images: A list of images.
      """
    transactions = set()
    for child in children_images:
        transactions.add(utils.lookup(child, 'transaction', 'transactionGroup', 'name'))

    return transactions


def _get_virtual_disks(children_images):
    """Returns datacenters as a table.

      :param children_images: A list of images.
      """

    table_virtual_disks = formatting.Table(['Device', 'Capacity', 'Size On Disk'])

    if utils.lookup(children_images[0], 'blockDevices'):
        for block_devices in children_images[0]['blockDevices']:
            device_name = utils.lookup(block_devices, 'diskImage', 'name')
            software_references = utils.lookup(block_devices, 'diskImage', 'softwareReferences')
            if len(software_references) > 0:
                device_name = utils.lookup(software_references[0], 'softwareDescription', 'longDescription')

            size_on_disk = formatting.b_to_gb(block_devices.get('diskSpace', 0))
            if block_devices.get('diskSpace', 0) == 0:
                size_on_disk = 'N/A'

            capacity = str(utils.lookup(block_devices, 'diskImage', 'capacity')) + \
                ' ' + utils.lookup(block_devices, 'diskImage', 'units')

            table_virtual_disks.add_row([
                device_name,
                capacity,
                size_on_disk
            ])

    return table_virtual_disks


def _get_share_image(image):
    """Returns shares image as a table.

      :param image: Detail information about image.
      """
    table_share_account = formatting.Table(['Account', 'Shared On'])
    if utils.lookup(image, 'accountReferences'):
        for account in image['accountReferences']:
            if account['accountId'] != image['accountId']:
                table_share_account.add_row([
                    utils.lookup(account, 'accountId'),
                    utils.lookup(account, 'createDate'),
                ])

    return table_share_account