Skip to content

Commit 645df87

Browse files
autoscale logs, tox fixes
1 parent ebc80cd commit 645df87

File tree

6 files changed

+68
-19
lines changed

6 files changed

+68
-19
lines changed

SoftLayer/CLI/autoscale/detail.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def cli(env, identifier):
2121

2222
# Group Config Table
2323
table = formatting.KeyValueTable(["Group", "Value"])
24+
table.align['Group'] = 'l'
25+
table.align['Value'] = 'l'
2426

2527
table.add_row(['Id', group.get('id')])
2628
# Ideally we would use regionalGroup->preferredDatacenter, but that generates an error.
@@ -31,7 +33,7 @@ def cli(env, identifier):
3133
table.add_row(['Current Members', group.get('virtualGuestMemberCount')])
3234
table.add_row(['Cooldown', "{} seconds".format(group.get('cooldown'))])
3335
table.add_row(['Last Action', utils.clean_time(group.get('lastActionDate'))])
34-
36+
3537
for network in group.get('networkVlans'):
3638
network_type = utils.lookup(network, 'networkVlan', 'networkSpace')
3739
router = utils.lookup(network, 'networkVlan', 'primaryRouter', 'hostname')
@@ -43,8 +45,11 @@ def cli(env, identifier):
4345

4446
# Template Config Table
4547
config_table = formatting.KeyValueTable(["Template", "Value"])
48+
config_table.align['Template'] = 'l'
49+
config_table.align['Value'] = 'l'
50+
4651
template = group.get('virtualGuestMemberTemplate')
47-
52+
4853
config_table.add_row(['Hostname', template.get('hostname')])
4954
config_table.add_row(['Domain', template.get('domain')])
5055
config_table.add_row(['Core', template.get('startCpus')])

SoftLayer/CLI/autoscale/list.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import click
55

6-
import SoftLayer
76
from SoftLayer.CLI import environment
87
from SoftLayer.CLI import formatting
98
from SoftLayer.managers.autoscale import AutoScaleManager
@@ -17,16 +16,14 @@ def cli(env):
1716

1817
autoscale = AutoScaleManager(env.client)
1918
groups = autoscale.list()
20-
# print(groups)
21-
# pp(groups)
22-
table = formatting.Table(["Id", "Name", "Status", "Min/Max", "Running"])
2319

20+
table = formatting.Table(["Id", "Name", "Status", "Min/Max", "Running"])
21+
table.align['Name'] = 'l'
2422
for group in groups:
2523
status = utils.lookup(group, 'status', 'name')
26-
min_max = "{}/{}".format(group.get('minimumMemberCount', '-'), group.get('maximumMemberCount'), '-')
24+
min_max = "{}/{}".format(group.get('minimumMemberCount'), group.get('maximumMemberCount'))
2725
table.add_row([
2826
group.get('id'), group.get('name'), status, min_max, group.get('virtualGuestMemberCount')
2927
])
3028

31-
3229
env.fout(table)

