зеркало из https://github.com/Azure/ARO-RP.git
Update error handling per new az-cli upstream guidelines
This commit is contained in:
Родитель
c3a79c4390
Коммит
1c8c7e28c7
|
@ -9,8 +9,9 @@ import uuid
|
|||
from azure.cli.core.commands.client_factory import get_mgmt_service_client
|
||||
from azure.cli.core.commands.client_factory import get_subscription_id
|
||||
from azure.cli.core.profiles import ResourceType
|
||||
from azure.cli.core.azclierror import CLIInternalError, InvalidArgumentValueError, \
|
||||
RequiredArgumentMissingError
|
||||
from knack.log import get_logger
|
||||
from knack.util import CLIError
|
||||
from msrestazure.azure_exceptions import CloudError
|
||||
from msrestazure.tools import is_valid_resource_id
|
||||
from msrestazure.tools import parse_resource_id
|
||||
|
@ -26,8 +27,8 @@ def validate_cidr(key):
|
|||
try:
|
||||
ipaddress.IPv4Network(cidr)
|
||||
except ValueError:
|
||||
raise CLIError("Invalid --%s '%s'." %
|
||||
(key.replace('_', '-'), cidr))
|
||||
raise InvalidArgumentValueError("Invalid --%s '%s'." %
|
||||
(key.replace('_', '-'), cidr))
|
||||
|
||||
return _validate_cidr
|
||||
|
||||
|
@ -37,16 +38,16 @@ def validate_client_id(namespace):
|
|||
try:
|
||||
uuid.UUID(namespace.client_id)
|
||||
except ValueError:
|
||||
raise CLIError("Invalid --client-id '%s'." % namespace.client_id)
|
||||
raise InvalidArgumentValueError("Invalid --client-id '%s'." % namespace.client_id)
|
||||
|
||||
if namespace.client_secret is None or not str(namespace.client_secret):
|
||||
raise CLIError('Must specify --client-secret with --client-id.')
|
||||
raise RequiredArgumentMissingError('Must specify --client-secret with --client-id.')
|
||||
|
||||
|
||||
def validate_client_secret(namespace):
|
||||
if namespace.client_secret is not None:
|
||||
if namespace.client_id is None or not str(namespace.client_id):
|
||||
raise CLIError('Must specify --client-id with --client-secret.')
|
||||
raise RequiredArgumentMissingError('Must specify --client-id with --client-secret.')
|
||||
|
||||
|
||||
def validate_cluster_resource_group(cmd, namespace):
|
||||
|
@ -55,7 +56,7 @@ def validate_cluster_resource_group(cmd, namespace):
|
|||
cmd.cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES)
|
||||
|
||||
if client.resource_groups.check_existence(namespace.cluster_resource_group):
|
||||
raise CLIError(
|
||||
raise InvalidArgumentValueError(
|
||||
"Invalid --cluster-resource-group '%s': resource group must not exist." %
|
||||
namespace.cluster_resource_group)
|
||||
|
||||
|
@ -66,8 +67,8 @@ def validate_domain(namespace):
|
|||
r'([a-z0-9]|[a-z0-9][-a-z0-9]{0,61}[a-z0-9])' +
|
||||
r'(\.([a-z0-9]|[a-z0-9][-a-z0-9]{0,61}[a-z0-9]))*' +
|
||||
r'$', namespace.domain):
|
||||
raise CLIError("Invalid --domain '%s'." %
|
||||
namespace.domain)
|
||||
raise InvalidArgumentValueError("Invalid --domain '%s'." %
|
||||
namespace.domain)
|
||||
|
||||
|
||||
def validate_pull_secret(namespace):
|
||||
|
@ -83,7 +84,7 @@ def validate_pull_secret(namespace):
|
|||
if not isinstance(json.loads(namespace.pull_secret), dict):
|
||||
raise Exception()
|
||||
except:
|
||||
raise CLIError("Invalid --pull-secret.")
|
||||
raise InvalidArgumentValueError("Invalid --pull-secret.")
|
||||
|
||||
|
||||
def validate_subnet(key):
|
||||
|
@ -92,8 +93,8 @@ def validate_subnet(key):
|
|||
|
||||
if not is_valid_resource_id(subnet):
|
||||
if not namespace.vnet:
|
||||
raise CLIError(
|
||||
'Must specify --vnet if --%s is not an id.' % key.replace('_', '-'))
|
||||
raise RequiredArgumentMissingError('Must specify --vnet if --%s is not an id.' %
|
||||
key.replace('_', '-'))
|
||||
|
||||
validate_vnet(cmd, namespace)
|
||||
|
||||
|
@ -103,28 +104,28 @@ def validate_subnet(key):
|
|||
parts = parse_resource_id(subnet)
|
||||
|
||||
if parts['subscription'] != get_subscription_id(cmd.cli_ctx):
|
||||
raise CLIError("--%s subscription '%s' must equal cluster subscription." %
|
||||
(key.replace('_', '-'), parts["subscription"]))
|
||||
raise InvalidArgumentValueError("--%s subscription '%s' must equal cluster subscription." %
|
||||
(key.replace('_', '-'), parts["subscription"]))
|
||||
|
||||
if parts['namespace'].lower() != 'microsoft.network':
|
||||
raise CLIError("--%s namespace '%s' must equal Microsoft.Network." %
|
||||
(key.replace('_', '-'), parts["namespace"]))
|
||||
raise InvalidArgumentValueError("--%s namespace '%s' must equal Microsoft.Network." %
|
||||
(key.replace('_', '-'), parts["namespace"]))
|
||||
|
||||
if parts['type'].lower() != 'virtualnetworks':
|
||||
raise CLIError("--%s type '%s' must equal virtualNetworks." %
|
||||
(key.replace('_', '-'), parts["type"]))
|
||||
raise InvalidArgumentValueError("--%s type '%s' must equal virtualNetworks." %
|
||||
(key.replace('_', '-'), parts["type"]))
|
||||
|
||||
if parts['last_child_num'] != 1:
|
||||
raise CLIError("--%s '%s' must have one child." %
|
||||
(key.replace('_', '-'), subnet))
|
||||
raise InvalidArgumentValueError("--%s '%s' must have one child." %
|
||||
(key.replace('_', '-'), subnet))
|
||||
|
||||
if 'child_namespace_1' in parts:
|
||||
raise CLIError("--%s '%s' must not have child namespace." %
|
||||
(key.replace('_', '-'), subnet))
|
||||
raise InvalidArgumentValueError("--%s '%s' must not have child namespace." %
|
||||
(key.replace('_', '-'), subnet))
|
||||
|
||||
if parts['child_type_1'].lower() != 'subnets':
|
||||
raise CLIError("--%s child type '%s' must equal subnets." %
|
||||
(key.replace('_', '-'), subnet))
|
||||
raise InvalidArgumentValueError("--%s child type '%s' must equal subnets." %
|
||||
(key.replace('_', '-'), subnet))
|
||||
|
||||
client = get_mgmt_service_client(
|
||||
cmd.cli_ctx, ResourceType.MGMT_NETWORK)
|
||||
|
@ -132,7 +133,7 @@ def validate_subnet(key):
|
|||
client.subnets.get(parts['resource_group'],
|
||||
parts['name'], parts['child_name_1'])
|
||||
except CloudError as err:
|
||||
raise CLIError(err.message)
|
||||
raise CLIInternalError(err.message)
|
||||
|
||||
return _validate_subnet
|
||||
|
||||
|
@ -142,16 +143,17 @@ def validate_subnets(master_subnet, worker_subnet):
|
|||
worker_parts = parse_resource_id(worker_subnet)
|
||||
|
||||
if master_parts['resource_group'].lower() != worker_parts['resource_group'].lower():
|
||||
raise CLIError("--master-subnet resource group '%s' must equal --worker-subnet resource group '%s'." %
|
||||
(master_parts['resource_group'], worker_parts['resource_group']))
|
||||
raise InvalidArgumentValueError("--master-subnet resource group '%s' must equal "
|
||||
"--worker-subnet resource group '%s'." %
|
||||
(master_parts['resource_group'], worker_parts['resource_group']))
|
||||
|
||||
if master_parts['name'].lower() != worker_parts['name'].lower():
|
||||
raise CLIError("--master-subnet vnet name '%s' must equal --worker-subnet vnet name '%s'." %
|
||||
(master_parts['name'], worker_parts['name']))
|
||||
raise InvalidArgumentValueError("--master-subnet vnet name '%s' must equal --worker-subnet vnet name '%s'." %
|
||||
(master_parts['name'], worker_parts['name']))
|
||||
|
||||
if master_parts['child_name_1'].lower() == worker_parts['child_name_1'].lower():
|
||||
raise CLIError("--master-subnet name '%s' must not equal --worker-subnet name '%s'." %
|
||||
(master_parts['child_name_1'], worker_parts['child_name_1']))
|
||||
raise InvalidArgumentValueError("--master-subnet name '%s' must not equal --worker-subnet name '%s'." %
|
||||
(master_parts['child_name_1'], worker_parts['child_name_1']))
|
||||
|
||||
return resource_id(
|
||||
subscription=master_parts['subscription'],
|
||||
|
@ -168,8 +170,8 @@ def validate_visibility(key):
|
|||
if visibility is not None:
|
||||
visibility = visibility.capitalize()
|
||||
if visibility not in ['Private', 'Public']:
|
||||
raise CLIError("Invalid --%s '%s'." %
|
||||
(key.replace('_', '-'), visibility))
|
||||
raise InvalidArgumentValueError("Invalid --%s '%s'." %
|
||||
(key.replace('_', '-'), visibility))
|
||||
|
||||
return _validate_visibility
|
||||
|
||||
|
@ -196,12 +198,10 @@ def validate_vnet_resource_group_name(namespace):
|
|||
def validate_worker_count(namespace):
|
||||
if namespace.worker_count:
|
||||
if namespace.worker_count < 3:
|
||||
raise CLIError(
|
||||
'--worker-count must be greater than or equal to 3.')
|
||||
raise InvalidArgumentValueError('--worker-count must be greater than or equal to 3.')
|
||||
|
||||
|
||||
def validate_worker_vm_disk_size_gb(namespace):
|
||||
if namespace.worker_vm_disk_size_gb:
|
||||
if namespace.worker_vm_disk_size_gb < 128:
|
||||
raise CLIError(
|
||||
'--worker-vm-disk-size-gb must be greater than or equal to 128.')
|
||||
raise InvalidArgumentValueError('--worker-vm-disk-size-gb must be greater than or equal to 128.')
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"azext.minCliCoreVersion": "2.0.67",
|
||||
"azext.minCliCoreVersion": "2.15.0",
|
||||
"azext.isPreview": true,
|
||||
"version": "1.0.1"
|
||||
}
|
||||
|
|
|
@ -8,12 +8,12 @@ from azure.cli.core.commands.client_factory import get_mgmt_service_client
|
|||
from azure.cli.core.commands.client_factory import get_subscription_id
|
||||
from azure.cli.core.profiles import ResourceType
|
||||
from azure.cli.core.util import sdk_no_wait
|
||||
from azure.cli.core.azclierror import ResourceNotFoundError, UnauthorizedError
|
||||
from azure.graphrbac.models import GraphErrorException
|
||||
from msrestazure.azure_exceptions import CloudError
|
||||
from msrestazure.tools import resource_id, parse_resource_id
|
||||
from msrest.exceptions import HttpOperationError
|
||||
from knack.log import get_logger
|
||||
from knack.util import CLIError
|
||||
|
||||
import azext_aro.vendored_sdks.azure.mgmt.redhatopenshift.v2020_04_30.models as openshiftcluster
|
||||
|
||||
|
@ -55,8 +55,8 @@ def aro_create(cmd, # pylint: disable=too-many-locals
|
|||
cmd.cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES)
|
||||
provider = resource_client.providers.get('Microsoft.RedHatOpenShift')
|
||||
if provider.registration_state != 'Registered':
|
||||
raise CLIError('Microsoft.RedHatOpenShift provider is not registered. Run `az provider ' +
|
||||
'register -n Microsoft.RedHatOpenShift --wait`.')
|
||||
raise UnauthorizedError('Microsoft.RedHatOpenShift provider is not registered.',
|
||||
'Run `az provider register -n Microsoft.RedHatOpenShift --wait`.')
|
||||
|
||||
vnet = validate_subnets(master_subnet, worker_subnet)
|
||||
resources = get_network_resources(cmd.cli_ctx, [master_subnet, worker_subnet], vnet)
|
||||
|
@ -77,7 +77,7 @@ def aro_create(cmd, # pylint: disable=too-many-locals
|
|||
rp_client_id = os.environ.get('AZURE_FP_CLIENT_ID', FP_CLIENT_ID)
|
||||
rp_client_sp = aad.get_service_principal(rp_client_id)
|
||||
if not rp_client_sp:
|
||||
raise CLIError("RP service principal not found.")
|
||||
raise ResourceNotFoundError("RP service principal not found.")
|
||||
|
||||
for sp_id in [client_sp.object_id, rp_client_sp.object_id]:
|
||||
for resource in sorted(resources):
|
||||
|
@ -162,7 +162,7 @@ def aro_delete(cmd, client, resource_group_name, resource_name, no_wait=False):
|
|||
try:
|
||||
rp_client_sp = aad.get_service_principal(rp_client_id)
|
||||
if not rp_client_sp:
|
||||
raise CLIError("RP service principal not found.")
|
||||
raise ResourceNotFoundError("RP service principal not found.")
|
||||
except GraphErrorException as e:
|
||||
logger.info(e.message)
|
||||
|
||||
|
@ -223,7 +223,7 @@ def aro_update(cmd, client, resource_group_name, resource_name, no_wait=False):
|
|||
try:
|
||||
rp_client_sp = aad.get_service_principal(rp_client_id)
|
||||
if not rp_client_sp:
|
||||
raise CLIError("RP service principal not found.")
|
||||
raise ResourceNotFoundError("RP service principal not found.")
|
||||
except GraphErrorException as e:
|
||||
logger.info(e.message)
|
||||
|
||||
|
@ -233,7 +233,7 @@ def aro_update(cmd, client, resource_group_name, resource_name, no_wait=False):
|
|||
try:
|
||||
client_sp = aad.get_service_principal(client_id)
|
||||
if not client_sp:
|
||||
raise CLIError("Cluster service principal not found.")
|
||||
raise ResourceNotFoundError("Cluster service principal not found.")
|
||||
except GraphErrorException as e:
|
||||
logger.info(e.message)
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче