Merge pull request #142 from johanste/vmoperations

Virtual Machine patch operations (disk attach/detach) + network convenience function (vm get-ip-addresses)
This commit is contained in:
Johan Stenberg (MSFT) 2016-04-21 10:21:17 -07:00
Родитель 6e7a74da55 19a1aeab1f
Коммит 521835baf3
6 изменённых файлов: 370 добавлений и 130 удалений

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

@ -58,6 +58,7 @@
<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-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>
</Compile>
@ -125,6 +126,8 @@
<SubType>Code</SubType>
</Compile>
<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\__init__.py" />
<Compile Include="command_modules\azure-cli-vm\setup.py" />
</ItemGroup>

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

@ -46,4 +46,7 @@ def main(args, file=sys.stdout): #pylint: disable=redefined-builtin
return ex.args[1] if len(ex.args) >= 2 else -1
except KeyboardInterrupt:
return -1
except Exception as ex: # pylint: disable=broad-except
logger.error(ex)
return -1

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

@ -1,131 +1,5 @@
from azure.mgmt.compute import ComputeManagementClient, ComputeManagementClientConfiguration
from azure.mgmt.compute.operations import (AvailabilitySetsOperations,
VirtualMachineExtensionImagesOperations,
VirtualMachineExtensionsOperations,
VirtualMachineImagesOperations,
UsageOperations,
VirtualMachineSizesOperations,
VirtualMachinesOperations,
VirtualMachineScaleSetsOperations,
VirtualMachineScaleSetVMsOperations)
from .generated import command_table as generated_command_table
from .custom import command_table as convenience_command_table
from azure.cli.commands._command_creation import get_mgmt_service_client
from azure.cli.commands._auto_command import build_operation, AutoCommandDefinition
from azure.cli.commands import CommandTable, LongRunningOperation
from azure.cli._locale import L
def _compute_client_factory(_):
return get_mgmt_service_client(ComputeManagementClient, ComputeManagementClientConfiguration)
command_table = CommandTable()
# pylint: disable=line-too-long
build_operation("vm availabilityset",
"availability_sets",
_compute_client_factory,
[
AutoCommandDefinition(AvailabilitySetsOperations.delete, None),
AutoCommandDefinition(AvailabilitySetsOperations.get, 'AvailabilitySet'),
AutoCommandDefinition(AvailabilitySetsOperations.list, '[AvailabilitySet]'),
AutoCommandDefinition(AvailabilitySetsOperations.list_available_sizes, '[VirtualMachineSize]', 'list-sizes')
],
command_table)
build_operation("vm machineextensionimage",
"virtual_machine_extension_images",
_compute_client_factory,
[
AutoCommandDefinition(VirtualMachineExtensionImagesOperations.get, 'VirtualMachineExtensionImage'),
AutoCommandDefinition(VirtualMachineExtensionImagesOperations.list_types, '[VirtualMachineImageResource]'),
AutoCommandDefinition(VirtualMachineExtensionImagesOperations.list_versions, '[VirtualMachineImageResource]'),
],
command_table)
build_operation("vm extension",
"virtual_machine_extensions",
_compute_client_factory,
[
AutoCommandDefinition(VirtualMachineExtensionsOperations.delete, LongRunningOperation(L('Deleting VM extension'), L('VM extension deleted'))),
AutoCommandDefinition(VirtualMachineExtensionsOperations.get, 'VirtualMachineExtension'),
],
command_table)
build_operation("vm image",
"virtual_machine_images",
_compute_client_factory,
[
AutoCommandDefinition(VirtualMachineImagesOperations.get, 'VirtualMachineImage'),
AutoCommandDefinition(VirtualMachineImagesOperations.list, '[VirtualMachineImageResource]'),
AutoCommandDefinition(VirtualMachineImagesOperations.list_offers, '[VirtualMachineImageResource]'),
AutoCommandDefinition(VirtualMachineImagesOperations.list_publishers, '[VirtualMachineImageResource]'),
AutoCommandDefinition(VirtualMachineImagesOperations.list_skus, '[VirtualMachineImageResource]'),
],
command_table)
build_operation("vm usage",
"usage",
_compute_client_factory,
[
AutoCommandDefinition(UsageOperations.list, '[Usage]'),
],
command_table)
build_operation("vm size",
"virtual_machine_sizes",
_compute_client_factory,
[
AutoCommandDefinition(VirtualMachineSizesOperations.list, '[VirtualMachineSize]'),
],
command_table)
build_operation("vm",
"virtual_machines",
_compute_client_factory,
[
AutoCommandDefinition(VirtualMachinesOperations.delete, LongRunningOperation(L('Deleting VM'), L('VM Deleted'))),
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)
build_operation("vm scaleset",
"virtual_machine_scale_sets",
_compute_client_factory,
[
AutoCommandDefinition(VirtualMachineScaleSetsOperations.deallocate, LongRunningOperation(L('Deallocating VM scale set'), L('VM scale set deallocated'))),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.delete, LongRunningOperation(L('Deleting VM scale set'), L('VM scale set deleted'))),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.get, 'VirtualMachineScaleSet'),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.delete_instances, LongRunningOperation(L('Deleting VM scale set instances'), L('VM scale set instances deleted'))),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.get_instance_view, 'VirtualMachineScaleSetInstanceView'),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.list, '[VirtualMachineScaleSet]'),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.list_all, '[VirtualMachineScaleSet]'),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.list_skus, '[VirtualMachineScaleSet]'),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.power_off, LongRunningOperation(L('Powering off VM scale set'), L('VM scale set powered off'))),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.restart, LongRunningOperation(L('Restarting VM scale set'), L('VM scale set restarted'))),
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)
build_operation("vm scalesetvm",
"virtual_machine_scale_set_vms",
_compute_client_factory,
[
AutoCommandDefinition(VirtualMachineScaleSetVMsOperations.deallocate, LongRunningOperation(L('Deallocating VM scale set VMs'), L('VM scale set VMs deallocated'))),
AutoCommandDefinition(VirtualMachineScaleSetVMsOperations.delete, LongRunningOperation(L('Deleting VM scale set VMs'), L('VM scale set VMs deleted'))),
AutoCommandDefinition(VirtualMachineScaleSetVMsOperations.get, 'VirtualMachineScaleSetVM'),
AutoCommandDefinition(VirtualMachineScaleSetVMsOperations.get_instance_view, 'VirtualMachineScaleSetVMInstanceView'),
AutoCommandDefinition(VirtualMachineScaleSetVMsOperations.list, '[VirtualMachineScaleSetVM]'),
AutoCommandDefinition(VirtualMachineScaleSetVMsOperations.power_off, LongRunningOperation(L('Powering off VM scale set VMs'), L('VM scale set VMs powered off'))),
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 = generated_command_table
command_table.update(convenience_command_table)

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

@ -0,0 +1,200 @@
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._command_creation import get_mgmt_service_client
from azure.mgmt.compute import ComputeManagementClient, ComputeManagementClientConfiguration
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.
'''
client = _compute_client_factory(args)
result = client.virtual_machines.get(args.get('resourcegroup'), args.get('vm_name'))
return result
def vm_setter(args, instance, start_msg, end_msg):
'''Update the given Virtual Machine instance
'''
instance.resources = None # Issue: https://github.com/Azure/autorest/issues/934
client = _compute_client_factory(args)
poller = client.virtual_machines.create_or_update(
resource_group_name=args.get('resourcegroup'),
vm_name=args.get('vm_name'),
parameters=instance)
return LongRunningOperation(start_msg, end_msg)(poller)
def patches_vm(start_msg, finish_msg):
'''Decorator indicating that the decorated function modifies an existing Virtual Machine
in Azure.
It automatically adds arguments required to identify the Virtual Machine to be patched and
handles the actual put call to the compute service, leaving the decorated function to only
have to worry about the modifications it has to do.
'''
def wrapped(func):
def invoke(args):
instance = vm_getter(args)
func(args, instance)
vm_setter(args, instance, 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({
'name': '--vm-name -n',
'dest': 'vm_name',
'help': 'Name of Virtual Machine to update',
'required': True
})
return invoke
return wrapped
@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)
@patches_vm('Attaching disk', 'Disk attached')
def _vm_disk_attach_new(args, instance):
disk = DataDisk(lun=args.get('lun'),
vhd=args.get('vhd'),
name=args.get('name'),
create_option=DiskCreateOptionTypes.empty,
disk_size_gb=args.get('disksize'))
instance.storage_profile.data_disks.append(disk)
@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)
@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
disk = DataDisk(lun=args.get('lun'),
vhd=args.get('vhd'),
name=args.get('name'),
create_option=DiskCreateOptionTypes.attach,
disk_size_gb=args.get('disksize'))
instance.storage_profile.data_disks.append(disk)
@command_table.command('vm disk detach')
@command_table.option('--diskname', dest='name', help='Disk name', required=True)
@patches_vm('Detaching disk', 'Disk detached')
def _vm_disk_detach(args, instance):
instance.resources = None # Issue: https://github.com/Azure/autorest/issues/934
try:
disk = next(d for d in instance.storage_profile.data_disks
if d.name == args.get('name'))
instance.storage_profile.data_disks.remove(disk)
except StopIteration:
raise RuntimeError("No disk with the name '%s' found" % args.get('name'))
#
# Composite convenience commands for the CLI
#
def _parse_rg_name(strid):
'''From an ID, extract the contained (resource group, name) tuple
'''
import re
parts = re.split('/', strid)
if parts[3] != 'resourceGroups':
raise KeyError()
return (parts[4], parts[8])
@command_table.command('vm get-ip-addresses')
@command_table.option('-g --resource_group_name', required=False)
@command_table.option('-n --vm-name', required=False)
def _vm_get_ip_addresses(args):
from azure.mgmt.network import NetworkManagementClient, NetworkManagementClientConfiguration
# We start by getting NICs as they are the smack in the middle of all data that we
# want to collect for a VM (as long as we don't need any info on the VM than what
# is available in the Id, we don't need to make any calls to the compute RP)
#
# Since there is no guarantee that a NIC is in the same resource group as a given
# Virtual Machine, we can't constrain the lookup to only a single group...
network_client = get_mgmt_service_client(NetworkManagementClient,
NetworkManagementClientConfiguration)
nics = network_client.network_interfaces.list_all()
public_ip_addresses = network_client.public_ip_addresses.list_all()
ip_address_lookup = {pip.id: pip for pip in list(public_ip_addresses)}
result = []
for nic in [n for n in list(nics) if n.virtual_machine]:
resource_group, vm_name = _parse_rg_name(nic.virtual_machine.id)
# If provided, make sure that resource group name and vm name match the NIC we are
# looking at before adding it to the result...
if (args.get('resource_group_name') in (None, resource_group)
and args.get('vm_name') in (None, vm_name)):
network_info = {
'privateIpAddresses': [],
'publicIpAddresses': []
}
for ip_configuration in nic.ip_configurations:
network_info['privateIpAddresses'].append(ip_configuration.private_ip_address)
if ip_configuration.public_ip_address:
public_ip_address = ip_address_lookup[ip_configuration.public_ip_address.id]
network_info['publicIpAddresses'].append({
'id': public_ip_address.id,
'name': public_ip_address.name,
'ipAddress': public_ip_address.ip_address,
'ipAllocationMethod': public_ip_address.public_ip_allocation_method
})
result.append({
'virtualMachine': {
'resourceGroup': resource_group,
'name': vm_name,
'network': network_info
}
})
return result

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

@ -0,0 +1,131 @@
from azure.mgmt.compute import ComputeManagementClient, ComputeManagementClientConfiguration
from azure.mgmt.compute.operations import (AvailabilitySetsOperations,
VirtualMachineExtensionImagesOperations,
VirtualMachineExtensionsOperations,
VirtualMachineImagesOperations,
UsageOperations,
VirtualMachineSizesOperations,
VirtualMachinesOperations,
VirtualMachineScaleSetsOperations,
VirtualMachineScaleSetVMsOperations)
from azure.cli.commands._command_creation import get_mgmt_service_client
from azure.cli.commands._auto_command import build_operation, AutoCommandDefinition
from azure.cli.commands import CommandTable, LongRunningOperation
from azure.cli._locale import L
command_table = CommandTable()
def _compute_client_factory(_):
return get_mgmt_service_client(ComputeManagementClient, ComputeManagementClientConfiguration)
# pylint: disable=line-too-long
build_operation("vm availabilityset",
"availability_sets",
_compute_client_factory,
[
AutoCommandDefinition(AvailabilitySetsOperations.delete, None),
AutoCommandDefinition(AvailabilitySetsOperations.get, 'AvailabilitySet'),
AutoCommandDefinition(AvailabilitySetsOperations.list, '[AvailabilitySet]'),
AutoCommandDefinition(AvailabilitySetsOperations.list_available_sizes, '[VirtualMachineSize]', 'list-sizes')
],
command_table)
build_operation("vm machineextensionimage",
"virtual_machine_extension_images",
_compute_client_factory,
[
AutoCommandDefinition(VirtualMachineExtensionImagesOperations.get, 'VirtualMachineExtensionImage'),
AutoCommandDefinition(VirtualMachineExtensionImagesOperations.list_types, '[VirtualMachineImageResource]'),
AutoCommandDefinition(VirtualMachineExtensionImagesOperations.list_versions, '[VirtualMachineImageResource]'),
],
command_table)
build_operation("vm extension",
"virtual_machine_extensions",
_compute_client_factory,
[
AutoCommandDefinition(VirtualMachineExtensionsOperations.delete, LongRunningOperation(L('Deleting VM extension'), L('VM extension deleted'))),
AutoCommandDefinition(VirtualMachineExtensionsOperations.get, 'VirtualMachineExtension'),
],
command_table)
build_operation("vm image",
"virtual_machine_images",
_compute_client_factory,
[
AutoCommandDefinition(VirtualMachineImagesOperations.get, 'VirtualMachineImage'),
AutoCommandDefinition(VirtualMachineImagesOperations.list, '[VirtualMachineImageResource]'),
AutoCommandDefinition(VirtualMachineImagesOperations.list_offers, '[VirtualMachineImageResource]'),
AutoCommandDefinition(VirtualMachineImagesOperations.list_publishers, '[VirtualMachineImageResource]'),
AutoCommandDefinition(VirtualMachineImagesOperations.list_skus, '[VirtualMachineImageResource]'),
],
command_table)
build_operation("vm usage",
"usage",
_compute_client_factory,
[
AutoCommandDefinition(UsageOperations.list, '[Usage]'),
],
command_table)
build_operation("vm size",
"virtual_machine_sizes",
_compute_client_factory,
[
AutoCommandDefinition(VirtualMachineSizesOperations.list, '[VirtualMachineSize]'),
],
command_table)
build_operation("vm",
"virtual_machines",
_compute_client_factory,
[
AutoCommandDefinition(VirtualMachinesOperations.delete, LongRunningOperation(L('Deleting VM'), L('VM Deleted'))),
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)
build_operation("vm scaleset",
"virtual_machine_scale_sets",
_compute_client_factory,
[
AutoCommandDefinition(VirtualMachineScaleSetsOperations.deallocate, LongRunningOperation(L('Deallocating VM scale set'), L('VM scale set deallocated'))),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.delete, LongRunningOperation(L('Deleting VM scale set'), L('VM scale set deleted'))),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.get, 'VirtualMachineScaleSet'),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.delete_instances, LongRunningOperation(L('Deleting VM scale set instances'), L('VM scale set instances deleted'))),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.get_instance_view, 'VirtualMachineScaleSetInstanceView'),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.list, '[VirtualMachineScaleSet]'),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.list_all, '[VirtualMachineScaleSet]'),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.list_skus, '[VirtualMachineScaleSet]'),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.power_off, LongRunningOperation(L('Powering off VM scale set'), L('VM scale set powered off'))),
AutoCommandDefinition(VirtualMachineScaleSetsOperations.restart, LongRunningOperation(L('Restarting VM scale set'), L('VM scale set restarted'))),
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)
build_operation("vm scalesetvm",
"virtual_machine_scale_set_vms",
_compute_client_factory,
[
AutoCommandDefinition(VirtualMachineScaleSetVMsOperations.deallocate, LongRunningOperation(L('Deallocating VM scale set VMs'), L('VM scale set VMs deallocated'))),
AutoCommandDefinition(VirtualMachineScaleSetVMsOperations.delete, LongRunningOperation(L('Deleting VM scale set VMs'), L('VM scale set VMs deleted'))),
AutoCommandDefinition(VirtualMachineScaleSetVMsOperations.get, 'VirtualMachineScaleSetVM'),
AutoCommandDefinition(VirtualMachineScaleSetVMsOperations.get_instance_view, 'VirtualMachineScaleSetVMInstanceView'),
AutoCommandDefinition(VirtualMachineScaleSetVMsOperations.list, '[VirtualMachineScaleSetVM]'),
AutoCommandDefinition(VirtualMachineScaleSetVMsOperations.power_off, LongRunningOperation(L('Powering off VM scale set VMs'), L('VM scale set VMs powered off'))),
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)

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

@ -0,0 +1,29 @@
import json
import unittest
from azure.cli.command_modules.vm.custom import MinMaxValue
class Test_Vm_Custom(unittest.TestCase):
@classmethod
def setUpClass(cls):
pass
def test_custom_minmax(self):
validator = MinMaxValue(1, 3)
self.assertEqual(1, validator(1))
self.assertEqual(2, validator(2))
self.assertEqual(3, validator(3))
self.assertEqual(1, validator('1'))
self.assertEqual(2, validator('2'))
self.assertEqual(3, validator('3'))
with self.assertRaises(ValueError):
validator(0)
with self.assertRaises(ValueError):
validator('0')
with self.assertRaises(ValueError):
validator(4)
with self.assertRaises(ValueError):
validator('4')
if __name__ == '__main__':
unittest.main()