Combine list and list-all VM commands.

This commit is contained in:
Travis Prescott 2016-04-26 09:47:28 -07:00
Родитель 76298c4830
Коммит 2b03f7a769
8 изменённых файлов: 109 добавлений и 67 удалений

Просмотреть файл

@ -58,6 +58,9 @@
<Compile Include="command_modules\azure-cli-network\azure\cli\command_modules\network\mgmt\__init__.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\tests\test_storage_validators.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\_params.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\_validators.py" />
<Compile Include="command_modules\azure-cli-vm\azure\cli\command_modules\vm\tests\test_custom_vm_commands.py" />
<Compile Include="command_modules\azure-cli-resource\azure\cli\command_modules\resource\tests\test_api_check.py">
<SubType>Code</SubType>
@ -128,6 +131,12 @@
<Compile Include="command_modules\azure-cli-vm\azure\cli\command_modules\vm\tests\test_commands.py" />
<Compile Include="command_modules\azure-cli-vm\azure\cli\command_modules\vm\generated.py" />
<Compile Include="command_modules\azure-cli-vm\azure\cli\command_modules\vm\custom.py" />
<Compile Include="command_modules\azure-cli-vm\azure\cli\command_modules\vm\_validators.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="command_modules\azure-cli-vm\azure\cli\command_modules\vm\_params.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="command_modules\azure-cli-vm\azure\cli\command_modules\vm\__init__.py" />
<Compile Include="command_modules\azure-cli-vm\setup.py" />
</ItemGroup>

Просмотреть файл

@ -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

Просмотреть файл

@ -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')

Просмотреть файл

@ -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
},
})

Просмотреть файл

@ -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))

Просмотреть файл

@ -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

Просмотреть файл

@ -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)

Просмотреть файл

@ -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):