File: upgrade.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 (152 lines) | stat: -rw-r--r-- 7,557 bytes parent folder | download | duplicates (2)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""Upgrade a hardware server."""
# :license: MIT, see LICENSE for more details.

import click
from SoftLayer import utils

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
from SoftLayer.CLI import formatting
from SoftLayer.CLI import helpers


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@click.option('--memory', type=click.INT, help="Memory Size in GB")
@click.option('--network', help="Network port speed in Mbps",
              default=None,
              type=click.Choice(['100', '100 Redundant', '100 Dual',
                                 '1000', '1000 Redundant', '1000 Dual',
                                 '10000', '10000 Redundant', '10000 Dual'])
              )
@click.option('--drive-controller',
              help="Drive Controller",
              default=None,
              type=click.Choice(['Non-RAID', 'RAID']))
@click.option('--public-bandwidth', type=click.INT, help="Public Bandwidth in GB")
@click.option('--add-disk', nargs=2, multiple=True, type=(int, int),
              help="Add a Hard disk in GB to a specific channel, e.g 1000 GB in disk2, it will be "
                   "--add-disk 1000 2")
@click.option('--resize-disk', nargs=2, multiple=True, type=(int, int),
              help="Upgrade a specific disk size in GB, e.g --resize-disk 2000 2")
@click.option('--test', is_flag=True, default=False, help="Do not actually upgrade the hardware server")
@environment.pass_env
def cli(env, identifier, memory, network, drive_controller, public_bandwidth, add_disk, resize_disk, test):
    """Upgrade a Hardware Server."""

    mgr = SoftLayer.HardwareManager(env.client)
    table = formatting.KeyValueTable(['name', 'value'])
    table.align['name'] = 'r'
    table.align['value'] = 'l'

    if not any([memory, network, drive_controller, public_bandwidth, add_disk, resize_disk]):
        raise exceptions.ArgumentError("Must provide "
                                       " [--memory], [--network], [--drive-controller], [--public-bandwidth],"
                                       "[--add-disk] or [--resize-disk]")

    hw_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'Hardware')
    if not test:
        if not (env.skip_confirmations or formatting.confirm(
                "This action will incur charges on your account. Continue?")):
            raise exceptions.CLIAbort('Aborted')

    disk_list = []
    if add_disk:
        for guest_disk in add_disk:
            disks = {'description': 'add_disk', 'capacity': guest_disk[0], 'number': guest_disk[1]}
            disk_list.append(disks)
    if resize_disk:
        for guest_disk in resize_disk:
            disks = {'description': 'resize_disk', 'capacity': guest_disk[0], 'number': guest_disk[1]}
            disk_list.append(disks)

    response = mgr.upgrade(hw_id, memory=memory, nic_speed=network, drive_controller=drive_controller,
                           public_bandwidth=public_bandwidth, disk=disk_list, test=test)

    if response:
        if test:
            add_data_to_table(response, table)
        else:
            table.add_row(['Order Date', response.get('orderDate')])
            table.add_row(['Order Id', response.get('orderId')])
            add_data_to_table(response['orderDetails'], table)
            place_order_table = get_place_order_information(response)
            table.add_row(['Place Order Information', place_order_table])
            order_detail_table = get_order_detail(response)
            table.add_row(['Order Detail (Billing Information)', order_detail_table])

    env.fout(table)


def add_data_to_table(response, table):
    """Add the hardware server upgrade result to the table"""
    table.add_row(['location', utils.lookup(response, 'locationObject', 'longName')])
    table.add_row(['quantity', response.get('quantity')])
    table.add_row(['Package Id', response.get('packageId')])
    table.add_row(['Currency Short Name', response.get('currencyShortName')])
    table.add_row(['Prorated Initial Charge', response.get('proratedInitialCharge')])
    table.add_row(['Prorated Order Total', response.get('proratedOrderTotal')])
    table.add_row(['Hourly Pricing', response.get('useHourlyPricing')])
    table_hardware = get_hardware_detail(response)
    table.add_row(['Hardware', table_hardware])
    table_prices = get_hardware_prices(response)
    table.add_row(['Prices', table_prices])


def get_place_order_information(response):
    """Get the hardware server place order information."""
    table_place_order = formatting.Table(['Id', 'Account Id', 'Status', 'Account CompanyName',
                                          'UserRecord FirstName', 'UserRecord LastName', 'UserRecord Username'])
    table_place_order.add_row([response.get('id'),
                               response.get('accountId'),
                               response.get('status'),
                               utils.lookup(response, 'account', 'companyName'),
                               utils.lookup(response, 'userRecord', 'firstName'),
                               utils.lookup(response, 'account', 'lastName'),
                               utils.lookup(response, 'account', 'username')])

    return table_place_order


def get_hardware_detail(response):
    """Get the hardware server detail."""
    table_hardware = formatting.Table(['Account Id', 'Hostname', 'Domain'])
    for hardware in response['hardware']:
        table_hardware.add_row([hardware.get('accountId'),
                                hardware.get('hostname'),
                                hardware.get('domain')])

    return table_hardware


def get_hardware_prices(response):
    """Get the hardware server prices."""
    table_prices = formatting.Table(['Id', 'HourlyRecurringFee', 'RecurringFee', 'Categories', 'Item Description',
                                     'Item Units'])
    for price in response['prices']:
        categories = price.get('categories')[0]
        table_prices.add_row([price.get('id'),
                              price.get('hourlyRecurringFee'),
                              price.get('recurringFee'),
                              categories.get('name'),
                              utils.lookup(price, 'item', 'description'),
                              utils.lookup(price, 'item', 'units')])

    return table_prices


def get_order_detail(response):
    """Get the hardware server order detail."""
    table_order_detail = formatting.Table(['Billing City', 'Billing Country Code', 'Billing Email',
                                           'Billing Name First', 'Billing Name Last', 'Billing Postal Code',
                                           'Billing State'])
    table_order_detail.add_row([utils.lookup(response, 'orderDetails', 'billingInformation', 'billingCity'),
                                utils.lookup(response, 'orderDetails', 'billingInformation', 'billingCountryCode'),
                                utils.lookup(response, 'orderDetails', 'billingInformation', 'billingEmail'),
                                utils.lookup(response, 'orderDetails', 'billingInformation', 'billingNameFirst'),
                                utils.lookup(response, 'orderDetails', 'billingInformation', 'billingNameLast'),
                                utils.lookup(response, 'orderDetails', 'billingInformation', 'billingPostalCode'),
                                utils.lookup(response, 'orderDetails', 'billingInformation', 'billingState')])

    return table_order_detail