|
4 | 4 | import click |
5 | 5 |
|
6 | 6 | import SoftLayer |
7 | | -from SoftLayer.CLI import columns as column_helper |
8 | 7 | from SoftLayer.CLI import environment |
9 | 8 | from SoftLayer.CLI import formatting |
10 | 9 | from SoftLayer.CLI import helpers |
11 | 10 |
|
12 | | -COLUMNS = [ |
13 | | - column_helper.Column('datacenter', ('datacenter', 'name')), |
14 | | - column_helper.Column( |
15 | | - 'created_by', |
16 | | - ('billingItem', 'orderItem', 'order', 'userRecord', 'username')), |
17 | | - column_helper.Column( |
18 | | - 'tags', |
19 | | - lambda server: formatting.tags(server.get('tagReferences')), |
20 | | - mask="tagReferences.tag.name"), |
21 | | -] |
22 | | - |
23 | | -DEFAULT_COLUMNS = [ |
24 | | - 'id', |
25 | | - 'name', |
26 | | - 'cpuCount', |
27 | | - 'diskCapacity', |
28 | | - 'memoryCapacity', |
29 | | - 'datacenter', |
30 | | - 'guestCount', |
31 | | -] |
32 | | - |
33 | 11 |
|
34 | 12 | @click.command(cls=SoftLayer.CLI.command.SLCommand, ) |
35 | | -@click.option('--cpu', '-c', help='Number of CPU cores', type=click.INT) |
36 | 13 | @helpers.multi_option('--tag', help='Filter by tags') |
37 | 14 | @click.option('--sortby', help='Column to sort by', |
38 | | - default='name', |
39 | | - show_default=True) |
40 | | -@click.option('--columns', |
41 | | - callback=column_helper.get_formatter(COLUMNS), |
42 | | - help='Columns to display. [options: %s]' |
43 | | - % ', '.join(column.name for column in COLUMNS), |
44 | | - default=','.join(DEFAULT_COLUMNS), |
| 15 | + default='Name', |
45 | 16 | show_default=True) |
46 | | -@click.option('--datacenter', '-d', help='Datacenter shortname') |
47 | | -@click.option('--name', '-H', help='Host portion of the FQDN') |
48 | | -@click.option('--memory', '-m', help='Memory capacity in mebibytes', |
49 | | - type=click.INT) |
50 | | -@click.option('--disk', '-D', help='Disk capacity') |
| 17 | +@click.option('--datacenter', '-d', help='Filter by datacenter shortname') |
| 18 | +@click.option('--name', '-H', help='Filter by host portion of the FQDN') |
| 19 | +@click.option('--order', help='Filter by ID of the order which purchased this dedicated host', type=click.INT) |
| 20 | +@click.option('--owner', help='Filter by owner of the dedicated host') |
51 | 21 | @environment.pass_env |
52 | | -def cli(env, sortby, cpu, columns, datacenter, name, memory, disk, tag): |
| 22 | +def cli(env, sortby, datacenter, name, tag, order, owner): |
53 | 23 | """List dedicated host.""" |
54 | 24 | mgr = SoftLayer.DedicatedHostManager(env.client) |
55 | | - hosts = mgr.list_instances(cpus=cpu, |
56 | | - datacenter=datacenter, |
57 | | - hostname=name, |
58 | | - memory=memory, |
59 | | - disk=disk, |
60 | | - tags=tag, |
61 | | - mask=columns.mask()) |
62 | | - |
63 | | - table = formatting.Table(columns.columns) |
| 25 | + dedicated_hosts = mgr.list_instances(datacenter=datacenter, |
| 26 | + hostname=name, |
| 27 | + tags=tag, |
| 28 | + order=order, |
| 29 | + owner=owner) |
| 30 | + |
| 31 | + table = formatting.Table(["Id", "Name", "Datacenter", "Router", "CPU (allocated/total)", |
| 32 | + "Memory (allocated/total)", "Disk (allocated/total)", "Guests"]) |
| 33 | + table.align['Name'] = 'l' |
64 | 34 | table.sortby = sortby |
65 | 35 |
|
66 | | - for host in hosts: |
67 | | - table.add_row([value or formatting.blank() |
68 | | - for value in columns.row(host)]) |
69 | | - |
70 | | - env.fout(table) |
| 36 | + if len(dedicated_hosts) == 0: |
| 37 | + click.secho("No dedicated hosts are found.") |
| 38 | + else: |
| 39 | + for host in dedicated_hosts: |
| 40 | + cpu_allocated = host.get('allocationStatus').get('cpuAllocated') |
| 41 | + cpu_total = host.get('allocationStatus').get('cpuCount') |
| 42 | + memory_allocated = host.get('allocationStatus').get('memoryAllocated') |
| 43 | + memory_total = host.get('allocationStatus').get('memoryCapacity') |
| 44 | + disk_allocated = host.get('allocationStatus').get('diskAllocated') |
| 45 | + disk_total = host.get('allocationStatus').get('diskCapacity') |
| 46 | + table.add_row([ |
| 47 | + host.get('id'), |
| 48 | + host.get('name'), |
| 49 | + host.get('datacenter').get('name'), |
| 50 | + host.get('backendRouter').get('hostname'), |
| 51 | + f"{cpu_allocated}/{cpu_total}", |
| 52 | + f"{memory_allocated}/{memory_total}", |
| 53 | + f"{disk_allocated}/{disk_total}", |
| 54 | + host.get('guestCount') |
| 55 | + ]) |
| 56 | + |
| 57 | + env.fout(table) |
0 commit comments