Skip to content

Commit

Permalink
Merge pull request Azure#389 from derekbekoe/completer-improvements-2
Browse files Browse the repository at this point in the history
Resource name value completion (for VM commands right now) and VM size value completion
  • Loading branch information
johanste authored Jun 10, 2016
2 parents fbbe372 + 4b7fead commit dfdbd5f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/azure/cli/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ def __init__(self):

def register_cli_argument(self, scope, dest, argtype, options_list, **kwargs):
argument = CliArgumentType(options_list=options_list, overrides=argtype,
completer=argtype.completer, validator=argtype.validator,
completer=kwargs.pop('completer', argtype.completer),
validator=kwargs.pop('validator', argtype.validator),
**kwargs)
self.arguments[scope][dest] = argument

Expand Down
27 changes: 27 additions & 0 deletions src/azure/cli/commands/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# pylint: disable=line-too-long
from azure.cli.commands import CliArgumentType, register_cli_argument
from azure.cli.commands.validators import validate_tag, validate_tags
from azure.cli._util import CLIError
from azure.cli.commands.client_factory import (get_subscription_service_client,
get_mgmt_service_client)
from azure.mgmt.resource.subscriptions import (SubscriptionClient,
Expand All @@ -21,6 +22,13 @@ def get_location_completion_list(prefix, **kwargs):#pylint: disable=unused-argum
result = get_subscription_locations()
return [l.name for l in result]

def get_one_of_subscription_locations():
result = get_subscription_locations()
if result:
return next((r.name for r in result if r.name.lower() == 'westus'), result[0].name)
else:
raise CLIError('Current subscription does not have valid location list')

def get_resource_groups():
rcf = get_mgmt_service_client(ResourceManagementClient, ResourceManagementClientConfiguration)
return list(rcf.resource_groups.list())
Expand All @@ -29,6 +37,25 @@ def get_resource_group_completion_list(prefix, **kwargs):#pylint: disable=unused
result = get_resource_groups()
return [l.name for l in result]

def get_resources_in_resource_group(resource_group_name, resource_type=None):
rcf = get_mgmt_service_client(ResourceManagementClient, ResourceManagementClientConfiguration)
filter_str = "resourceType eq '{}'".format(resource_type) if resource_type else None
return list(rcf.resource_groups.list_resources(resource_group_name, filter=filter_str))

def get_resources_in_subscription(resource_type=None):
rcf = get_mgmt_service_client(ResourceManagementClient, ResourceManagementClientConfiguration)
filter_str = "resourceType eq '{}'".format(resource_type) if resource_type else None
return list(rcf.resources.list(filter=filter_str))

def get_resource_name_completion_list(resource_type=None):
def completer(prefix, action, parsed_args, **kwargs):#pylint: disable=unused-argument
if parsed_args.resource_group_name:
rg = parsed_args.resource_group_name
return [r.name for r in get_resources_in_resource_group(rg, resource_type=resource_type)]
else:
return [r.name for r in get_resources_in_subscription(resource_type=resource_type)]
return completer

resource_group_name_type = CliArgumentType(
options_list=('--resource-group', '-g'),
completer=get_resource_group_completion_list,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

from azure.cli._util import CLIError
from azure.cli.application import APPLICATION
from azure.cli.commands.parameters import get_one_of_subscription_locations

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

from ._factory import _compute_client_factory, _subscription_client_factory
from ._factory import _compute_client_factory
from ._vm_utils import read_content_if_is_file

class VMImageFieldAction(argparse.Action): #pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -202,17 +203,8 @@ def _load_extension_images_from_publisher(publisher):

return all_images

def get_subscription_locations():
subscription_client, subscription_id = _subscription_client_factory()
return list(subscription_client.subscriptions.list_locations(subscription_id))

def get_one_of_subscription_locations():
result = get_subscription_locations()
if result:
return next((r.name for r in result if r.name.lower() == 'westus'), result[0].name)
else:
#this should never happen, just in case
raise CLIError('Current subscription does not have valid location list')
def get_vm_sizes(location):
return list(_compute_client_factory().virtual_machine_sizes.list(location))

def _partial_matched(pattern, string):
if not pattern:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@
VMSSHFieldAction,
VMDNSNameAction,
load_images_from_aliases_doc,
get_subscription_locations)
from azure.cli.commands.parameters import location_type
get_vm_sizes)
from azure.cli.commands.parameters import (location_type,
get_location_completion_list,
get_one_of_subscription_locations,
get_resource_name_completion_list)
from azure.cli.commands import register_cli_argument, CliArgumentType, register_extra_cli_argument

