Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to attach existing managed data disks at VM creation #1430

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 51 additions & 35 deletions plugins/modules/azure_rm_virtualmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
- Type of OS disk caching.
type: str
choices:
- None
- ReadOnly
- ReadWrite
aliases:
Expand Down Expand Up @@ -270,6 +271,11 @@
description:
- ID of disk encryption set for data disk.
type: str
managed_disk_id:
description:
- The ID of the existing data disk.
- If specified, attach mode will be chosen.
type: str
managed_disk_type:
description:
- Managed data disk type.
Expand Down Expand Up @@ -313,6 +319,7 @@
- Type of data disk caching.
type: str
choices:
- None
- ReadOnly
- ReadWrite
public_ip_allocation_method:
Expand Down Expand Up @@ -1108,7 +1115,7 @@ def __init__(self):
storage_account_name=dict(type='str', aliases=['storage_account']),
storage_container_name=dict(type='str', aliases=['storage_container'], default='vhds'),
storage_blob_name=dict(type='str', aliases=['storage_blob']),
os_disk_caching=dict(type='str', aliases=['disk_caching'], choices=['ReadOnly', 'ReadWrite']),
os_disk_caching=dict(type='str', aliases=['disk_caching'], choices=['None', 'ReadOnly', 'ReadWrite']),
os_disk_size_gb=dict(type='int'),
os_disk_encryption_set=dict(type='str'),
managed_disk_type=dict(type='str', choices=['Standard_LRS', 'StandardSSD_LRS', 'StandardSSD_ZRS', 'Premium_LRS', 'Premium_ZRS', 'UltraSSD_LRS']),
Expand Down Expand Up @@ -1136,12 +1143,13 @@ def __init__(self):
lun=dict(type='int', required=True),
disk_size_gb=dict(type='int'),
disk_encryption_set=dict(type='str'),
managed_disk_id=dict(type='str'),
managed_disk_type=dict(type='str', choices=['Standard_LRS', 'StandardSSD_LRS',
'StandardSSD_ZRS', 'Premium_LRS', 'Premium_ZRS', 'UltraSSD_LRS']),
storage_account_name=dict(type='str'),
storage_container_name=dict(type='str', default='vhds'),
storage_blob_name=dict(type='str'),
caching=dict(type='str', choices=['ReadOnly', 'ReadWrite'])
caching=dict(type='str', choices=['None', 'ReadOnly', 'ReadWrite'])
)
),
plan=dict(type='dict'),
Expand Down Expand Up @@ -1909,41 +1917,49 @@ def exec_module(self, **kwargs):
count = 0

for data_disk in self.data_disks:
if not data_disk.get('managed_disk_type'):
if not data_disk.get('storage_blob_name'):
data_disk['storage_blob_name'] = self.name + '-data-' + str(count) + '.vhd'
count += 1

if data_disk.get('storage_account_name'):
data_disk_storage_account = self.get_storage_account(self.resource_group, data_disk['storage_account_name'])
else:
data_disk_storage_account = self.create_default_storage_account()
self.log("data disk storage account:")
self.log(self.serialize_obj(data_disk_storage_account, 'StorageAccount'), pretty_print=True)

if not data_disk.get('storage_container_name'):
data_disk['storage_container_name'] = 'vhds'

data_disk_requested_vhd_uri = 'https://{0}.blob.{1}/{2}/{3}'.format(
data_disk_storage_account.name,
self._cloud_environment.suffixes.storage_endpoint,
data_disk['storage_container_name'],
data_disk['storage_blob_name']
)
data_disk_vhd = None
disk_name = None

if not data_disk.get('managed_disk_type'):
data_disk_managed_disk = None
disk_name = data_disk['storage_blob_name']
data_disk_vhd = self.compute_models.VirtualHardDisk(uri=data_disk_requested_vhd_uri)
if data_disk.get('managed_disk_id'):
create_option = self.compute_models.DiskCreateOptionTypes.attach
data_disk_managed_disk = self.compute_models.ManagedDiskParameters(id=data_disk.get('managed_disk_id'))
else:
data_disk_vhd = None
data_disk_managed_disk = self.compute_models.ManagedDiskParameters(storage_account_type=data_disk['managed_disk_type'])
if data_disk.get('disk_encryption_set'):
data_disk_managed_disk.disk_encryption_set = self.compute_models.DiskEncryptionSetParameters(
id=data_disk['disk_encryption_set']
create_option = self.compute_models.DiskCreateOptionTypes.empty

if not data_disk.get('managed_disk_type'):
if not data_disk.get('storage_blob_name'):
data_disk['storage_blob_name'] = self.name + '-data-' + str(count) + '.vhd'
count += 1

if data_disk.get('storage_account_name'):
data_disk_storage_account = self.get_storage_account(self.resource_group, data_disk['storage_account_name'])
else:
data_disk_storage_account = self.create_default_storage_account()
self.log("data disk storage account:")
self.log(self.serialize_obj(data_disk_storage_account, 'StorageAccount'), pretty_print=True)

if not data_disk.get('storage_container_name'):
data_disk['storage_container_name'] = 'vhds'

data_disk_requested_vhd_uri = 'https://{0}.blob.{1}/{2}/{3}'.format(
data_disk_storage_account.name,
self._cloud_environment.suffixes.storage_endpoint,
data_disk['storage_container_name'],
data_disk['storage_blob_name']
)
disk_name = self.name + "-datadisk-" + str(count)
count += 1

if not data_disk.get('managed_disk_type'):
data_disk_managed_disk = None
disk_name = data_disk['storage_blob_name']
data_disk_vhd = self.compute_models.VirtualHardDisk(uri=data_disk_requested_vhd_uri)
else:
data_disk_managed_disk = self.compute_models.ManagedDiskParameters(storage_account_type=data_disk['managed_disk_type'])
if data_disk.get('disk_encryption_set'):
data_disk_managed_disk.disk_encryption_set = self.compute_models.DiskEncryptionSetParameters(
id=data_disk['disk_encryption_set']
)
disk_name = self.name + "-datadisk-" + str(count)
count += 1

data_disk['caching'] = data_disk.get(
'caching', 'ReadOnly'
Expand All @@ -1954,7 +1970,7 @@ def exec_module(self, **kwargs):
name=disk_name,
vhd=data_disk_vhd,
caching=data_disk['caching'],
create_option=self.compute_models.DiskCreateOptionTypes.empty,
create_option=create_option,
disk_size_gb=data_disk['disk_size_gb'],
managed_disk=data_disk_managed_disk,
))
Expand Down