|
1 | | -"""Lists all active billing items on this account. See https://cloud.ibm.com/billing/billing-items""" |
2 | | -# :license: MIT, see LICENSE for more details. |
3 | | -import click |
4 | | - |
5 | | -from SoftLayer.CLI import environment |
6 | | -from SoftLayer.CLI import formatting |
7 | | -from SoftLayer.managers.account import AccountManager as AccountManager |
8 | | -from SoftLayer import utils |
9 | | - |
10 | | - |
11 | | -@click.command() |
12 | | -@environment.pass_env |
13 | | -def cli(env): |
14 | | - """Lists billing items with some other useful information. |
15 | | -
|
16 | | - Similiar to https://cloud.ibm.com/billing/billing-items |
17 | | - """ |
18 | | - |
19 | | - manager = AccountManager(env.client) |
20 | | - items = manager.get_account_billing_items() |
21 | | - table = item_table(items) |
22 | | - |
23 | | - env.fout(table) |
24 | | - |
25 | | - |
26 | | -def item_table(items): |
27 | | - """Formats a table for billing items""" |
28 | | - table = formatting.Table([ |
29 | | - "Id", |
30 | | - "Create Date", |
31 | | - "Cost", |
32 | | - "Category Code", |
33 | | - "Ordered By", |
34 | | - "Description", |
35 | | - "Notes" |
36 | | - ], title="Billing Items") |
37 | | - table.align['Description'] = 'l' |
38 | | - table.align['Category Code'] = 'l' |
39 | | - for item in items: |
40 | | - description = item.get('description') |
41 | | - fqdn = "{}.{}".format(item.get('hostName', ''), item.get('domainName', '')) |
42 | | - if fqdn != ".": |
43 | | - description = fqdn |
44 | | - user = utils.lookup(item, 'orderItem', 'order', 'userRecord') |
45 | | - ordered_by = "IBM" |
46 | | - create_date = utils.clean_time(item.get('createDate'), in_format='%Y-%m-%d', out_format='%Y-%m-%d') |
47 | | - if user: |
48 | | - # ordered_by = "{} ({})".format(user.get('displayName'), utils.lookup(user, 'userStatus', 'name')) |
49 | | - ordered_by = user.get('displayName') |
50 | | - |
51 | | - table.add_row([ |
52 | | - item.get('id'), |
53 | | - create_date, |
54 | | - item.get('nextInvoiceTotalRecurringAmount'), |
55 | | - item.get('categoryCode'), |
56 | | - ordered_by, |
57 | | - utils.trim_to(description, 50), |
58 | | - utils.trim_to(item.get('notes', 'None'), 40), |
59 | | - ]) |
60 | | - return table |
| 1 | +"""Lists all active billing items on this account. See https://cloud.ibm.com/billing/billing-items""" |
| 2 | +# :license: MIT, see LICENSE for more details. |
| 3 | +import click |
| 4 | + |
| 5 | +from SoftLayer.CLI import environment |
| 6 | +from SoftLayer.CLI import formatting |
| 7 | +from SoftLayer.managers.account import AccountManager as AccountManager |
| 8 | +from SoftLayer import utils |
| 9 | + |
| 10 | + |
| 11 | +@click.command() |
| 12 | +@environment.pass_env |
| 13 | +def cli(env): |
| 14 | + """Lists billing items with some other useful information. |
| 15 | +
|
| 16 | + Similiar to https://cloud.ibm.com/billing/billing-items |
| 17 | + """ |
| 18 | + |
| 19 | + manager = AccountManager(env.client) |
| 20 | + items = manager.get_account_billing_items() |
| 21 | + table = item_table(items) |
| 22 | + |
| 23 | + env.fout(table) |
| 24 | + |
| 25 | + |
| 26 | +def item_table(items): |
| 27 | + """Formats a table for billing items""" |
| 28 | + table = formatting.Table([ |
| 29 | + "Id", |
| 30 | + "Create Date", |
| 31 | + "Cost", |
| 32 | + "Category Code", |
| 33 | + "Ordered By", |
| 34 | + "Description", |
| 35 | + "Notes" |
| 36 | + ], title="Billing Items") |
| 37 | + table.align['Description'] = 'l' |
| 38 | + table.align['Category Code'] = 'l' |
| 39 | + for item in items: |
| 40 | + description = item.get('description') |
| 41 | + fqdn = "{}.{}".format(item.get('hostName', ''), item.get('domainName', '')) |
| 42 | + if fqdn != ".": |
| 43 | + description = fqdn |
| 44 | + user = utils.lookup(item, 'orderItem', 'order', 'userRecord') |
| 45 | + ordered_by = "IBM" |
| 46 | + create_date = utils.clean_time(item.get('createDate'), in_format='%Y-%m-%d', out_format='%Y-%m-%d') |
| 47 | + if user: |
| 48 | + # ordered_by = "{} ({})".format(user.get('displayName'), utils.lookup(user, 'userStatus', 'name')) |
| 49 | + ordered_by = user.get('displayName') |
| 50 | + |
| 51 | + table.add_row([ |
| 52 | + item.get('id'), |
| 53 | + create_date, |
| 54 | + item.get('nextInvoiceTotalRecurringAmount'), |
| 55 | + item.get('categoryCode'), |
| 56 | + ordered_by, |
| 57 | + utils.trim_to(description, 50), |
| 58 | + utils.trim_to(item.get('notes', 'None'), 40), |
| 59 | + ]) |
| 60 | + return table |
0 commit comments