|
| 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)) |
0 commit comments