Skip to content

Commit

Permalink
azure_managed_disk enhancement (ansible#50317)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwzho authored and kbreit committed Jan 11, 2019
1 parent 8863536 commit 09a93af
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 41 deletions.
47 changes: 23 additions & 24 deletions lib/ansible/modules/cloud/azure/azure_rm_managed_disk.py
Expand Up @@ -51,17 +51,17 @@
- Premium_LRS
create_option:
description:
- "Allowed values: empty, import, copy. C(import) from a VHD file in I(source_uri) and C(copy) from previous managed disk I(source_resource_uri)."
- "Allowed values: empty, import, copy.
- C(import) from a VHD file in I(source_uri) and C(copy) from previous managed disk I(source_uri)."
choices:
- empty
- import
- copy
source_uri:
description:
- URI to a valid VHD file to be used when I(create_option) is C(import).
source_resource_uri:
description:
- The resource ID of the managed disk to copy when I(create_option) is C(copy).
- URI to a valid VHD file to be used or the resource ID of the managed disk to copy.
aliases:
- source_resource_uri
os_type:
description:
- "Type of Operating System: C(linux) or C(windows). Used when I(create_option) is either C(copy) or C(import) and the source is an OS disk."
Expand All @@ -74,7 +74,8 @@
managed_by:
description:
- Name of an existing virtual machine with which the disk is or will be associated, this VM should be in the same resource group.
- To detach a disk from a vm, keep undefined.
- To detach a disk from a vm, explicitly set to ''.
- If this option is unset, the value will not be changed.
version_added: 2.5
tags:
description:
Expand Down Expand Up @@ -154,8 +155,7 @@ def managed_disk_to_dict(managed_disk):
location=managed_disk.location,
tags=managed_disk.tags,
create_option=create_data.create_option.value.lower(),
source_uri=create_data.source_uri,
source_resource_uri=create_data.source_resource_id,
source_uri=create_data.source_uri or create_data.source_resource_id,
disk_size_gb=managed_disk.disk_size_gb,
os_type=managed_disk.os_type.value if managed_disk.os_type else None,
storage_account_type=managed_disk.sku.name.value if managed_disk.sku else None,
Expand Down Expand Up @@ -193,10 +193,8 @@ def __init__(self):
choices=['empty', 'import', 'copy']
),
source_uri=dict(
type='str'
),
source_resource_uri=dict(
type='str'
type='str',
aliases=['source_resource_uri']
),
os_type=dict(
type='str',
Expand All @@ -211,7 +209,7 @@ def __init__(self):
)
required_if = [
('create_option', 'import', ['source_uri']),
('create_option', 'copy', ['source_resource_uri']),
('create_option', 'copy', ['source_uri']),
('create_option', 'empty', ['disk_size_gb'])
]
self.results = dict(
Expand All @@ -224,7 +222,6 @@ def __init__(self):
self.storage_account_type = None
self.create_option = None
self.source_uri = None
self.source_resource_uri = None
self.os_type = None
self.disk_size_gb = None
self.tags = None
Expand Down Expand Up @@ -261,15 +258,17 @@ def exec_module(self, **kwargs):
result = True

# unmount from the old virtual machine and mount to the new virtual machine
vm_name = parse_resource_id(disk_instance.get('managed_by', '')).get('name') if disk_instance else None
if self.managed_by != vm_name:
changed = True
if not self.check_mode:
if vm_name:
self.detach(vm_name, result)
if self.managed_by:
self.attach(self.managed_by, result)
result = self.get_managed_disk()
if self.managed_by or self.managed_by == '':
vm_name = parse_resource_id(disk_instance.get('managed_by', '')).get('name') if disk_instance else None
vm_name = vm_name or ''
if self.managed_by != vm_name:
changed = True
if not self.check_mode:
if vm_name:
self.detach(vm_name, result)
if self.managed_by:
self.attach(self.managed_by, result)
result = self.get_managed_disk()

if self.state == 'absent' and disk_instance:
changed = True
Expand Down Expand Up @@ -331,7 +330,7 @@ def generate_managed_disk_property(self):
creation_data['source_uri'] = self.source_uri
elif self.create_option == 'copy':
creation_data['create_option'] = self.compute_models.DiskCreateOption.copy
creation_data['source_resource_id'] = self.source_resource_uri
creation_data['source_resource_id'] = self.source_uri
disk_params['creation_data'] = creation_data
return disk_params

Expand Down
40 changes: 38 additions & 2 deletions lib/ansible/modules/cloud/azure/azure_rm_managed_disk_facts.py
Expand Up @@ -73,6 +73,43 @@
description: List of managed disk dicts.
returned: always
type: list
contains:
id:
description:
- Resource id.
name:
description:
- Name of the managed disk.
location:
description:
- Valid Azure location.
storage_account_type:
description:
- Type of storage for the managed disk
sample: Standard_LRS
type: str
create_option:
description:
- Create option of the disk
sample: copy
type: str
source_uri:
description:
- URI to a valid VHD file to be used or the resource ID of the managed disk to copy.
os_type:
description:
- "Type of Operating System: C(linux) or C(windows)."
disk_size_gb:
description:
- Size in GB of the managed disk to be created.
managed_by:
description:
- Name of an existing virtual machine with which the disk is or will be associated, this VM should be in the same resource group.
tags:
description:
- Tags to assign to the managed disk.
sample: { "tag": "value" }
type: dict
'''

from ansible.module_utils.azure_rm_common import AzureRMModuleBase
Expand All @@ -93,8 +130,7 @@ def managed_disk_to_dict(managed_disk):
location=managed_disk.location,
tags=managed_disk.tags,
create_option=create_data.create_option.value.lower(),
source_uri=create_data.source_uri,
source_resource_uri=create_data.source_resource_id,
source_uri=create_data.source_uri or create_data.source_resource_id,
disk_size_gb=managed_disk.disk_size_gb,
os_type=managed_disk.os_type.value if managed_disk.os_type else None,
storage_account_type=managed_disk.sku.name.value if managed_disk.sku else None,
Expand Down
28 changes: 13 additions & 15 deletions test/integration/targets/azure_rm_managed_disk/tasks/main.yml
Expand Up @@ -65,7 +65,7 @@
resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}2"
create_option: "copy"
source_resource_uri: "{{ output.state.id }}"
source_uri: "{{ output.state.id }}"
disk_size_gb: 1
register: copy

Expand Down Expand Up @@ -175,11 +175,12 @@
azure_rm_managed_disk_facts:
resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}1"
register: fact

- assert:
that:
- "azure_managed_disk | length == 1"
- azure_managed_disk[0].storage_account_type == "Premium_LRS"
- azure_managed_disk[0].disk_size_gb == 2

- name: Gather facts
azure_rm_managed_disk_facts:
Expand All @@ -192,7 +193,7 @@
- set_fact:
parameter: "{{parameter |combine({item.key: item.value})}}"
when: "{{item.key not in ['id', 'changed'] and item.value != None}}"
with_dict: "{{ fact.ansible_facts.azure_managed_disk[0] }}"
with_dict: "{{ azure_managed_disk[0] }}"

- name: Create disk with facts return value
azure_rm_managed_disk:
Expand Down Expand Up @@ -294,7 +295,6 @@
resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}1"
disk_size_gb: 2
managed_by: "tr{{ rpfx }}"
tags:
testing: testing
delete: never
Expand All @@ -310,6 +310,7 @@
resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}1"
disk_size_gb: 2
managed_by: ''
tags:
testing: testing
delete: never
Expand All @@ -325,6 +326,7 @@
resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}1"
disk_size_gb: 2
managed_by: ''
tags:
testing: testing
delete: never
Expand All @@ -339,6 +341,7 @@
azure_rm_managed_disk:
resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}1"
managed_by: ''
disk_size_gb: 2
tags:
testing: testing
Expand Down Expand Up @@ -406,20 +409,15 @@
- output.changed
- output.state

- name: Delete managed disk
- name: Delete all managed disk
azure_rm_managed_disk:
resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}2"
name: "md{{ rpfx }}{{ item }}"
managed_by: ''
state: absent
check_mode: no

- name: Delete copied managed disk
azure_rm_managed_disk:
resource_group: "{{ resource_group }}"
name: "md{{ rpfx }}1"
disk_size_gb: 2
state: absent
check_mode: no
with_items:
- 1
- 2

- name: Delete virtual machine
azure_rm_virtualmachine:
Expand Down

0 comments on commit 09a93af

Please sign in to comment.