diff --git a/src/quantum/HISTORY.rst b/src/quantum/HISTORY.rst index 0ceb668db8f..2438df26dfb 100644 --- a/src/quantum/HISTORY.rst +++ b/src/quantum/HISTORY.rst @@ -3,6 +3,10 @@ Release History =============== +1.0.0b30 +++++++++++++++ +* Fixed crashes in ``az quantum offerings list -o table`` and ``az quantum workspace create`` when a V2-only provider lacks a legacy marketplace ManagedApplication association. + 1.0.0b29 ++++++++++++++ * Clarified which suite-offer and workspace quota commands apply to V2 providers and workspaces. diff --git a/src/quantum/azext_quantum/commands.py b/src/quantum/azext_quantum/commands.py index c1ab07ab0c8..e673c01c0bb 100644 --- a/src/quantum/azext_quantum/commands.py +++ b/src/quantum/azext_quantum/commands.py @@ -10,6 +10,7 @@ from collections import OrderedDict from azure.cli.core.commands import CliCommandType from ._validators import validate_workspace_info, validate_workspace_user, validate_target_info, validate_workspace_and_target_info, validate_provider_and_sku_info +from .operations.offerings import OFFER_NOT_AVAILABLE, PUBLISHER_NOT_AVAILABLE logger = logging.getLogger(__name__) @@ -94,11 +95,14 @@ def transform_file_list(results): def transform_offerings(offerings): def one(offering): + properties = offering['properties'] + managed_application = properties.get('managedApplication') or {} + return OrderedDict([ ('Provider ID', offering['id']), - ('SKU', ', '.join([s['id'] for s in offering['properties']['skus']])), - ('Publisher ID', offering['properties']['managedApplication']['publisherId']), - ('Offer ID', offering['properties']['managedApplication']['offerId']) + ('SKU', ', '.join([s['id'] for s in properties['skus']])), + ('Publisher ID', managed_application.get('publisherId') or PUBLISHER_NOT_AVAILABLE), + ('Offer ID', managed_application.get('offerId') or OFFER_NOT_AVAILABLE) ]) return [one(offering) for offering in offerings] diff --git a/src/quantum/azext_quantum/operations/offerings.py b/src/quantum/azext_quantum/operations/offerings.py index ec789cf2f36..1ddedaa689b 100644 --- a/src/quantum/azext_quantum/operations/offerings.py +++ b/src/quantum/azext_quantum/operations/offerings.py @@ -33,8 +33,15 @@ def _get_publisher_and_offer_from_provider_id(providers, provider_id): offer_id = None for p in providers: if p.id.lower() == provider_id.lower(): - offer_id = p.properties.managed_application.offer_id - publisher_id = p.properties.managed_application.publisher_id + managed_application = p.properties.managed_application + if managed_application is None: + # V2-only providers have no legacy marketplace ManagedApplication association; + # treat the same as the existing "N/A" sentinel instead of raising. + publisher_id = PUBLISHER_NOT_AVAILABLE + offer_id = OFFER_NOT_AVAILABLE + else: + offer_id = managed_application.offer_id + publisher_id = managed_application.publisher_id break return (publisher_id, offer_id) diff --git a/src/quantum/azext_quantum/tests/latest/test_quantum_offerings.py b/src/quantum/azext_quantum/tests/latest/test_quantum_offerings.py index 0d66f2fb44f..7e163cf9255 100644 --- a/src/quantum/azext_quantum/tests/latest/test_quantum_offerings.py +++ b/src/quantum/azext_quantum/tests/latest/test_quantum_offerings.py @@ -3,10 +3,19 @@ # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- +import unittest +from types import SimpleNamespace + import pytest from azure.cli.testsdk import ScenarioTest from .utils import issue_cmd_with_param_missing +from ...commands import transform_offerings +from ...operations.offerings import ( + _get_publisher_and_offer_from_provider_id, + PUBLISHER_NOT_AVAILABLE, + OFFER_NOT_AVAILABLE, +) class QuantumOfferingsScenarioTest(ScenarioTest): @@ -14,3 +23,98 @@ class QuantumOfferingsScenarioTest(ScenarioTest): def test_offerings_errors(self): issue_cmd_with_param_missing(self, "az quantum offerings accept-terms", "az quantum offerings accept-terms -p MyProviderId -k MySKU -l MyLocation\nOnce terms have been reviewed, accept the invoking this command.") issue_cmd_with_param_missing(self, "az quantum offerings show-terms", "az quantum offerings show-terms -p MyProviderId -k MySKU -l MyLocation\nUse a Provider Id and SKU from `az quantum offerings list` to review the terms.") + + +class QuantumOfferingsUnitTest(unittest.TestCase): + + def _make_provider(self, provider_id, managed_application): + return SimpleNamespace( + id=provider_id, + properties=SimpleNamespace(managed_application=managed_application), + ) + + def test_get_publisher_and_offer_from_provider_id_returns_values(self): + providers = [ + self._make_provider( + "rigetti", + SimpleNamespace(publisher_id="rigetti-computing", offer_id="rigetti-azure-basic"), + ), + ] + + self.assertEqual( + _get_publisher_and_offer_from_provider_id(providers, "rigetti"), + ("rigetti-computing", "rigetti-azure-basic"), + ) + + def test_get_publisher_and_offer_from_provider_id_matches_case_insensitively(self): + providers = [ + self._make_provider( + "Rigetti", + SimpleNamespace(publisher_id="rigetti-computing", offer_id="rigetti-azure-basic"), + ), + ] + + self.assertEqual( + _get_publisher_and_offer_from_provider_id(providers, "RIGETTI"), + ("rigetti-computing", "rigetti-azure-basic"), + ) + + def test_get_publisher_and_offer_from_provider_id_returns_none_when_not_found(self): + providers = [ + self._make_provider( + "rigetti", + SimpleNamespace(publisher_id="rigetti-computing", offer_id="rigetti-azure-basic"), + ), + ] + + self.assertEqual( + _get_publisher_and_offer_from_provider_id(providers, "contoso-v2-provider"), + (None, None), + ) + + def test_get_publisher_and_offer_from_provider_id_handles_missing_managed_application(self): + # V2-only providers (e.g. contoso-v2-provider) have no legacy marketplace ManagedApplication + # association at all; this must not raise and should be treated like the existing + # "N/A" sentinel (no terms to accept) instead of crashing. + providers = [self._make_provider("contoso-v2-provider", None)] + + self.assertEqual( + _get_publisher_and_offer_from_provider_id(providers, "contoso-v2-provider"), + (PUBLISHER_NOT_AVAILABLE, OFFER_NOT_AVAILABLE), + ) + + def test_transform_offerings_handles_missing_managed_application(self): + offerings = [ + { + 'id': 'rigetti', + 'properties': { + 'skus': [{'id': 'azure-basic-qvm-only-unlimited'}], + 'managedApplication': { + 'publisherId': 'rigetticoinc1644276861431', + 'offerId': 'rigetti-aq', + }, + }, + }, + { + 'id': 'atom-dev', + 'properties': { + 'skus': [{'id': 'default'}], + 'managedApplication': None, + }, + }, + ] + + table = transform_offerings(offerings) + + self.assertEqual(len(table), 2) + self.assertEqual( + list(table[0].keys()), + ['Provider ID', 'SKU', 'Publisher ID', 'Offer ID'], + ) + self.assertEqual(table[0]['Provider ID'], 'rigetti') + self.assertEqual(table[0]['Publisher ID'], 'rigetticoinc1644276861431') + self.assertEqual(table[0]['Offer ID'], 'rigetti-aq') + self.assertEqual(table[1]['Provider ID'], 'atom-dev') + self.assertEqual(table[1]['SKU'], 'default') + self.assertEqual(table[1]['Publisher ID'], PUBLISHER_NOT_AVAILABLE) + self.assertEqual(table[1]['Offer ID'], OFFER_NOT_AVAILABLE) diff --git a/src/quantum/azext_quantum/tests/latest/test_quantum_workspace.py b/src/quantum/azext_quantum/tests/latest/test_quantum_workspace.py index 6060a0b55d3..367bbf0e04b 100644 --- a/src/quantum/azext_quantum/tests/latest/test_quantum_workspace.py +++ b/src/quantum/azext_quantum/tests/latest/test_quantum_workspace.py @@ -21,7 +21,7 @@ from ..._version_check_helper import check_version from ..._params import QuotaAction from ..._validators import validate_email, validate_workspace_user -from ...operations.workspace import _apply_target_quotas, _require_v2_workspace, _validate_target_quota_bounds, _validate_storage_account, _autoadd_providers, create, _resolve_user_id, _list_user_workspace_role_assignments, _scope_distance, _select_user_workspace_role_assignment, add_user, remove_user, list_users, update, QUANTUM_WORKSPACE_DATA_CONTRIBUTOR_ROLE_ID, QUANTUM_WORKSPACE_OWNER_ROLE_ID, SUPPORTED_STORAGE_SKU_TIERS, SUPPORTED_STORAGE_KINDS, DEPLOYMENT_NAME_PREFIX +from ...operations.workspace import _apply_target_quotas, _require_v2_workspace, _validate_target_quota_bounds, _validate_storage_account, _autoadd_providers, _add_quantum_providers, create, _resolve_user_id, _list_user_workspace_role_assignments, _scope_distance, _select_user_workspace_role_assignment, add_user, remove_user, list_users, update, QUANTUM_WORKSPACE_DATA_CONTRIBUTOR_ROLE_ID, QUANTUM_WORKSPACE_OWNER_ROLE_ID, SUPPORTED_STORAGE_SKU_TIERS, SUPPORTED_STORAGE_KINDS, DEPLOYMENT_NAME_PREFIX from ...operations.workspace import _merge_workspace_quotas from ...commands import transform_workspace_quotas from ...vendored_sdks.azure_mgmt_quantum.models import Provider, TargetQuotaAllocations @@ -280,6 +280,23 @@ def test_workspace_user_v2(self): class QuantumWorkspacesUnitTest(unittest.TestCase): + @patch('azext_quantum.operations.workspace.cf_offerings') + def test_add_quantum_providers_handles_provider_without_managed_application(self, cf_offerings_mock): + # Regression test: V2-only providers (e.g. contoso-v2-provider) can have no legacy marketplace + # ManagedApplication association at all; workspace create must not crash for them. + provider = SimpleNamespace( + id='contoso-v2-provider', + properties=SimpleNamespace(managed_application=None, skus=[]), + ) + cf_offerings_mock.return_value.list.return_value = [provider] + workspace = SimpleNamespace(location='westus', properties=SimpleNamespace(providers=[])) + + _add_quantum_providers(Mock(), workspace, 'contoso-v2-provider/default', auto_accept=True, skip_autoadd=True) + + self.assertEqual(len(workspace.properties.providers), 1) + self.assertEqual(workspace.properties.providers[0].provider_id, 'contoso-v2-provider') + self.assertEqual(workspace.properties.providers[0].provider_sku, 'default') + def test_run_cleanup_commands_attempts_all_commands(self): test_case = SimpleNamespace(cmd=Mock(side_effect=[RuntimeError('first cleanup failed'), None])) cleanup_commands = [('first resource', 'first command'), ('second resource', 'second command')] diff --git a/src/quantum/setup.py b/src/quantum/setup.py index e63067e68e8..d3d152ecc18 100644 --- a/src/quantum/setup.py +++ b/src/quantum/setup.py @@ -17,7 +17,7 @@ # This version should match the latest entry in HISTORY.rst # Also, when updating this, please review the version used by the extension to # submit requests, which can be found at './azext_quantum/__init__.py' -VERSION = '1.0.0b29' +VERSION = '1.0.0b30' # The full list of classifiers is available at # https://pypi.python.org/pypi?%3Aaction=list_classifiers