1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

gce_pd: add delete_on_termination option (#20201)

Add delete_on_termination option to gce_pd, which will delete the disk
when the attached instance is terminated.
This commit is contained in:
Laurent Goujon 2017-01-18 10:52:23 -08:00 committed by Ryan Brown
parent aea5bcd52f
commit d3c17d632b

View file

@ -128,6 +128,12 @@ options:
default: "pd-standard"
choices: ["pd-standard", "pd-ssd"]
aliases: []
delete_on_termination:
version_added: "2.3"
description:
- If yes, deletes the volume when instance is terminated
default: no
choices: ["yes", "no"]
requirements:
- "python >= 2.6"
@ -158,6 +164,7 @@ except ImportError:
def main():
module = AnsibleModule(
argument_spec = dict(
delete_on_termination = dict(type='bool'),
detach_only = dict(type='bool'),
instance_name = dict(),
mode = dict(default='READ_ONLY', choices=['READ_WRITE', 'READ_ONLY']),
@ -179,6 +186,7 @@ def main():
gce = gce_connect(module)
delete_on_termination = module.params.get('delete_on_termination')
detach_only = module.params.get('detach_only')
instance_name = module.params.get('instance_name')
mode = module.params.get('mode')
@ -190,6 +198,11 @@ def main():
state = module.params.get('state')
zone = module.params.get('zone')
if delete_on_termination and not instance_name:
module.fail_json(
msg='Must specify an instance name when requesting delete on termination',
changed=False)
if detach_only and not instance_name:
module.fail_json(
msg='Must specify an instance name when detaching a disk',
@ -273,11 +286,14 @@ def main():
changed = True
if inst and not is_attached:
try:
gce.attach_volume(inst, disk, device=name, ex_mode=mode)
gce.attach_volume(inst, disk, device=name, ex_mode=mode,
ex_auto_delete=delete_on_termination)
except Exception as e:
module.fail_json(msg=unexpected_error_msg(e), changed=False)
json_output['attached_to_instance'] = inst.name
json_output['attached_mode'] = mode
if delete_on_termination:
json_output['delete_on_termination'] = True
changed = True
# user wants to delete a disk (or perhaps just detach it).