Skip to content

Commit

Permalink
{Compute} az vm image: Migrate commands using Code Gen V2 (#30559)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanzhudd authored Dec 24, 2024
1 parent ec666ca commit 59412fc
Show file tree
Hide file tree
Showing 67 changed files with 89,961 additions and 55,319 deletions.
89 changes: 61 additions & 28 deletions src/azure-cli/azure/cli/command_modules/vm/_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,57 +49,89 @@ def _get_thread_count():
def load_images_thru_services(cli_ctx, publisher, offer, sku, location, edge_zone, architecture):
from concurrent.futures import ThreadPoolExecutor, as_completed

from .aaz.latest.vm.image.edge_zone import (ListPublishers as VMImageEdgeZoneListPublishers,
ListOffers as VMImageEdgeZoneListOffers,
ListSkus as VMImageEdgeZoneListSkus,
List as VMImageEdgeZoneList)
from .aaz.latest.vm.image import (ListPublishers as VMImageListPublishers,
ListOffers as VMImageListOffers,
ListSkus as VMImageListSkus,
List as VMImageList)

all_images = []
client = _compute_client_factory(cli_ctx)
if location is None:
location = get_one_of_subscription_locations(cli_ctx)

def _load_images_from_publisher(publisher):
from azure.core.exceptions import ResourceNotFoundError
try:
if edge_zone is not None:
offers = edge_zone_client.list_offers(location=location, edge_zone=edge_zone, publisher_name=publisher)
offers = VMImageEdgeZoneListOffers(cli_ctx=cli_ctx)(command_args={
'location': location,
'edge_zone': edge_zone,
'publisher': publisher,
})
else:
offers = client.virtual_machine_images.list_offers(location=location, publisher_name=publisher)
offers = VMImageListOffers(cli_ctx=cli_ctx)(command_args={
'location': location,
'publisher': publisher,
})
except ResourceNotFoundError as e:
logger.warning(str(e))
return
if offer:
offers = [o for o in offers if _matched(offer, o.name)]
offers = [o for o in offers if _matched(offer, o['name'])]
for o in offers:
try:
if edge_zone is not None:
skus = edge_zone_client.list_skus(location=location, edge_zone=edge_zone,
publisher_name=publisher, offer=o.name)
skus = VMImageEdgeZoneListSkus(cli_ctx=cli_ctx)(command_args={
'location': location,
'edge_zone': edge_zone,
'publisher': publisher,
'offer': o['name']
})
else:
skus = client.virtual_machine_images.list_skus(location=location, publisher_name=publisher,
offer=o.name)
skus = VMImageListSkus(cli_ctx=cli_ctx)(command_args={
'location': location,
'publisher': publisher,
'offer': o['name']
})
except ResourceNotFoundError as e:
logger.warning(str(e))
continue
if sku:
skus = [s for s in skus if _matched(sku, s.name)]
skus = [s for s in skus if _matched(sku, s['name'])]
for s in skus:
try:
expand = "properties/imageDeprecationStatus"
if edge_zone is not None:
images = edge_zone_client.list(location=location, edge_zone=edge_zone, publisher_name=publisher,
offer=o.name, skus=s.name, expand=expand)
images = VMImageEdgeZoneList(cli_ctx=cli_ctx)(command_args={
'location': location,
'edge_zone': edge_zone,
'publisher': publisher,
'offer': o['name'],
'sku': s['name'],
'expand': expand,
})
else:
images = client.virtual_machine_images.list(location=location, publisher_name=publisher,
offer=o.name, skus=s.name, expand=expand)
images = VMImageList(cli_ctx=cli_ctx)(command_args={
'location': location,
'publisher': publisher,
'offer': o['name'],
'sku': s['name'],
'expand': expand,
})
except ResourceNotFoundError as e:
logger.warning(str(e))
continue
for i in images:
image_info = {
'publisher': publisher,
'offer': o.name,
'sku': s.name,
'version': i.name,
'architecture': i.additional_properties.get("properties", {}).get("architecture", None) or "",
'imageDeprecationStatus': i.additional_properties.get(
"properties", {}).get("imageDeprecationStatus", {}) or ""
'offer': o['name'],
'sku': s['name'],
'version': i['name'],
'architecture': i.get("properties", {}).get("architecture", None) or "",
'imageDeprecationStatus': i.get("properties", {}).get("imageDeprecationStatus", {}) or ""
}
if edge_zone is not None:
image_info['edge_zone'] = edge_zone
Expand All @@ -108,24 +140,25 @@ def _load_images_from_publisher(publisher):
all_images.append(image_info)

if edge_zone is not None:
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from azure.cli.core.profiles import ResourceType
edge_zone_client = get_mgmt_service_client(cli_ctx,
ResourceType.MGMT_COMPUTE).virtual_machine_images_edge_zone
publishers = edge_zone_client.list_publishers(location=location, edge_zone=edge_zone)
publishers = VMImageEdgeZoneListPublishers(cli_ctx=cli_ctx)(command_args={
'location': location,
'edge_zone': edge_zone
})
else:
publishers = client.virtual_machine_images.list_publishers(location=location)
publishers = VMImageListPublishers(cli_ctx=cli_ctx)(command_args={
'location': location,
})
if publisher:
publishers = [p for p in publishers if _matched(publisher, p.name)]
publishers = [p for p in publishers if _matched(publisher, p['name'])]

publisher_num = len(publishers)
if publisher_num > 1:
with ThreadPoolExecutor(max_workers=_get_thread_count()) as executor:
tasks = [executor.submit(_load_images_from_publisher, p.name) for p in publishers]
tasks = [executor.submit(_load_images_from_publisher, p['name']) for p in publishers]
for t in as_completed(tasks):
t.result() # don't use the result but expose exceptions from the threads
elif publisher_num == 1:
_load_images_from_publisher(publishers[0].name)
_load_images_from_publisher(publishers[0]['name'])

return all_images

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#
# Code generated by aaz-dev-tools
# --------------------------------------------------------------------------------------------

# pylint: skip-file
# flake8: noqa

from azure.cli.core.aaz import *


class __CMDGroup(AAZCommandGroup):
"""Information on available virtual machine images.
"""
pass


__all__ = ["__CMDGroup"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#
# Code generated by aaz-dev-tools
# --------------------------------------------------------------------------------------------

# pylint: skip-file
# flake8: noqa

from .__cmd_group import *
from ._list import *
from ._list_offers import *
from ._list_publishers import *
from ._list_skus import *
from ._show import *
Loading

0 comments on commit 59412fc

Please sign in to comment.