forked from softlayer/softlayer-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedit.py
57 lines (46 loc) · 2.37 KB
/
edit.py
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
"""Edits an Autoscale group."""
# :license: MIT, see LICENSE for more details.
import click
from SoftLayer.CLI import environment
from SoftLayer.managers.autoscale import AutoScaleManager
@click.command()
@click.argument('identifier')
@click.option('--name', help="Scale group's name.")
@click.option('--min', 'minimum', type=click.INT, help="Set the minimum number of guests")
@click.option('--max', 'maximum', type=click.INT, help="Set the maximum number of guests")
@click.option('--userdata', help="User defined metadata string")
@click.option('--userfile', '-F', help="Read userdata from a file",
type=click.Path(exists=True, readable=True, resolve_path=True))
@click.option('--cpu', type=click.INT, help="Number of CPUs for new guests (existing not effected")
@click.option('--memory', type=click.INT, help="RAM in MB or GB for new guests (existing not effected")
@environment.pass_env
def cli(env, identifier, name, minimum, maximum, userdata, userfile, cpu, memory):
"""Edits an Autoscale group."""
template = {}
autoscale = AutoScaleManager(env.client)
group = autoscale.details(identifier)
template['name'] = name
template['minimumMemberCount'] = minimum
template['maximumMemberCount'] = maximum
virt_template = {}
if userdata:
virt_template['userData'] = [{"value": userdata}]
elif userfile:
with open(userfile, 'r', encoding="utf-8") as userfile_obj:
virt_template['userData'] = [{"value": userfile_obj.read()}]
virt_template['startCpus'] = cpu
virt_template['maxMemory'] = memory
# Remove any entries that are `None` as the API will complain about them.
template['virtualGuestMemberTemplate'] = clean_dict(virt_template)
clean_template = clean_dict(template)
# If there are any values edited in the template, we need to get the OLD template values and replace them.
if template['virtualGuestMemberTemplate']:
# Update old template with new values
for key, value in clean_template['virtualGuestMemberTemplate'].items():
group['virtualGuestMemberTemplate'][key] = value
clean_template['virtualGuestMemberTemplate'] = group['virtualGuestMemberTemplate']
autoscale.edit(identifier, clean_template)
click.echo("Done")
def clean_dict(dictionary):
"""Removes any `None` entires from the dictionary"""
return {k: v for k, v in dictionary.items() if v}