Skip to content

Commit 878db94

Browse files
Edit autoscale groups command
1 parent 58b27c6 commit 878db94

File tree

5 files changed

+99
-15
lines changed

5 files changed

+99
-15
lines changed

SoftLayer/CLI/autoscale/edit.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,49 @@
99

1010
@click.command()
1111
@click.argument('identifier')
12-
# Suspend / Unsuspend
13-
# Name
14-
# Min/Max
15-
# template->userData
16-
# 'hostname', 'domain', 'startCpus', 'maxMemory', 'localDiskFlag'
17-
# 'blockDeviceTemplateGroup.globalIdentifier
18-
# blockDevices.diskImage.capacity
19-
# sshKeys.id
20-
# postInstallScriptUri
21-
@environment.pass_env
22-
def cli(env, identifier):
12+
@click.option('--name', help="Scale group's name.")
13+
@click.option('--min', 'minimum', type=click.INT, help="Set the minimum number of guests")
14+
@click.option('--max', 'maximum', type=click.INT, help="Set the maximum number of guests")
15+
@click.option('--userdata', help="User defined metadata string")
16+
@click.option('--userfile', '-F', help="Read userdata from a file",
17+
type=click.Path(exists=True, readable=True, resolve_path=True))
18+
@click.option('--cpu', type=click.INT, help="Number of CPUs for new guests (existing not effected")
19+
@click.option('--memory', type=click.INT, help="RAM in MB or GB for new guests (existing not effected")
20+
@environment.pass_env
21+
def cli(env, identifier, name, minimum, maximum, userdata, userfile, cpu, memory):
2322
"""Edits an Autoscale group."""
2423

24+
template = {}
2525
autoscale = AutoScaleManager(env.client)
26-
groups = autoscale.details(identifier)
27-
click.echo(groups)
26+
group = autoscale.details(identifier)
27+
28+
template['name'] = name
29+
template['minimumMemberCount'] = minimum
30+
template['maximumMemberCount'] = maximum
31+
virt_template = {}
32+
if userdata:
33+
virt_template['userData'] = [{"value":userdata}]
34+
elif userfile:
35+
with open(userfile, 'r') as userfile_obj:
36+
virt_template['userData'] = [{"value":userfile_obj.read()}]
37+
virt_template['startCpus'] = cpu
38+
virt_template['maxMemory'] = memory
39+
40+
# Remove any entries that are `None` as the API will complain about them.
41+
template['virtualGuestMemberTemplate'] = clean_dict(virt_template)
42+
clean_template = clean_dict(template)
43+
44+
# If there are any values edited in the template, we need to get the OLD template values and replace them.
45+
if template['virtualGuestMemberTemplate']:
46+
# Update old template with new values
47+
for key, value in clean_template['virtualGuestMemberTemplate'].items():
48+
group['virtualGuestMemberTemplate'][key] = value
49+
clean_template['virtualGuestMemberTemplate'] = group['virtualGuestMemberTemplate']
50+
51+
result = autoscale.edit(identifier, clean_template)
52+
click.echo("Done")
53+
54+
55+
def clean_dict(dictionary):
56+
"""Removes any `None` entires from the dictionary"""
57+
return {k: v for k, v in dictionary.items() if v}

SoftLayer/fixtures/SoftLayer_Scale_Group.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,5 @@
453453
}
454454
},
455455
]
456+
457+
editObject = True

SoftLayer/managers/autoscale.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,16 @@ def get_virtual_guests(self, identifier, mask=None):
103103
https://sldn.softlayer.com/reference/services/SoftLayer_Scale_Group/getVirtualGuestMembers/
104104
"""
105105
return self.client.call('SoftLayer_Scale_Group', 'getVirtualGuestMembers', id=identifier, mask=mask, iter=True)
106+
107+
def edit(self, identifier, template):
108+
"""Calls `SoftLayer_Scale_Group::editObject()`_
109+
110+
:param identifier: SoftLayer_Scale_Group id
111+
:param template: `SoftLayer_Scale_Group`_
112+
.. _SoftLayer_Scale_Group::editObject():
113+
https://sldn.softlayer.com/reference/services/SoftLayer_Scale_Group/editObject/
114+
.. _SoftLayer_Scale_Group: https://sldn.softlayer.com/reference/datatypes/SoftLayer_Scale_Group/
115+
"""
116+
117+
118+
return self.client.call('SoftLayer_Scale_Group', 'editObject', template, id=identifier)

tests/CLI/modules/autoscale_tests.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
88
Tests for the autoscale cli command
99
"""
10+
import mock
11+
import tempfile
12+
from SoftLayer import fixtures
1013
from SoftLayer import testing
1114

1215

@@ -49,6 +52,33 @@ def test_autoscale_tag(self):
4952
result = self.run_command(['autoscale', 'tag', '12345'])
5053
self.assert_no_fail(result)
5154

52-
def test_autoscale_edit(self):
53-
result = self.run_command(['autoscale', 'edit', '12345'])
55+
@mock.patch('SoftLayer.managers.autoscale.AutoScaleManager.edit')
56+
def test_autoscale_edit(self, manager):
57+
result = self.run_command(['autoscale', 'edit', '12345', '--name', 'test'])
5458
self.assert_no_fail(result)
59+
manager.assert_called_with('12345', {'name': 'test'})
60+
61+
@mock.patch('SoftLayer.managers.autoscale.AutoScaleManager.edit')
62+
def test_autoscale_edit_userdata(self, manager):
63+
group = fixtures.SoftLayer_Scale_Group.getObject
64+
template = {
65+
'virtualGuestMemberTemplate': group['virtualGuestMemberTemplate']
66+
}
67+
template['virtualGuestMemberTemplate']['userData'] = [{'value': 'test'}]
68+
69+
result = self.run_command(['autoscale', 'edit', '12345', '--userdata', 'test'])
70+
self.assert_no_fail(result)
71+
manager.assert_called_with('12345', template)
72+
73+
@mock.patch('SoftLayer.managers.autoscale.AutoScaleManager.edit')
74+
def test_autoscale_edit_userfile(self, manager):
75+
group = fixtures.SoftLayer_Scale_Group.getObject
76+
template = {
77+
'virtualGuestMemberTemplate': group['virtualGuestMemberTemplate']
78+
}
79+
template['virtualGuestMemberTemplate']['userData'] = [{'value': ''}]
80+
81+
with tempfile.NamedTemporaryFile() as userfile:
82+
result = self.run_command(['autoscale', 'edit', '12345', '--userfile', userfile.name])
83+
self.assert_no_fail(result)
84+
manager.assert_called_with('12345', template)

tests/managers/autoscale_tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,12 @@ def test_autoscale_get_virtual_guests_mask(self):
114114
identifier=11111,
115115
mask=test_mask
116116
)
117+
118+
def test_edit_object(self):
119+
template = {'name': 'test'}
120+
self.autoscale.edit(12345, template)
121+
self.assert_called_with(
122+
'SoftLayer_Scale_Group',
123+
'editObject',
124+
args=(template,),
125+
identifier=12345)

0 commit comments

Comments
 (0)