Skip to content

Commit ebc80cd

Browse files
added autoscale scale commands
1 parent feb8f9a commit ebc80cd

File tree

5 files changed

+95
-22
lines changed

5 files changed

+95
-22
lines changed

SoftLayer/CLI/autoscale/detail.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
1-
"""List Autoscale groups."""
1+
"""Get details of an Autoscale groups."""
22
# :license: MIT, see LICENSE for more details.
33

44
import click
55

66
import SoftLayer
7-
from SoftLayer.CLI import columns as column_helper
87
from SoftLayer.CLI import environment
98
from SoftLayer.CLI import formatting
10-
from SoftLayer.CLI import helpers
119
from SoftLayer.managers.autoscale import AutoScaleManager
1210
from SoftLayer import utils
1311

14-
from pprint import pprint as pp
1512

1613
@click.command()
1714
@click.argument('identifier')
1815
@environment.pass_env
1916
def cli(env, identifier):
20-
"""List AutoScale Groups."""
17+
"""Get details of an Autoscale groups."""
2118

2219
autoscale = AutoScaleManager(env.client)
2320
group = autoscale.details(identifier)
24-
# print(groups)
25-
# pp(group)
2621

2722
# Group Config Table
2823
table = formatting.KeyValueTable(["Group", "Value"])
@@ -46,7 +41,6 @@ def cli(env, identifier):
4641

4742
env.fout(table)
4843

49-
5044
# Template Config Table
5145
config_table = formatting.KeyValueTable(["Template", "Value"])
5246
template = group.get('virtualGuestMemberTemplate')
@@ -75,25 +69,14 @@ def cli(env, identifier):
7569

7670
env.fout(config_table)
7771

78-
7972
# Policy Config Table
8073
policy_table = formatting.KeyValueTable(["Policy", "Cooldown"])
8174
policies = group.get('policies')
82-
# pp(policies)
8375
for policy in policies:
8476
policy_table.add_row([policy.get('name'), policy.get('cooldown') or group.get('cooldown')])
85-
# full_policy = autoscale.get_policy(policy.get('id'))
86-
# pp(full_policy)
8777

8878
env.fout(policy_table)
8979

90-
# LB Config Table
91-
# Not sure if this still still a thing?
92-
# lb_table = formatting.KeyValueTable(["Load Balancer", "Value"])
93-
# loadbal = group.get('loadBalancers')
94-
95-
# env.fout(lb_table)
96-
9780
# Active Guests
9881
member_table = formatting.Table(['Id', 'Hostname', 'Created'], title="Active Guests")
9982
guests = group.get('virtualGuestMembers')

SoftLayer/CLI/autoscale/list.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
import click
55

66
import SoftLayer
7-
from SoftLayer.CLI import columns as column_helper
87
from SoftLayer.CLI import environment
98
from SoftLayer.CLI import formatting
10-
from SoftLayer.CLI import helpers
119
from SoftLayer.managers.autoscale import AutoScaleManager
1210
from SoftLayer import utils
1311

14-
from pprint import pprint as pp
1512

1613
@click.command()
1714
@environment.pass_env

SoftLayer/CLI/autoscale/scale.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Scales an Autoscale group"""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
6+
import SoftLayer
7+
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import formatting
9+
from SoftLayer.managers.autoscale import AutoScaleManager
10+
from SoftLayer import utils
11+
12+
13+
@click.command()
14+
@click.argument('identifier')
15+
@click.option('--up/--down', 'scale_up', is_flag=True, default=True,
16+
help="'--up' adds guests, '--down' removes guests.")
17+
@click.option('--by/--to', 'scale_by', is_flag=True, required=True,
18+
help="'--by' will add/remove the specified number of guests." \
19+
" '--to' will add/remove a number of guests to get the group's guest count to the specified number.")
20+
@click.option('--amount', required=True, type=click.INT, help="Number of guests for the scale action.")
21+
@environment.pass_env
22+
def cli(env, identifier, scale_up, scale_by, amount):
23+
"""Scales an Autoscale group. Bypasses a scale group's cooldown period."""
24+
25+
autoscale = AutoScaleManager(env.client)
26+
27+
# Scale By, and go down, need to use negative amount
28+
if not scale_up and scale_by:
29+
amount = amount * -1
30+
31+
result = []
32+
if scale_by:
33+
click.secho("Scaling group {} by {}".format(identifier, amount), fg='green')
34+
result = autoscale.scale(identifier, amount)
35+
else:
36+
click.secho("Scaling group {} to {}".format(identifier, amount), fg='green')
37+
result = autoscale.scale_to(identifier, amount)
38+
39+
try:
40+
# 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
44+
45+
if cancellationDate:
46+
member_table = formatting.Table(['Id', 'Hostname', 'Created'], title="Cancelled Guests")
47+
else:
48+
member_table = formatting.Table(['Id', 'Hostname', 'Created'], title="Added Guests")
49+
50+
for guest in result:
51+
real_guest = guest.get('virtualGuest')
52+
member_table.add_row([
53+
guest.get('id'), real_guest.get('hostname'), utils.clean_time(real_guest.get('createDate'))
54+
])
55+
56+
env.fout(member_table)

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@
306306
('autoscale', 'SoftLayer.CLI.autoscale'),
307307
('autoscale:list', 'SoftLayer.CLI.autoscale.list:cli'),
308308
('autoscale:detail', 'SoftLayer.CLI.autoscale.detail:cli'),
309+
('autoscale:scale', 'SoftLayer.CLI.autoscale.scale:cli'),
309310
]
310311