def get_location_completion_list(prefix, **kwargs):#pylint: disable=unused-argument
result = get_subscription_locations()
return [l.name for l in result]

def get_urn_aliases_completion_list(prefix, **kwargs):#pylint: disable=unused-argument
images = load_images_from_aliases_doc()
return [i['urn alias'] for i in images]

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
name_arg_type = CliArgumentType(options_list=('--name', '-n'), metavar='NAME')
multi_ids_type = CliArgumentType(
Expand All @@ -30,17 +34,19 @@ def get_urn_aliases_completion_list(prefix, **kwargs):#pylint: disable=unused-ar

admin_username_type = CliArgumentType(options_list=('--admin-username',), default=getpass.getuser(), required=False)

register_cli_argument('vm', 'vm_name', name_arg_type, help='The name of the virtual machine')
register_cli_argument('vm scaleset', 'vm_scale_set_name', name_arg_type)
register_cli_argument('vm', 'vm_name', name_arg_type, help='The name of the virtual machine', completer=get_resource_name_completion_list('Microsoft.Compute/virtualMachines'))
register_cli_argument('vm', 'size', CliArgumentType(completer=get_vm_size_completion_list))

register_cli_argument('vm scaleset', 'vm_scale_set_name', name_arg_type, completer=get_resource_name_completion_list('Microsoft.Compute/virtualMachineScaleSets'))
register_cli_argument('vm scaleset', 'virtual_machine_scale_set_name', name_arg_type)
register_cli_argument('vm scaleset', 'instance_ids', multi_ids_type)
register_cli_argument('vm', 'diskname', CliArgumentType(options_list=('--name', '-n')))
register_cli_argument('vm', 'disksize', CliArgumentType(help='Size of disk (Gb)', default=1023, type=MinMaxValue(1, 1023)))
register_cli_argument('vm', 'lun', CliArgumentType(
type=int, help='0-based logical unit number (LUN). Max value depends on the Virutal Machine size.'))
type=int, help='0-based logical unit number (LUN). Max value depends on the Virtual Machine size.'))
register_cli_argument('vm', 'vhd', CliArgumentType(type=VirtualHardDisk))

register_cli_argument('vm availability-set', 'availability_set_name', name_arg_type)
register_cli_argument('vm availability-set', 'availability_set_name', name_arg_type, completer=get_resource_name_completion_list('Microsoft.Compute/availabilitySets'))

register_cli_argument('vm access', 'username', CliArgumentType(options_list=('--username', '-u'), help='The user name'))
register_cli_argument('vm access', 'password', CliArgumentType(options_list=('--password', '-p'), help='The user password'))
Expand All @@ -54,6 +60,7 @@ def get_urn_aliases_completion_list(prefix, **kwargs):#pylint: disable=unused-ar
default=os.path.join(os.path.expanduser('~'), '.ssh', 'id_rsa.pub')
)
)
register_cli_argument('vm container create', 'agent_vm_size', CliArgumentType(completer=get_vm_size_completion_list))

register_cli_argument('vm capture', 'overwrite', CliArgumentType(action='store_true'))
register_cli_argument('vm nic', 'nic_ids', multi_ids_type)
Expand Down

0 comments on commit dfdbd5f

Please sign in to comment.