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

{Core} Delay import #13843

Merged
merged 2 commits into from
Jun 8, 2020
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
18 changes: 11 additions & 7 deletions src/azure-cli-core/azure/cli/core/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,11 +827,13 @@ def _build_kwargs(self, func, ns): # pylint: disable=no-self-use

@staticmethod
def remove_additional_prop_layer(obj, converted_dic):
from msrest.serialization import Model
if isinstance(obj, Model):
# let us make sure this is the additional properties auto-generated by SDK
if ('additionalProperties' in converted_dic and isinstance(obj.additional_properties, dict)):
# Follow EAFP to flatten `additional_properties` auto-generated by SDK
# See https://docs.python.org/3/glossary.html#term-eafp
try:
if 'additionalProperties' in converted_dic and isinstance(obj.additional_properties, dict):
converted_dic.update(converted_dic.pop('additionalProperties'))
except AttributeError:
pass
return converted_dic

def _validate_cmd_level(self, ns, cmd_validator): # pylint: disable=no-self-use
Expand All @@ -843,12 +845,14 @@ def _validate_cmd_level(self, ns, cmd_validator): # pylint: disable=no-self-use
pass

def _validate_arg_level(self, ns, **_): # pylint: disable=no-self-use
from msrest.exceptions import ValidationError
for validator in getattr(ns, '_argument_validators', []):
try:
validator(**self._build_kwargs(validator, ns))
except ValidationError:
logger.debug('Validation error in %s.', str(validator))
except Exception as ex:
# Delay the import and mimic an exception handler
from msrest.exceptions import ValidationError
if isinstance(ex, ValidationError):
logger.debug('Validation error in %s.', str(validator))
Comment on lines +851 to +855
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mimic the behavior of

def handle_exception(ex): # pylint: disable=too-many-return-statements
# For error code, follow guidelines at https://docs.python.org/2/library/sys.html#sys.exit,
from jmespath.exceptions import JMESPathTypeError
from msrestazure.azure_exceptions import CloudError
from msrest.exceptions import HttpOperationError, ValidationError, ClientRequestError
from azure.cli.core.azlogging import CommandLoggerContext

raise
try:
delattr(ns, '_argument_validators')
Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli-core/azure/cli/core/commands/arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ def resource_exists(cli_ctx, resource_group, name, namespace, type, **_): # pyl
def register_ids_argument(cli_ctx):

from knack import events
from msrestazure.tools import parse_resource_id, is_valid_resource_id

ids_metadata = {}

Expand Down Expand Up @@ -305,6 +304,7 @@ def assemble_json(ids):
if full_id_list:
setattr(namespace, '_ids', full_id_list)

from azure.mgmt.core.tools import parse_resource_id, is_valid_resource_id
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migrate from msrestazure.tools to azure.mgmt.core.tools.

for val in full_id_list:
if not is_valid_resource_id(val):
raise CLIError('invalid resource ID: {}'.format(val))
Expand Down
7 changes: 3 additions & 4 deletions src/azure-cli-core/azure/cli/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@
import logging

from six.moves.urllib.request import urlopen # pylint: disable=import-error

from azure.common import AzureException
from azure.core.exceptions import AzureError
from knack.log import get_logger
from knack.util import CLIError, to_snake_case
from inspect import getfullargspec as get_arg_spec

logger = get_logger(__name__)

Expand Down Expand Up @@ -61,6 +57,8 @@ def handle_exception(ex): # pylint: disable=too-many-return-statements
from msrestazure.azure_exceptions import CloudError
from msrest.exceptions import HttpOperationError, ValidationError, ClientRequestError
from azure.cli.core.azlogging import CommandLoggerContext
from azure.common import AzureException
from azure.core.exceptions import AzureError

with CommandLoggerContext(logger):
if isinstance(ex, JMESPathTypeError):
Expand Down Expand Up @@ -477,6 +475,7 @@ def is_track2(client_class):
""" IS this client a autorestv3/track2 one?.
Could be refined later if necessary.
"""
from inspect import getfullargspec as get_arg_spec
args = get_arg_spec(client_class.__init__).args
return "credential" in args

Expand Down