From cf61510f48b38f799ffe0f9e82aab2913c0609ee Mon Sep 17 00:00:00 2001 From: Yoan Blanc Date: Tue, 28 Aug 2018 18:19:43 +0200 Subject: [PATCH] cs instance root_disk size update resizes the root volume (#43817) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * cloudstack: resize volume on root_disk_size changes Signed-off-by: Yoan Blanc Reviewed-by: Marc-Aurèle Brothier * fixup! cloudstack: resize volume on root_disk_size changes Signed-off-by: Yoan Blanc * fixup! fixup! cloudstack: resize volume on root_disk_size changes Signed-off-by: Yoan Blanc * cs_instance: rename flag to allow_root_disk_shrink Signed-off-by: Yoan Blanc --- .../modules/cloud/cloudstack/cs_instance.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_instance.py b/lib/ansible/modules/cloud/cloudstack/cs_instance.py index c20f0b14fc6..5289ab8eca9 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_instance.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_instance.py @@ -147,6 +147,12 @@ options: - Force stop/start the instance if required to apply changes, otherwise a running instance will not be changed. type: bool default: no + allow_root_disk_shrink: + description: + - Enables a volume shrinkage when the new size is smaller than the old one. + type: bool + default: no + version_added: '2.7' tags: description: - List of tags. Tags are a list of dictionaries having keys C(key) and C(value). @@ -743,11 +749,33 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): security_groups_changed = self.security_groups_has_changed() + # Volume data + + args_volume_update = {} + root_disk_size = self.module.params.get('root_disk_size') + root_disk_size_changed = False + + if root_disk_size is not None: + res = self.query_api('listVolumes', type='ROOT', virtualmachineid=instance['id']) + [volume] = res['volume'] + + size = volume['size'] >> 30 + + args_volume_update['id'] = volume['id'] + args_volume_update['size'] = root_disk_size + + shrinkok = self.module.params.get('allow_root_disk_shrink') + if shrinkok: + args_volume_update['shrinkok'] = shrinkok + + root_disk_size_changed = root_disk_size != size + changed = [ service_offering_changed, instance_changed, security_groups_changed, ssh_key_changed, + root_disk_size_changed, ] if any(changed): @@ -787,6 +815,11 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): instance = self.poll_job(instance, 'virtualmachine') self.instance = instance + # Root disk size + if root_disk_size_changed: + async_result = self.query_api('resizeVolume', **args_volume_update) + self.poll_job(async_result, 'volume') + # Start VM again if it was running before if instance_state == 'running' and start_vm: instance = self.start_instance() @@ -986,6 +1019,7 @@ def main(): tags=dict(type='list', aliases=['tag']), details=dict(type='dict'), poll_async=dict(type='bool', default=True), + allow_root_disk_shrink=dict(type='bool', default=False), )) required_together = cs_required_together()