Skip to content

Commit e43b782

Browse files
authored
{Compute} az vm nic: Migrate commands to aaz (#32640)
1 parent bfaf1d4 commit e43b782

File tree

4 files changed

+3155
-39
lines changed

4 files changed

+3155
-39
lines changed

src/azure-cli/azure/cli/command_modules/vm/custom.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,13 +2910,14 @@ def show_vm_nic(cmd, resource_group_name, vm_name, nic):
29102910

29112911
NicShow = import_aaz_by_profile(cmd.cli_ctx.cloud.profile, "network.nic").Show
29122912

2913-
vm = get_vm(cmd, resource_group_name, vm_name)
2913+
vm = get_vm_by_aaz(cmd, resource_group_name, vm_name)
2914+
29142915
found = next(
2915-
(n for n in vm.network_profile.network_interfaces if nic.lower() == n.id.lower()), None
2916+
(n for n in vm.get("networkProfile", {}).get("networkInterfaces", []) if nic.lower() == n["id"].lower()), None
29162917
# pylint: disable=no-member
29172918
)
29182919
if found:
2919-
nic_name = parse_resource_id(found.id)['name']
2920+
nic_name = parse_resource_id(found["id"])['name']
29202921
return NicShow(cli_ctx=cmd.cli_ctx)(command_args={
29212922
'name': nic_name,
29222923
'resource_group': resource_group_name
@@ -2925,12 +2926,12 @@ def show_vm_nic(cmd, resource_group_name, vm_name, nic):
29252926

29262927

29272928
def list_vm_nics(cmd, resource_group_name, vm_name):
2928-
vm = get_vm(cmd, resource_group_name, vm_name)
2929-
return vm.network_profile.network_interfaces # pylint: disable=no-member
2929+
vm = get_vm_by_aaz(cmd, resource_group_name, vm_name)
2930+
return vm.get("networkProfile", {}).get("networkInterfaces", []) # pylint: disable=no-member
29302931

29312932

29322933
def add_vm_nic(cmd, resource_group_name, vm_name, nics, primary_nic=None):
2933-
vm = get_vm_to_update(cmd, resource_group_name, vm_name)
2934+
vm = get_vm_to_update_by_aaz(cmd, resource_group_name, vm_name)
29342935
new_nics = _build_nic_list(cmd, nics)
29352936
existing_nics = _get_existing_nics(vm)
29362937
return _update_vm_nics(cmd, vm, existing_nics + new_nics, primary_nic)
@@ -2939,25 +2940,25 @@ def add_vm_nic(cmd, resource_group_name, vm_name, nics, primary_nic=None):
29392940
def remove_vm_nic(cmd, resource_group_name, vm_name, nics, primary_nic=None):
29402941

29412942
def to_delete(nic_id):
2942-
return [n for n in nics_to_delete if n.id.lower() == nic_id.lower()]
2943+
return [n for n in nics_to_delete if n["id"].lower() == nic_id.lower()]
29432944

2944-
vm = get_vm_to_update(cmd, resource_group_name, vm_name)
2945+
vm = get_vm_to_update_by_aaz(cmd, resource_group_name, vm_name)
29452946
nics_to_delete = _build_nic_list(cmd, nics)
29462947
existing_nics = _get_existing_nics(vm)
2947-
survived = [x for x in existing_nics if not to_delete(x.id)]
2948+
2949+
survived = [x for x in existing_nics if not to_delete(x["id"])]
2950+
29482951
return _update_vm_nics(cmd, vm, survived, primary_nic)
29492952

29502953

29512954
def set_vm_nic(cmd, resource_group_name, vm_name, nics, primary_nic=None):
2952-
vm = get_vm_to_update(cmd, resource_group_name, vm_name)
2955+
vm = get_vm_to_update_by_aaz(cmd, resource_group_name, vm_name)
29532956
nics = _build_nic_list(cmd, nics)
29542957
return _update_vm_nics(cmd, vm, nics, primary_nic)
29552958

29562959

29572960
def _build_nic_list(cmd, nic_ids):
29582961
NicShow = import_aaz_by_profile(cmd.cli_ctx.cloud.profile, "network.nic").Show
2959-
2960-
NetworkInterfaceReference = cmd.get_models('NetworkInterfaceReference')
29612962
nic_list = []
29622963
if nic_ids:
29632964
# pylint: disable=no-member
@@ -2967,46 +2968,45 @@ def _build_nic_list(cmd, nic_ids):
29672968
'name': name,
29682969
'resource_group': rg
29692970
})
2970-
nic_list.append(NetworkInterfaceReference(id=nic['id'], primary=False))
2971+
nic_list.append({"id": nic["id"], "primary": False})
29712972
return nic_list
29722973

29732974

29742975
def _get_existing_nics(vm):
2975-
network_profile = getattr(vm, 'network_profile', None)
2976+
network_profile = vm.get("networkProfile", None)
29762977
nics = []
29772978
if network_profile is not None:
2978-
nics = network_profile.network_interfaces or []
2979+
nics = network_profile.get("networkInterfaces", [])
29792980
return nics
29802981

29812982

29822983
def _update_vm_nics(cmd, vm, nics, primary_nic):
2983-
NetworkProfile = cmd.get_models('NetworkProfile')
2984+
from .operations.vm import convert_show_result_to_snake_case
29842985

29852986
if primary_nic:
29862987
try:
29872988
_, primary_nic_name = _parse_rg_name(primary_nic)
29882989
except IndexError:
29892990
primary_nic_name = primary_nic
29902991

2991-
matched = [n for n in nics if _parse_rg_name(n.id)[1].lower() == primary_nic_name.lower()]
2992+
matched = [n for n in nics if _parse_rg_name(n["id"])[1].lower() == primary_nic_name.lower()]
29922993
if not matched:
29932994
raise CLIError('Primary Nic {} is not found'.format(primary_nic))
29942995
if len(matched) > 1:
29952996
raise CLIError('Duplicate Nic entries with name {}'.format(primary_nic))
29962997
for n in nics:
2997-
n.primary = False
2998-
matched[0].primary = True
2998+
n["primary"] = False
2999+
matched[0]["primary"] = True
29993000
elif nics:
3000-
if not [n for n in nics if n.primary]:
3001-
nics[0].primary = True
3002-
3003-
network_profile = getattr(vm, 'network_profile', None)
3004-
if network_profile is None:
3005-
vm.network_profile = NetworkProfile(network_interfaces=nics)
3006-
else:
3007-
network_profile.network_interfaces = nics
3001+
if not [n for n in nics if n["primary"]]:
3002+
nics[0]["primary"] = True
30083003

3009-
return set_vm(cmd, vm).network_profile.network_interfaces
3004+
if "networkProfile" not in vm:
3005+
vm["networkProfile"] = {}
3006+
vm["networkProfile"]["networkInterfaces"] = nics
3007+
vm = convert_show_result_to_snake_case(vm)
3008+
result = set_vm_by_aaz(cmd, vm)
3009+
return (result.get("networkProfile") or {}).get("networkInterfaces") or []
30103010
# endregion
30113011

30123012

src/azure-cli/azure/cli/command_modules/vm/tests/latest/recordings/test_vm_create_multi_nics.yaml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,7 +3053,7 @@ interactions:
30533053
User-Agent:
30543054
- AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
30553055
method: GET
3056-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2024-11-01
3056+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2025-04-01
30573057
response:
30583058
body:
30593059
string: "{\r\n \"name\": \"multinicvm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1\",\r\n
@@ -3129,7 +3129,7 @@ interactions:
31293129
User-Agent:
31303130
- AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
31313131
method: GET
3132-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2024-11-01
3132+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2025-04-01
31333133
response:
31343134
body:
31353135
string: "{\r\n \"name\": \"multinicvm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1\",\r\n
@@ -3255,7 +3255,7 @@ interactions:
32553255
User-Agent:
32563256
- AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
32573257
method: GET
3258-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2024-11-01
3258+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2025-04-01
32593259
response:
32603260
body:
32613261
string: "{\r\n \"name\": \"multinicvm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1\",\r\n
@@ -3398,7 +3398,7 @@ interactions:
33983398
User-Agent:
33993399
- AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
34003400
method: PUT
3401-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2024-11-01
3401+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2025-04-01
34023402
response:
34033403
body:
34043404
string: "{\r\n \"name\": \"multinicvm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1\",\r\n
@@ -3536,7 +3536,7 @@ interactions:
35363536
User-Agent:
35373537
- AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
35383538
method: GET
3539-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2024-11-01
3539+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2025-04-01
35403540
response:
35413541
body:
35423542
string: "{\r\n \"name\": \"multinicvm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1\",\r\n
@@ -3612,7 +3612,7 @@ interactions:
36123612
User-Agent:
36133613
- AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
36143614
method: GET
3615-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2024-11-01
3615+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2025-04-01
36163616
response:
36173617
body:
36183618
string: "{\r\n \"name\": \"multinicvm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1\",\r\n
@@ -3756,7 +3756,7 @@ interactions:
37563756
User-Agent:
37573757
- AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
37583758
method: PUT
3759-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2024-11-01
3759+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2025-04-01
37603760
response:
37613761
body:
37623762
string: "{\r\n \"name\": \"multinicvm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1\",\r\n
@@ -3947,7 +3947,7 @@ interactions:
39473947
User-Agent:
39483948
- AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
39493949
method: GET
3950-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2024-11-01
3950+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2025-04-01
39513951
response:
39523952
body:
39533953
string: "{\r\n \"name\": \"multinicvm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1\",\r\n
@@ -4023,7 +4023,7 @@ interactions:
40234023
User-Agent:
40244024
- AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
40254025
method: GET
4026-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2024-11-01
4026+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2025-04-01
40274027
response:
40284028
body:
40294029
string: "{\r\n \"name\": \"multinicvm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1\",\r\n
@@ -4215,7 +4215,7 @@ interactions:
42154215
User-Agent:
42164216
- AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
42174217
method: PUT
4218-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2024-11-01
4218+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2025-04-01
42194219
response:
42204220
body:
42214221
string: "{\r\n \"name\": \"multinicvm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1\",\r\n
@@ -4406,7 +4406,7 @@ interactions:
44064406
User-Agent:
44074407
- AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
44084408
method: GET
4409-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2024-11-01
4409+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1?api-version=2025-04-01
44104410
response:
44114411
body:
44124412
string: "{\r\n \"name\": \"multinicvm1\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_multi_nic_vm000001/providers/Microsoft.Compute/virtualMachines/multinicvm1\",\r\n

0 commit comments

Comments
 (0)