|
9 | 9 | from SoftLayer.CLI import environment |
10 | 10 | from SoftLayer.CLI import formatting |
11 | 11 | from SoftLayer.CLI import helpers |
12 | | -from SoftLayer import utils |
13 | 12 |
|
14 | 13 | # pylint: disable=unnecessary-lambda |
15 | 14 |
|
|
56 | 55 | @click.option('--hourly', is_flag=True, help='Show only hourly instances') |
57 | 56 | @click.option('--monthly', is_flag=True, help='Show only monthly instances') |
58 | 57 | @click.option('--transient', help='Filter by transient instances', type=click.BOOL) |
59 | | -@click.option('--hardware', is_flag=True, default=False, help='Show the all VSI related to hardware') |
60 | | -@click.option('--all-guests', is_flag=True, default=False, help='Show the all VSI and hardware VSIs') |
| 58 | +@click.option('--search', is_flag=False, flag_value="", default=None, |
| 59 | + help="Use the more flexible Search API to list instances. See `slcli search --types` for list " + |
| 60 | + "of searchable fields.") |
61 | 61 | @helpers.multi_option('--tag', help='Filter by tags') |
62 | | -@click.option('--sortby', |
63 | | - help='Column to sort by', |
64 | | - default='hostname', |
65 | | - show_default=True) |
| 62 | +@click.option('--sortby', default='hostname', show_default=True, help='Column to sort by') |
66 | 63 | @click.option('--columns', |
67 | 64 | callback=column_helper.get_formatter(COLUMNS), |
68 | 65 | help='Columns to display. [options: %s]' |
69 | 66 | % ', '.join(column.name for column in COLUMNS), |
70 | 67 | default=','.join(DEFAULT_COLUMNS), |
71 | 68 | show_default=True) |
72 | | -@click.option('--limit', '-l', |
73 | | - help='How many results to get in one api call, default is 100', |
74 | | - default=100, |
75 | | - show_default=True) |
| 69 | +@click.option('--limit', '-l', default=100, show_default=True, |
| 70 | + help='How many results to get in one api call, default is 100') |
76 | 71 | @environment.pass_env |
77 | 72 | def cli(env, sortby, cpu, domain, datacenter, hostname, memory, network, |
78 | | - hourly, monthly, tag, columns, limit, transient, hardware, all_guests): |
| 73 | + hourly, monthly, tag, columns, limit, transient, search): |
79 | 74 | """List virtual servers.""" |
80 | 75 |
|
81 | | - vsi = SoftLayer.VSManager(env.client) |
82 | | - guests = vsi.list_instances(hourly=hourly, |
83 | | - monthly=monthly, |
84 | | - hostname=hostname, |
85 | | - domain=domain, |
86 | | - cpus=cpu, |
87 | | - memory=memory, |
88 | | - datacenter=datacenter, |
89 | | - nic_speed=network, |
90 | | - transient=transient, |
91 | | - tags=tag, |
92 | | - mask=columns.mask(), |
93 | | - limit=limit) |
| 76 | + guests = [] |
| 77 | + if search is not None: |
| 78 | + object_mask = f"mask[resource(SoftLayer_Virtual_Guest)[{columns.mask()}]]" |
| 79 | + search_manager = SoftLayer.SearchManager(env.client) |
| 80 | + guests = search_manager.search_instances(hostname=hostname, domain=domain, datacenter=datacenter, |
| 81 | + tags=tag, search_string=search, mask=object_mask) |
| 82 | + else: |
| 83 | + vsi = SoftLayer.VSManager(env.client) |
| 84 | + guests = vsi.list_instances(hourly=hourly, monthly=monthly, hostname=hostname, domain=domain, |
| 85 | + cpus=cpu, memory=memory, datacenter=datacenter, nic_speed=network, |
| 86 | + transient=transient, tags=tag, mask=columns.mask(), limit=limit) |
94 | 87 |
|
95 | 88 | table = formatting.Table(columns.columns) |
96 | 89 | table.sortby = sortby |
97 | | - if not hardware or all_guests: |
98 | | - for guest in guests: |
99 | | - table.add_row([value or formatting.blank() |
100 | | - for value in columns.row(guest)]) |
101 | 90 |
|
102 | | - env.fout(table) |
| 91 | + for guest in guests: |
| 92 | + table.add_row([value or formatting.blank() |
| 93 | + for value in columns.row(guest)]) |
103 | 94 |
|
104 | | - if hardware or all_guests: |
105 | | - hardware_guests = vsi.get_hardware_guests() |
106 | | - for hd_guest in hardware_guests: |
107 | | - if hd_guest['virtualHost']['guests']: |
108 | | - title = "Hardware(id = {hardwareId}) guests associated".format(hardwareId=hd_guest['id']) |
109 | | - table_hardware_guest = formatting.Table(['id', 'hostname', 'CPU', 'Memory', 'Start Date', 'Status', |
110 | | - 'powerState'], title=title) |
111 | | - table_hardware_guest.sortby = 'hostname' |
112 | | - for guest in hd_guest['virtualHost']['guests']: |
113 | | - table_hardware_guest.add_row([ |
114 | | - guest['id'], |
115 | | - guest['hostname'], |
116 | | - '%i %s' % (guest['maxCpu'], guest['maxCpuUnits']), |
117 | | - guest['maxMemory'], |
118 | | - utils.clean_time(guest['createDate']), |
119 | | - guest['status']['keyName'], |
120 | | - guest['powerState']['keyName'] |
121 | | - ]) |
122 | | - env.fout(table_hardware_guest) |
| 95 | + env.fout(table) |
0 commit comments