From c11d73575b9be98caa77eb5cdc9ea4dd2442a6d1 Mon Sep 17 00:00:00 2001 From: haiyuan_zhang Date: Tue, 12 Nov 2019 13:05:15 +0800 Subject: [PATCH] Fixes azure_rm_snapshot (#64542) (#64547) * feature enhancement: make a snapshot from an existing managed disk. * remove default creation option in argu_spec of azure_rm_snapshot * fix pep8 related issue * fixing broken integration test --- .../module_utils/azure_rm_common_ext.py | 2 ++ .../modules/cloud/azure/azure_rm_snapshot.py | 35 ++++++++++++++----- .../targets/azure_rm_gallery/tasks/main.yml | 2 +- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/ansible/module_utils/azure_rm_common_ext.py b/lib/ansible/module_utils/azure_rm_common_ext.py index 4935b2c2c1a..bb7af27f03f 100644 --- a/lib/ansible/module_utils/azure_rm_common_ext.py +++ b/lib/ansible/module_utils/azure_rm_common_ext.py @@ -19,6 +19,8 @@ class AzureRMModuleBaseExt(AzureRMModuleBase): # first check if option was passed param = body.get(name) if not param: + if spec[name].get('purgeIfNone', False): + body.pop(name, None) continue # check if pattern needs to be used pattern = spec[name].get('pattern', None) diff --git a/lib/ansible/modules/cloud/azure/azure_rm_snapshot.py b/lib/ansible/modules/cloud/azure/azure_rm_snapshot.py index f79ea6d5b90..759fd2d69b8 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_snapshot.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_snapshot.py @@ -17,7 +17,7 @@ DOCUMENTATION = ''' --- module: azure_rm_snapshot version_added: '2.9' -short_description: Manage Azure Snapshot instance +short_description: Manage Azure Snapshot instance. description: - Create, update and delete instance of Azure Snapshot. options: @@ -68,13 +68,17 @@ options: description: - This enumerates the possible sources of a disk's creation. type: str - default: Import choices: - Import + - Copy source_uri: description: - If I(createOption=Import), this is the URI of a blob to be imported into a managed disk. type: str + source_id: + description: + - If I(createOption=Copy), this is the resource ID of a managed disk to be copied from. + type: str state: description: - Assert the state of the Snapshot. @@ -82,8 +86,8 @@ options: default: present type: str choices: - - absent - - present + - absent + - present extends_documentation_fragment: - azure - azure_tags @@ -101,6 +105,15 @@ EXAMPLES = ''' creation_data: create_option: Import source_uri: 'https://mystorageaccount.blob.core.windows.net/osimages/osimage.vhd' + +- name: Create a snapshot by copying an existing managed disk. + azure_rm_snapshot: + resource_group: myResourceGroup + name: mySnapshot + location: eastus + creation_data: + create_option: Copy + source_id: '/subscriptions/sub123/resourceGroups/group123/providers/Microsoft.Compute/disks/disk123' ''' RETURN = ''' @@ -114,10 +127,8 @@ id: import time import json -import re from ansible.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt from ansible.module_utils.azure_rm_common_rest import GenericRestClient -from copy import deepcopy try: from msrestazure.azure_exceptions import CloudError except ImportError: @@ -177,12 +188,17 @@ class AzureRMSnapshots(AzureRMModuleBaseExt): create_option=dict( type='str', disposition='createOption', - choices=['Import'], - default='Import' + choices=['Import', 'Copy'], ), source_uri=dict( type='str', - disposition='sourceUri' + disposition='sourceUri', + purgeIfNone=True + ), + source_id=dict( + type='str', + disposition='sourceResourceId', + purgeIfNone=True ) ) ), @@ -355,6 +371,7 @@ class AzureRMSnapshots(AzureRMModuleBaseExt): expected_status_codes=self.status_code, polling_timeout=600, polling_interval=30) + response = json.loads(response.text) found = True self.log("Response : {0}".format(response)) # self.log("Snapshot instance : {0} found".format(response.name)) diff --git a/test/integration/targets/azure_rm_gallery/tasks/main.yml b/test/integration/targets/azure_rm_gallery/tasks/main.yml index 85f1695a682..4f18cb88cbe 100644 --- a/test/integration/targets/azure_rm_gallery/tasks/main.yml +++ b/test/integration/targets/azure_rm_gallery/tasks/main.yml @@ -53,7 +53,7 @@ - name: Create a snapshot by importing an unmanaged blob from the same subscription. azure_rm_snapshot: resource_group: "{{ resource_group }}" - name: mySnapshot + name: "mySnapshot-{{ rpfx }}" location: eastus creation_data: create_option: Import