Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feature search #1833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SoftLayer/CLI/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@
('vlan:cancel', 'SoftLayer.CLI.vlan.cancel:cli'),

('summary', 'SoftLayer.CLI.summary:cli'),
('search', 'SoftLayer.CLI.search:cli'),

('report', 'SoftLayer.CLI.report'),
('report:bandwidth', 'SoftLayer.CLI.report.bandwidth:cli'),
Expand Down
61 changes: 61 additions & 0 deletions SoftLayer/CLI/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Search for SoftLayer Resources by simple phrase."""
# :license: MIT, see LICENSE for more details.


import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer.managers.search import SearchManager

object_types = {'ticket': 'SoftLayer_Ticket',
'firewall': 'SoftLayer_Network_Vlan_Firewall',
'vlan': 'SoftLayer_Network_Vlan',
'subnet': 'SoftLayer_Network_Subnet_IpAddress',
'delivery': 'SoftLayer_Network_Application_Delivery_Controller',
'dedicated': 'SoftLayer_Virtual_DedicatedHost',
'log': 'SoftLayer_Event_Log',
'hardware': 'SoftLayer_Hardware',
'virtual': 'SoftLayer_Virtual_Guest'}


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('query', nargs=-1)
@click.option('--types', is_flag=True, default=False, is_eager=True, help="Display searchable types.")
@click.option('--advanced', is_flag=True, help="Calls the AdvancedSearh API.")
@environment.pass_env
def cli(env, query, types, advanced):
"""Perform a query against the SoftLayer search database.

Read More: https://sldn.softlayer.com/reference/services/SoftLayer_Search/search/
Examples:
slcli search test.com
slcli search _objectType:SoftLayer_Virtual_Guest test.com
slcli -vvv search _objectType:SoftLayer_Hardware hostname:testibm --advanced
"""
search = SearchManager(env.client)
# query is in array, so we need to convert it to a normal string for the API
query = " ".join(query)
if types:
search_types = search.get_object_types()

table = formatting.Table(["Name", "Properties"])
table.align = "r"

for type_object in search_types:
prop_table = formatting.Table(["Property", "Sortable"])
prop_table.align = "l"
for prop in type_object.get('properties'):
prop_table.add_row([prop.get('name'), prop.get('sortableFlag')])
table.add_row([type_object.get('name'), prop_table])

env.fout(table)
return
if advanced:
result = search.advanced(query)

env.fout(formatting.iter_to_table(result))
else:
result = search.search(query)
env.fout(formatting.iter_to_table(result))
28 changes: 28 additions & 0 deletions SoftLayer/fixtures/SoftLayer_Search.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,31 @@
}
}
]

search = advancedSearch

getObjectTypes = [{"name": "SoftLayer_Event_Log",
"properties": [
{
"name": "accountId",
"sortableFlag": True,
"type": "integer"
}]},
{"name": "SoftLayer_Hardware",
"properties": [
{
"name": "accountId",
"sortableFlag": True,
"type": "integer"
},
{
"name": "datacenter.longName",
"sortableFlag": True,
"type": "string"
},
{
"name": "deviceStatus.name",
"sortableFlag": True,
"type": "string"
}]
}]
36 changes: 36 additions & 0 deletions SoftLayer/managers/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
SoftLayer.search
~~~~~~~~~~~~~~~~~~
Search Manager

:license: MIT, see LICENSE for more details.
"""


class SearchManager(object):
"""Manager to help searcha via the SoftLayer API.

:param SoftLayer.API.BaseClient client: the client instance
"""

def __init__(self, client):
self.client = client
self.search_manager = client['SoftLayer_Search']

def get_object_types(self):
"""returns a collection of SoftLayer_Container_Search_ObjectType containers.

"""
return self.search_manager.getObjectTypes()

def search(self, search_string):
"""allows for searching for SoftLayer resources by simple phrase.

"""
return self.search_manager.search(search_string)

def advanced(self, search_string):
"""allows for searching for SoftLayer resources by simple phrase.

"""
return self.search_manager.advancedSearch(search_string)
10 changes: 10 additions & 0 deletions docs/cli/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ Can be called with an un-authenticated API call.
.. click:: SoftLayer.CLI.metadata:cli
:prog: metadata
:show-nested:

search
========

Is an API service that lets you make complex queries about data indexed by the service.
Can be called with an un-authenticated API call.

.. click:: SoftLayer.CLI.search:cli
:prog: search
:show-nested:
19 changes: 19 additions & 0 deletions tests/CLI/modules/search_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
SoftLayer.tests.CLI.modules.find_tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:license: MIT, see LICENSE for more details.
"""

from SoftLayer import testing


class FindTests(testing.TestCase):

def test_find(self):
result = self.run_command(['search', '--types'])
self.assert_no_fail(result)

def test_find_advanced(self):
result = self.run_command(['search', 'hardware', '--advanced'])
self.assert_no_fail(result)
27 changes: 27 additions & 0 deletions tests/managers/search_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
SoftLayer.tests.managers.search_tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:license: MIT, see LICENSE for more details.
"""

from SoftLayer.managers.search import SearchManager
from SoftLayer import testing


class SearchTests(testing.TestCase):

def set_up(self):
self.search = SearchManager(self.client)

def test_search_type(self):
self.search.get_object_types()
self.assert_called_with('SoftLayer_Search', 'getObjectTypes')

def test_search(self):
self.search.search('SoftLayer_Hardware')
self.assert_called_with('SoftLayer_Search', 'search')

def test_search_advanced(self):
self.search.advanced('SoftLayer_Hardware')
self.assert_called_with('SoftLayer_Search', 'advancedSearch')