Adds support for creating GCE persistent disks from snapshots

reviewable/pr18780/r1
Chris Conway 11 years ago
parent c631178db0
commit 03250b0ace

@ -24,9 +24,7 @@ short_description: utilize GCE persistent disk resources
description: description:
- This module can create and destroy unformatted GCE persistent disks - This module can create and destroy unformatted GCE persistent disks
U(https://developers.google.com/compute/docs/disks#persistentdisks). U(https://developers.google.com/compute/docs/disks#persistentdisks).
It also supports attaching and detaching disks from running instances It also supports attaching and detaching disks from running instances.
but does not support creating boot disks from images or snapshots. The
'gce' module supports creating instances with boot disks.
Full install/configuration instructions for the gce* modules can Full install/configuration instructions for the gce* modules can
be found in the comments of ansible/test/gce_tests.py. be found in the comments of ansible/test/gce_tests.py.
options: options:
@ -68,6 +66,12 @@ options:
required: false required: false
default: null default: null
aliases: [] aliases: []
snapshot:
description:
- the source snapshot to use for the disk
required: false
default: null
aliases: []
state: state:
description: description:
- desired state of the persistent disk - desired state of the persistent disk
@ -139,6 +143,7 @@ def main():
name = dict(required=True), name = dict(required=True),
size_gb = dict(default=10), size_gb = dict(default=10),
image = dict(), image = dict(),
snapshot = dict(),
state = dict(default='present'), state = dict(default='present'),
zone = dict(default='us-central1-b'), zone = dict(default='us-central1-b'),
service_account_email = dict(), service_account_email = dict(),
@ -155,6 +160,7 @@ def main():
name = module.params.get('name') name = module.params.get('name')
size_gb = module.params.get('size_gb') size_gb = module.params.get('size_gb')
image = module.params.get('image') image = module.params.get('image')
snapshot = module.params.get('snapshot')
state = module.params.get('state') state = module.params.get('state')
zone = module.params.get('zone') zone = module.params.get('zone')
@ -212,11 +218,20 @@ def main():
instance_name, zone), changed=False) instance_name, zone), changed=False)
if not disk: if not disk:
if image is not None and snapshot is not None:
module.fail_json(
msg='Cannot give both image (%s) and snapshot (%s)' % (
image, snapshot), changed=False)
lc_image = None lc_image = None
lc_snapshot = None
if image is not None: if image is not None:
lc_image = gce.ex_get_image(image) lc_image = gce.ex_get_image(image)
elif snapshot is not None:
lc_snapshot = gce.ex_get_snapshot(snapshot)
try: try:
disk = gce.create_volume(size_gb, name, location=zone, image=lc_image) disk = gce.create_volume(
size_gb, name, location=zone, image=lc_image,
snapshot=lc_snapshot)
except ResourceExistsError: except ResourceExistsError:
pass pass
except QuotaExceededError: except QuotaExceededError:
@ -225,7 +240,10 @@ def main():
except Exception, e: except Exception, e:
module.fail_json(msg=unexpected_error_msg(e), changed=False) module.fail_json(msg=unexpected_error_msg(e), changed=False)
json_output['size_gb'] = size_gb json_output['size_gb'] = size_gb
json_output['image'] = image if image is not None:
json_output['image'] = image
if snapshot is not None:
json_output['snapshot'] = snapshot
changed = True changed = True
if inst and not is_attached: if inst and not is_attached:
try: try:

Loading…
Cancel
Save