Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
54f72c1
security-groups-request-ids : Add output for RequestIDs
fisherma91 Aug 23, 2017
4c0badf
security-groups-request-ids : Add output for RequestIDs
fisherma91 Aug 25, 2017
7768a83
security-groups-request-ids : Add output for RequestIDs
fisherma91 Oct 6, 2017
d3650de
security-groups-request-ids : Add output for RequestIDs
fisherma91 Oct 23, 2017
f4bc42f
security-groups-request-ids : Add output for RequestIDs
fisherma91 Oct 23, 2017
d7a736f
security-groups-request-ids : Add output for RequestIDs
fisherma91 Oct 24, 2017
d851de6
security-groups-request-ids : Add output for RequestIDs
fisherma91 Oct 24, 2017
4228058
security-groups-request-ids : Add output for RequestIDs
fisherma91 Oct 24, 2017
9d04a8e
security-groups-request-ids : Add output for RequestIDs
fisherma91 Oct 24, 2017
a3406a1
security-groups-request-ids : Add output for RequestIDs
fisherma91 Oct 24, 2017
884d117
security-groups-request-ids : Add output for RequestIDs
fisherma91 Oct 25, 2017
7c25d97
security-groups-request-ids : Add output for RequestIDs
fisherma91 Oct 26, 2017
265460a
security-groups-request-ids : Add output for RequestIDs
fisherma91 Oct 27, 2017
a453d6d
Merge branch 'master' into security-groups-request-ids
fisherma91 Jan 9, 2018
01ee771
security-groups-request-ids : Add output for RequestIDs
fisherma91 Jan 9, 2018
3a72d7f
security-groups-request-ids : Add output for RequestIDs
fisherma91 Feb 1, 2018
6333687
Major refactoring of audit log code
fisherma91 Feb 8, 2018
6ae6814
Updated fixture.
Feb 5, 2019
495dcc2
Merge branch 'master' into finishfish
erick-sapp Feb 5, 2019
e2648c6
Fixing typos and refactoring work.
Feb 5, 2019
5a406b4
More refactoring.
Feb 5, 2019
8bbbe78
Formating changes.
Feb 5, 2019
82574e0
Updates to fixture and unit test.
Feb 5, 2019
5320df0
Refactoring. Audi-log is no more. All references has been changed to …
Feb 5, 2019
5aee15e
Merge pull request #3 from erick-sapp/master
erick-sapp Feb 5, 2019
bb1717c
Made the metadata field optional, and handles empty responses.
Feb 6, 2019
8bb2b1e
Updated unit tests.
Feb 6, 2019
004e5af
Strips out leading and trailing curly-brackets from metadata if displ…
Feb 6, 2019
d21cbd1
Added and renamed fields.
Feb 7, 2019
6a16076
Finished unit test.
Feb 10, 2019
62e66b7
Formating changes.
Feb 10, 2019
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/event_log/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Event Logs."""
66 changes: 66 additions & 0 deletions SoftLayer/CLI/event_log/get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""Get Event Logs."""
# :license: MIT, see LICENSE for more details.

import json

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting

COLUMNS = ['event', 'object', 'type', 'date', 'username']


@click.command()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO:
Add a way to limit results. Something like in slcli vs list maybe.

@click.option('--date-min', '-d',
help='The earliest date we want to search for event logs in mm/dd/yyyy format.')
@click.option('--date-max', '-D',
help='The latest date we want to search for event logs in mm/dd/yyyy format.')
@click.option('--obj-event', '-e',
help="The event we want to get event logs for")
@click.option('--obj-id', '-i',
help="The id of the object we want to get event logs for")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

help="The id of the object (server, storage, user, etc) to get logs for"

@click.option('--obj-type', '-t',
help="The type of the object we want to get event logs for")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

help="The type of the object we want to get event logs for. See slcli event-log types for a list"

@click.option('--utc-offset', '-z',
help="UTC Offset for searching with dates. The default is -0000")
@click.option('--metadata/--no-metadata', default=False,
help="Display metadata if present")
@environment.pass_env
def cli(env, date_min, date_max, obj_event, obj_id, obj_type, utc_offset, metadata):
"""Get Event Logs"""
mgr = SoftLayer.EventLogManager(env.client)
usrmgr = SoftLayer.UserManager(env.client)
request_filter = mgr.build_filter(date_min, date_max, obj_event, obj_id, obj_type, utc_offset)
logs = mgr.get_event_logs(request_filter)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_event_logs should likely return an empty list at least.