311312
ALL_ALIASES = {

SoftLayer/managers/autoscale.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,23 @@ def __init__(self, client):
1515

1616

1717
def list(self, mask=None):
18+
"""Calls SoftLayer_Account getScaleGroups()_
19+
20+
:param mask: optional SoftLayer_Scale_Group objectMask
21+
.. getScaleGroups(): https://sldn.softlayer.com/reference/services/SoftLayer_Account/getScaleGroups/
22+
"""
1823
if not mask:
1924
mask = "mask[status,virtualGuestMemberCount]"
2025

2126
return self.client.call('SoftLayer_Account', 'getScaleGroups', mask=mask, iter=True)
2227

2328
def details(self, identifier, mask=None):
29+
"""Calls SoftLayer_Scale_Group getObject()_
30+
31+
:param identifier: SoftLayer_Scale_Group id
32+
:param mask: optional SoftLayer_Scale_Group objectMask
33+
.. _getObject(): https://sldn.softlayer.com/reference/services/SoftLayer_Scale_Group/getObject/
34+
"""
2435
if not mask:
2536
mask = """mask[virtualGuestMembers[id,virtualGuest[hostname,domain,provisionDate]], terminationPolicy,
2637
virtualGuestMemberCount, virtualGuestMemberTemplate[sshKeys],
@@ -30,9 +41,34 @@ def details(self, identifier, mask=None):
3041
return self.client.call('SoftLayer_Scale_Group', 'getObject', id=identifier, mask=mask)
3142

3243
def get_policy(self, identifier, mask=None):
44+
"""Calls SoftLayer_Scale_Policy getObject()_
45+
46+
:param identifier: SoftLayer_Scale_Policy id
47+
:param mask: optional SoftLayer_Scale_Policy objectMask
48+
.. _getObject(): https://sldn.softlayer.com/reference/services/SoftLayer_Scale_Policy/getObject/
49+
"""
3350
if not mask:
3451
mask = """mask[cooldown, createDate, id, name, actions, triggers[type]
3552
3653
]"""
3754

3855
return self.client.call('SoftLayer_Scale_Policy', 'getObject', id=identifier, mask=mask)
56+
57+
def scale(self, identifier, amount):
58+
"""Calls SoftLayer_Scale_Group scale()_
59+
60+
:param identifier: SoftLayer_Scale_Group Id
61+
:param amount: positive or negative number to scale the group by
62+
63+
.. _scale(): https://sldn.softlayer.com/reference/services/SoftLayer_Scale_Group/scale/
64+
"""
65+
return self.client.call('SoftLayer_Scale_Group', 'scale', amount, id=identifier)
66+
67+
def scale_to(self, identifier, amount):
68+
"""Calls SoftLayer_Scale_Group scaleTo()_
69+
70+
:param identifier: SoftLayer_Scale_Group Id
71+
:param amount: number to scale the group to.
72+
.. _scaleTo(): https://sldn.softlayer.com/reference/services/SoftLayer_Scale_Group/scaleTo/
73+
"""
74+
return self.client.call('SoftLayer_Scale_Group', 'scaleTo', amount, id=identifier)

0 commit comments

Comments
 (0)