diff --git a/src/azure-cli/azure/cli/command_modules/vm/_validators.py b/src/azure-cli/azure/cli/command_modules/vm/_validators.py index fd2572f0738..a534d8cf5dc 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/vm/_validators.py @@ -254,14 +254,18 @@ def _parse_image_argument(cmd, namespace): # 4 - attempt to match an URN alias (most likely) from azure.cli.command_modules.vm._actions import load_images_from_aliases_doc - images = load_images_from_aliases_doc(cmd.cli_ctx) - matched = next((x for x in images if x['urnAlias'].lower() == namespace.image.lower()), None) - if matched: - namespace.os_publisher = matched['publisher'] - namespace.os_offer = matched['offer'] - namespace.os_sku = matched['sku'] - namespace.os_version = matched['version'] - return 'urn' + import requests + try: + images = load_images_from_aliases_doc(cmd.cli_ctx) + matched = next((x for x in images if x['urnAlias'].lower() == namespace.image.lower()), None) + if matched: + namespace.os_publisher = matched['publisher'] + namespace.os_offer = matched['offer'] + namespace.os_sku = matched['sku'] + namespace.os_version = matched['version'] + return 'urn' + except requests.exceptions.ConnectionError: + pass # 5 - check if an existing managed disk image resource compute_client = _compute_client_factory(cmd.cli_ctx) @@ -271,9 +275,15 @@ def _parse_image_argument(cmd, namespace): 'images', 'Microsoft.Compute') return 'image_id' except CloudError: - err = 'Invalid image "{}". Use a valid image URN, custom image name, custom image id, VHD blob URI, or ' \ - 'pick an image from {}.\nSee vm create -h for more information on specifying an image.' - raise CLIError(err.format(namespace.image, [x['urnAlias'] for x in images])) + if images is not None: + err = 'Invalid image "{}". Use a valid image URN, custom image name, custom image id, ' \ + 'VHD blob URI, or pick an image from {}.\nSee vm create -h for more information ' \ + 'on specifying an image.'.format(namespace.image, [x['urnAlias'] for x in images]) + else: + err = 'Failed to connect to remote source of image aliases. Invalid image "{}". Use a ' \ + 'valid image URN, custom image name, custom image id, or VHD blob URI.\nSee vm ' \ + 'create -h for more information on specifying an image.'.format(namespace.image) + raise CLIError(err) def _get_image_plan_info_if_exists(cmd, namespace):