diff --git a/src/azure-cli/azure/cli/command_modules/vm/commands.py b/src/azure-cli/azure/cli/command_modules/vm/commands.py index 0b127c6b260..5a494b19fa7 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/commands.py +++ b/src/azure-cli/azure/cli/command_modules/vm/commands.py @@ -180,9 +180,10 @@ def load_command_table(self, _): g.generic_update_command('update', getter_name='get_vm_to_update_by_aaz', setter_name='update_vm', setter_type=compute_custom, command_type=compute_custom, supports_no_wait=True, validator=process_vm_update_namespace) g.wait_command('wait', getter_name='get_instance_view', getter_type=compute_custom) - from .operations.vm import VMCapture, VMDeallocate + from .operations.vm import VMCapture, VMDeallocate, VMDelete self.command_table['vm capture'] = VMCapture(loader=self) self.command_table['vm deallocate'] = VMDeallocate(loader=self) + self.command_table['vm delete'] = VMDelete(loader=self) with self.command_group('vm availability-set') as g: g.custom_command('create', 'create_av_set', table_transformer=deployment_validate_table_format, supports_no_wait=True, exception_handler=handle_template_based_exception) diff --git a/src/azure-cli/azure/cli/command_modules/vm/operations/vm.py b/src/azure-cli/azure/cli/command_modules/vm/operations/vm.py index 69d88f8faad..58d2b20705d 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/operations/vm.py +++ b/src/azure-cli/azure/cli/command_modules/vm/operations/vm.py @@ -10,7 +10,8 @@ from azure.cli.core.aaz import AAZStrType from ..aaz.latest.vm import (Show as _VMShow, ListSizes as _VMListSizes, Patch as _VMPatch, Update as _VMUpdate, Capture as _VMCapture, Create as _VMCreate, - ListUsage as _VMListUsage, Deallocate as _VMDeallocate) + ListUsage as _VMListUsage, Deallocate as _VMDeallocate, + Delete as _VMDelete) from .._vm_utils import IdentityType logger = get_logger(__name__) @@ -275,6 +276,17 @@ def _build_arguments_schema(cls, *args, **kwargs): return args_schema +class VMDelete(_VMDelete): + class VirtualMachinesDelete(_VMDelete.VirtualMachinesDelete): + def on_204(self, session): + from azure.cli.core.azclierror import ResourceNotFoundError + raise ResourceNotFoundError( + "The VM '{}' under resource group '{}' was not found.".format( + str(self.ctx.args.name), str(self.ctx.args.resource_group) + ) + ) + + def convert_show_result_to_snake_case(result): new_result = {} if "id" in result: diff --git a/src/azure-cli/azure/cli/command_modules/vm/tests/latest/test_custom_vm_commands.py b/src/azure-cli/azure/cli/command_modules/vm/tests/latest/test_custom_vm_commands.py index 0291e22490b..7bdcab22c26 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/tests/latest/test_custom_vm_commands.py +++ b/src/azure-cli/azure/cli/command_modules/vm/tests/latest/test_custom_vm_commands.py @@ -214,5 +214,30 @@ def __init__(self, is_linux, version): self.type_handler_version = version +class TestVMDeleteOn204(unittest.TestCase): + """Unit tests for VMDelete.VirtualMachinesDelete.on_204 behavior.""" + + def test_on_204_raises_resource_not_found_error(self): + from azure.cli.core.azclierror import ResourceNotFoundError + from azure.cli.command_modules.vm.operations.vm import VMDelete + + cli_ctx = DummyCli() + vm_delete = VMDelete(cli_ctx=cli_ctx) + + # Simulate the ctx that would be set during command execution + ctx = mock.MagicMock() + ctx.args.name = "nonexistent-vm" + ctx.args.resource_group = "nonexistent-rg" + + # Instantiate the inner operation class with the mocked ctx + vm_delete_op = VMDelete.VirtualMachinesDelete(ctx=ctx) + + with self.assertRaises(ResourceNotFoundError) as cm: + vm_delete_op.on_204(session=mock.MagicMock()) + + self.assertIn("nonexistent-vm", str(cm.exception)) + self.assertIn("nonexistent-rg", str(cm.exception)) + + if __name__ == '__main__': unittest.main()