From 2b03f7a769fc663c1d731e9afb26331dbfc00d0b Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Tue, 26 Apr 2016 09:47:28 -0700 Subject: [PATCH] Combine list and list-all VM commands. --- azure-cli.pyproj | 9 +++ src/azure/cli/commands/__init__.py | 5 ++ .../cli/command_modules/storage/_params.py | 7 +- .../azure/cli/command_modules/vm/_params.py | 38 ++++++++++ .../cli/command_modules/vm/_validators.py | 19 +++++ .../azure/cli/command_modules/vm/custom.py | 74 +++++++------------ .../azure/cli/command_modules/vm/generated.py | 22 +++--- .../vm/tests/test_custom_vm_commands.py | 2 +- 8 files changed, 109 insertions(+), 67 deletions(-) create mode 100644 src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/_params.py create mode 100644 src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/_validators.py diff --git a/azure-cli.pyproj b/azure-cli.pyproj index b87aa5493..5d08163a7 100644 --- a/azure-cli.pyproj +++ b/azure-cli.pyproj @@ -58,6 +58,9 @@ Code + + + Code @@ -128,6 +131,12 @@ + + Code + + + Code + diff --git a/src/azure/cli/commands/__init__.py b/src/azure/cli/commands/__init__.py index 187895285..2294337ae 100644 --- a/src/azure/cli/commands/__init__.py +++ b/src/azure/cli/commands/__init__.py @@ -33,6 +33,11 @@ COMMON_PARAMETERS = { } } +def extend_parameter(parameter_metadata, **kwargs): + modified_parameter_metadata = parameter_metadata.copy() + modified_parameter_metadata.update(kwargs) + return modified_parameter_metadata + class LongRunningOperation(object): #pylint: disable=too-few-public-methods progress_file = sys.stderr diff --git a/src/command_modules/azure-cli-storage/azure/cli/command_modules/storage/_params.py b/src/command_modules/azure-cli-storage/azure/cli/command_modules/storage/_params.py index 3cd1dea39..a224a324e 100644 --- a/src/command_modules/azure-cli-storage/azure/cli/command_modules/storage/_params.py +++ b/src/command_modules/azure-cli-storage/azure/cli/command_modules/storage/_params.py @@ -1,6 +1,6 @@ from os import environ -from azure.cli.commands import COMMON_PARAMETERS as GLOBAL_COMMON_PARAMETERS +from azure.cli.commands import (COMMON_PARAMETERS as GLOBAL_COMMON_PARAMETERS, extend_parameter) from azure.cli._locale import L from ._validators import ( @@ -10,11 +10,6 @@ from ._validators import ( # HELPER METHODS -def extend_parameter(parameter_metadata, **kwargs): - modified_parameter_metadata = parameter_metadata.copy() - modified_parameter_metadata.update(kwargs) - return modified_parameter_metadata - def get_account_name(string): return string if string != 'query' else environ.get('AZURE_STORAGE_ACCOUNT') 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 new file mode 100644 index 000000000..1f7060e64 --- /dev/null +++ b/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/_params.py @@ -0,0 +1,38 @@ +from os import environ + +from azure.mgmt.compute.models import DataDisk, VirtualHardDisk + +from azure.cli.commands import (COMMON_PARAMETERS as GLOBAL_COMMON_PARAMETERS, extend_parameter) +from azure.cli._locale import L +from azure.cli.command_modules.vm._validators import MinMaxValue + +PARAMETER_ALIASES = GLOBAL_COMMON_PARAMETERS.copy() +PARAMETER_ALIASES.update({ + 'diskname': { + 'name': '--diskname', + 'dest': 'name', + 'help': L('Disk name'), + 'required': True + }, + 'disksize': { + 'name': '--disksize', + 'dest': 'disksize', + 'help': L('Size of disk (Gb)'), + 'type': MinMaxValue(1, 1023), + 'default': 1023 + }, + 'lun': { + 'name': '--lun', + 'dest': 'lun', + 'help': L('0-based logical unit number (LUN). Max value depend on the Virtual Machine size'), + 'type': int, + 'required': True + }, + 'optional_resource_group_name': + extend_parameter(GLOBAL_COMMON_PARAMETERS['resource_group_name'], required=False), + 'vhd': { + 'name': '--vhd', + 'required': True, + 'type': VirtualHardDisk + }, +}) diff --git a/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/_validators.py b/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/_validators.py new file mode 100644 index 000000000..69cbd5017 --- /dev/null +++ b/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/_validators.py @@ -0,0 +1,19 @@ +class MinMaxValue(object): # pylint: disable=too-few-public-methods + '''Converter/validator for range type values. Intended use is as the type parameter + for argparse options + ''' + def __init__(self, min_value, max_value, value_type=int): + self.min_value = min_value + self.max_value = max_value + self.value_type = value_type + + def __call__(self, strvalue): + value = self.value_type(strvalue) + if value < self.min_value or value > self.max_value: + raise ValueError() + return value + + def __repr__(self): + '''Used by argparse to display error messages + ''' + return 'value. Valid values: %s - %s, given' % (str(self.min_value), str(self.max_value)) \ No newline at end of file diff --git a/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/custom.py b/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/custom.py index fbda25215..9d209dfa8 100644 --- a/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/custom.py +++ b/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/custom.py @@ -1,52 +1,17 @@ from azure.mgmt.compute.models import DataDisk, VirtualHardDisk from azure.mgmt.compute.models.compute_management_client_enums import DiskCreateOptionTypes from azure.cli._locale import L -from azure.cli.commands import CommandTable, COMMON_PARAMETERS, LongRunningOperation +from azure.cli.commands import (CommandTable, LongRunningOperation, extend_parameter) from azure.cli.commands._command_creation import get_mgmt_service_client from azure.mgmt.compute import ComputeManagementClient, ComputeManagementClientConfiguration +from ._params import PARAMETER_ALIASES + def _compute_client_factory(_): return get_mgmt_service_client(ComputeManagementClient, ComputeManagementClientConfiguration) command_table = CommandTable() -class MinMaxValue(object): # pylint: disable=too-few-public-methods - '''Converter/validator for range type values. Intended use is as the type parameter - for argparse options - ''' - def __init__(self, min_value, max_value, value_type=int): - self.min_value = min_value - self.max_value = max_value - self.value_type = value_type - - def __call__(self, strvalue): - value = self.value_type(strvalue) - if value < self.min_value or value > self.max_value: - raise ValueError() - return value - - def __repr__(self): - '''Used by argparse to display error messages - ''' - return 'value. Valid values: %s - %s, given' % (str(self.min_value), str(self.max_value)) - - -LUN_PARAMETER = { - 'name': '--lun', - 'dest': 'lun', - 'help': '0-based logical unit number (LUN). Max value depend on the Virtual Machine size', - 'type': int, - 'required': True - } - -DISKSIZE_PARAMETER = { - 'name': '--disksize', - 'dest': 'disksize', - 'help': 'Size of disk (Gb)', - 'type': MinMaxValue(1, 1023), - 'default': 1023 - } - def vm_getter(args): ''' Retreive a VM based on the `args` passed in. ''' @@ -80,7 +45,7 @@ def patches_vm(start_msg, finish_msg): # All Virtual Machines are identified with a resource group name/name pair, so # we add these parameters to all commands - command_table[invoke]['arguments'].append(COMMON_PARAMETERS['resource_group_name']) + command_table[invoke]['arguments'].append(PARAMETER_ALIASES['resource_group_name']) command_table[invoke]['arguments'].append({ 'name': '--vm-name -n', 'dest': 'vm_name', @@ -90,12 +55,23 @@ def patches_vm(start_msg, finish_msg): return invoke return wrapped +@command_table.command('vm list', description=L('List Virtual Machines.')) +@command_table.option(**PARAMETER_ALIASES['optional_resource_group_name']) +def list_vm(args): + ccf = _compute_client_factory({}) + group = args.get('resourcegroup') + if group: + vm_list = ccf.virtual_machines.list(resource_group_name=group) + else: + vm_list = ccf.virtual_machines.list_all() + return list(vm_list) + @command_table.command('vm disk attach-new', help=L('Attach a new disk to an existing Virtual Machine')) -@command_table.option(**LUN_PARAMETER) -@command_table.option('--diskname', dest='name', help='Disk name', required=True) -@command_table.option(**DISKSIZE_PARAMETER) -@command_table.option('--vhd', required=True, type=VirtualHardDisk) +@command_table.option(**PARAMETER_ALIASES['lun']) +@command_table.option(**PARAMETER_ALIASES['diskname']) +@command_table.option(**PARAMETER_ALIASES['disksize']) +@command_table.option(**PARAMETER_ALIASES['vhd']) @patches_vm('Attaching disk', 'Disk attached') def _vm_disk_attach_new(args, instance): disk = DataDisk(lun=args.get('lun'), @@ -107,10 +83,10 @@ def _vm_disk_attach_new(args, instance): @command_table.command('vm disk attach-existing', help=L('Attach an existing disk to an existing Virtual Machine')) -@command_table.option(**LUN_PARAMETER) -@command_table.option('--diskname', dest='name', help='Disk name', required=True) -@command_table.option('--vhd', required=True, type=VirtualHardDisk) -@command_table.option(**DISKSIZE_PARAMETER) +@command_table.option(**PARAMETER_ALIASES['lun']) +@command_table.option(**PARAMETER_ALIASES['diskname']) +@command_table.option(**PARAMETER_ALIASES['disksize']) +@command_table.option(**PARAMETER_ALIASES['vhd']) @patches_vm('Attaching disk', 'Disk attached') def _vm_disk_attach_existing(args, instance): # TODO: figure out size of existing disk instead of making the default value 1023 @@ -122,7 +98,7 @@ def _vm_disk_attach_existing(args, instance): instance.storage_profile.data_disks.append(disk) @command_table.command('vm disk detach') -@command_table.option('--diskname', dest='name', help='Disk name', required=True) +@command_table.option(**PARAMETER_ALIASES['diskname']) @patches_vm('Detaching disk', 'Disk detached') def _vm_disk_detach(args, instance): instance.resources = None # Issue: https://github.com/Azure/autorest/issues/934 @@ -147,7 +123,7 @@ def _parse_rg_name(strid): return (parts[4], parts[8]) @command_table.command('vm get-ip-addresses') -@command_table.option('-g --resource_group_name', required=False) +@command_table.option(**PARAMETER_ALIASES['optional_resource_group_name']) @command_table.option('-n --vm-name', required=False) def _vm_get_ip_addresses(args): from azure.mgmt.network import NetworkManagementClient, NetworkManagementClientConfiguration diff --git a/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/generated.py b/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/generated.py index 510f6da6c..1f3a6363e 100644 --- a/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/generated.py +++ b/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/generated.py @@ -14,6 +14,8 @@ from azure.cli.commands._auto_command import build_operation, AutoCommandDefinit from azure.cli.commands import CommandTable, LongRunningOperation from azure.cli._locale import L +from ._params import PARAMETER_ALIASES + command_table = CommandTable() def _compute_client_factory(_): @@ -29,7 +31,7 @@ build_operation("vm availabilityset", AutoCommandDefinition(AvailabilitySetsOperations.list, '[AvailabilitySet]'), AutoCommandDefinition(AvailabilitySetsOperations.list_available_sizes, '[VirtualMachineSize]', 'list-sizes') ], - command_table) + command_table, PARAMETER_ALIASES) build_operation("vm machineextensionimage", @@ -40,7 +42,7 @@ build_operation("vm machineextensionimage", AutoCommandDefinition(VirtualMachineExtensionImagesOperations.list_types, '[VirtualMachineImageResource]'), AutoCommandDefinition(VirtualMachineExtensionImagesOperations.list_versions, '[VirtualMachineImageResource]'), ], - command_table) + command_table, PARAMETER_ALIASES) build_operation("vm extension", "virtual_machine_extensions", @@ -49,7 +51,7 @@ build_operation("vm extension", AutoCommandDefinition(VirtualMachineExtensionsOperations.delete, LongRunningOperation(L('Deleting VM extension'), L('VM extension deleted'))), AutoCommandDefinition(VirtualMachineExtensionsOperations.get, 'VirtualMachineExtension'), ], - command_table) + command_table, PARAMETER_ALIASES) build_operation("vm image", "virtual_machine_images", @@ -61,7 +63,7 @@ build_operation("vm image", AutoCommandDefinition(VirtualMachineImagesOperations.list_publishers, '[VirtualMachineImageResource]'), AutoCommandDefinition(VirtualMachineImagesOperations.list_skus, '[VirtualMachineImageResource]'), ], - command_table) + command_table, PARAMETER_ALIASES) build_operation("vm usage", "usage", @@ -69,7 +71,7 @@ build_operation("vm usage", [ AutoCommandDefinition(UsageOperations.list, '[Usage]'), ], - command_table) + command_table, PARAMETER_ALIASES) build_operation("vm size", "virtual_machine_sizes", @@ -77,7 +79,7 @@ build_operation("vm size", [ AutoCommandDefinition(VirtualMachineSizesOperations.list, '[VirtualMachineSize]'), ], - command_table) + command_table, PARAMETER_ALIASES) build_operation("vm", "virtual_machines", @@ -87,14 +89,12 @@ build_operation("vm", AutoCommandDefinition(VirtualMachinesOperations.deallocate, LongRunningOperation(L('Deallocating VM'), L('VM Deallocated'))), AutoCommandDefinition(VirtualMachinesOperations.generalize, None), AutoCommandDefinition(VirtualMachinesOperations.get, 'VirtualMachine'), - AutoCommandDefinition(VirtualMachinesOperations.list, '[VirtualMachine]'), - AutoCommandDefinition(VirtualMachinesOperations.list_all, '[VirtualMachine]'), AutoCommandDefinition(VirtualMachinesOperations.list_available_sizes, '[VirtualMachineSize]', 'list-sizes'), AutoCommandDefinition(VirtualMachinesOperations.power_off, LongRunningOperation(L('Powering off VM'), L('VM powered off'))), AutoCommandDefinition(VirtualMachinesOperations.restart, LongRunningOperation(L('Restarting VM'), L('VM Restarted'))), AutoCommandDefinition(VirtualMachinesOperations.start, LongRunningOperation(L('Starting VM'), L('VM Started'))), ], - command_table) + command_table, PARAMETER_ALIASES) build_operation("vm scaleset", "virtual_machine_scale_sets", @@ -113,7 +113,7 @@ build_operation("vm scaleset", AutoCommandDefinition(VirtualMachineScaleSetsOperations.start, LongRunningOperation(L('Starting VM scale set'), L('VM scale set started'))), AutoCommandDefinition(VirtualMachineScaleSetsOperations.update_instances, LongRunningOperation(L('Updating VM scale set instances'), L('VM scale set instances updated'))), ], - command_table) + command_table, PARAMETER_ALIASES) build_operation("vm scalesetvm", "virtual_machine_scale_set_vms", @@ -128,4 +128,4 @@ build_operation("vm scalesetvm", AutoCommandDefinition(VirtualMachineScaleSetVMsOperations.restart, LongRunningOperation(L('Restarting VM scale set VMs'), L('VM scale set VMs restarted'))), AutoCommandDefinition(VirtualMachineScaleSetVMsOperations.start, LongRunningOperation(L('Starting VM scale set VMs'), L('VM scale set VMs started'))), ], - command_table) + command_table, PARAMETER_ALIASES) diff --git a/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/tests/test_custom_vm_commands.py b/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/tests/test_custom_vm_commands.py index dffa0c032..51077643c 100644 --- a/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/tests/test_custom_vm_commands.py +++ b/src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/tests/test_custom_vm_commands.py @@ -1,6 +1,6 @@ import json import unittest -from azure.cli.command_modules.vm.custom import MinMaxValue +from azure.cli.command_modules.vm._validators import MinMaxValue class Test_Vm_Custom(unittest.TestCase):