Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/connectedvmware/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Release History
* Added delete-from-host flag for `vm delete`
* Deprecated VM List option as VM Instance is a child resource of Machines.
* Updated tests and helps accordingly.
* raising better exception types instead of CLIError

0.1.12
++++++
Expand Down
99 changes: 56 additions & 43 deletions src/connectedvmware/azext_connectedvmware/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@

from azure.cli.command_modules.acs._client_factory import get_resources_client
from azure.cli.core.util import sdk_no_wait
from azure.cli.core.azclierror import (
UnrecognizedArgumentError,
RequiredArgumentMissingError,
MutuallyExclusiveArgumentError,
InvalidArgumentValueError,
)
from azure.core.exceptions import ResourceNotFoundError # type: ignore
from knack.util import CLIError
from msrestazure.tools import is_valid_resource_id

from .pwinput import pwinput
Expand Down Expand Up @@ -277,8 +282,8 @@ def create_resource_pool(
VMWARE_NAMESPACE,
VCENTER_RESOURCE_TYPE,
vcenter,
INVENTORY_ITEM_TYPE,
inventory_item,
child_type_1=INVENTORY_ITEM_TYPE,
child_name_1=inventory_item,
)

resource_pool = ResourcePool(
Expand Down Expand Up @@ -356,8 +361,8 @@ def create_cluster(
VMWARE_NAMESPACE,
VCENTER_RESOURCE_TYPE,
vcenter,
INVENTORY_ITEM_TYPE,
inventory_item,
child_type_1=INVENTORY_ITEM_TYPE,
child_name_1=inventory_item,
)

cluster = Cluster(
Expand Down Expand Up @@ -435,8 +440,8 @@ def create_datastore(
VMWARE_NAMESPACE,
VCENTER_RESOURCE_TYPE,
vcenter,
INVENTORY_ITEM_TYPE,
inventory_item,
child_type_1=INVENTORY_ITEM_TYPE,
child_name_1=inventory_item,
)

datastore = Datastore(
Expand Down Expand Up @@ -514,8 +519,8 @@ def create_host(
VMWARE_NAMESPACE,
VCENTER_RESOURCE_TYPE,
vcenter,
INVENTORY_ITEM_TYPE,
inventory_item,
child_type_1=INVENTORY_ITEM_TYPE,
child_name_1=inventory_item,
)

host = Host(
Expand Down Expand Up @@ -593,8 +598,8 @@ def create_virtual_network(
VMWARE_NAMESPACE,
VCENTER_RESOURCE_TYPE,
vcenter,
INVENTORY_ITEM_TYPE,
inventory_item,
child_type_1=INVENTORY_ITEM_TYPE,
child_name_1=inventory_item,
)

virtual_network = VirtualNetwork(
Expand Down Expand Up @@ -678,8 +683,8 @@ def create_vm_template(
VMWARE_NAMESPACE,
VCENTER_RESOURCE_TYPE,
vcenter,
INVENTORY_ITEM_TYPE,
inventory_item,
child_type_1=INVENTORY_ITEM_TYPE,
child_name_1=inventory_item,
)

vm_template = VirtualMachineTemplate(
Expand Down Expand Up @@ -732,13 +737,15 @@ def get_hcrp_machine_id(
resource_group_name,
resource_name,
):
return get_resource_id(
machine_id = get_resource_id(
cmd,
resource_group_name,
HCRP_NAMESPACE,
MACHINES_RESOURCE_TYPE,
resource_name,
)
assert machine_id is not None
return machine_id


def create_vm(
Expand Down Expand Up @@ -766,34 +773,34 @@ def create_vm(
no_wait=False,
):
if not any([vm_template, inventory_item, datastore]):
raise CLIError(
raise RequiredArgumentMissingError(
"either vm_template, inventory_item id or datastore must be provided."
)

if vm_template is not None or datastore is not None:
if not any([resource_pool, cluster, host]):
raise CLIError(
raise RequiredArgumentMissingError(
"either resource_pool, cluster or host must be provided while creating a VM."
)

if len([i for i in [resource_pool, cluster, host] if i is not None]) > 1:
raise CLIError(
raise MutuallyExclusiveArgumentError(
"at max one of resource_pool, cluster or host can be provided."
)

if inventory_item is not None:
if vm_template is not None:
raise CLIError(
raise MutuallyExclusiveArgumentError(
"both vm_template and inventory_item id cannot be provided together."
)

if any([resource_pool, cluster, host, datastore]):
raise CLIError(
raise MutuallyExclusiveArgumentError(
"Placement input cannot be provided together with inventory_item."
)

if not is_valid_resource_id(inventory_item) and not vcenter:
raise CLIError(
raise RequiredArgumentMissingError(
"Cannot determine inventory item ID. " +
"vCenter name or ID is required when inventory item name is specified."
)
Expand Down Expand Up @@ -861,9 +868,10 @@ def create_vm(
VMWARE_NAMESPACE,
VCENTER_RESOURCE_TYPE,
vcenter,
INVENTORY_ITEM_TYPE,
inventory_item,
child_type_1=INVENTORY_ITEM_TYPE,
child_name_1=inventory_item,
)
assert inventory_item_id is not None

vcenter_id = "/".join(inventory_item_id.rstrip("/").split("/")[:-2])

Expand All @@ -872,7 +880,7 @@ def create_vm(
)
else:
if vcenter is None:
raise CLIError("Missing parameter, provide vcenter name or id.")
raise RequiredArgumentMissingError("Missing parameter, provide vcenter name or id.")

vcenter_id = get_resource_id(
cmd, resource_group_name, VMWARE_NAMESPACE, VCENTER_RESOURCE_TYPE, vcenter
Expand Down Expand Up @@ -945,6 +953,8 @@ def create_vm(
infrastructure_profile=infrastructure_profile,
)

assert vcenter_id is not None

# The subscription of the vCenter can be different from the machine resource.
# There was no straightforward way to change the subscription for vcenter client factory.
# Hence using the generic get client.
Expand All @@ -956,13 +966,13 @@ def create_vm(
machine = None
try:
machine = machine_client.get(resource_group_name, resource_name)
if machine.kind != vcenter.kind:
raise CLIError(
if f"{machine.kind}".lower() != f"{vcenter.kind}".lower():
raise InvalidArgumentValueError(
"The existing Machine resource is not of the same kind as the vCenter. " +
f"Machine kind: {machine.kind}, vCenter kind: {vcenter.kind}"
f"Machine kind: '{machine.kind}', vCenter kind: '{vcenter.kind}'"
)
if location is not None and machine.location != location:
raise CLIError(
raise InvalidArgumentValueError(
"The location of the existing Machine cannot be updated. " +
"Either specify the existing location or keep the location unspecified. " +
f"Existing location: {machine.location}, Provided location: {location}"
Expand All @@ -974,7 +984,7 @@ def create_vm(
machine = machine_client.update(resource_group_name, resource_name, m)
except ResourceNotFoundError as e:
if location is None:
raise CLIError(
raise RequiredArgumentMissingError(
"The parent Machine resource does not exist, " +
"location is required while creating a new machine."
) from e
Expand Down Expand Up @@ -1024,7 +1034,7 @@ def update_vm(
memory_size is None and
tags is None
):
raise CLIError("No inputs were given to update the vm.")
raise RequiredArgumentMissingError("No inputs were given to update the vm.")

if (
num_CPUs is not None or
Expand All @@ -1037,6 +1047,9 @@ def update_vm(
num_cores_per_socket=num_cores_per_socket,
)

if hardware_profile is None:
return client.get(machine_id)

vm_update = VirtualMachineInstanceUpdate(
hardware_profile=hardware_profile,
)
Expand All @@ -1062,7 +1075,7 @@ def delete_vm(
):

if retain and delete_from_host:
raise CLIError(
raise MutuallyExclusiveArgumentError(
"Arguments --retain and --delete-from-host cannot be used together." +
"VM is retained in VMWare by default, it is deleted when --delete-from-host is provided."
)
Expand All @@ -1077,7 +1090,7 @@ def delete_vm(

if no_wait and delete_machine:
if delete_from_host:
raise CLIError(
raise MutuallyExclusiveArgumentError(
"Cannot delete VMWare VM from host when --no-wait and --delete-machine is provided."
)
machine_client.delete(resource_group_name, resource_name)
Expand Down Expand Up @@ -1219,7 +1232,7 @@ def get_network_interfaces(
elif key == GATEWAY:
ip_settings.gateway = value.split(GATEWAY_SEPERATOR)
else:
raise CLIError(
raise UnrecognizedArgumentError(
'Invalid parameter: {name} specified for nic.'.format(name=key)
)

Expand Down Expand Up @@ -1250,7 +1263,7 @@ def get_disks(input_disks):
elif key == UNIT_NUMBER:
disk.unit_number = value
else:
raise CLIError(
raise UnrecognizedArgumentError(
'Invalid parameter: {name} specified for disk.'.format(name=key)
)
disks.append(disk)
Expand Down Expand Up @@ -1339,7 +1352,7 @@ def update_nic(
"""

if nic_name is None and device_key is None:
raise CLIError(
raise RequiredArgumentMissingError(
"Either nic name or device key must be specified to update the nic."
)

Expand Down Expand Up @@ -1386,7 +1399,7 @@ def update_nic(
nic.name is not None and
nic.name != nic_name
) or (device_key is not None and nic.device_key != device_key):
raise CLIError(
raise InvalidArgumentValueError(
"Incorrect nic-name and device-key combination, Expected " +
"nic-name: " +
str(nic.name) +
Expand All @@ -1406,7 +1419,7 @@ def update_nic(
nics_update.append(nic_update)

if not nic_found:
raise CLIError("Given nic is not present in the virtual machine.")
raise InvalidArgumentValueError("Given nic is not present in the virtual machine.")

network_profile = NetworkProfileUpdate(network_interfaces=nics_update)
vm_update = VirtualMachineInstanceUpdate(network_profile=network_profile)
Expand Down Expand Up @@ -1513,7 +1526,7 @@ def delete_nics(
if nics_to_delete[nic_name]:
not_found_nics = not_found_nics + nic_name + ", "
if not_found_nics != "":
raise CLIError(
raise InvalidArgumentValueError(
"Nics with name " +
not_found_nics +
'not present in the given virtual machine.'
Expand Down Expand Up @@ -1603,7 +1616,7 @@ def update_disk(
"""

if disk_name is None and device_key is None:
raise CLIError(
raise RequiredArgumentMissingError(
"Either disk name or device key must be specified to update the disk."
)

Expand Down Expand Up @@ -1638,7 +1651,7 @@ def update_disk(
disk.name is not None and
disk.name != disk_name
) or (device_key is not None and disk.device_key != device_key):
raise CLIError(
raise InvalidArgumentValueError(
"Incorrect disk-name and device-key combination, Expected "
"disk-name: " +
str(disk.name) +
Expand All @@ -1662,7 +1675,7 @@ def update_disk(
disks_update.append(disk_update)

if not disk_found:
raise CLIError("The provided disk is not present in the virtual machine.")
raise InvalidArgumentValueError("The provided disk is not present in the virtual machine.")

storage_profile = StorageProfileUpdate(disks=disks_update)
vm_update = VirtualMachineInstanceUpdate(storage_profile=storage_profile)
Expand Down Expand Up @@ -1764,7 +1777,7 @@ def delete_disks(
if disks_to_delete[disk_name]:
not_found_disks = not_found_disks + disk_name + ", "
if not_found_disks != "":
raise CLIError(
raise InvalidArgumentValueError(
"Disks with name " +
not_found_disks +
"not present in the given virtual machine."
Expand Down Expand Up @@ -1936,8 +1949,8 @@ def connectedvmware_extension_create(
HCRP_NAMESPACE,
MACHINES_RESOURCE_TYPE,
vm_name,
EXTENSIONS_RESOURCE_TYPE,
name
child_type_1=EXTENSIONS_RESOURCE_TYPE,
child_name_1=name
)

machine_extension = MachineExtension(
Expand Down
Loading