$ ./slcli event-log get -t "Bare Metal Instance"
An unexpected error has occured:
Traceback (most recent call last):
  File "C:\Users\allmi\Source\softlayer-python\SoftLayer\CLI\core.py", line 182, in main
    cli.main(**kwargs)
  File "C:\Users\allmi\Source\py36\lib\site-packages\click\core.py", line 717, in main
    rv = self.invoke(ctx)
  File "C:\Users\allmi\Source\py36\lib\site-packages\click\core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\allmi\Source\py36\lib\site-packages\click\core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\allmi\Source\py36\lib\site-packages\click\core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\allmi\Source\py36\lib\site-packages\click\core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "C:\Users\allmi\Source\py36\lib\site-packages\click\decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "C:\Users\allmi\Source\py36\lib\site-packages\click\core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "C:\Users\allmi\Source\softlayer-python\SoftLayer\CLI\event_log\get.py", line 39, in cli
    for log in logs:
TypeError: 'NoneType' object is not iterable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be taken care of with the latest push.


if logs is None:
env.fout('None available.')
return

if metadata and 'metadata' not in COLUMNS:
COLUMNS.append('metadata')

table = formatting.Table(COLUMNS)
if metadata:
table.align['metadata'] = "l"

for log in logs:
user = log['userType']
if user == "CUSTOMER":
user = usrmgr.get_user(log['userId'], "mask[username]")['username']
if metadata:
try:
metadata_data = json.dumps(json.loads(log['metaData']), indent=4, sort_keys=True)
if env.format == "table":
metadata_data = metadata_data.strip("{}\n\t")
except ValueError:
metadata_data = log['metaData']

table.add_row([log['eventName'], log['label'], log['objectName'],
log['eventCreateDate'], user, metadata_data])
else:
table.add_row([log['eventName'], log['label'], log['objectName'],
log['eventCreateDate'], user])
env.fout(table)
26 changes: 26 additions & 0 deletions SoftLayer/CLI/event_log/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Get Event Log Types."""
# :license: MIT, see LICENSE for more details.

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting

COLUMNS = ['types']


@click.command()
@environment.pass_env
def cli(env):
"""Get Event Log Types"""
mgr = SoftLayer.EventLogManager(env.client)

event_log_types = mgr.get_event_log_types()

table = formatting.Table(COLUMNS)

for event_log_type in event_log_types:
table.add_row([event_log_type])

env.fout(table)
5 changes: 5 additions & 0 deletions SoftLayer/CLI/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
('block:volume-order', 'SoftLayer.CLI.block.order:cli'),
('block:volume-set-lun-id', 'SoftLayer.CLI.block.lun:cli'),

('event-log', 'SoftLayer.CLI.event_log'),
('event-log:get', 'SoftLayer.CLI.event_log.get:cli'),
('event-log:types', 'SoftLayer.CLI.event_log.types:cli'),

('file', 'SoftLayer.CLI.file'),
('file:access-authorize', 'SoftLayer.CLI.file.access.authorize:cli'),
('file:access-list', 'SoftLayer.CLI.file.access.list:cli'),
Expand Down Expand Up @@ -256,6 +260,7 @@
'SoftLayer.CLI.securitygroup.interface:add'),
('securitygroup:interface-remove',
'SoftLayer.CLI.securitygroup.interface:remove'),
('securitygroup:event-log', 'SoftLayer.CLI.securitygroup.event_log:get_by_request_id'),

('sshkey', 'SoftLayer.CLI.sshkey'),
('sshkey:add', 'SoftLayer.CLI.sshkey.add:cli'),
Expand Down
32 changes: 32 additions & 0 deletions SoftLayer/CLI/securitygroup/event_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Get event logs relating to security groups"""
# :license: MIT, see LICENSE for more details.

import json

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting

COLUMNS = ['event', 'label', 'date', 'metadata']


@click.command()
@click.argument('request_id')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: I'm not really sure using the request_id here is useful, as that doesn't seem to be a very accessible piece of information for a user to get. Maybe it would be better to get the logs by the security group id?

