File: search.py

package info (click to toggle)
python-softlayer 6.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,508 kB
  • sloc: python: 57,195; makefile: 133; xml: 97; sh: 59
file content (78 lines) | stat: -rw-r--r-- 2,756 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""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.")
@click.help_option('--help', '-h')
@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
    """

    # Before any Search operation
    def check_opt(list_opt=None):
        check = False
        for input_ in list_opt:
            if input_ is True:
                check = True
                break
        return check

    list_opt = [query, types, advanced]

    if not check_opt(list_opt):
        raise click.UsageError('Search query must be provided')

    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))