Merge pull request #4190 from jbaublitz/gce_suspend_node

Add states for stopping and starting GCE ☁️ instances
reviewable/pr18780/r1
Ryan Brown 8 years ago committed by GitHub
commit 92a17c61f4

@ -121,7 +121,7 @@ options:
- desired state of the resource
required: false
default: "present"
choices: ["active", "present", "absent", "deleted"]
choices: ["active", "present", "absent", "deleted", "started", "stopped", "terminated"]
tags:
description:
- a comma-separated list of tags to associate with the instance
@ -502,20 +502,21 @@ def create_instances(module, gce, instance_names):
return (changed, instance_json_data, instance_names)
def terminate_instances(module, gce, instance_names, zone_name):
"""Terminates a list of instances.
def change_instance_state(module, gce, instance_names, zone_name, state):
"""Changes the state of a list of instances. For example,
change from started to stopped, or started to absent.
module: Ansible module object
gce: authenticated GCE connection object
instance_names: a list of instance names to terminate
zone_name: the zone where the instances reside prior to termination
state: 'state' parameter passed into module as argument
Returns a dictionary of instance names that were terminated.
Returns a dictionary of instance names that were changed.
"""
changed = False
terminated_instance_names = []
changed_instance_names = []
for name in instance_names:
inst = None
try:
@ -524,13 +525,22 @@ def terminate_instances(module, gce, instance_names, zone_name):
pass
except Exception as e:
module.fail_json(msg=unexpected_error_msg(e), changed=False)
if inst:
if inst and state in ['absent', 'deleted']:
gce.destroy_node(inst)
terminated_instance_names.append(inst.name)
changed_instance_names.append(inst.name)
changed = True
elif inst and state == 'started' and \
inst.state == libcloud.compute.types.NodeState.STOPPED:
gce.ex_start_node(inst)
changed_instance_names.append(inst.name)
changed = True
elif inst and state in ['stopped', 'terminated'] and \
inst.state == libcloud.compute.types.NodeState.RUNNING:
gce.ex_stop_node(inst)
changed_instance_names.append(inst.name)
changed = True
return (changed, terminated_instance_names)
return (changed, changed_instance_names)
def main():
module = AnsibleModule(
@ -544,7 +554,8 @@ def main():
subnetwork = dict(),
persistent_boot_disk = dict(type='bool', default=False),
disks = dict(type='list'),
state = dict(choices=['active', 'present', 'absent', 'deleted'],
state = dict(choices=['active', 'present', 'absent', 'deleted',
'started', 'stopped', 'terminated'],
default='present'),
tags = dict(type='list'),
zone = dict(default='us-central1-a'),
@ -604,15 +615,15 @@ def main():
changed=False)
json_output = {'zone': zone}
if state in ['absent', 'deleted']:
json_output['state'] = 'absent'
(changed, terminated_instance_names) = terminate_instances(
module, gce, inames, zone)
if state in ['absent', 'deleted', 'started', 'stopped', 'terminated']:
json_output['state'] = state
(changed, changed_instance_names) = change_instance_state(
module, gce, inames, zone, state)
# based on what user specified, return the same variable, although
# value could be different if an instance could not be destroyed
if instance_names:
json_output['instance_names'] = terminated_instance_names
json_output['instance_names'] = changed_instance_names
elif name:
json_output['name'] = name

Loading…
Cancel
Save