Skip to content

Commit d897325

Browse files
authored
[Compute] Support associating disk and snapshot with a disk-access resource (Azure#14624)
* Add support for associating disk-access with disk and snapshot * add scenario test * src/azure-cli/azure/cli/command_modules/vm/tests/latest/test_vm_commands.py * fix arg_type for network_access_policy
1 parent 20dbdc3 commit d897325

15 files changed

+4147
-3248
lines changed

Diff for: src/azure-cli-core/azure/cli/core/profiles/_shared.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def default_api_version(self):
137137
'disks': '2020-05-01',
138138
'disk_encryption_sets': '2020-05-01',
139139
'disk_accesses': '2020-05-01',
140-
'snapshots': '2019-07-01',
140+
'snapshots': '2020-05-01',
141141
'galleries': '2019-12-01',
142142
'gallery_images': '2019-12-01',
143143
'gallery_image_versions': '2019-12-01',

Diff for: src/azure-cli/azure/cli/command_modules/vm/_help.py

+11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
- name: Create a disk and specify maximum number of VMs that can attach to the disk at the same time.
5858
text: >
5959
az disk create -g MyResourceGroup -n MyDisk --size-gb 256 --max-shares 2 -l centraluseuap
60+
- name: Create a disk and associate it with a disk access resource.
61+
text: >
62+
az disk create -g MyResourceGroup -n MyDisk --size-gb 10 --network-access-policy AllowPrivate --disk-access MyDiskAccessID
6063
"""
6164

6265
helps['disk delete'] = """
@@ -106,6 +109,9 @@
106109
text: |
107110
az disk update --name MyManagedDisk --resource-group MyResourceGroup --size-gb 20
108111
crafted: true
112+
- name: Update a managed disk and associate it with a disk access resource.
113+
text: |
114+
az disk update --name MyManagedDisk --resource-group MyResourceGroup --network-access-policy AllowPrivate --disk-access MyDiskAccessID
109115
"""
110116

111117
helps['disk wait'] = """
@@ -681,6 +687,8 @@
681687
text: az snapshot create -g MyResourceGroup -n MySnapshot2 --source MyDisk
682688
- name: Create a snapshot from an existing disk in another resource group.
683689
text: az snapshot create -g MyResourceGroup -n MySnapshot2 --source "/subscriptions/00000/resourceGroups/AnotherResourceGroup/providers/Microsoft.Compute/disks/MyDisk"
690+
- name: Create a snapshot and associate it with a disk access resource.
691+
text: az snapshot create -g MyResourceGroup -n MySnapshot --size-gb 10 --network-access-policy AllowPrivate --disk-access MyDiskAccessID
684692
"""
685693

686694
helps['snapshot grant-access'] = """
@@ -716,6 +724,9 @@
716724
text: |
717725
az snapshot update --name MySnapshot --resource-group MyResourceGroup --subscription MySubscription
718726
crafted: true
727+
- name: Update a snapshot and associate it with a disk access resource.
728+
text: |
729+
az snapshot update --name MySnapshot --resource-group MyResourceGroup --network-access-policy AllowPrivate --disk-access MyDiskAccessID
719730
"""
720731

721732
helps['snapshot wait'] = """

Diff for: src/azure-cli/azure/cli/command_modules/vm/_params.py

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ def load_arguments(self, _):
118118
help='Encryption type. EncryptionAtRestWithPlatformKey: Disk is encrypted with XStore managed key at rest. It is the default encryption type. EncryptionAtRestWithCustomerKey: Disk is encrypted with Customer managed key at rest.')
119119
c.argument('disk_encryption_set', min_api='2019-07-01', help='Name or ID of disk encryption set that is used to encrypt the disk.')
120120
c.argument('location', help='Location. Values from: `az account list-locations`. You can configure the default location using `az configure --defaults location=<location>`. If location is not specified and no default location specified, location will be automatically set as same as the resource group.')
121+
operation_group = 'disks' if scope == 'disk' else 'snapshots'
122+
c.argument('network_access_policy', min_api='2020-05-01', help='Policy for accessing the disk via network.', arg_type=get_enum_type(self.get_models('NetworkAccessPolicy', operation_group=operation_group)))
123+
c.argument('disk_access', min_api='2020-05-01', help='Name or ID of the disk access resource for using private endpoints on disks.')
121124

122125
for scope in ['disk create', 'snapshot create']:
123126
with self.argument_context(scope) as c:

Diff for: src/azure-cli/azure/cli/command_modules/vm/custom.py

+39-4
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ def create_managed_disk(cmd, resource_group_name, disk_name, location=None, # p
272272
encryption_type=None, disk_encryption_set=None, max_shares=None,
273273
disk_iops_read_only=None, disk_mbps_read_only=None,
274274
image_reference=None, image_reference_lun=None,
275-
gallery_image_reference=None, gallery_image_reference_lun=None):
275+
gallery_image_reference=None, gallery_image_reference_lun=None,
276+
network_access_policy=None, disk_access=None):
276277
from msrestazure.tools import resource_id, is_valid_resource_id
277278
from azure.cli.core.commands.client_factory import get_subscription_id
278279

@@ -340,6 +341,11 @@ def create_managed_disk(cmd, resource_group_name, disk_name, location=None, # p
340341
subscription=get_subscription_id(cmd.cli_ctx), resource_group=resource_group_name,
341342
namespace='Microsoft.Compute', type='diskEncryptionSets', name=disk_encryption_set)
342343

