Skip to content
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

{Misc} Remove direct call to msrestazure #29856

Merged
merged 6 commits into from
Oct 8, 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
16 changes: 7 additions & 9 deletions src/azure-cli-core/azure/cli/core/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,21 +486,19 @@ def test_handle_exception_clierror(self, mock_logger_error):
self.assertEqual(ex_result, 1)

@mock.patch('azure.cli.core.azclierror.logger.error', autospec=True)
def test_handle_exception_clouderror(self, mock_logger_error):
from msrestazure.azure_exceptions import CloudError
def test_handle_exception_httpresponseerror(self, mock_logger_error):
from azure.core.exceptions import HttpResponseError

# create test CloudError Exception
err_detail = "There was a Cloud Error."
err_msg = "CloudError"
mock_cloud_error = mock.MagicMock(spec=CloudError)
mock_cloud_error.args = (err_detail, err_msg)
# create test HttpResponseError Exception
mock_http_response_error = HttpResponseError(response=mock.MagicMock(status_code="xxx",
reason="There was a Http Response Error."))

# call handle_exception
ex_result = handle_exception(mock_cloud_error)
ex_result = handle_exception(mock_http_response_error)

# test behavior
self.assertTrue(mock_logger_error.called)
self.assertIn(mock_cloud_error.args[0], mock_logger_error.call_args[0][0])
self.assertIn(mock_http_response_error.args[0], mock_logger_error.call_args[0][0])
self.assertEqual(ex_result, 1)

@mock.patch('azure.cli.core.azclierror.logger.error', autospec=True)
Expand Down
5 changes: 2 additions & 3 deletions src/azure-cli-core/azure/cli/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@
def handle_exception(ex): # pylint: disable=too-many-locals, too-many-statements, too-many-branches
# For error code, follow guidelines at https://docs.python.org/2/library/sys.html#sys.exit,
from jmespath.exceptions import JMESPathError
from msrestazure.azure_exceptions import CloudError
from msrest.exceptions import HttpOperationError, ValidationError, ClientRequestError
from azure.common import AzureException
from azure.core.exceptions import AzureError, ServiceRequestError
from azure.core.exceptions import AzureError, ServiceRequestError, HttpResponseError
from requests.exceptions import SSLError, HTTPError
from azure.cli.core import azclierror
from msal_extensions.persistence import PersistenceError
Expand All @@ -83,7 +82,7 @@ def handle_exception(ex): # pylint: disable=too-many-locals, too-many-statement
az_error = azclierror.AzureConnectionError(error_msg)
az_error.set_recommendation(SSLERROR_TEMPLATE)

elif isinstance(ex, CloudError):
elif isinstance(ex, HttpResponseError):
if extract_common_error_message(ex):
error_msg = extract_common_error_message(ex)
status_code = str(getattr(ex, 'status_code', 'Unknown Code'))
Expand Down
4 changes: 2 additions & 2 deletions src/azure-cli-testsdk/azure/cli/testsdk/preparers.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,12 @@ def create_resource(self, name, **kwargs):

def remove_resource(self, name, **kwargs):
if not self.dev_setting_name:
from msrestazure.azure_exceptions import CloudError
from azure.core.exceptions import HttpResponseError
try:
self.live_only_execute(
self.cli_ctx,
'az network vnet delete --name {} --resource-group {}'.format(name, self._get_resource_group(**kwargs)))
except CloudError:
except HttpResponseError:
# deletion of vnet may fail as service could create subresources like IPConfig. We could rely on the deletion of resource group to delete the vnet.
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import requests
from knack.log import get_logger
from knack.util import CLIError
from msrestazure.azure_exceptions import CloudError
from azure.core.exceptions import HttpResponseError
from azure.cli.core.profiles import ResourceType, get_sdk
from ._constants import TASK_VALID_VSTS_URLS

Expand Down Expand Up @@ -46,7 +46,7 @@ def upload_source_code(cmd, client,
resource_group_name, registry_name)
upload_url = source_upload_location.upload_url
relative_path = source_upload_location.relative_path
except (AttributeError, CloudError) as e:
except (AttributeError, HttpResponseError) as e:
raise CLIError("Failed to get a SAS URL to upload context. Error: {}".format(e.message))

if not upload_url:
Expand Down
4 changes: 2 additions & 2 deletions src/azure-cli/azure/cli/command_modules/acr/_run_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import time

from azure.core.polling import PollingMethod, LROPoller
from azure.core.exceptions import HttpResponseError
from msrest import Deserializer
from msrestazure.azure_exceptions import CloudError

from ._constants import get_acr_task_models, get_finished_run_status, get_succeeded_run_status

Expand Down Expand Up @@ -90,7 +90,7 @@ def _set_operation_status(self, response):
self.operation_result = self._deserialize(response)
self.operation_status = self.operation_result.status or RunStatus.queued.value
return
raise CloudError(response)
raise HttpResponseError(response)

