From 05537e1bef6b5d2d05c689dfd063553df6a0207e Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Thu, 25 Aug 2016 17:43:03 -0700 Subject: [PATCH] Fix folded parameter logic to make new optional. Update existing uses to work with new logic. --- src/azure/cli/commands/template_create.py | 12 +++++++----- .../azure/cli/command_modules/network/_params.py | 10 +++++----- .../azure/cli/command_modules/vm/_params.py | 6 +++--- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/azure/cli/commands/template_create.py b/src/azure/cli/commands/template_create.py index dbc156c7c7f..eb606ce163f 100644 --- a/src/azure/cli/commands/template_create.py +++ b/src/azure/cli/commands/template_create.py @@ -19,7 +19,7 @@ def register_folded_cli_argument(scope, base_name, resource_type, parent_name=None, # pylint: disable=too-many-arguments parent_type=None, type_field=None, existing_id_flag_value='existingId', new_flag_value='new', - none_flag_value='none', default_value_flag='new', allow_none=True, + none_flag_value='none', default_value_flag='new', **kwargs): type_field_name = type_field or base_name + '_type' @@ -29,7 +29,6 @@ def register_folded_cli_argument(scope, base_name, resource_type, parent_name=No existing_id_flag_value, new_flag_value, none_flag_value, - allow_none, parent_name, parent_type) custom_validator = kwargs.pop('validator', None) @@ -42,7 +41,7 @@ def wrapped(namespace): validator = fold_validator quotes = '""' if platform.system() == 'Windows' else "''" - quote_text = ' Use {} for none.'.format(quotes) if allow_none else '' + quote_text = ' Use {} for none.'.format(quotes) if none_flag_value else '' flag_texts = { new_flag_value: ' Creates new by default.{}'.format(quote_text), existing_id_flag_value: ' Uses existing resource.{}' @@ -55,7 +54,7 @@ def wrapped(namespace): register_cli_argument(scope, type_field_name, help=argparse.SUPPRESS, default=None) def _name_id_fold(base_name, resource_type, type_field, #pylint: disable=too-many-arguments - existing_id_flag_value, new_flag_value, none_flag_value, allow_none=True, + existing_id_flag_value, new_flag_value, none_flag_value, parent_name=None, parent_type=None): def handle_folding(namespace): base_name_val = getattr(namespace, base_name) @@ -70,7 +69,7 @@ def handle_folding(namespace): # An empty name specified - that means that we are neither referencing an existing # field, or the name is set to an empty string. We check for all types of quotes # so scripts can run cross-platform. - if not allow_none: + if not none_flag_value: raise CLIError('Field {} cannot be none.'.format(make_camel_case(base_name))) setattr(namespace, type_field, none_flag_value) setattr(namespace, base_name, None) @@ -103,6 +102,9 @@ def handle_folding(namespace): raise CLIError('ID {} does not exist. Please specify ' 'a name to create a new resource.'.format( resource_id(**resource_id_parts))) + elif not new_flag_value: + raise CLIError('Referenced resource {} does not exist. Please create the required ' + 'resource and try again.'.format(resource_id(**resource_id_parts))) else: setattr(namespace, type_field, new_flag_value) diff --git a/src/command_modules/azure-cli-network/azure/cli/command_modules/network/_params.py b/src/command_modules/azure-cli-network/azure/cli/command_modules/network/_params.py index c8964f4c6fe..6a69d5f2af6 100644 --- a/src/command_modules/azure-cli-network/azure/cli/command_modules/network/_params.py +++ b/src/command_modules/azure-cli-network/azure/cli/command_modules/network/_params.py @@ -126,7 +126,7 @@ def completer(prefix, action, parsed_args, **kwargs): # pylint: disable=unused-a register_cli_argument('network application-gateway', 'sku_tier', completer=get_enum_type_completion_list(ApplicationGatewayTier)) register_cli_argument('network application-gateway', 'routing_rule_type', completer=get_enum_type_completion_list(ApplicationGatewayRequestRoutingRuleType)) register_cli_argument('network application-gateway', 'virtual_network_name', virtual_network_name_type) -register_folded_cli_argument('network application-gateway', 'subnet', 'subnets', parent_name='virtual_network_name', parent_type='Microsoft.Network/virtualNetworks', validator=validate_address_prefixes, completer=get_subnet_completion_list()) +register_folded_cli_argument('network application-gateway', 'subnet', 'subnets', parent_name='virtual_network_name', parent_type='Microsoft.Network/virtualNetworks', none_flag_value=None, validator=validate_address_prefixes, completer=get_subnet_completion_list()) register_folded_cli_argument('network application-gateway', 'public_ip', 'Microsoft.Network/publicIPAddresses', completer=get_resource_name_completion_list('Microsoft.Network/publicIPAddresses')) register_cli_argument('network application-gateway', 'virtual_network_type', help=argparse.SUPPRESS) register_cli_argument('network application-gateway', 'private_ip_address_allocation', help=argparse.SUPPRESS) @@ -230,9 +230,9 @@ def completer(prefix, action, parsed_args, **kwargs): # pylint: disable=unused-a register_cli_argument('network nic create', 'network_interface_name', name_arg_type, validator=process_nic_create_namespace) register_cli_argument('network nic create', 'enable_ip_forwarding', options_list=('--ip-forwarding',), action='store_true') register_cli_argument('network nic create', 'use_dns_settings', help=argparse.SUPPRESS) -register_folded_cli_argument('network nic create', 'public_ip_address', 'Microsoft.Network/publicIPAddresses', completer=get_resource_name_completion_list('Microsoft.Network/publicIPAddresses')) -register_folded_cli_argument('network nic create', 'subnet', 'subnets', parent_name='virtual_network_name', parent_type='Microsoft.Network/virtualNetworks', completer=get_subnet_completion_list()) -register_folded_cli_argument('network nic create', 'network_security_group', 'Microsoft.Network/networkSecurityGroups', completer=get_resource_name_completion_list('Microsoft.Network/networkSecurityGroups')) +register_folded_cli_argument('network nic create', 'public_ip_address', 'Microsoft.Network/publicIPAddresses', new_flag_value=None, default_value_flag='none', completer=get_resource_name_completion_list('Microsoft.Network/publicIPAddresses')) +register_folded_cli_argument('network nic create', 'subnet', 'subnets', none_flag_value=None, new_flag_value=None, default_value_flag='existingId', parent_name='virtual_network_name', parent_type='Microsoft.Network/virtualNetworks', completer=get_subnet_completion_list()) +register_folded_cli_argument('network nic create', 'network_security_group', 'Microsoft.Network/networkSecurityGroups', new_flag_value=None, default_value_flag='none', completer=get_resource_name_completion_list('Microsoft.Network/networkSecurityGroups')) register_cli_argument('network nic update', 'enable_ip_forwarding', options_list=('--ip-forwarding',), choices=['true', 'false']) register_cli_argument('network nic update', 'network_security_group', validator=validate_nsg_name_or_id, completer=get_resource_name_completion_list('Microsoft.Network/networkSecurityGroups')) @@ -353,7 +353,7 @@ def completer(prefix, action, parsed_args, **kwargs): # pylint: disable=unused-a register_cli_argument('network vpn-gateway create', 'gateway_type', choices=get_enum_choices(gatewayType)) register_cli_argument('network vpn-gateway create', 'sku', choices=get_enum_choices(sku)) register_cli_argument('network vpn-gateway create', 'vpn_type', choices=get_enum_choices(vpnType)) -register_folded_cli_argument('network vpn-gateway create', 'public_ip_address', 'Microsoft.Network/publicIPAddresses', default_value_flag='existingId', allow_none=False, required=True) +register_folded_cli_argument('network vpn-gateway create', 'public_ip_address', 'Microsoft.Network/publicIPAddresses', default_value_flag='existingId', none_flag_value=None, new_flag_value=None, required=True) register_cli_argument('network vpn-gateway', 'cert_name', help='Root certificate name', options_list=('--name', '-n')) register_cli_argument('network vpn-gateway', 'gateway_name', help='Virtual network gateway name') register_cli_argument('network vpn-gateway root-cert create', 'public_cert_data', help='Base64 contents of the root certificate file or file path.', validator=load_cert_file('public_cert_data')) diff --git a/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/_params.py b/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/_params.py index 9e6db752355..95c0c08f5dc 100644 --- a/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/_params.py +++ b/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/_params.py @@ -170,15 +170,15 @@ def get_vm_size_completion_list(prefix, action, parsed_args, **kwargs):#pylint: register_cli_argument(scope, 'ssh_dest_key_path', completer=FilesCompleter()) register_cli_argument(scope, 'dns_name_for_public_ip', action=VMDNSNameAction, options_list=('--public-ip-address-dns-name',), help='Globally unique DNS Name for the Public IP.') register_cli_argument(scope, 'authentication_type', authentication_type) - register_folded_cli_argument(scope, 'availability_set', 'Microsoft.Compute/availabilitySets', default_value_flag='none') + register_folded_cli_argument(scope, 'availability_set', 'Microsoft.Compute/availabilitySets', new_flag_value=None, default_value_flag='none') register_cli_argument(scope, 'private_ip_address_allocation', help=argparse.SUPPRESS) register_cli_argument(scope, 'virtual_network_ip_address_prefix', options_list=('--vnet-ip-address-prefix',)) register_cli_argument(scope, 'subnet_ip_address_prefix', options_list=('--subnet-ip-address-prefix',)) register_cli_argument(scope, 'private_ip_address', help='Static private IP address (e.g. 10.0.0.5).', options_list=('--private-ip-address',), action=PrivateIpAction) register_cli_argument(scope, 'public_ip_address_allocation', choices=['dynamic', 'static'], help='', default='dynamic', type=str.lower) register_folded_cli_argument(scope, 'public_ip_address', 'Microsoft.Network/publicIPAddresses') - register_folded_cli_argument(scope, 'storage_account', 'Microsoft.Storage/storageAccounts', validator=_find_default_storage_account, allow_none=False, default_value_flag='existingId') - register_folded_cli_argument(scope, 'virtual_network', 'Microsoft.Network/virtualNetworks', options_list=('--vnet',), validator=_find_default_vnet, allow_none=False, default_value_flag='existingId') + register_folded_cli_argument(scope, 'storage_account', 'Microsoft.Storage/storageAccounts', validator=_find_default_storage_account, none_flag_value=None, default_value_flag='existingId') + register_folded_cli_argument(scope, 'virtual_network', 'Microsoft.Network/virtualNetworks', options_list=('--vnet',), validator=_find_default_vnet, none_flag_value=None, default_value_flag='existingId') register_folded_cli_argument(scope, 'network_security_group', 'Microsoft.Network/networkSecurityGroups', options_list=('--nsg',)) register_folded_cli_argument(scope, 'load_balancer', 'Microsoft.Network/loadBalancers') register_cli_argument(scope, 'network_security_group_rule', nsg_rule_type, options_list=('--nsg-rule',))