File: list.py

package info (click to toggle)
python-softlayer 6.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,100 kB
  • sloc: python: 53,771; makefile: 289; sh: 57
file content (92 lines) | stat: -rw-r--r-- 3,175 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
"""List VLANs."""
# :license: MIT, see LICENSE for more details.

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer.CLI.vlan.detail import get_gateway_firewall
from SoftLayer import utils

COLUMNS = ['Id',
           'Number',
           'Fully qualified name',
           'Name',
           'Network',
           'Data center',
           'Pod',
           'Gateway/Firewall',
           'Hardware',
           'Virtual servers',
           'Public ips',
           'Premium',
           'Tags']


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.option('--sortby',
              help='Column to sort by',
              type=click.Choice(COLUMNS))
@click.option('--datacenter', '-d',
              help='Filter by datacenter shortname (sng01, dal05, ...)')
@click.option('--number', '-n', help='Filter by VLAN number')
@click.option('--name', help='Filter by VLAN name')
@click.option('--limit', '-l',
              help='How many results to get in one api call, default is 100',
              default=100,
              show_default=True)
@environment.pass_env
def cli(env, sortby, datacenter, number, name, limit):
    """List VLANs.

    Note: A * Indicates a POD is closing soon. Ex:[red] Pod01* [/red]
    """

    mgr = SoftLayer.NetworkManager(env.client)

    table = formatting.Table(COLUMNS)
    table.sortby = sortby

    vlans = mgr.list_vlans(datacenter=datacenter,
                           vlan_number=number,
                           name=name,
                           limit=limit)

    mask = """mask[name, datacenterLongName, frontendRouterId, capabilities, datacenterId, backendRouterId,
                    backendRouterName, frontendRouterName]"""
    pods = mgr.get_pods(mask=mask)

    for vlan in vlans:
        billing = 'Yes' if vlan.get('billingItem') else 'No'

        table.add_row([
            vlan.get('id'),
            vlan.get('vlanNumber'),
            vlan.get('fullyQualifiedName'),
            vlan.get('name') or formatting.blank(),
            vlan.get('networkSpace', 'Direct Link').capitalize(),
            utils.lookup(vlan, 'primaryRouter', 'datacenter', 'name'),
            get_pod_with_closed_announcement(vlan, pods),
            get_gateway_firewall(vlan),
            vlan.get('hardwareCount'),
            vlan.get('virtualGuestCount'),
            vlan.get('totalPrimaryIpAddressCount'),
            billing,
            formatting.tags(vlan['tagReferences'])
        ])

    env.fout(table)


def get_pod_with_closed_announcement(vlan, pods):
    """Gets pods with announcement to close"""
    for pod in pods:
        if utils.lookup(pod, 'backendRouterId') == utils.lookup(vlan, 'primaryRouter', 'id') \
                or utils.lookup(pod, 'frontendRouterId') == utils.lookup(vlan, 'primaryRouter', 'id'):
            if 'CLOSURE_ANNOUNCED' in utils.lookup(pod, 'capabilities'):
                name_pod = utils.lookup(pod, 'name').split('.')[1] + '*'
                return "[red]" + name_pod.capitalize() + "[/red]"
            else:
                return utils.lookup(pod, 'name').split('.')[1].capitalize()
    return ''