def _update_status(self):
self._response = self._client._pipeline.run( # pylint: disable=protected-access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import requests
from knack.util import CLIError
from knack.log import get_logger
from msrestazure.azure_exceptions import CloudError
from azure.core.exceptions import HttpResponseError
from azure.common import AzureHttpError
from azure.cli.core.profiles import ResourceType, get_sdk
from ._azure_utils import get_blob_info
Expand Down Expand Up @@ -41,7 +41,7 @@ def stream_logs(cmd, client,
else:
log_file_sas = response.log_artifact_link
artifact = True
except (AttributeError, CloudError) as e:
except (AttributeError, HttpResponseError) as e:
logger.debug("%s Exception: %s", error_msg, e)
raise CLIError(error_msg)

Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli/azure/cli/command_modules/acr/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def get_scope_map_from_id(cmd, scope_map_id):
def resolve_identity_client_id(cli_ctx, managed_identity_resource_id):
from azure.mgmt.msi import ManagedServiceIdentityClient
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from msrestazure.tools import parse_resource_id
from azure.mgmt.core.tools import parse_resource_id

res = parse_resource_id(managed_identity_resource_id)
client = get_mgmt_service_client(cli_ctx, ManagedServiceIdentityClient, subscription_id=res['subscription'])
Expand Down
6 changes: 3 additions & 3 deletions src/azure-cli/azure/cli/command_modules/acr/check_health.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,10 @@ def _check_registry_health(cmd, registry_name, ignore_errors):
if not valid_identity and registry.identity.user_assigned_identities:
for k, v in registry.identity.user_assigned_identities.items():
if v.client_id == client_id:
from msrestazure.azure_exceptions import CloudError
from azure.core.exceptions import HttpResponseError
try:
valid_identity = (resolve_identity_client_id(cmd.cli_ctx, k) == client_id)
except CloudError:
except HttpResponseError:
pass
if not valid_identity:
from ._errors import CMK_MANAGED_IDENTITY_ERROR
Expand All @@ -344,7 +344,7 @@ def _check_registry_health(cmd, registry_name, ignore_errors):

def _check_private_endpoint(cmd, registry_name, vnet_of_private_endpoint): # pylint: disable=too-many-locals, too-many-statements
import socket
from msrestazure.tools import parse_resource_id, is_valid_resource_id, resource_id
from azure.mgmt.core.tools import parse_resource_id, is_valid_resource_id, resource_id

if registry_name is None:
raise CLIError("Registry name must be provided to verify DNS routings of its private endpoints")
Expand Down
4 changes: 2 additions & 2 deletions src/azure-cli/azure/cli/command_modules/acr/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def acr_create(cmd,
lro_poller = client.begin_create(resource_group_name, registry_name, registry)

if workspace:
from msrestazure.tools import is_valid_resource_id, resource_id
from azure.mgmt.core.tools import is_valid_resource_id, resource_id
from azure.cli.core.commands import LongRunningOperation
from azure.cli.core.commands.client_factory import get_subscription_id
acr = LongRunningOperation(cmd.cli_ctx)(lro_poller)
Expand Down Expand Up @@ -609,7 +609,7 @@ def _analyze_identities(identities):


def _ensure_identity_resource_id(subscription_id, resource_group, resource):
from msrestazure.tools import resource_id, is_valid_resource_id
from azure.mgmt.core.tools import resource_id, is_valid_resource_id
if is_valid_resource_id(resource):
return resource
return resource_id(subscription=subscription_id,
Expand Down
4 changes: 2 additions & 2 deletions src/azure-cli/azure/cli/command_modules/acr/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from knack.util import CLIError
from knack.log import get_logger
from msrestazure.tools import is_valid_resource_id, parse_resource_id
from azure.mgmt.core.tools import is_valid_resource_id, parse_resource_id
from azure.cli.command_modules.acr._docker_utils import get_login_server_suffix
from azure.cli.core.commands import LongRunningOperation
from azure.cli.core.util import sdk_no_wait
Expand Down Expand Up @@ -118,7 +118,7 @@ def acr_import(cmd, # pylint: disable=too-many-locals


def _handle_import_exception(e, cmd, source_registry, source_image, registry):
from msrestazure.azure_exceptions import ClientException
from msrest.exceptions import ClientException
try:
# if command fails, it might be because user specified registry twice in --source and --registry
if source_registry:
Expand Down
4 changes: 2 additions & 2 deletions src/azure-cli/azure/cli/command_modules/acr/network_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ def acr_network_rule_remove(cmd,


def _validate_subnet(cli_ctx, subnet, vnet_name, resource_group_name):
from msrestazure.tools import is_valid_resource_id
from azure.mgmt.core.tools import is_valid_resource_id
subnet_is_id = is_valid_resource_id(subnet)

if subnet_is_id and not vnet_name:
return subnet
if subnet and not subnet_is_id and vnet_name:
from msrestazure.tools import resource_id
from azure.mgmt.core.tools import resource_id
from azure.cli.core.commands.client_factory import get_subscription_id
return resource_id(
subscription=get_subscription_id(cli_ctx),
Expand Down
4 changes: 2 additions & 2 deletions src/azure-cli/azure/cli/command_modules/acs/_completers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ def _get_location(cli_ctx, namespace):

def _get_location_from_resource_group(cli_ctx, resource_group_name):
from azure.cli.command_modules.acs._client_factory import get_resource_groups_client
from msrestazure.azure_exceptions import CloudError
from azure.core.exceptions import HttpResponseError

try:
rg = get_resource_groups_client(cli_ctx).get(resource_group_name)
return rg.location
except CloudError as err:
except HttpResponseError as err:
# Print a warning if the user hit [TAB] but the `--resource-group` argument was incorrect.
# For example: "Warning: Resource group 'bogus' could not be found."
from argcomplete import warn
Expand Down
4 changes: 1 addition & 3 deletions src/azure-cli/azure/cli/command_modules/acs/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
UnclassifiedUserFault,
)
from azure.core.exceptions import AzureError, HttpResponseError, ServiceRequestError, ServiceResponseError
from msrestazure.azure_exceptions import CloudError

# type variables
ManagedCluster = TypeVar("ManagedCluster")
Expand Down Expand Up @@ -208,8 +207,7 @@ def get_user_assigned_identity(cli_ctx, subscription_id, resource_group_name, id
identity = msi_client.user_assigned_identities.get(
resource_group_name=resource_group_name, resource_name=identity_name
)
# track 1 sdk raise exception from msrestazure.azure_exceptions
except CloudError as ex:
except HttpResponseError as ex:
if "was not found" in ex.message:
raise ResourceNotFoundError("Identity '{}' not found.".format(identity_name))
raise ServiceError(ex.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from azure.core.exceptions import HttpResponseError, ResourceExistsError
from knack.log import get_logger
from knack.prompting import prompt_y_n
from msrestazure.azure_exceptions import CloudError

logger = get_logger(__name__)

Expand Down Expand Up @@ -127,7 +126,7 @@ def add_role_assignment(cmd, role, service_principal_msi_id, is_service_principa
resolve_assignee=is_service_principal,
)
break
except (CloudError, HttpResponseError) as ex:
except HttpResponseError as ex:
if isinstance(ex, ResourceExistsError) or "The role assignment already exists." in ex.message:
break
logger.info(ex.message)
Expand Down Expand Up @@ -209,7 +208,7 @@ def delete_role_assignments(cli_ctx, role, service_principal, delay=2, scope=Non
cli_ctx, role=role, assignee=service_principal, scope=scope, is_service_principal=is_service_principal
)
break
except (CloudError, HttpResponseError) as ex:
except HttpResponseError as ex:
logger.info(ex.message)
except Exception as ex: # pylint: disable=broad-except
logger.error(str(ex))
Expand Down Expand Up @@ -321,15 +320,15 @@ def ensure_aks_acr_role_assignment(cmd, assignee, registry_id, detach=False, is_

# pylint: disable=unused-argument
def ensure_aks_acr(cmd, assignee, acr_name_or_id, subscription_id, detach=False, is_service_principal=True):
from msrestazure.tools import is_valid_resource_id, parse_resource_id
from azure.mgmt.core.tools import is_valid_resource_id, parse_resource_id

# Check if the ACR exists by resource ID.
if is_valid_resource_id(acr_name_or_id):
try:
parsed_registry = parse_resource_id(acr_name_or_id)
acr_client = get_container_registry_client(cmd.cli_ctx, subscription_id=parsed_registry["subscription"])
registry = acr_client.registries.get(parsed_registry["resource_group"], parsed_registry["name"])
except (CloudError, HttpResponseError) as ex:
except HttpResponseError as ex:
raise AzCLIError(ex.message)
ensure_aks_acr_role_assignment(cmd, assignee, registry.id, detach, is_service_principal)
return
Expand All @@ -339,7 +338,7 @@ def ensure_aks_acr(cmd, assignee, acr_name_or_id, subscription_id, detach=False,
registry_resource = "Microsoft.ContainerRegistry/registries"
try:
registry = get_resource_by_name(cmd.cli_ctx, registry_name, registry_resource)
except (CloudError, HttpResponseError) as ex:
except HttpResponseError as ex:
if "was not found" in ex.message:
raise AzCLIError("ACR {} not found. Have you provided the right ACR name?".format(registry_name))
raise AzCLIError(ex.message)
Expand Down
20 changes: 10 additions & 10 deletions src/azure-cli/azure/cli/command_modules/acs/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def validate_pod_subnet_id(namespace):
def _validate_subnet_id(subnet_id, name):
if subnet_id is None or subnet_id == '':
return
from msrestazure.tools import is_valid_resource_id
from azure.mgmt.core.tools import is_valid_resource_id
if not is_valid_resource_id(subnet_id):
raise InvalidArgumentValueError(name + " is not a valid Azure resource ID.")

Expand All @@ -374,7 +374,7 @@ def validate_ppg(namespace):
if namespace.ppg is not None:
if namespace.ppg == '':
return
from msrestazure.tools import is_valid_resource_id
from azure.mgmt.core.tools import is_valid_resource_id
if not is_valid_resource_id(namespace.ppg):
raise CLIError("--ppg is not a valid Azure resource ID.")

Expand Down Expand Up @@ -478,7 +478,7 @@ def validate_assign_identity(namespace):
if namespace.assign_identity is not None:
if namespace.assign_identity == '':
return
from msrestazure.tools import is_valid_resource_id
from azure.mgmt.core.tools import is_valid_resource_id
if not is_valid_resource_id(namespace.assign_identity):
raise InvalidArgumentValueError("--assign-identity is not a valid Azure resource ID.")

Expand All @@ -487,35 +487,35 @@ def validate_assign_kubelet_identity(namespace):
if namespace.assign_kubelet_identity is not None:
if namespace.assign_kubelet_identity == '':
return
from msrestazure.tools import is_valid_resource_id
from azure.mgmt.core.tools import is_valid_resource_id
if not is_valid_resource_id(namespace.assign_kubelet_identity):
raise InvalidArgumentValueError("--assign-kubelet-identity is not a valid Azure resource ID.")


def validate_nodepool_id(namespace):
from msrestazure.tools import is_valid_resource_id
from azure.mgmt.core.tools import is_valid_resource_id
if not is_valid_resource_id(namespace.nodepool_id):
raise InvalidArgumentValueError("--nodepool-id is not a valid Azure resource ID.")


def validate_snapshot_id(namespace):
if namespace.snapshot_id:
from msrestazure.tools import is_valid_resource_id
from azure.mgmt.core.tools import is_valid_resource_id
if not is_valid_resource_id(namespace.snapshot_id):
raise InvalidArgumentValueError("--snapshot-id is not a valid Azure resource ID.")


def validate_host_group_id(namespace):
if namespace.host_group_id:
from msrestazure.tools import is_valid_resource_id
from azure.mgmt.core.tools import is_valid_resource_id
if not is_valid_resource_id(namespace.host_group_id):
raise InvalidArgumentValueError("--host-group-id is not a valid Azure resource ID.")


def validate_crg_id(namespace):
if namespace.crg_id is None:
return
from msrestazure.tools import is_valid_resource_id
from azure.mgmt.core.tools import is_valid_resource_id
if not is_valid_resource_id(namespace.crg_id):
raise InvalidArgumentValueError(
"--crg-id is not a valid Azure resource ID.")
Expand Down Expand Up @@ -630,7 +630,7 @@ def validate_azure_keyvault_kms_key_vault_resource_id(namespace):
key_vault_resource_id = namespace.azure_keyvault_kms_key_vault_resource_id
if key_vault_resource_id is None or key_vault_resource_id == '':
return
from msrestazure.tools import is_valid_resource_id
from azure.mgmt.core.tools import is_valid_resource_id
if not is_valid_resource_id(key_vault_resource_id):
raise InvalidArgumentValueError("--azure-keyvault-kms-key-vault-resource-id is not a valid Azure resource ID.")

Expand Down Expand Up @@ -792,7 +792,7 @@ def validate_application_security_groups(namespace):
if not asg_ids:
return

from msrestazure.tools import is_valid_resource_id
from azure.mgmt.core.tools import is_valid_resource_id
for asg in asg_ids:
if not is_valid_resource_id(asg):
raise InvalidArgumentValueError(asg + " is not a valid Azure resource ID.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from azure.cli.core.profiles import ResourceType
from azure.cli.core.util import send_raw_request
from azure.core.exceptions import HttpResponseError
from azure.mgmt.core.tools import parse_resource_id, resource_id
from knack.log import get_logger
from msrestazure.tools import parse_resource_id, resource_id

logger = get_logger(__name__)
# mapping for azure public cloud
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def get_amw_region(cmd, azure_monitor_workspace_resource_id):
import re
from msrestazure.tools import parse_resource_id
from azure.mgmt.core.tools import parse_resource_id
# Define the allowed characters in the final string
allowed_chars = re.compile(r'[^a-zA-Z0-9]')

Expand Down
Loading