344+
if disk_access is not None and not is_valid_resource_id(disk_access):
345+
disk_access = resource_id(
346+
subscription=get_subscription_id(cmd.cli_ctx), resource_group=resource_group_name,
347+
namespace='Microsoft.Compute', type='diskAccesses', name=disk_access)
348+
343349
encryption = None
344350
if disk_encryption_set:
345351
encryption = Encryption(type=encryption_type, disk_encryption_set_id=disk_encryption_set)
@@ -362,6 +368,10 @@ def create_managed_disk(cmd, resource_group_name, disk_name, location=None, # p
362368
disk.disk_iops_read_only = disk_iops_read_only
363369
if disk_mbps_read_only is not None:
364370
disk.disk_mbps_read_only = disk_mbps_read_only
371+
if network_access_policy is not None:
372+
disk.network_access_policy = network_access_policy
373+
if disk_access is not None:
374+
disk.disk_access_id = disk_access
365375

366376
client = _compute_client_factory(cmd.cli_ctx)
367377
return sdk_no_wait(no_wait, client.disks.create_or_update, resource_group_name, disk_name, disk)
@@ -380,7 +390,8 @@ def list_managed_disks(cmd, resource_group_name=None):
380390

381391

382392
def update_managed_disk(cmd, resource_group_name, instance, size_gb=None, sku=None, disk_iops_read_write=None,
383-
disk_mbps_read_write=None, encryption_type=None, disk_encryption_set=None):
393+
disk_mbps_read_write=None, encryption_type=None, disk_encryption_set=None,
394+
network_access_policy=None, disk_access=None):
384395
from msrestazure.tools import resource_id, is_valid_resource_id
385396
from azure.cli.core.commands.client_factory import get_subscription_id
386397

@@ -403,6 +414,13 @@ def update_managed_disk(cmd, resource_group_name, instance, size_gb=None, sku=No
403414
instance.encryption.disk_encryption_set_id = disk_encryption_set
404415
if encryption_type is not None:
405416
instance.encryption.type = encryption_type
417+
if network_access_policy is not None:
418+
instance.network_access_policy = network_access_policy
419+
if disk_access is not None and not is_valid_resource_id(disk_access):
420+
disk_access = resource_id(
421+
subscription=get_subscription_id(cmd.cli_ctx), resource_group=resource_group_name,
422+
namespace='Microsoft.Compute', type='diskAccesses', name=disk_access)
423+
instance.disk_access_id = disk_access
406424
return instance
407425
# endregion
408426

@@ -481,7 +499,7 @@ def create_snapshot(cmd, resource_group_name, snapshot_name, location=None, size
481499
# below are generated internally from 'source'
482500
source_blob_uri=None, source_disk=None, source_snapshot=None, source_storage_account_id=None,
483501
hyper_v_generation=None, tags=None, no_wait=False, disk_encryption_set=None,
484-
encryption_type=None):
502+
encryption_type=None, network_access_policy=None, disk_access=None):
485503
from msrestazure.tools import resource_id, is_valid_resource_id
486504
from azure.cli.core.commands.client_factory import get_subscription_id
487505

@@ -511,6 +529,11 @@ def create_snapshot(cmd, resource_group_name, snapshot_name, location=None, size
511529
subscription=get_subscription_id(cmd.cli_ctx), resource_group=resource_group_name,
512530
namespace='Microsoft.Compute', type='diskEncryptionSets', name=disk_encryption_set)
513531

532+
if disk_access is not None and not is_valid_resource_id(disk_access):
533+
disk_access = resource_id(
534+
subscription=get_subscription_id(cmd.cli_ctx), resource_group=resource_group_name,
535+
namespace='Microsoft.Compute', type='diskAccesses', name=disk_access)
536+
514537
if disk_encryption_set is not None and encryption_type is None:
515538
raise CLIError('usage error: Please specify --encryption-type.')
516539
if encryption_type is not None:
@@ -523,6 +546,10 @@ def create_snapshot(cmd, resource_group_name, snapshot_name, location=None, size
523546
encryption=encryption)
524547
if hyper_v_generation:
525548
snapshot.hyper_vgeneration = hyper_v_generation
549+
if network_access_policy is not None:
550+
snapshot.network_access_policy = network_access_policy
551+
if disk_access is not None:
552+
snapshot.disk_access_id = disk_access
526553

527554
client = _compute_client_factory(cmd.cli_ctx)
528555
return sdk_no_wait(no_wait, client.snapshots.create_or_update, resource_group_name, snapshot_name, snapshot)
@@ -540,7 +567,8 @@ def list_snapshots(cmd, resource_group_name=None):
540567
return client.snapshots.list()
541568

542569

543-
def update_snapshot(cmd, resource_group_name, instance, sku=None, disk_encryption_set=None, encryption_type=None):
570+
def update_snapshot(cmd, resource_group_name, instance, sku=None, disk_encryption_set=None,
571+
encryption_type=None, network_access_policy=None, disk_access=None):
544572
from msrestazure.tools import resource_id, is_valid_resource_id
545573
from azure.cli.core.commands.client_factory import get_subscription_id
546574

@@ -557,6 +585,13 @@ def update_snapshot(cmd, resource_group_name, instance, sku=None, disk_encryptio
557585
instance.encryption.disk_encryption_set_id = disk_encryption_set
558586
if encryption_type is not None:
559587
instance.encryption.type = encryption_type
588+
if network_access_policy is not None:
589+
instance.network_access_policy = network_access_policy
590+
if disk_access is not None and not is_valid_resource_id(disk_access):
591+
disk_access = resource_id(
592+
subscription=get_subscription_id(cmd.cli_ctx), resource_group=resource_group_name,
593+
namespace='Microsoft.Compute', type='diskAccesses', name=disk_access)
594+
instance.disk_access_id = disk_access
560595
return instance
561596
# endregion
562597

0 commit comments

Comments
 (0)