Skip to content

Commit 1a21c11

Browse files
{Compute} az image create: Migrate command to aaz-based implementation (#32884)
1 parent b0ae902 commit 1a21c11

22 files changed

Lines changed: 96 additions & 47 deletions

src/azure-cli/azure/cli/command_modules/vm/_client_factory.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ def cf_disk_accesses(cli_ctx, _):
6363
return _compute_client_factory(cli_ctx).disk_accesses
6464

6565

66-
def cf_images(cli_ctx, _):
67-
return _compute_client_factory(cli_ctx).images
68-
69-
7066
def cf_rolling_upgrade_commands(cli_ctx, _):
7167
return _compute_client_factory(cli_ctx).virtual_machine_scale_set_rolling_upgrades
7268

src/azure-cli/azure/cli/command_modules/vm/_params.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
validate_asg_names_or_ids, validate_keyvault, _validate_proximity_placement_group,
2525
validate_vm_name_for_monitor_metrics)
2626

27-
from azure.cli.command_modules.vm._vm_utils import MSI_LOCAL_ID, UpgradeMode
27+
from azure.cli.command_modules.vm._vm_utils import MSI_LOCAL_ID, CachingTypes, UpgradeMode
2828
from azure.cli.command_modules.vm._image_builder import ScriptType
2929

3030
from azure.cli.command_modules.monitor.validators import validate_metric_dimension
@@ -38,7 +38,7 @@ def load_arguments(self, _):
3838
# Model imports
3939
DiskStorageAccountTypes = self.get_models('DiskStorageAccountTypes', operation_group='disks')
4040
SnapshotStorageAccountTypes = self.get_models('SnapshotStorageAccountTypes', operation_group='snapshots')
41-
CachingTypes, OperatingSystemTypes = self.get_models('CachingTypes', 'OperatingSystemTypes')
41+
OperatingSystemTypes = self.get_models('OperatingSystemTypes')
4242
HyperVGenerationTypes = self.get_models('HyperVGenerationTypes')
4343
DedicatedHostLicenseTypes = self.get_models('DedicatedHostLicenseTypes')
4444
ReplicationMode = self.get_models('ReplicationMode', operation_group='gallery_image_versions')

src/azure-cli/azure/cli/command_modules/vm/_validators.py

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,27 +2169,33 @@ def process_image_create_namespace(cmd, namespace):
21692169
'virtualMachines', 'Microsoft.Compute')
21702170
res = parse_resource_id(res_id)
21712171
if res['type'] == 'virtualMachines':
2172-
compute_client = _compute_client_factory(cmd.cli_ctx, subscription_id=res['subscription'])
2173-
vm_info = compute_client.virtual_machines.get(res['resource_group'], res['name'])
2172+
from .operations.vm import VMShow
2173+
command_args = {
2174+
'subscription': res['subscription'],
2175+
'resource_group': res['resource_group'],
2176+
'vm_name': res['name']
2177+
}
2178+
vm_info = VMShow(cli_ctx=cmd.cli_ctx)(command_args=command_args)
21742179
source_from_vm = True
21752180
except ResourceNotFoundError:
21762181
pass
21772182