@environment.pass_env
def get_by_request_id(env, request_id):
"""Search for event logs by request id"""
mgr = SoftLayer.NetworkManager(env.client)

logs = mgr.get_event_logs_by_request_id(request_id)

table = formatting.Table(COLUMNS)
table.align['metadata'] = "l"

for log in logs:
metadata = json.dumps(json.loads(log['metaData']), indent=4, sort_keys=True)

table.add_row([log['eventName'], log['label'], log['eventCreateDate'], metadata])

env.fout(table)
24 changes: 18 additions & 6 deletions SoftLayer/CLI/securitygroup/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
'interface',
'ipAddress', ]

REQUEST_COLUMNS = ['requestId']


@click.command()
@click.argument('securitygroup_id')
Expand Down Expand Up @@ -90,11 +92,16 @@ def add(env, securitygroup_id, network_component, server, interface):
mgr = SoftLayer.NetworkManager(env.client)
component_id = _get_component_id(env, network_component, server, interface)

success = mgr.attach_securitygroup_component(securitygroup_id,
component_id)
if not success:
ret = mgr.attach_securitygroup_component(securitygroup_id,
component_id)
if not ret:
raise exceptions.CLIAbort("Could not attach network component")

table = formatting.Table(REQUEST_COLUMNS)
table.add_row([ret['requestId']])

env.fout(table)


@click.command()
@click.argument('securitygroup_id')
Expand All @@ -113,11 +120,16 @@ def remove(env, securitygroup_id, network_component, server, interface):
mgr = SoftLayer.NetworkManager(env.client)
component_id = _get_component_id(env, network_component, server, interface)

success = mgr.detach_securitygroup_component(securitygroup_id,
component_id)
if not success:
ret = mgr.detach_securitygroup_component(securitygroup_id,
component_id)
if not ret:
raise exceptions.CLIAbort("Could not detach network component")

table = formatting.Table(REQUEST_COLUMNS)
table.add_row([ret['requestId']])

env.fout(table)


def _validate_args(network_component, server, interface):
use_server = bool(server and interface and not network_component)
Expand Down
27 changes: 25 additions & 2 deletions SoftLayer/CLI/securitygroup/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
'createDate',
'modifyDate']

REQUEST_BOOL_COLUMNS = ['requestId', 'response']
REQUEST_RULES_COLUMNS = ['requestId', 'rules']


@click.command()
@click.argument('securitygroup_id')
Expand Down Expand Up @@ -109,6 +112,11 @@ def add(env, securitygroup_id, remote_ip, remote_group,
if not ret:
raise exceptions.CLIAbort("Failed to add security group rule")

table = formatting.Table(REQUEST_RULES_COLUMNS)
table.add_row([ret['requestId'], str(ret['rules'])])

env.fout(table)


@click.command()
@click.argument('securitygroup_id')
Expand Down Expand Up @@ -149,9 +157,16 @@ def edit(env, securitygroup_id, rule_id, remote_ip, remote_group,
if protocol:
data['protocol'] = protocol

if not mgr.edit_securitygroup_rule(securitygroup_id, rule_id, **data):
ret = mgr.edit_securitygroup_rule(securitygroup_id, rule_id, **data)

if not ret:
raise exceptions.CLIAbort("Failed to edit security group rule")

table = formatting.Table(REQUEST_BOOL_COLUMNS)
table.add_row([ret['requestId']])

env.fout(table)


@click.command()
@click.argument('securitygroup_id')
Expand All @@ -160,5 +175,13 @@ def edit(env, securitygroup_id, rule_id, remote_ip, remote_group,
def remove(env, securitygroup_id, rule_id):
"""Remove a rule from a security group."""
mgr = SoftLayer.NetworkManager(env.client)
if not mgr.remove_securitygroup_rule(securitygroup_id, rule_id):

ret = mgr.remove_securitygroup_rule(securitygroup_id, rule_id)

if not ret:
raise exceptions.CLIAbort("Failed to remove security group rule")

table = formatting.Table(REQUEST_BOOL_COLUMNS)
table.add_row([ret['requestId']])

env.fout(table)
2 changes: 1 addition & 1 deletion SoftLayer/CLI/user/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@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 audit log for this user.")
help="Show event log for this user.")
@environment.pass_env
def cli(env, identifier, keys, permissions, hardware, virtual, logins, events):
"""User details."""
Expand Down
Loading