Skip to content

Commit 4addadf

Browse files
Merge pull request #1833 from caberos/issue1815
feature search
2 parents 40437dc + 1fd47bf commit 4addadf

File tree

7 files changed

+182
-0
lines changed

7 files changed

+182
-0
lines changed

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@
387387
('vlan:cancel', 'SoftLayer.CLI.vlan.cancel:cli'),
388388

389389
('summary', 'SoftLayer.CLI.summary:cli'),
390+
('search', 'SoftLayer.CLI.search:cli'),
390391

391392
('report', 'SoftLayer.CLI.report'),
392393
('report:bandwidth', 'SoftLayer.CLI.report.bandwidth:cli'),

SoftLayer/CLI/search.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Search for SoftLayer Resources by simple phrase."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
5+
import click
6+
7+
import SoftLayer
8+
from SoftLayer.CLI import environment
9+
from SoftLayer.CLI import formatting
10+
from SoftLayer.managers.search import SearchManager
11+
12+
object_types = {'ticket': 'SoftLayer_Ticket',
13+
'firewall': 'SoftLayer_Network_Vlan_Firewall',
14+
'vlan': 'SoftLayer_Network_Vlan',
15+
'subnet': 'SoftLayer_Network_Subnet_IpAddress',
16+
'delivery': 'SoftLayer_Network_Application_Delivery_Controller',
17+
'dedicated': 'SoftLayer_Virtual_DedicatedHost',
18+
'log': 'SoftLayer_Event_Log',
19+
'hardware': 'SoftLayer_Hardware',
20+
'virtual': 'SoftLayer_Virtual_Guest'}
21+
22+
23+
@click.command(cls=SoftLayer.CLI.command.SLCommand, )
24+
@click.argument('query', nargs=-1)
25+
@click.option('--types', is_flag=True, default=False, is_eager=True, help="Display searchable types.")
26+
@click.option('--advanced', is_flag=True, help="Calls the AdvancedSearh API.")
27+
@environment.pass_env
28+
def cli(env, query, types, advanced):
29+
"""Perform a query against the SoftLayer search database.
30+
31+
Read More: https://sldn.softlayer.com/reference/services/SoftLayer_Search/search/
32+
Examples:
33+
slcli search test.com
34+
slcli search _objectType:SoftLayer_Virtual_Guest test.com
35+
slcli -vvv search _objectType:SoftLayer_Hardware hostname:testibm --advanced
36+
"""
37+
search = SearchManager(env.client)
38+
# query is in array, so we need to convert it to a normal string for the API
39+
query = " ".join(query)
40+
if types:
41+
search_types = search.get_object_types()
42+
43+
table = formatting.Table(["Name", "Properties"])
44+
table.align = "r"
45+
46+
for type_object in search_types:
47+
prop_table = formatting.Table(["Property", "Sortable"])
48+
prop_table.align = "l"
49+
for prop in type_object.get('properties'):
50+
prop_table.add_row([prop.get('name'), prop.get('sortableFlag')])
51+
table.add_row([type_object.get('name'), prop_table])
52+
53+
env.fout(table)
54+
return
55+
if advanced:
56+
result = search.advanced(query)
57+
58+
env.fout(formatting.iter_to_table(result))
59+
else:
60+
result = search.search(query)
61+
env.fout(formatting.iter_to_table(result))

SoftLayer/fixtures/SoftLayer_Search.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,31 @@
2121
}
2222
}
2323
]
24+
25+
search = advancedSearch
26+
27+
getObjectTypes = [{"name": "SoftLayer_Event_Log",
28+
"properties": [
29+
{
30+
"name": "accountId",
31+
"sortableFlag": True,
32+
"type": "integer"
33+
}]},
34+
{"name": "SoftLayer_Hardware",
35+
"properties": [
36+
{
37+
"name": "accountId",
38+
"sortableFlag": True,
39+
"type": "integer"
40+
},
41+
{
42+
"name": "datacenter.longName",
43+
"sortableFlag": True,
44+
"type": "string"
45+
},
46+
{
47+
"name": "deviceStatus.name",
48+
"sortableFlag": True,
49+
"type": "string"
50+
}]
51+
}]

SoftLayer/managers/search.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
SoftLayer.search
3+
~~~~~~~~~~~~~~~~~~
4+
Search Manager
5+
6+
:license: MIT, see LICENSE for more details.
7+
"""
8+
9+
10+
class SearchManager(object):
11+
"""Manager to help searcha via the SoftLayer API.
12+
13+
:param SoftLayer.API.BaseClient client: the client instance
14+
"""
15+
16+
def __init__(self, client):
17+
self.client = client
18+
self.search_manager = client['SoftLayer_Search']
19+
20+
def get_object_types(self):
21+
"""returns a collection of SoftLayer_Container_Search_ObjectType containers.
22+
23+
"""
24+
return self.search_manager.getObjectTypes()
25+
26+
def search(self, search_string):
27+
"""allows for searching for SoftLayer resources by simple phrase.
28+
29+
"""
30+
return self.search_manager.search(search_string)
31+
32+
def advanced(self, search_string):
33+
"""allows for searching for SoftLayer resources by simple phrase.
34+
35+
"""
36+
return self.search_manager.advancedSearch(search_string)

docs/cli/commands.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,13 @@ Can be called with an un-authenticated API call.
3535
.. click:: SoftLayer.CLI.metadata:cli
3636
:prog: metadata
3737
:show-nested:
38+
39+
search
40+
========
41+
42+
Is an API service that lets you make complex queries about data indexed by the service.
43+
Can be called with an un-authenticated API call.
44+
45+
.. click:: SoftLayer.CLI.search:cli
46+
:prog: search
47+
:show-nested:

tests/CLI/modules/search_tests.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
SoftLayer.tests.CLI.modules.find_tests
3+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
5+
:license: MIT, see LICENSE for more details.
6+
"""
7+
8+
from SoftLayer import testing
9+
10+
11+
class FindTests(testing.TestCase):
12+
13+
def test_find(self):
14+
result = self.run_command(['search', '--types'])
15+
self.assert_no_fail(result)
16+
17+
def test_find_advanced(self):
18+
result = self.run_command(['search', 'hardware', '--advanced'])
19+
self.assert_no_fail(result)

tests/managers/search_tests.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
SoftLayer.tests.managers.search_tests
3+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
5+
:license: MIT, see LICENSE for more details.
6+
"""
7+
8+
from SoftLayer.managers.search import SearchManager
9+
from SoftLayer import testing
10+
11+
12+
class SearchTests(testing.TestCase):
13+
14+
def set_up(self):
15+
self.search = SearchManager(self.client)
16+
17+
def test_search_type(self):
18+
self.search.get_object_types()
19+
self.assert_called_with('SoftLayer_Search', 'getObjectTypes')
20+
21+
def test_search(self):
22+
self.search.search('SoftLayer_Hardware')
23+
self.assert_called_with('SoftLayer_Search', 'search')
24+
25+
def test_search_advanced(self):
26+
self.search.advanced('SoftLayer_Hardware')
27+
self.assert_called_with('SoftLayer_Search', 'advancedSearch')

0 commit comments

Comments
 (0)