def test_override_argtype_with_argtype(self): existing_options_list = ('--default', '-d') arg = CliArgumentType(options_list=existing_options_list, validator=None, completer='base', help='base', required=True) overriding_argtype = CliArgumentType(options_list=('--overridden', ), validator='overridden', completer=None, overrides=arg, help='overridden', required=CliArgumentType.REMOVE) self.assertEqual(overriding_argtype.settings['validator'], 'overridden') self.assertEqual(overriding_argtype.settings['completer'], None) self.assertEqual(overriding_argtype.settings['options_list'], ('--overridden', )) self.assertEqual(overriding_argtype.settings['help'], 'overridden') self.assertEqual(overriding_argtype.settings['required'], CliArgumentType.REMOVE) cmd_arg = CliCommandArgument(dest='whatever', argtype=overriding_argtype, help=CliArgumentType.REMOVE) self.assertFalse('required' in cmd_arg.options) self.assertFalse('help' in cmd_arg.options)
def test_register_cli_argument(self): command_table.clear() cli_command('test register sample-vm-get', Test_command_registration.sample_vm_get) register_cli_argument( 'test register sample-vm-get', 'vm_name', CliArgumentType(options_list=('--wonky-name', '-n'), metavar='VMNAME', help='Completely WONKY name...', required=False)) _update_command_definitions(command_table) self.assertEqual(len(command_table), 1, 'We expect exactly one command in the command table') command_metadata = command_table['test register sample-vm-get'] self.assertEqual(len(command_metadata.arguments), 4, 'We expected exactly 4 arguments') some_expected_arguments = { 'resource_group_name': CliArgumentType(dest='resource_group_name', required=True), 'vm_name': CliArgumentType(dest='vm_name', required=False), } for probe in some_expected_arguments: existing = next(arg for arg in command_metadata.arguments if arg == probe) self.assertDictContainsSubset( some_expected_arguments[existing].settings, command_metadata.arguments[existing].options) self.assertEqual(command_metadata.arguments['vm_name'].options_list, ('--wonky-name', '-n'))
def test_override_existing_option_string(self): arg = CliArgumentType(options_list=('--funky', '-f')) updated_options_list = ('--something-else', '-s') arg.update(options_list=updated_options_list, validator=lambda: (), completer=lambda: ()) self.assertEqual(arg.settings['options_list'], updated_options_list) self.assertIsNotNone(arg.settings['validator']) self.assertIsNotNone(arg.settings['completer'])
def test_register_extra_cli_argument(self): command_table.clear() cli_command('test command sample-vm-get', Test_command_registration.sample_vm_get, None) register_extra_cli_argument('test command sample-vm-get', 'added_param', options_list=('--added-param', ), metavar='ADDED', help='Just added this right now!', required=True) _update_command_definitions(command_table) self.assertEqual(len(command_table), 1, 'We expect exactly one command in the command table') command_metadata = command_table['test command sample-vm-get'] self.assertEqual(len(command_metadata.arguments), 5, 'We expected exactly 5 arguments') some_expected_arguments = { 'added_param': CliArgumentType(dest='added_param', required=True) } for probe in some_expected_arguments: existing = next(arg for arg in command_metadata.arguments if arg == probe) self.assertDictContainsSubset( some_expected_arguments[existing].settings, command_metadata.arguments[existing].options) command_table.clear()
def test_command_build_argument_help_text(self): def sample_sdk_method_with_weird_docstring(param_a, param_b, param_c): # pylint: disable=unused-argument """ An operation with nothing good. :param dict param_a: :param param_b: The name of nothing. :param param_c: The name of nothing2. """ command_table.clear() cli_command('test command foo', sample_sdk_method_with_weird_docstring, None) _update_command_definitions(command_table) command_metadata = command_table['test command foo'] self.assertEqual(len(command_metadata.arguments), 3, 'We expected exactly 3 arguments') some_expected_arguments = { 'param_a': CliArgumentType(dest='param_a', required=True, help=''), 'param_b': CliArgumentType(dest='param_b', required=True, help='The name of nothing.'), 'param_c': CliArgumentType(dest='param_c', required=True, help='The name of nothing2.') } for probe in some_expected_arguments: existing = next(arg for arg in command_metadata.arguments if arg == probe) self.assertDictContainsSubset( some_expected_arguments[existing].settings, command_metadata.arguments[existing].options) command_table.clear()
def test_register_command(self): command_table.clear() cli_command('test command sample-vm-get', Test_command_registration.sample_vm_get, None) self.assertEqual(len(command_table), 1, 'We expect exactly one command in the command table') command_metadata = command_table['test command sample-vm-get'] self.assertEqual(len(command_metadata.arguments), 4, 'We expected exactly 4 arguments') some_expected_arguments = { 'resource_group_name': CliArgumentType(dest='resource_group_name', required=True, help='The name of the resource group.'), 'vm_name': CliArgumentType(dest='vm_name', required=True, help='The name of the virtual machine.'), 'opt_param': CliArgumentType( required=False, help= 'Used to verify reflection correctly identifies optional params.' ), # pylint: disable=line-too-long 'expand': CliArgumentType( required=False, help='The expand expression to apply on the operation.') } for probe in some_expected_arguments: existing = next(arg for arg in command_metadata.arguments if arg == probe) self.assertDictContainsSubset( some_expected_arguments[existing].settings, command_metadata.arguments[existing].options) self.assertEqual( command_metadata.arguments['resource_group_name'].options_list, ['--resource-group-name'])
def test_register_cli_argument_with_overrides(self): command_table.clear() global_vm_name_type = CliArgumentType(options_list=('--foo', '-f'), metavar='FOO', help='foo help') derived_vm_name_type = CliArgumentType(base_type=global_vm_name_type, help='first modification') cli_command('test vm-get', Test_command_registration.sample_vm_get, None) cli_command('test command vm-get-1', Test_command_registration.sample_vm_get, None) cli_command('test command vm-get-2', Test_command_registration.sample_vm_get, None) register_cli_argument('test', 'vm_name', global_vm_name_type) register_cli_argument('test command', 'vm_name', derived_vm_name_type) register_cli_argument('test command vm-get-2', 'vm_name', derived_vm_name_type, help='second modification') _update_command_definitions(command_table) self.assertEqual( len(command_table), 3, 'We expect exactly three commands in the command table') command1 = command_table['test vm-get'].arguments['vm_name'] command2 = command_table['test command vm-get-1'].arguments['vm_name'] command3 = command_table['test command vm-get-2'].arguments['vm_name'] self.assertTrue(command1.options['help'] == 'foo help') self.assertTrue(command2.options['help'] == 'first modification') self.assertTrue(command3.options['help'] == 'second modification') command_table.clear()
def test_generic_update_ids(self): my_objs = [ { 'prop': 'val', 'list': [ 'a', 'b', ['c', {'d': 'e'}] ] }, { 'prop': 'val', 'list': [ 'a', 'b', ['c', {'d': 'e'}] ] }] def my_get(name, resource_group): #pylint:disable=unused-argument # name is None when tests are run in a batch on Python <=2.7.9 if sys.version_info < (2, 7, 10): return my_objs[0] return my_objs[int(name)] def my_set(**kwargs): #pylint:disable=unused-argument return my_objs register_cli_argument('gencommand', 'name', CliArgumentType(options_list=('--name', '-n'), metavar='NAME', id_part='name')) register_generic_update('gencommand', my_get, my_set) config = Configuration([]) APPLICATION.initialize(config) id_str = ('/subscriptions/00000000-0000-0000-0000-0000000000000/resourceGroups/rg/' 'providers/Microsoft.Compute/virtualMachines/') APPLICATION.execute('gencommand --ids {0}0 {0}1 --resource-group bar --set prop=newval' .format(id_str).split()) self.assertEqual(my_objs[0]['prop'], 'newval', 'first object updated') # name is None when tests are run in a batch on Python <=2.7.9 if not sys.version_info < (2, 7, 10): self.assertEqual(my_objs[1]['prop'], 'newval', 'second object updated')
client = _network_client_factory() if parsed_args.resource_group_name and parsed_args.load_balancer_name: rg = parsed_args.resource_group_name lb = parsed_args.load_balancer_name return [ r.name for r in client.load_balancers.get( resource_group_name=rg, load_balancer_name=lb).inbound_nat_rules ] # pylint: disable=no-member return completer # BASIC PARAMETER CONFIGURATION name_arg_type = CliArgumentType(options_list=('--name', '-n'), metavar='NAME') nsg_name_type = CliArgumentType(options_list=('--nsg-name', ), metavar='NSG', help='Name of the network security group.') virtual_network_name_type = CliArgumentType( options_list=('--vnet-name', ), metavar='VNET_NAME', help='The virtual network (VNET) name.', completer=get_resource_name_completion_list( 'Microsoft.Network/virtualNetworks')) subnet_name_type = CliArgumentType(options_list=('--subnet-name', ), metavar='SUBNET_NAME', help='The subnet name.') load_balancer_name_type = CliArgumentType( options_list=('--lb-name', ), metavar='LB_NAME',
#--------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. #--------------------------------------------------------------------------------------------- from azure.cli.commands import CliArgumentType, register_cli_argument from azure.cli.commands.parameters import tags_type from azure.mgmt.web import WebSiteManagementClient from azure.cli.commands.client_factory import get_mgmt_service_client from azure.cli.commands.template_create import register_folded_cli_argument # FACTORIES def _web_client_factory(**_): return get_mgmt_service_client(WebSiteManagementClient) # PARAMETER REGISTRATION register_cli_argument('webapp', 'name', CliArgumentType(options_list=('--name', '-n'))) register_cli_argument('webapp', 'tags', tags_type) register_folded_cli_argument('webapp create', 'hosting_plan', 'Microsoft.Web/serverfarms', help='Name or ID of the web application\'s hosting plan.' ' Creates if resource doesn\'t exist.')
def test_override_remove_validator(self): existing_options_list = ('--something-else', '-s') arg = CliArgumentType(options_list=existing_options_list, validator=lambda *args, **kwargs: ()) arg.update(validator=None) self.assertIsNone(arg.settings['validator'])
def test_dont_override_existing_option_string(self): existing_options_list = ('--something-else', '-s') arg = CliArgumentType(options_list=existing_options_list) arg.update() self.assertEqual(arg.settings['options_list'], existing_options_list)
def get_vm_size_completion_list(prefix, action, parsed_args, **kwargs): #pylint: disable=unused-argument location = parsed_args.location if parsed_args.location else get_one_of_subscription_locations( ) result = get_vm_sizes(location) return [r.name for r in result] # BASIC PARAMETER CONFIGURATION choices_caching_types = [e.value for e in CachingTypes] choices_container_service_orchestrator_types = [ e.value for e in ContainerServiceOchestratorTypes ] choices_upgrade_mode = [e.value.lower() for e in UpgradeMode] choices_ip_allocation_method = [e.value.lower() for e in IPAllocationMethod] name_arg_type = CliArgumentType(options_list=('--name', '-n'), metavar='NAME') multi_ids_type = CliArgumentType(nargs='+') admin_username_type = CliArgumentType(options_list=('--admin-username', ), default=getpass.getuser(), required=False) existing_vm_name = CliArgumentType(overrides=name_arg_type, help='The name of the virtual machine', completer=get_resource_name_completion_list( 'Microsoft.Compute/virtualMachines'), id_part='name') register_cli_argument('vm', 'vm_name', existing_vm_name) register_cli_argument('vm', 'size', CliArgumentType(completer=get_vm_size_completion_list)) register_cli_argument('vm', 'tags', tags_type) register_cli_argument('vm', 'name', arg_type=name_arg_type)
return completer def get_enum_type_completion_list(enum_type=None): ENUM_MAPPING_KEY = '_value2member_map_' def completer(prefix, action, parsed_args, **kwargs): # pylint: disable=unused-argument return list(enum_type.__dict__[ENUM_MAPPING_KEY].keys()) return completer resource_group_name_type = CliArgumentType( options_list=('--resource-group', '-g'), completer=get_resource_group_completion_list, id_part='resource_group', help='Name of resource group') name_type = CliArgumentType(options_list=('--name', '-n'), help='the primary resource name') location_type = CliArgumentType(options_list=('--location', '-l'), completer=get_location_completion_list, help='Location.', metavar='LOCATION') deployment_name_type = CliArgumentType(help=argparse.SUPPRESS, required=False, validator=generate_deployment_name)
from azure.cli.commands import CliArgumentType, register_cli_argument from .custom import load_subscriptions # BASIC PARAMETER CONFIGURATION def get_subscription_id_list(prefix, **kwargs): #pylint: disable=unused-argument subscriptions = load_subscriptions() result = [] for subscription in subscriptions: result.append(subscription['id']) result.append(subscription['name']) return result password_type = CliArgumentType( options_list=('--password', '-p'), help='User password or client secret. Will prompt if not given.') service_principal_type = CliArgumentType( action='store_true', help='The credential representing a service principal.') subscription_name_or_id_type = CliArgumentType( options_list=('--subscription-name-or-id', '-n'), metavar='SUBSCRIPTION_NAME_OR_ID', help='Subscription id. Unique name also works.', completer=get_subscription_id_list) tenant_type = CliArgumentType( options_list=('--tenant', '-t'), help='The tenant associated with the service principal.')
'container': PublicAccess.Container } lease_duration_values = {'min': 15, 'max': 60, 'infinite': -1} lease_duration_values_string = 'Between {} and {} seconds. ({} for infinite)'.format( lease_duration_values['min'], lease_duration_values['max'], lease_duration_values['infinite']) blob_types = { 'block': BlockBlobService, 'page': PageBlobService, 'append': AppendBlobService } # PARAMETER TYPE DEFINITIONS account_name_type = CliArgumentType(options_list=('--account-name', '-n'), help='the storage account name') account_type_type = CliArgumentType(options_list=('--account-type', ), choices=choices_sku_name, help='the storage account type') blob_name_type = CliArgumentType(options_list=('--blob-name', '-b')) blob_type_type = CliArgumentType(options_list=('--blob-type', ), choices=list(blob_types.keys()), type=str.lower) container_name_type = CliArgumentType(options_list=('--container-name', '-c')) copy_source_type = CliArgumentType(options_list=('--source-uri', '-u'))
#--------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. #--------------------------------------------------------------------------------------------- from azure.cli.commands import register_cli_argument, CliArgumentType # BASIC PARAMETER CONFIGURATION register_cli_argument( 'component', 'component_name', CliArgumentType(options_list=('--name', '-n'), help='Name of component')) register_cli_argument( 'component', 'force', CliArgumentType(options_list=('--force', '-f'), help='Supress delete confirmation prompt', action='store_true')) register_cli_argument( 'component', 'link', CliArgumentType( options_list=('--link', '-l'), help= 'If a url or path to an html file, parse for links to archives. If local path or file://' 'url that\'s a directory, then look for archives in the directory listing.' )) register_cli_argument( 'component', 'private', CliArgumentType(options_list=('--private', '-p'), help='Get from the project PyPI server', action='store_true')) register_cli_argument(
from argcomplete.completers import FilesCompleter from azure.mgmt.resource.resources.models import DeploymentMode from azure.cli.commands import register_cli_argument, CliArgumentType from azure.cli.commands.parameters import (resource_group_name_type, tag_type, tags_type, get_resource_group_completion_list) from ._validators import validate_resource_type, validate_parent, resolve_resource_parameters # BASIC PARAMETER CONFIGURATION choices_deployment_mode = [e.value.lower() for e in DeploymentMode] resource_type_type = CliArgumentType( help='The resource type in <namespace>/<type> format.', type=validate_resource_type, validator=resolve_resource_parameters) register_cli_argument('resource', 'resource_name', CliArgumentType(options_list=('--name', '-n'))) register_cli_argument( 'resource', 'api_version', CliArgumentType(help='The api version of the resource (omit for latest)', required=False)) register_cli_argument('resource', 'resource_provider_namespace', CliArgumentType(help=argparse.SUPPRESS, required=False)) register_cli_argument('resource', 'resource_type', resource_type_type) register_cli_argument( 'resource', 'parent_resource_path', CliArgumentType(help='The parent resource type in <type>/<name> format.', type=validate_parent,