File: detail.py

package info (click to toggle)
python-softlayer 6.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,100 kB
  • sloc: python: 53,771; makefile: 289; sh: 57
file content (153 lines) | stat: -rw-r--r-- 6,231 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""User details."""
# :license: MIT, see LICENSE for more details.

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer.CLI import helpers
from SoftLayer import utils


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@click.option('--keys', '-k', is_flag=True, default=False,
              help="Show the users API key.")
@click.option('--permissions', '-p', is_flag=True, default=False,
              help="Display permissions assigned to this user. Master users will show no permissions")
@click.option('--hardware', '-h', is_flag=True, default=False,
              help="Display hardware this user has access to.")
@click.option('--virtual', '-v', is_flag=True, default=False,
              help="Display virtual guests this user has access to.")
@click.option('--logins', '-l', is_flag=True, default=False,
              help="Show login history of this user for the last 30 days")
@click.option('--events', '-e', is_flag=True, default=False,
              help="Show event log for this user.")
@environment.pass_env
def cli(env, identifier, keys, permissions, hardware, virtual, logins, events):
    """User details."""

    mgr = SoftLayer.UserManager(env.client)
    user_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'username')
    object_mask = "userStatus[name], parent[id, username], apiAuthenticationKeys[authenticationKey], "\
                  "unsuccessfulLogins, successfulLogins"

    user = mgr.get_user(user_id, object_mask)
    env.fout(basic_info(user, keys))

    if permissions:
        perms = mgr.get_user_permissions(user_id)
        env.fout(print_permissions(perms))
    if hardware:
        mask = "id, hardware, dedicatedHosts"
        access = mgr.get_user(user_id, mask)
        env.fout(print_dedicated_access(access.get('dedicatedHosts', [])))
        env.fout(print_access(access.get('hardware', []), 'Hardware'))
    if virtual:
        mask = "id, virtualGuests"
        access = mgr.get_user(user_id, mask)
        env.fout(print_access(access.get('virtualGuests', []), 'Virtual Guests'))
    if logins:
        login_log = mgr.get_logins(user_id)
        env.fout(print_logins(login_log))
    if events:
        event_log = mgr.get_events(user_id)
        env.fout(print_events(event_log))


def basic_info(user, keys):
    """Prints a table of basic user information"""

    table = formatting.KeyValueTable(['Title', 'Basic Information'])
    table.align['Title'] = 'r'
    table.align['Basic Information'] = 'l'

    table.add_row(['Id', user.get('id', '-')])
    table.add_row(['Username', user.get('username', '-')])
    if keys:
        for key in user.get('apiAuthenticationKeys'):
            table.add_row(['APIKEY', key.get('authenticationKey')])
    table.add_row(['Name', "%s %s" % (user.get('firstName', '-'), user.get('lastName', '-'))])
    table.add_row(['Email', user.get('email')])
    table.add_row(['OpenID', user.get('openIdConnectUserName')])
    address = "%s %s %s %s %s %s" % (
        user.get('address1'), user.get('address2'), user.get('city'), user.get('state'),
        user.get('country'), user.get('postalCode'))
    table.add_row(['Address', address])
    table.add_row(['Company', user.get('companyName')])
    table.add_row(['Created', user.get('createDate')])
    table.add_row(['Phone Number', user.get('officePhone')])
    if user.get('parentId', False):
        table.add_row(['Parent User', utils.lookup(user, 'parent', 'username')])
    table.add_row(
        ['Status', utils.lookup(user, 'userStatus', 'name')])
    table.add_row(['SSL VPN', user.get('sslVpnAllowedFlag', 'No')])
    for login in user.get('unsuccessfulLogins', {}):
        login_string = "%s From: %s" % (login.get('createDate'), login.get('ipAddress'))
        table.add_row(['Last Failed Login', login_string])
        break
    for login in user.get('successfulLogins', {}):
        login_string = "%s From: %s" % (login.get('createDate'), login.get('ipAddress'))
        table.add_row(['Last Login', login_string])
        break

    return table


def print_permissions(permissions):
    """Prints out a users permissions"""

    table = formatting.Table(['keyName', 'Description'])
    for perm in permissions:
        table.add_row([perm['keyName'], perm['name']])
    return table


def print_access(access, title):
    """Prints out the hardware or virtual guests a user can access"""

    columns = ['id', 'hostname', 'Primary Public IP', 'Primary Private IP', 'Created']
    table = formatting.Table(columns, title)

    for host in access:
        host_id = host.get('id')
        host_fqdn = host.get('fullyQualifiedDomainName', '-')
        host_primary = host.get('primaryIpAddress')
        host_private = host.get('primaryBackendIpAddress')
        host_created = host.get('provisionDate')
        table.add_row([host_id, host_fqdn, host_primary, host_private, host_created])
    return table


def print_dedicated_access(access):
    """Prints out the dedicated hosts a user can access"""

    table = formatting.Table(['id', 'Name', 'Cpus', 'Memory', 'Disk', 'Created'], 'Dedicated Access')
    for host in access:
        host_id = host.get('id')
        host_fqdn = host.get('name')
        host_cpu = host.get('cpuCount')
        host_mem = host.get('memoryCapacity')
        host_disk = host.get('diskCapacity')
        host_created = host.get('createDate')
        table.add_row([host_id, host_fqdn, host_cpu, host_mem, host_disk, host_created])
    return table


def print_logins(logins):
    """Prints out the login history for a user"""
    table = formatting.Table(['Date', 'IP Address', 'Successufl Login?'])
    for login in logins:
        table.add_row([login.get('createDate'), login.get('ipAddress'), login.get('successFlag')])
    return table


def print_events(events):
    """Prints out the event log for a user"""
    columns = ['Date', 'Type', 'IP Address', 'label', 'username']
    table = formatting.Table(columns)
    for event in events:
        table.add_row([event.get('eventCreateDate'), event.get('eventName'),
                       event.get('ipAddress'), event.get('label'), event.get('username')])
    return table