SoftLayer/CLI/autoscale/logs.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Retreive logs for an autoscale group"""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
6+
from SoftLayer.CLI import environment
7+
from SoftLayer.CLI import formatting
8+
from SoftLayer.managers.autoscale import AutoScaleManager
9+
from SoftLayer import utils
10+
11+
12+
@click.command()
13+
@click.argument('identifier')
14+
@click.option('--date-min', '-d', 'date_min', type=click.DateTime(formats=["%Y-%m-%d", "%m/%d/%Y"]),
15+
help='Earliest date to retreive logs for.')
16+
@environment.pass_env
17+
def cli(env, identifier, date_min):
18+
"""Retreive logs for an autoscale group"""
19+
20+
autoscale = AutoScaleManager(env.client)
21+
22+
mask = "mask[id,createDate,description]"
23+
object_filter = {}
24+
if date_min:
25+
object_filter['logs'] = {
26+
'createDate': {
27+
'operation': 'greaterThanDate',
28+
'options': [{'name': 'date', 'value': [date_min.strftime("%m/%d/%Y")]}]
29+
}
30+
}
31+
32+
logs = autoscale.get_logs(identifier, mask=mask, object_filter=object_filter)
33+
table = formatting.Table(['Date', 'Entry'], title="Logs")
34+
table.align['Entry'] = 'l'
35+
for log in logs:
36+
table.add_row([utils.clean_time(log.get('createDate')), log.get('description')])
37+
env.fout(table)

SoftLayer/CLI/autoscale/scale.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import click
55

6-
import SoftLayer
76
from SoftLayer.CLI import environment
87
from SoftLayer.CLI import formatting
98
from SoftLayer.managers.autoscale import AutoScaleManager
@@ -15,7 +14,7 @@
1514
@click.option('--up/--down', 'scale_up', is_flag=True, default=True,
1615
help="'--up' adds guests, '--down' removes guests.")
1716
@click.option('--by/--to', 'scale_by', is_flag=True, required=True,
18-
help="'--by' will add/remove the specified number of guests." \
17+
help="'--by' will add/remove the specified number of guests."
1918
" '--to' will add/remove a number of guests to get the group's guest count to the specified number.")
2019
@click.option('--amount', required=True, type=click.INT, help="Number of guests for the scale action.")
2120
@environment.pass_env
@@ -30,19 +29,19 @@ def cli(env, identifier, scale_up, scale_by, amount):
3029

3130
result = []
3231
if scale_by:
33-
click.secho("Scaling group {} by {}".format(identifier, amount), fg='green')
32+
click.secho("Scaling group {} by {}".format(identifier, amount), fg='green')
3433
result = autoscale.scale(identifier, amount)
3534
else:
36-
click.secho("Scaling group {} to {}".format(identifier, amount), fg='green')
35+
click.secho("Scaling group {} to {}".format(identifier, amount), fg='green')
3736
result = autoscale.scale_to(identifier, amount)
3837

3938
try:
4039
# Check if the first guest has a cancellation date, assume we are removing guests if it is.
41-
cancellationDate = result[0]['virtualGuest']['billingItem']['cancellationDate'] or False
42-
except (IndexError, KeyError, TypeError) as e:
43-
cancellationDate = False
40+
cancel_date = result[0]['virtualGuest']['billingItem']['cancellationDate'] or False
41+
except (IndexError, KeyError, TypeError):
42+
cancel_date = False
4443

45-
if cancellationDate:
44+
if cancel_date:
4645
member_table = formatting.Table(['Id', 'Hostname', 'Created'], title="Cancelled Guests")
4746
else:
4847
member_table = formatting.Table(['Id', 'Hostname', 'Created'], title="Added Guests")

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@
307307
('autoscale:list', 'SoftLayer.CLI.autoscale.list:cli'),
308308
('autoscale:detail', 'SoftLayer.CLI.autoscale.detail:cli'),
309309
('autoscale:scale', 'SoftLayer.CLI.autoscale.scale:cli'),
310+
('autoscale:logs', 'SoftLayer.CLI.autoscale.logs:cli'),
310311
]
311312

312313
ALL_ALIASES = {

SoftLayer/managers/autoscale.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
"""
88

99

10-
1110
class AutoScaleManager(object):
11+
"""Manager for interacting with Autoscale instances."""
1212

1313
def __init__(self, client):
1414
self.client = client
1515

16-
1716
def list(self, mask=None):
1817
"""Calls SoftLayer_Account getScaleGroups()_
1918
@@ -71,4 +70,15 @@ def scale_to(self, identifier, amount):
7170
:param amount: number to scale the group to.
7271
.. _scaleTo(): https://sldn.softlayer.com/reference/services/SoftLayer_Scale_Group/scaleTo/
7372
"""
74-
return self.client.call('SoftLayer_Scale_Group', 'scaleTo', amount, id=identifier)
73+
return self.client.call('SoftLayer_Scale_Group', 'scaleTo', amount, id=identifier)
74+
75+
def get_logs(self, identifier, mask=None, object_filter=None):
76+
"""Calls SoftLayer_Scale_Group getLogs()_
77+
78+
:param identifier: SoftLayer_Scale_Group Id
79+
:param mask: optional SoftLayer_Scale_Group_Log objectMask
80+
:param object_filter: optional SoftLayer_Scale_Group_Log objectFilter
81+
.. getLogs(): https://sldn.softlayer.com/reference/services/SoftLayer_Scale_Group/getLogs/
82+
"""
83+
return self.client.call('SoftLayer_Scale_Group', 'getLogs', id=identifier, mask=mask, filter=object_filter,
84+
iter=True)

0 commit comments

Comments
 (0)