Skip to content
Open
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
3 changes: 2 additions & 1 deletion src/azure-cli/azure/cli/command_modules/vm/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion src/azure-cli/azure/cli/command_modules/vm/operations/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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)
)
)
Comment on lines +279 to +287


def convert_show_result_to_snake_case(result):
new_result = {}
if "id" in result:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Comment on lines +217 to +236

self.assertIn("nonexistent-vm", str(cm.exception))
self.assertIn("nonexistent-rg", str(cm.exception))


if __name__ == '__main__':
unittest.main()
Loading