Skip to content

{Compute} az vm image: Migrate commands using Code Gen V2 #30559

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

Merged
merged 9 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
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
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