File: orders.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 (56 lines) | stat: -rw-r--r-- 2,219 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
"""Lists account orders."""
# :license: MIT, see LICENSE for more details.

import click

from SoftLayer.CLI.command import SLCommand as SLCommand
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer.managers.account import AccountManager as AccountManager
from SoftLayer import utils


def upgrade_table(upgrades):
    """Formats a table for upgrade orders"""
    table = formatting.Table(['Id', 'Maintance window', 'Status', 'Created Date',
                              'Case'], title="Upgrade orders")
    table.align['Subject'] = 'l'
    table.align['Impacted Resources'] = 'l'
    for upgrade in upgrades:
        table.add_row([upgrade.get('id'),
                       upgrade.get('maintenanceStartTimeUtc'),
                       upgrade.get('statusId'),
                       upgrade.get('createDate'),
                       upgrade.get('ticketId') or '--'])
    return table


@click.command(cls=SLCommand)
@click.option('--limit', '-l',
              help='How many results to get in one api call',
              default=100,
              show_default=True)
@click.option('--upgrades', is_flag=True, default=False,
              help="Show upgrades orders.")
@environment.pass_env
def cli(env, limit, upgrades):
    """Lists account orders. Use `slcli order lookup <ID>` to find more details about a specific order."""
    manager = AccountManager(env.client)
    orders = manager.get_account_all_billing_orders(limit)
    upgrade = manager.get_account_upgrade_orders(limit)

    order_table = formatting.Table(['Id', 'State', 'User', 'Date', 'Amount', 'Item'],
                                   title="orders")
    order_table.align = 'l'

    for order in orders:
        items = []
        for item in order['items']:
            items.append(item['description'])
        create_date = utils.clean_time(order['createDate'], in_format='%Y-%m-%d', out_format='%Y-%m-%d')

        order_table.add_row([order['id'], order['status'], order['userRecord']['username'], create_date,
                             order['orderTotalAmount'], utils.trim_to(' '.join(map(str, items)), 50)])
    env.fout(order_table)
    if upgrades:
        env.fout(upgrade_table(upgrade))