From 03250b0ace90e04b554c7b971ba3bb427cae6c19 Mon Sep 17 00:00:00 2001 From: Chris Conway Date: Fri, 11 Apr 2014 15:45:56 -0700 Subject: [PATCH] Adds support for creating GCE persistent disks from snapshots --- cloud/gce_pd | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/cloud/gce_pd b/cloud/gce_pd index 98e8e8d15e8..0032acd57e4 100644 --- a/cloud/gce_pd +++ b/cloud/gce_pd @@ -24,9 +24,7 @@ short_description: utilize GCE persistent disk resources description: - This module can create and destroy unformatted GCE persistent disks U(https://developers.google.com/compute/docs/disks#persistentdisks). - 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. + It also supports attaching and detaching disks from running instances. Full install/configuration instructions for the gce* modules can be found in the comments of ansible/test/gce_tests.py. options: @@ -68,6 +66,12 @@ options: required: false default: null aliases: [] + snapshot: + description: + - the source snapshot to use for the disk + required: false + default: null + aliases: [] state: description: - desired state of the persistent disk @@ -139,6 +143,7 @@ def main(): name = dict(required=True), size_gb = dict(default=10), image = dict(), + snapshot = dict(), state = dict(default='present'), zone = dict(default='us-central1-b'), service_account_email = dict(), @@ -155,6 +160,7 @@ def main(): name = module.params.get('name') size_gb = module.params.get('size_gb') image = module.params.get('image') + snapshot = module.params.get('snapshot') state = module.params.get('state') zone = module.params.get('zone') @@ -212,11 +218,20 @@ def main(): instance_name, zone), changed=False) 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_snapshot = 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: - 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: pass except QuotaExceededError: @@ -225,7 +240,10 @@ def main(): except Exception, e: module.fail_json(msg=unexpected_error_msg(e), changed=False) 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 if inst and not is_attached: try: