|
| 1 | +"""List guests which are in a dedicated host server.""" |
| 2 | +# :license: MIT, see LICENSE for more details. |
| 3 | + |
| 4 | +import click |
| 5 | + |
| 6 | +import SoftLayer |
| 7 | +from SoftLayer.CLI import columns as column_helper |
| 8 | +from SoftLayer.CLI import environment |
| 9 | +from SoftLayer.CLI import formatting |
| 10 | +from SoftLayer.CLI import helpers |
| 11 | + |
| 12 | +COLUMNS = [ |
| 13 | + column_helper.Column('guid', ('globalIdentifier',)), |
| 14 | + column_helper.Column('cpu', ('maxCpu',)), |
| 15 | + column_helper.Column('memory', ('maxMemory',)), |
| 16 | + column_helper.Column('datacenter', ('datacenter', 'name')), |
| 17 | + column_helper.Column('primary_ip', ('primaryIpAddress',)), |
| 18 | + column_helper.Column('backend_ip', ('primaryBackendIpAddress',)), |
| 19 | + column_helper.Column( |
| 20 | + 'created_by', |
| 21 | + ('billingItem', 'orderItem', 'order', 'userRecord', 'username')), |
| 22 | + column_helper.Column('power_state', ('powerState', 'name')), |
| 23 | + column_helper.Column( |
| 24 | + 'tags', |
| 25 | + lambda server: formatting.tags(server.get('tagReferences')), |
| 26 | + mask="tagReferences.tag.name"), |
| 27 | +] |
| 28 | + |
| 29 | +DEFAULT_COLUMNS = [ |
| 30 | + 'id', |
| 31 | + 'hostname', |
| 32 | + 'domain', |
| 33 | + 'primary_ip', |
| 34 | + 'backend_ip', |
| 35 | + 'power_state' |
| 36 | +] |
| 37 | + |
| 38 | + |
| 39 | +@click.command() |
| 40 | +@click.argument('identifier') |
| 41 | +@click.option('--cpu', '-c', help='Number of CPU cores', type=click.INT) |
| 42 | +@click.option('--domain', '-D', help='Domain portion of the FQDN') |
| 43 | +@click.option('--hostname', '-H', help='Host portion of the FQDN') |
| 44 | +@click.option('--memory', '-m', help='Memory in mebibytes', type=click.INT) |
| 45 | +@helpers.multi_option('--tag', help='Filter by tags') |
| 46 | +@click.option('--sortby', |
| 47 | + help='Column to sort by', |
| 48 | + default='hostname', |
| 49 | + show_default=True) |
| 50 | +@click.option('--columns', |
| 51 | + callback=column_helper.get_formatter(COLUMNS), |
| 52 | + help='Columns to display. [options: %s]' |
| 53 | + % ', '.join(column.name for column in COLUMNS), |
| 54 | + default=','.join(DEFAULT_COLUMNS), |
| 55 | + show_default=True) |
| 56 | +@environment.pass_env |
| 57 | +def cli(env, identifier, sortby, cpu, domain, hostname, memory, tag, columns): |
| 58 | + """List guests which are in a dedicated host server.""" |
| 59 | + |
| 60 | + mgr = SoftLayer.DedicatedHostManager(env.client) |
| 61 | + guests = mgr.list_guests(host_id=identifier, |
| 62 | + cpus=cpu, |
| 63 | + hostname=hostname, |
| 64 | + domain=domain, |
| 65 | + memory=memory, |
| 66 | + tags=tag, |
| 67 | + mask=columns.mask()) |
| 68 | + |
| 69 | + table = formatting.Table(columns.columns) |
| 70 | + table.sortby = sortby |
| 71 | + |
| 72 | + for guest in guests: |
| 73 | + table.add_row([value or formatting.blank() |
| 74 | + for value in columns.row(guest)]) |
| 75 | + |
| 76 | + env.fout(table) |
0 commit comments