File: billing_items.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 (66 lines) | stat: -rw-r--r-- 2,339 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
"""Lists all active billing items on this account. See https://cloud.ibm.com/billing/billing-items"""
# :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


@click.command(cls=SLCommand)
@click.option('--create', '-c', help='The date the billing item was created.')
@click.option('--ordered', '-o', help='Name that ordered the item')
@click.option('--category', '-C', help='Category name')
@environment.pass_env
def cli(env, create, category, ordered):
    """Lists billing items with some other useful information.

    Similiar to https://cloud.ibm.com/billing/billing-items
    """

    manager = AccountManager(env.client)
    items = manager.get_account_billing_items(create, category)
    table = item_table(items, ordered)

    env.fout(table)


def item_table(items, ordered=None):
    """Formats a table for billing items"""
    table = formatting.Table([
        "Id",
        "Create Date",
        "Cost",
        "Category Code",
        "Ordered By",
        "Description",
        "Notes"
    ], title="Billing Items")
    table.align['Description'] = 'l'
    table.align['Category Code'] = 'l'
    for item in items:
        description = item.get('description')
        fqdn = f"{item.get('hostName', '')}.{item.get('domainName', '')}"
        if fqdn != ".":
            description = fqdn
        user = utils.lookup(item, 'orderItem', 'order', 'userRecord')
        ordered_by = "IBM"
        create_date = utils.clean_time(item.get('createDate'), in_format='%Y-%m-%d', out_format='%Y-%m-%d')
        if user:
            # ordered_by = "{} ({})".format(user.get('displayName'), utils.lookup(user, 'userStatus', 'name'))
            ordered_by = user.get('displayName')
        if ordered:
            if ordered != ordered_by:
                continue
        table.add_row([
            item.get('id'),
            create_date,
            item.get('nextInvoiceTotalRecurringAmount'),
            item.get('categoryCode'),
            ordered_by,
            utils.trim_to(description, 50),
            utils.trim_to(item.get('notes', 'None'), 40),
        ])
    return table