From 056a049f026ef323479179bae1a01ad01befd17a Mon Sep 17 00:00:00 2001 From: william051200 Date: Wed, 25 Feb 2026 09:15:24 +0800 Subject: [PATCH 1/3] Migrate vm open-port --- src/azure-cli/azure/cli/command_modules/vm/commands.py | 2 +- src/azure-cli/azure/cli/command_modules/vm/custom.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) 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 cc60cf53afc..3943ed5f9af 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/commands.py +++ b/src/azure-cli/azure/cli/command_modules/vm/commands.py @@ -286,6 +286,7 @@ def load_command_table(self, _): g.custom_command('list', 'list_vm', table_transformer=transform_vm_list) g.custom_show_command('show', 'show_vm', table_transformer=transform_vm) 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.custom_command('open-port', 'open_vm_port') with self.command_group('vm', compute_vm_sdk) as g: g.custom_command('application set', 'set_vm_applications', validator=process_set_applications_namespace, min_api='2021-07-01') @@ -295,7 +296,6 @@ def load_command_table(self, _): g.custom_command('list-ip-addresses', 'list_vm_ip_addresses', table_transformer=transform_ip_addresses) g.custom_command('list-skus', 'list_skus', table_transformer=transform_sku_for_table_output, min_api='2017-03-30') g.command('list-usage', 'list', command_type=compute_vm_usage_sdk, transform=transform_vm_usage_list, table_transformer='[].{Name:localName, CurrentValue:currentValue, Limit:limit}') - g.custom_command('open-port', 'open_vm_port') g.custom_command('resize', 'resize_vm', supports_no_wait=True) g.custom_command('restart', 'restart_vm', supports_no_wait=True) g.command('stop', 'begin_power_off', supports_no_wait=True, validator=process_vm_vmss_stop) diff --git a/src/azure-cli/azure/cli/command_modules/vm/custom.py b/src/azure-cli/azure/cli/command_modules/vm/custom.py index 9577f66cc88..7f973f07ed9 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/custom.py +++ b/src/azure-cli/azure/cli/command_modules/vm/custom.py @@ -1619,12 +1619,12 @@ def open_vm_port(cmd, resource_group_name, vm_name, port, priority=900, network_ NSGShow, NSGCreate = _nsg.Show, _nsg.Create NSGRuleCreate = import_aaz_by_profile(cmd.cli_ctx.cloud.profile, "network.nsg.rule").Create - vm = get_vm(cmd, resource_group_name, vm_name) - location = vm.location + vm = get_vm_by_aaz(cmd, resource_group_name, vm_name) + location = vm.get('location', '') if not vm.network_profile: raise CLIError("Network profile not found for VM '{}'".format(vm_name)) - nic_ids = list(vm.network_profile.network_interfaces) + nic_ids = vm.get('networkProfile', {}).get('networkInterfaces', []) if len(nic_ids) > 1: raise CLIError('Multiple NICs is not supported for this command. Create rules on the NSG ' 'directly.') @@ -1634,7 +1634,7 @@ def open_vm_port(cmd, resource_group_name, vm_name, port, priority=900, network_ # get existing NSG or create a new one created_nsg = False nic = NicShow(cli_ctx=cmd.cli_ctx)(command_args={ - 'name': os.path.split(nic_ids[0].id)[1], + 'name': os.path.split(nic_ids[0].get('id'))[1], 'resource_group': resource_group_name }) if not apply_to_subnet: From 93924db8249ca5ba395cc7dad61ba5fd47539ed1 Mon Sep 17 00:00:00 2001 From: william051200 Date: Wed, 25 Feb 2026 10:34:45 +0800 Subject: [PATCH 2/3] Fix code --- src/azure-cli/azure/cli/command_modules/vm/custom.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/custom.py b/src/azure-cli/azure/cli/command_modules/vm/custom.py index 7f973f07ed9..0d105ea6435 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/custom.py +++ b/src/azure-cli/azure/cli/command_modules/vm/custom.py @@ -1621,7 +1621,7 @@ def open_vm_port(cmd, resource_group_name, vm_name, port, priority=900, network_ vm = get_vm_by_aaz(cmd, resource_group_name, vm_name) location = vm.get('location', '') - if not vm.network_profile: + if not vm.get('networkProfile'): raise CLIError("Network profile not found for VM '{}'".format(vm_name)) nic_ids = vm.get('networkProfile', {}).get('networkInterfaces', []) From bb8792ce1aa0523f91c6dfe93fc19bd37949f18d Mon Sep 17 00:00:00 2001 From: william051200 Date: Wed, 25 Feb 2026 10:34:53 +0800 Subject: [PATCH 3/3] Update test recording version --- .../vm/tests/latest/recordings/test_vm_open_port.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/tests/latest/recordings/test_vm_open_port.yaml b/src/azure-cli/azure/cli/command_modules/vm/tests/latest/recordings/test_vm_open_port.yaml index 09422c1d8f7..0a79e627e5c 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/tests/latest/recordings/test_vm_open_port.yaml +++ b/src/azure-cli/azure/cli/command_modules/vm/tests/latest/recordings/test_vm_open_port.yaml @@ -828,7 +828,7 @@ interactions: - AZURECLI/2.47.0 azsdk-python-azure-mgmt-compute/29.1.0 Python/3.10.11 (Linux-5.15.0-1036-azure-x86_64-with-glibc2.31) VSTS_7b238909-6802-4b65-b90d-184bca47f458_build_220_0 method: GET - uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_open_port000001/providers/Microsoft.Compute/virtualMachines/vm1?api-version=2024-11-01 + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_open_port000001/providers/Microsoft.Compute/virtualMachines/vm1?api-version=2025-04-01 response: body: string: "{\r\n \"name\": \"vm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_open_port000001/providers/Microsoft.Compute/virtualMachines/vm1\",\r\n @@ -1424,7 +1424,7 @@ interactions: - AZURECLI/2.47.0 azsdk-python-azure-mgmt-compute/29.1.0 Python/3.10.11 (Linux-5.15.0-1036-azure-x86_64-with-glibc2.31) VSTS_7b238909-6802-4b65-b90d-184bca47f458_build_220_0 method: GET - uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_open_port000001/providers/Microsoft.Compute/virtualMachines/vm1?api-version=2024-11-01 + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_open_port000001/providers/Microsoft.Compute/virtualMachines/vm1?api-version=2025-04-01 response: body: string: "{\r\n \"name\": \"vm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_open_port000001/providers/Microsoft.Compute/virtualMachines/vm1\",\r\n @@ -2051,7 +2051,7 @@ interactions: - AZURECLI/2.47.0 azsdk-python-azure-mgmt-compute/29.1.0 Python/3.10.11 (Linux-5.15.0-1036-azure-x86_64-with-glibc2.31) VSTS_7b238909-6802-4b65-b90d-184bca47f458_build_220_0 method: GET - uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_open_port000001/providers/Microsoft.Compute/virtualMachines/vm1?api-version=2024-11-01 + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_open_port000001/providers/Microsoft.Compute/virtualMachines/vm1?api-version=2025-04-01 response: body: string: "{\r\n \"name\": \"vm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_open_port000001/providers/Microsoft.Compute/virtualMachines/vm1\",\r\n