21782183
if source_from_vm:
21792184
# pylint: disable=no-member
2180-
namespace.os_type = vm_info.storage_profile.os_disk.os_type
2185+
namespace.os_type = vm_info.get('storageProfile', {}).get('osDisk', {}).get('osType')
21812186
namespace.source_virtual_machine = res_id
21822187
if namespace.data_disk_sources:
21832188
raise CLIError("'--data-disk-sources' is not allowed when capturing "
21842189
"images from virtual machines")
21852190
else:
2186-
namespace.os_blob_uri, namespace.os_disk, namespace.os_snapshot, _, _ = _figure_out_storage_source(cmd.cli_ctx, namespace.resource_group_name, namespace.source) # pylint: disable=line-too-long
2191+
namespace.os_blob_uri, namespace.os_disk, namespace.os_snapshot, _, _ = \
2192+
_figure_out_storage_source_by_aaz(cmd.cli_ctx, namespace.resource_group_name, namespace.source)
21872193
namespace.data_blob_uris = []
21882194
namespace.data_disks = []
21892195
namespace.data_snapshots = []
21902196
if namespace.data_disk_sources:
21912197
for data_disk_source in namespace.data_disk_sources:
2192-
source_blob_uri, source_disk, source_snapshot, _, _ = _figure_out_storage_source(
2198+
source_blob_uri, source_disk, source_snapshot, _, _ = _figure_out_storage_source_by_aaz(
21932199
cmd.cli_ctx, namespace.resource_group_name, data_disk_source)
21942200
if source_blob_uri:
21952201
namespace.data_blob_uris.append(source_blob_uri)
@@ -2226,6 +2232,30 @@ def _figure_out_storage_source(cli_ctx, resource_group_name, source):
22262232
return (source_blob_uri, source_disk, source_snapshot, source_restore_point, source_info)
22272233

22282234

2235+
def _figure_out_storage_source_by_aaz(cli_ctx, resource_group_name, source):
2236+
source_blob_uri = None
2237+
source_disk = None
2238+
source_snapshot = None
2239+
source_info = None
2240+
source_restore_point = None
2241+
if urlparse(source).scheme: # a uri?
2242+
source_blob_uri = source
2243+
elif '/disks/' in source.lower():
2244+
source_disk = source
2245+
elif '/snapshots/' in source.lower():
2246+
source_snapshot = source
2247+
elif '/restorepoints/' in source.lower():
2248+
source_restore_point = source
2249+
else:
2250+
source_info, is_snapshot = _get_disk_or_snapshot_info_by_aaz(cli_ctx, resource_group_name, source)
2251+
if is_snapshot:
2252+
source_snapshot = source_info.get('id')
2253+
else:
2254+
source_disk = source_info.get('id')
2255+
2256+
return (source_blob_uri, source_disk, source_snapshot, source_restore_point, source_info)
2257+
2258+
22292259
def _get_disk_or_snapshot_info(cli_ctx, resource_group_name, source):
22302260
compute_client = _compute_client_factory(cli_ctx)
22312261
is_snapshot = True
@@ -2239,6 +2269,28 @@ def _get_disk_or_snapshot_info(cli_ctx, resource_group_name, source):
22392269
return info, is_snapshot
22402270

22412271

2272+
def _get_disk_or_snapshot_info_by_aaz(cli_ctx, resource_group_name, source):
2273+
from .aaz.latest.snapshot import Show as SnapshotShow
2274+
from .aaz.latest.disk import Show as DiskShow
2275+
is_snapshot = True
2276+
2277+
try:
2278+
command_args = {
2279+
'resource_group': resource_group_name,
2280+
'snapshot_name': source
2281+
}
2282+
info = SnapshotShow(cli_ctx=cli_ctx)(command_args=command_args)
2283+
except ResourceNotFoundError:
2284+
command_args = {
2285+
'resource_group': resource_group_name,
2286+
'disk_name': source
2287+
}
2288+
is_snapshot = False
2289+
info = DiskShow(cli_ctx=cli_ctx)(command_args=command_args)
2290+
2291+
return info, is_snapshot
2292+
2293+
22422294
def process_disk_encryption_namespace(cmd, namespace):
22432295
namespace.disk_encryption_keyvault = _get_resource_id(cmd.cli_ctx, namespace.disk_encryption_keyvault,
22442296
namespace.resource_group_name,

src/azure-cli/azure/cli/command_modules/vm/_vm_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,12 @@ class VMGuestPatchClassificationLinux(Enum):
812812
SECURITY = 'Security'
813813

814814

815+
class CachingTypes(Enum):
816+
NONE = 'None'
817+
READ_ONLY = 'ReadOnly'
818+
READ_WRITE = 'ReadWrite'
819+
820+
815821
class DiskCreateOptionTypes(Enum):
816822
ATTACH = 'Attach'
817823
COPY = 'Copy'

src/azure-cli/azure/cli/command_modules/vm/commands.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from azure.cli.command_modules.vm._client_factory import (cf_vm,
77
cf_vm_ext, cf_vm_ext_image,
88
cf_vm_image_term, cf_usage,
9-
cf_vmss, cf_images,
9+
cf_vmss,
1010
cf_galleries, cf_gallery_images, cf_gallery_image_versions,
1111
cf_proximity_placement_groups,
1212
cf_dedicated_hosts, cf_dedicated_host_groups,
@@ -62,11 +62,6 @@ def load_command_table(self, _):
6262
operation_group='availability_sets'
6363
)
6464

65-
compute_image_sdk = CliCommandType(
66-
operations_tmpl='azure.mgmt.compute.operations#ImagesOperations.{}',
67-
client_factory=cf_images
68-
)
69-
7065
compute_vm_sdk = CliCommandType(
7166
operations_tmpl='azure.mgmt.compute.operations#VirtualMachinesOperations.{}',
7267
client_factory=cf_vm
@@ -216,7 +211,7 @@ def load_command_table(self, _):
216211
self.command_table['disk-encryption-set identity remove'] = DiskEncryptionSetIdentityRemove(loader=self)
217212
g.custom_show_command('show', 'show_disk_encryption_set_identity')
218213

219-
with self.command_group('image', compute_image_sdk) as g:
214+
with self.command_group('image') as g:
220215
g.custom_command('create', 'create_image', validator=process_image_create_namespace)
221216

222217
with self.command_group('image builder', image_builder_image_templates_sdk, custom_command_type=image_builder_custom) as g:

src/azure-cli/azure/cli/command_modules/vm/tests/latest/recordings/test_community_gallery_operations.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4840,7 +4840,7 @@ interactions:
48404840
User-Agent:
48414841
- AZURECLI/2.80.0 azsdk-python-core/1.35.0 Python/3.11.9 (Windows-10-10.0.26200-SP0)
48424842
method: GET
4843-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_community_gallery_operations_000001/providers/Microsoft.Compute/virtualMachines/vm000002?api-version=2024-11-01
4843+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_community_gallery_operations_000001/providers/Microsoft.Compute/virtualMachines/vm000002?api-version=2025-04-01
48444844
response:
48454845
body:
48464846
string: "{\r\n \"name\": \"vm000002\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_community_gallery_operations_000001/providers/Microsoft.Compute/virtualMachines/vm000002\",\r\n

src/azure-cli/azure/cli/command_modules/vm/tests/latest/recordings/test_create_image_version_with_allow_replicated_location_deletion.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5074,7 +5074,7 @@ interactions:
50745074
User-Agent:
50755075
- AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
50765076
method: GET
5077-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Compute/virtualMachines/vm000002?api-version=2024-11-01
5077+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Compute/virtualMachines/vm000002?api-version=2025-04-01
50785078
response:
50795079
body:
50805080
string: "{\r\n \"name\": \"vm000002\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Compute/virtualMachines/vm000002\",\r\n

src/azure-cli/azure/cli/command_modules/vm/tests/latest/recordings/test_create_vm_with_community_gallery_image.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5057,7 +5057,7 @@ interactions:
50575057
User-Agent:
50585058
- AZURECLI/2.80.0 azsdk-python-core/1.35.0 Python/3.11.9 (Windows-10-10.0.26200-SP0)
50595059
method: GET
5060-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Compute/virtualMachines/vm000002?api-version=2024-11-01
5060+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Compute/virtualMachines/vm000002?api-version=2025-04-01
50615061
response:
50625062
body:
50635063
string: "{\r\n \"name\": \"vm000002\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Compute/virtualMachines/vm000002\",\r\n

src/azure-cli/azure/cli/command_modules/vm/tests/latest/recordings/test_create_vm_with_shared_gallery_image.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6735,7 +6735,7 @@ interactions:
67356735
User-Agent:
67366736
- AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.12.9 (Linux-6.8.0-1021-azure-x86_64-with-glibc2.36)
67376737
method: GET
6738-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Compute/virtualMachines/vm000002?api-version=2024-11-01
6738+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Compute/virtualMachines/vm000002?api-version=2025-04-01
67396739
response:
67406740
body:
67416741
string: "{\r\n \"name\": \"vm000002\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Compute/virtualMachines/vm000002\",\r\n

src/azure-cli/azure/cli/command_modules/vm/tests/latest/recordings/test_gallery_e2e.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2002,7 +2002,7 @@ interactions:
20022002
User-Agent:
20032003
- AZURECLI/2.72.0 azsdk-python-core/1.31.0 Python/3.11.9 (Windows-10-10.0.26100-SP0)
20042004
method: GET
2005-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Compute/virtualMachines/vm1?api-version=2024-11-01
2005+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Compute/virtualMachines/vm1?api-version=2025-04-01
20062006
response:
20072007
body:
20082008
string: "{\r\n \"name\": \"vm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Compute/virtualMachines/vm1\",\r\n

0 commit comments

Comments
 (0)