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

[Compute] az disk create: --gallery-image-reference adds support for creating disk from shared gallery image version or community gallery image version #22756

Merged
merged 8 commits into from
Jun 22, 2022
10 changes: 8 additions & 2 deletions src/azure-cli/azure/cli/command_modules/vm/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,18 @@
- name: Create a disk from image.
text: >
az disk create -g MyResourceGroup -n MyDisk --image-reference Canonical:UbuntuServer:18.04-LTS:18.04.202002180
- name: Create a disk from the OS Disk of a gallery image version
- name: Create a disk from the OS Disk of a compute gallery image version
text: >
az disk create -g MyResourceGroup -n MyDisk --gallery-image-reference /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myRG/providers/Microsoft.Compute/galleries/myGallery/images/myImage/versions/1.0.0
- name: Create a disk from the OS Disk of the latest version in a gallery image
- name: Create a disk from the OS Disk of the latest version in a compute gallery image
text: >
az disk create -g MyResourceGroup -n MyDisk --gallery-image-reference /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myRG/providers/Microsoft.Compute/galleries/myGallery/images/myImage
- name: Create a disk from the OS Disk of a shared gallery image version
text: >
az disk create -g MyResourceGroup -n MyDisk --gallery-image-reference /SharedGalleries/sharedGalleryUniqueName/Images/imageName/Versions/1.0.0
- name: Create a disk from the OS Disk of a community gallery image version
text: >
az disk create -g MyResourceGroup -n MyDisk --gallery-image-reference /CommunityGalleries/communityGalleryPublicGalleryName/Images/imageName/Versions/1.0.0
- name: Create a disk from the Data Disk of a gallery image
text: >
az disk create -g MyResourceGroup -n MyDisk --gallery-image-reference /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myRG/providers/Microsoft.Compute/galleries/myGallery/images/myImage/versions/1.0.0 --gallery-image-reference-lun 0
Expand Down
10 changes: 10 additions & 0 deletions src/azure-cli/azure/cli/command_modules/vm/_image_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ class ScriptType(Enum):
FILE = "file"


class GalleryImageReferenceType(Enum):
COMPUTE = (0, 'id')
COMMUNITY = (1, 'communityGalleryImageId')
SHARED = (2, 'sharedGalleryImageId')

def __init__(self, index, backend_key):
self.index = index
self.backend_key = backend_key


# region Client Factories

def image_builder_client_factory(cli_ctx, _):
Expand Down
3 changes: 2 additions & 1 deletion src/azure-cli/azure/cli/command_modules/vm/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ def load_arguments(self, _):
c.argument('disk_mbps_read_only', type=int, help='The total throughput (MBps) that will be allowed across all VMs mounting the shared disk as ReadOnly. MBps means millions of bytes per second - MB here uses the ISO notation, of powers of 10')
c.argument('image_reference', help='ID or URN (publisher:offer:sku:version) of the image from which to create a disk')
c.argument('image_reference_lun', type=int, help='If the disk is created from an image\'s data disk, this is an index that indicates which of the data disks in the image to use. For OS disks, this field is null')
c.argument('gallery_image_reference', help='ID of the Compute Gallery image version from which to create a disk')
c.argument('gallery_image_reference', help='ID of the Compute, Shared or Community Gallery image version from which to create a disk. For details about valid format, please refer to the help sample')
c.ignore('gallery_image_reference_type')
c.argument('gallery_image_reference_lun', type=int, help='If the disk is created from an image\'s data disk, this is an index that indicates which of the data disks in the image to use. For OS disks, this field is null')
c.argument('logical_sector_size', type=int, help='Logical sector size in bytes for Ultra disks. Supported values are 512 ad 4096. 4096 is the default.')
c.argument('tier', help='Performance tier of the disk (e.g, P4, S10) as described here: https://azure.microsoft.com/pricing/details/managed-disks/. Does not apply to Ultra disks.')
Expand Down
30 changes: 30 additions & 0 deletions src/azure-cli/azure/cli/command_modules/vm/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1713,10 +1713,40 @@ def validate_vmss_disk(cmd, namespace):
raise CLIError('usage error: --disk EXIST_DISK --instance-id ID')


