def get_op_handler(self, operation): """ Import and load the operation handler """ # Patch the unversioned sdk path to include the appropriate API version for the # resource type in question. from importlib import import_module import types from azure.cli.core.profiles import AZURE_API_PROFILES from azure.cli.core.profiles._shared import get_versioned_sdk_path for rt in AZURE_API_PROFILES[self.cli_ctx.cloud.profile]: if operation.startswith(rt.import_prefix + ".operations."): subs = operation[len(rt.import_prefix + ".operations."):] operation_group = subs[:subs.index('_operations')] operation = operation.replace( rt.import_prefix, get_versioned_sdk_path(self.cli_ctx.cloud.profile, rt, operation_group=operation_group)) elif operation.startswith(rt.import_prefix): operation = operation.replace(rt.import_prefix, get_versioned_sdk_path(self.cli_ctx.cloud.profile, rt)) try: mod_to_import, attr_path = operation.split('#') op = import_module(mod_to_import) for part in attr_path.split('.'): op = getattr(op, part) if isinstance(op, types.FunctionType): return op return six.get_method_function(op) except (ValueError, AttributeError): raise ValueError("The operation '{}' is invalid.".format(operation))
def get_op_handler(self, operation): """ Import and load the operation handler """ # Patch the unversioned sdk path to include the appropriate API version for the # resource type in question. from importlib import import_module import types from azure.cli.core.profiles import AZURE_API_PROFILES from azure.cli.core.profiles._shared import get_versioned_sdk_path for rt in AZURE_API_PROFILES[self.cli_ctx.cloud.profile]: if operation.startswith(rt.import_prefix + ".operations."): subs = operation[len(rt.import_prefix + ".operations."):] operation_group = subs[:subs.index('_operations')] operation = operation.replace( rt.import_prefix, get_versioned_sdk_path(self.cli_ctx.cloud.profile, rt, operation_group=operation_group)) elif operation.startswith(rt.import_prefix): operation = operation.replace( rt.import_prefix, get_versioned_sdk_path(self.cli_ctx.cloud.profile, rt)) try: mod_to_import, attr_path = operation.split('#') op = import_module(mod_to_import) for part in attr_path.split('.'): op = getattr(op, part) if isinstance(op, types.FunctionType): return op return six.get_method_function(op) except (ValueError, AttributeError): raise ValueError( "The operation '{}' is invalid.".format(operation))
def test_get_versioned_sdk_path_date(self): test_profile = {'latest': {ResourceType.MGMT_STORAGE: '2020-10-10'}} with mock.patch('azure.cli.core.profiles._shared.AZURE_API_PROFILES', test_profile): self.assertEqual( get_versioned_sdk_path('latest', ResourceType.MGMT_STORAGE), "azure.mgmt.storage.v2020_10_10")
def test_get_versioned_sdk_path_semver(self): test_profile = {'latest': {ResourceType.DATA_KEYVAULT: '7.0'}} with mock.patch('azure.cli.core.profiles._shared.AZURE_API_PROFILES', test_profile): self.assertEqual( get_versioned_sdk_path('latest', ResourceType.DATA_KEYVAULT), "azure.keyvault.v7_0")
def get_op_handler(self, op_path): """ Import and load the operation handler by path """ # Patch the unversioned sdk path to include the appropriate API version for the # resource type in question. from importlib import import_module import types from azure.cli.core.profiles import AZURE_API_PROFILES from azure.cli.core.profiles._shared import get_versioned_sdk_path for rt in AZURE_API_PROFILES[self.cli_ctx.cloud.profile]: if op_path.startswith(rt.import_prefix + '.'): op_path = op_path.replace( rt.import_prefix, get_versioned_sdk_path( self.cli_ctx.cloud.profile, rt, operation_group=self.operation_group)) try: mod_to_import, attr_path = op_path.split('#') handler = import_module(mod_to_import) for part in attr_path.split('.'): handler = getattr(handler, part) if isinstance(handler, types.FunctionType): return handler return handler.__func__ except (ValueError, AttributeError): raise ValueError("The operation '{}' is invalid.".format(op_path))
def test_get_versioned_sdk_path_semver(self): test_profile = {'latest': {ResourceType.DATA_KEYVAULT: '7.0'}} with mock.patch('azure.cli.core.profiles._shared.AZURE_API_PROFILES', test_profile): self.assertEqual( get_versioned_sdk_path('latest', ResourceType.DATA_KEYVAULT), "azure.keyvault.v7_0" )
def test_get_versioned_sdk_path_date(self): test_profile = {'latest': {ResourceType.MGMT_STORAGE: '2020-10-10'}} with mock.patch('azure.cli.core.profiles._shared.AZURE_API_PROFILES', test_profile): self.assertEqual( get_versioned_sdk_path('latest', ResourceType.MGMT_STORAGE), "azure.mgmt.storage.v2020_10_10" )
def get_op_handler(operation): """ Import and load the operation handler """ # Patch the unversioned sdk path to include the appropriate API version for the # resource type in question. from azure.cli.core._profile import CLOUD for rt in ResourceType: if operation.startswith(rt.import_prefix): operation = operation.replace(rt.import_prefix, get_versioned_sdk_path(CLOUD.profile, rt)) try: mod_to_import, attr_path = operation.split('#') op = import_module(mod_to_import) for part in attr_path.split('.'): op = getattr(op, part) return op except (ValueError, AttributeError): raise ValueError("The operation '{}' is invalid.".format(operation))
def get_op_handler(operation): """ Import and load the operation handler """ # Patch the unversioned sdk path to include the appropriate API version for the # resource type in question. from azure.cli.core._profile import CLOUD for rt in ResourceType: if operation.startswith(rt.import_prefix): operation = operation.replace(rt.import_prefix, get_versioned_sdk_path(CLOUD.profile, rt)) try: mod_to_import, attr_path = operation.split('#') op = import_module(mod_to_import) for part in attr_path.split('.'): op = getattr(op, part) return op except (ValueError, AttributeError): raise ValueError("The operation '{}' is invalid.".format(operation))