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
16 changes: 16 additions & 0 deletions src/azure-cli/azure/cli/command_modules/network/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -6756,3 +6756,19 @@ def create_ddos_custom_policy(cmd, ddos_custom_policy_name, resource_group_name,
policy['no_wait'] = no_wait

return DdosCustomPolicyCreate(cli_ctx=cmd.cli_ctx)(command_args=policy)


def get_vm(cli_ctx, resource_group_name, vm_name):
from ..vm.operations.vm import VMShow
return VMShow(cli_ctx=cli_ctx)(command_args={
'resource_group': resource_group_name,
'vm_name': vm_name
})


def get_vmss(cli_ctx, resource_group_name, vmss_name):
from ..vm.operations.vmss import VMSSShow
return VMSSShow(cli_ctx=cli_ctx)(command_args={
'resource_group': resource_group_name,
'vm_scale_set_name': vmss_name
})
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ def get_network_watcher_from_location(cmd, watcher_name="watcher_name", rg_name=


def get_network_watcher_from_vm(cmd):
from ..custom import get_vm
args = cmd.ctx.args
compute_client = get_mgmt_service_client(cmd.cli_ctx, ResourceType.MGMT_COMPUTE).virtual_machines
vm_name = parse_resource_id(args.vm.to_serialized_data())["name"]
vm = compute_client.get(args.resource_group_name, vm_name)
vm = get_vm(cmd.cli_ctx, args.resource_group_name, vm_name)
args.location = vm.location
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_vm returns the deserialized output from VMShow (a dict), but this code still accesses vm.location like an SDK model. This will raise AttributeError at runtime (e.g., for az network watcher test-ip-flow, show-next-hop, etc.). Use the returned mapping to set the location (e.g., vm["location"] / vm.get("location")) before calling get_network_watcher_from_location.

Suggested change
args.location = vm.location
location = vm.get("location") if isinstance(vm, dict) else getattr(vm, "location", None)
if not location:
raise ValidationError("Unable to determine location for the specified virtual machine.")
args.location = location

Copilot uses AI. Check for mistakes.
get_network_watcher_from_location(cmd)

Expand All @@ -88,10 +88,10 @@ def get_network_watcher_from_resource(cmd):


def get_network_watcher_from_vmss(cmd):
from ..custom import get_vmss
args = cmd.ctx.args
compute_client = get_mgmt_service_client(cmd.cli_ctx, ResourceType.MGMT_COMPUTE).virtual_machine_scale_sets
vmss_name = parse_resource_id(args.target.to_serialized_data())["name"]
vmss = compute_client.get(args.resource_group_name, vmss_name)
vmss = get_vmss(cmd.cli_ctx, args.resource_group_name, vmss_name)
args.location = vmss.location
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_vmss returns the deserialized output from VMSSShow (a dict), but this code still accesses vmss.location like an SDK model. This will raise AttributeError when --target-type AzureVMSS flows call this helper. Read the location from the dict output (e.g., vmss["location"] / vmss.get("location")).

Suggested change
args.location = vmss.location
args.location = vmss["location"]

Copilot uses AI. Check for mistakes.
get_network_watcher_from_location(cmd)

Expand Down Expand Up @@ -387,16 +387,16 @@ def _build_arguments_schema(cls, *args, **kwargs):
return args_schema

def pre_operations(self):
from ..custom import get_vm
args = self.ctx.args
compute_client = get_mgmt_service_client(self.cli_ctx, ResourceType.MGMT_COMPUTE).virtual_machines
id_parts = parse_resource_id(args.source_resource.to_serialized_data())
vm_name = id_parts["name"]
rg = args.resource_group_name or id_parts.get("resource_group", None)
if not rg:
raise ValidationError("usage error: --source-resource ID | --source-resource NAME --resource-group NAME")

vm = compute_client.get(rg, vm_name)
args.location = vm.location
vm = get_vm(self.cli_ctx, rg, vm_name)
args.location = vm.get('location')
get_network_watcher_from_location(self)

if has_value(args.source_resource) and not is_valid_resource_id(args.source_resource.to_serialized_data()):
Expand Down
Loading