From c0e82c87e9d93c8bfb597776aa25725ff4f88832 Mon Sep 17 00:00:00 2001 From: deveshdama <87668846+deveshdama@users.noreply.github.com> Date: Wed, 6 Mar 2024 23:53:04 -0800 Subject: [PATCH] [AKS] `az aks mesh get-upgrades`: Fix command failure with a traceback if ASM addon is disabled (#28507) --- .../azure/cli/command_modules/acs/custom.py | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/acs/custom.py b/src/azure-cli/azure/cli/command_modules/acs/custom.py index 0bc0c8c183f..80ae7f79b18 100644 --- a/src/azure-cli/azure/cli/command_modules/acs/custom.py +++ b/src/azure-cli/azure/cli/command_modules/acs/custom.py @@ -2833,6 +2833,17 @@ def aks_mesh_get_revisions( return None +def check_iterator(iterator): + import itertools + try: + first = next(iterator) + except StopIteration: # iterator is empty + return True, iterator + except TypeError: # iterator is not iterable, e.g. None + return True, iterator + return False, itertools.chain([first], iterator) + + def aks_mesh_get_upgrades( cmd, client, @@ -2840,16 +2851,14 @@ def aks_mesh_get_upgrades( name ): upgradeProfiles = client.list_mesh_upgrade_profiles(resource_group_name, name) - # 'upgradeProfiles' is an ItemPaged object - upgrades = [] - # Iterate over items within pages - for page in upgradeProfiles.by_page(): - for item in page: - upgrades.append(item) - - if upgrades: - return upgrades[0].properties - + is_empty, upgradeProfiles = check_iterator(upgradeProfiles) + if is_empty: + logger.warning("No mesh upgrade profiles found for the cluster '%s' " + + "in the resource group '%s'.", name, resource_group_name) + return None + upgrade = next(upgradeProfiles, None) + if upgrade: + return upgrade.properties return None