def _validate_gallery_image_reference(cmd, namespace):
from azure.cli.core.profiles import ResourceType
is_validate = 'gallery_image_reference' in namespace and namespace.gallery_image_reference is not None \
and cmd.supported_api_version(resource_type=ResourceType.MGMT_COMPUTE,
operation_group='disks', min_api='2022-03-02')
if not is_validate:
return

from azure.cli.command_modules.vm._image_builder import GalleryImageReferenceType
from ._vm_utils import is_compute_gallery_image_id, is_community_gallery_image_id, \
is_shared_gallery_image_id

gallery_image_reference = namespace.gallery_image_reference
if is_compute_gallery_image_id(gallery_image_reference):
namespace.gallery_image_reference_type = GalleryImageReferenceType.COMPUTE.backend_key
return
if is_community_gallery_image_id(gallery_image_reference):
namespace.gallery_image_reference_type = GalleryImageReferenceType.COMMUNITY.backend_key
return
if is_shared_gallery_image_id(gallery_image_reference):
namespace.gallery_image_reference_type = GalleryImageReferenceType.SHARED.backend_key
return

from azure.cli.core.parser import InvalidArgumentValueError
raise InvalidArgumentValueError('usage error: {} is an invalid gallery image reference, please provide valid '
'compute, shared or community gallery image version. For details about valid '
'format, please refer to the help sample'.format(gallery_image_reference))


def process_disk_or_snapshot_create_namespace(cmd, namespace):
from azure.core.exceptions import HttpResponseError
validate_tags(namespace)
validate_edge_zone(cmd, namespace)
_validate_gallery_image_reference(cmd, namespace)
if namespace.source:
usage_error = 'usage error: --source {SNAPSHOT | DISK} | --source VHD_BLOB_URI [--source-storage-account-id ID]'
try:
Expand Down
12 changes: 12 additions & 0 deletions src/azure-cli/azure/cli/command_modules/vm/_vm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,18 @@ def parse_shared_gallery_image_id(image_reference):
return image_info.group(1), image_info.group(2)


def is_compute_gallery_image_id(image_reference):
if not image_reference:
return False

compute_gallery_id_pattern = re.compile(r'^/subscriptions/[^/]*/resourceGroups/[^/]*/providers/Microsoft.Compute/'
r'galleries/[^/]*/images/.*$', re.IGNORECASE)
if compute_gallery_id_pattern.match(image_reference):
return True

return False


def is_community_gallery_image_id(image_reference):
if not image_reference:
return False
Expand Down
5 changes: 3 additions & 2 deletions src/azure-cli/azure/cli/command_modules/vm/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def create_managed_disk(cmd, resource_group_name, disk_name, location=None, # p
network_access_policy=None, disk_access=None, logical_sector_size=None,
tier=None, enable_bursting=None, edge_zone=None, security_type=None, support_hibernation=None,
public_network_access=None, accelerated_network=None, architecture=None,
data_access_auth_mode=None):
data_access_auth_mode=None, gallery_image_reference_type=None):
from msrestazure.tools import resource_id, is_valid_resource_id
from azure.cli.core.commands.client_factory import get_subscription_id

Expand Down Expand Up @@ -347,7 +347,8 @@ def create_managed_disk(cmd, resource_group_name, disk_name, location=None, # p
image_reference['lun'] = image_reference_lun

if gallery_image_reference is not None:
gallery_image_reference = {'id': gallery_image_reference}
key = gallery_image_reference_type if gallery_image_reference_type else 'id'
gallery_image_reference = {key: gallery_image_reference}
if gallery_image_reference_lun is not None:
gallery_image_reference['lun'] = gallery_image_reference_lun

Expand Down
Loading