From e861195773cae4214b49176549f059efbf92468e Mon Sep 17 00:00:00 2001 From: Simon Li Date: Thu, 6 Oct 2016 13:43:03 +0100 Subject: [PATCH 1/4] Add volume_src to os_volume Depending on the OpenStack installation it may be quicker to create a volume from an existing volume (copy-on-write) compared to from a snapshot (allocating a completely new volume). This adds a new `volume_src` parameter to the `os_module` which accepts a volume id or name. --- lib/ansible/modules/cloud/openstack/os_volume.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/cloud/openstack/os_volume.py b/lib/ansible/modules/cloud/openstack/os_volume.py index 6d6cc08d749..2f96fab129a 100644 --- a/lib/ansible/modules/cloud/openstack/os_volume.py +++ b/lib/ansible/modules/cloud/openstack/os_volume.py @@ -67,6 +67,11 @@ options: - Volume snapshot id to create from required: false default: None + volume_src: + description: + - Volume source to create from + required: false + default: None state: description: - Should the resource be present or absent. @@ -109,6 +114,10 @@ def _present_volume(module, cloud): image_id = cloud.get_image_id(module.params['image']) volume_args['imageRef'] = image_id + if module.params['volume_src']: + volume_id = cloud.get_volume_id(module.params['volume_src']) + volume_args['source_volid'] = volume_id + volume = cloud.create_volume( wait=module.params['wait'], timeout=module.params['timeout'], **volume_args) @@ -134,11 +143,12 @@ def main(): display_description=dict(default=None, aliases=['description']), image=dict(default=None), snapshot_id=dict(default=None), + volume_src=dict(default=None), state=dict(default='present', choices=['absent', 'present']), ) module_kwargs = openstack_module_kwargs( mutually_exclusive=[ - ['image', 'snapshot_id'], + ['image', 'snapshot_id', 'volume_src'], ], ) module = AnsibleModule(argument_spec=argument_spec, **module_kwargs) From 178811d1749c27523334843d55c603e33f2a28a5 Mon Sep 17 00:00:00 2001 From: Simon Li Date: Thu, 6 Oct 2016 14:00:38 +0100 Subject: [PATCH 2/4] Return an error if volume_src wasn't found --- lib/ansible/modules/cloud/openstack/os_volume.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/ansible/modules/cloud/openstack/os_volume.py b/lib/ansible/modules/cloud/openstack/os_volume.py index 2f96fab129a..3c1b72e0f23 100644 --- a/lib/ansible/modules/cloud/openstack/os_volume.py +++ b/lib/ansible/modules/cloud/openstack/os_volume.py @@ -116,6 +116,8 @@ def _present_volume(module, cloud): if module.params['volume_src']: volume_id = cloud.get_volume_id(module.params['volume_src']) + if not volume_id: + module.fail_json(msg="Failed to find volume source '%s'" % module.params['volume_src']) volume_args['source_volid'] = volume_id volume = cloud.create_volume( From 9bfa2963b68a36e8e1033de9175e461127442634 Mon Sep 17 00:00:00 2001 From: Simon Li Date: Thu, 6 Oct 2016 14:52:45 +0100 Subject: [PATCH 3/4] Add version_added 2.3 as suggested by shippable --- lib/ansible/modules/cloud/openstack/os_volume.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ansible/modules/cloud/openstack/os_volume.py b/lib/ansible/modules/cloud/openstack/os_volume.py index 3c1b72e0f23..366cfae53aa 100644 --- a/lib/ansible/modules/cloud/openstack/os_volume.py +++ b/lib/ansible/modules/cloud/openstack/os_volume.py @@ -72,6 +72,7 @@ options: - Volume source to create from required: false default: None + version_added: "2.3" state: description: - Should the resource be present or absent. From de714e5af86eba22222b4db4a4e5e21300c93485 Mon Sep 17 00:00:00 2001 From: Simon Li Date: Fri, 7 Oct 2016 15:11:08 +0100 Subject: [PATCH 4/4] Use `volume` as input parameter name to match `image`. --- lib/ansible/modules/cloud/openstack/os_volume.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/ansible/modules/cloud/openstack/os_volume.py b/lib/ansible/modules/cloud/openstack/os_volume.py index 366cfae53aa..e523e3bebe2 100644 --- a/lib/ansible/modules/cloud/openstack/os_volume.py +++ b/lib/ansible/modules/cloud/openstack/os_volume.py @@ -67,9 +67,9 @@ options: - Volume snapshot id to create from required: false default: None - volume_src: + volume: description: - - Volume source to create from + - Volume name or id to create from required: false default: None version_added: "2.3" @@ -115,10 +115,10 @@ def _present_volume(module, cloud): image_id = cloud.get_image_id(module.params['image']) volume_args['imageRef'] = image_id - if module.params['volume_src']: - volume_id = cloud.get_volume_id(module.params['volume_src']) + if module.params['volume']: + volume_id = cloud.get_volume_id(module.params['volume']) if not volume_id: - module.fail_json(msg="Failed to find volume source '%s'" % module.params['volume_src']) + module.fail_json(msg="Failed to find volume '%s'" % module.params['volume']) volume_args['source_volid'] = volume_id volume = cloud.create_volume( @@ -146,12 +146,12 @@ def main(): display_description=dict(default=None, aliases=['description']), image=dict(default=None), snapshot_id=dict(default=None), - volume_src=dict(default=None), + volume=dict(default=None), state=dict(default='present', choices=['absent', 'present']), ) module_kwargs = openstack_module_kwargs( mutually_exclusive=[ - ['image', 'snapshot_id', 'volume_src'], + ['image', 'snapshot_id', 'volume'], ], ) module = AnsibleModule(argument_spec=argument_spec, **module_kwargs)