Skip to content

Commit 73338ec

Browse files
#1862 added some basic search functionality for vs list
1 parent a4fee13 commit 73338ec

File tree

4 files changed

+58
-57
lines changed

4 files changed

+58
-57
lines changed

SoftLayer/CLI/virt/list.py

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
'action',
4646
]
4747

48+
def search_callback(ctx, param, value):
49+
print(f"SEARCH_CALLBACK: value: {value}")
50+
if not value:
51+
print("Value is true now")
52+
value = True
53+
return value
4854

4955
@click.command(cls=SLCommand, short_help="List virtual servers.")
5056
@click.option('--cpu', '-c', help='Number of CPU cores', type=click.INT)
@@ -56,67 +62,41 @@
5662
@click.option('--hourly', is_flag=True, help='Show only hourly instances')
5763
@click.option('--monthly', is_flag=True, help='Show only monthly instances')
5864
@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')
65+
@click.option('--search', is_flag=False, flag_value="", default=None,
66+
help="Use the more flexible Search API to list instances. See `slcli search --types` for list " +
67+
"of searchable fields.")
6168
@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)
69+
@click.option('--sortby', default='hostname', show_default=True, help='Column to sort by')
6670
@click.option('--columns',
6771
callback=column_helper.get_formatter(COLUMNS),
6872
help='Columns to display. [options: %s]'
6973
% ', '.join(column.name for column in COLUMNS),
7074
default=','.join(DEFAULT_COLUMNS),
7175
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)
76+
@click.option('--limit', '-l', default=100, show_default=True,
77+
help='How many results to get in one api call, default is 100')
7678
@environment.pass_env
7779
def cli(env, sortby, cpu, domain, datacenter, hostname, memory, network,
78-
hourly, monthly, tag, columns, limit, transient, hardware, all_guests):
80+
hourly, monthly, tag, columns, limit, transient, search):
7981
"""List virtual servers."""
8082

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)
83+
guests = []
84+
if search is not None:
85+
sm = SoftLayer.SearchManager(env.client)
86+
guests = sm.search_instances(hostname=hostname, domain=domain, datacenter=datacenter,
87+
tags=tag, search_string=search, mask=columns.mask())
88+
else:
89+
vsi = SoftLayer.VSManager(env.client)
90+
guests = vsi.list_instances(hourly=hourly, monthly=monthly, hostname=hostname, domain=domain,
91+
cpus=cpu, memory=memory, datacenter=datacenter, nic_speed=network,
92+
transient=transient, tags=tag, mask=columns.mask(), limit=limit)
9493

9594
table = formatting.Table(columns.columns)
9695
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)])
10196

102-
env.fout(table)
97+
for guest in guests:
98+
table.add_row([value or formatting.blank()
99+
for value in columns.row(guest)])
100+
101+
env.fout(table)
103102

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)

SoftLayer/managers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from SoftLayer.managers.network import NetworkManager
2525
from SoftLayer.managers.object_storage import ObjectStorageManager
2626
from SoftLayer.managers.ordering import OrderingManager
27+
from SoftLayer.managers.search import SearchManager
2728
from SoftLayer.managers.sshkey import SshKeyManager
2829
from SoftLayer.managers.ssl import SSLManager
2930
from SoftLayer.managers.tags import TagManager
@@ -53,6 +54,7 @@
5354
'ObjectStorageManager',
5455
'OrderingManager',
5556
'PlacementManager',
57+
'SearchManager',
5658
'SshKeyManager',
5759
'SSLManager',
5860
'TagManager',

SoftLayer/managers/search.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,31 @@ def advanced(self, search_string):
3434
3535
"""
3636
return self.search_manager.advancedSearch(search_string)
37+
38+
def search_instances(self, search_string, mask=None, **kwargs):
39+
"""Lists VSIs based in the search_string.
40+
41+
Also takes in a few search terms as **kwargs. such as hostname, datacenter, domain and tags
42+
"""
43+
44+
# This forces the Search API to do a fuzzy search on our term, kinda. Not sure why the ** are
45+
# Required but it will do an exact search without them.
46+
if search_string:
47+
search_string = f"*{search_string}*"
48+
search_string = f"_objectType:SoftLayer_Virtual_Guest {search_string}"
49+
if kwargs.get('hostname'):
50+
search_string = f"{search_string} hostname: *{kwargs.get('hostname')}*"
51+
if kwargs.get('domain'):
52+
search_string = f"{search_string} domain: *{kwargs.get('domain')}*"
53+
if kwargs.get('datacenter'):
54+
search_string = f"{search_string} datacenter.longName: *{kwargs.get('datacenter')}*"
55+
if kwargs.get('tags'):
56+
tags = " ".join(kwargs.get("tags", []))
57+
search_string = f"{search_string} internalTagReferences.tag.name: {kwargs.get('tags')}"
58+
result = self.search_manager.advancedSearch(search_string)
59+
guests = []
60+
for x in result:
61+
guests.append(x.get('resource'))
62+
63+
return guests
64+

SoftLayer/managers/vs.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,15 +1441,6 @@ def migrate_dedicated(self, instance_id, host_id):
14411441
"""
14421442
return self.guest.migrateDedicatedHost(host_id, id=instance_id)
14431443

1444-
def get_hardware_guests(self):
1445-
"""Returns all virtualHost capable hardware objects and their guests.
1446-
1447-
:return SoftLayer_Hardware[].
1448-
"""
1449-
object_filter = {"hardware": {"virtualHost": {"id": {"operation": "not null"}}}}
1450-
mask = "mask[virtualHost[guests[powerState]]]"
1451-
return self.client.call('SoftLayer_Account', 'getHardware', mask=mask, filter=object_filter)
1452-
14531444
def authorize_storage(self, vs_id, username_storage):
14541445
"""Authorize File or Block Storage to a Virtual Server.
14551446

0 commit comments

Comments
 (0)