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

Loading…
Cancel
Save