Skip to content

Commit c992d74

Browse files
authored
{VM} Clean up Network SDK dependencies (#25805)
1 parent 32dd295 commit c992d74

File tree

153 files changed

+109100
-5166
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

153 files changed

+109100
-5166
lines changed

src/azure-cli/azure/cli/command_modules/network/tests/hybrid_2018_03_01/recordings/test_network_nic_convenience_commands.yaml

Lines changed: 626 additions & 556 deletions
Large diffs are not rendered by default.

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,6 @@ def _compute_client_factory(cli_ctx, **kwargs):
1212
aux_subscriptions=kwargs.get('aux_subscriptions'))
1313

1414

15-
def cf_ni(cli_ctx, _):
16-
from azure.cli.core.profiles import ResourceType
17-
from azure.cli.core.commands.client_factory import get_mgmt_service_client
18-
# TODO: Remove hard coded api-version once
19-
# https://github.com/Azure/azure-rest-api-specs/issues/570
20-
# is fixed.
21-
ni = get_mgmt_service_client(cli_ctx, ResourceType.MGMT_NETWORK).network_interfaces
22-
ni.api_version = '2016-03-30'
23-
return ni
24-
25-
26-
def cf_public_ip_addresses(cli_ctx):
27-
from azure.cli.core.profiles import ResourceType
28-
from azure.cli.core.commands.client_factory import get_mgmt_service_client
29-
public_ip_ops = get_mgmt_service_client(cli_ctx, ResourceType.MGMT_NETWORK).public_ip_addresses
30-
return public_ip_ops
31-
32-
3315
def cf_avail_set(cli_ctx, _):
3416
return _compute_client_factory(cli_ctx).availability_sets
3517

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -728,15 +728,16 @@ def load_arguments(self, _):
728728
help='Specify it to create new virtual machines to upgrade the scale set, rather than updating the existing virtual machines.')
729729

730730
with self.argument_context('vmss create', arg_group='Network Balancer') as c:
731-
LoadBalancerSkuName = self.get_models('LoadBalancerSkuName', resource_type=ResourceType.MGMT_NETWORK)
732731
c.argument('application_gateway', help='Name to use when creating a new application gateway (default) or referencing an existing one. Can also reference an existing application gateway by ID or specify "" for none.', options_list=['--app-gateway'])
733732
c.argument('app_gateway_capacity', help='The number of instances to use when creating a new application gateway.')
734733
c.argument('app_gateway_sku', help='SKU when creating a new application gateway.')
735734
c.argument('app_gateway_subnet_address_prefix', help='The subnet IP address prefix to use when creating a new application gateway in CIDR format.')
736735
c.argument('backend_pool_name', help='Name to use for the backend pool when creating a new load balancer or application gateway.')
737736
c.argument('backend_port', help='When creating a new load balancer, backend port to open with NAT rules (Defaults to 22 on Linux and 3389 on Windows). When creating an application gateway, the backend port to use for the backend HTTP settings.', type=int)
738737
c.argument('load_balancer', help='Name to use when creating a new load balancer (default) or referencing an existing one. Can also reference an existing load balancer by ID or specify "" for none.', options_list=['--load-balancer', '--lb'])
739-
c.argument('load_balancer_sku', resource_type=ResourceType.MGMT_NETWORK, min_api='2017-08-01', options_list=['--lb-sku'], arg_type=get_enum_type(LoadBalancerSkuName),
738+
c.argument('load_balancer_sku', resource_type=ResourceType.MGMT_NETWORK, min_api='2017-08-01', max_api='2021-02-01', options_list=['--lb-sku'], arg_type=get_enum_type(['Basic', 'Standard']),
739+
help="Sku of the Load Balancer to create. Default to 'Standard' when single placement group is turned off; otherwise, default to 'Basic'. The public IP is supported to be created on edge zone only when it is 'Standard'")
740+
c.argument('load_balancer_sku', resource_type=ResourceType.MGMT_NETWORK, min_api='2021-02-01', options_list=['--lb-sku'], arg_type=get_enum_type(['Basic', 'Standard', 'Gateway']),
740741
help="Sku of the Load Balancer to create. Default to 'Standard' when single placement group is turned off; otherwise, default to 'Basic'. The public IP is supported to be created on edge zone only when it is 'Standard'")
741742
c.argument('nat_pool_name', help='Name to use for the NAT pool when creating a new load balancer.', options_list=['--lb-nat-pool-name', '--nat-pool-name'], deprecate_info=c.deprecate(target='--nat-pool-name', redirect='--nat-rule-name', hide=True))
742743
c.argument('nat_rule_name', help='Name to use for the NAT rule v2 when creating a new load balancer. (NAT rule V2 is used to replace NAT pool)', options_list=['--lb-nat-rule-name', '--nat-rule-name'])
@@ -1051,9 +1052,8 @@ def load_arguments(self, _):
10511052
c.argument('public_ip_address_allocation', help=None, default=None, arg_type=get_enum_type(['dynamic', 'static']))
10521053
c.argument('public_ip_address_dns_name', help='Globally unique DNS name for a newly created public IP.')
10531054
if self.supported_api_version(min_api='2017-08-01', resource_type=ResourceType.MGMT_NETWORK):
1054-
PublicIPAddressSkuName = self.get_models('PublicIPAddressSkuName', resource_type=ResourceType.MGMT_NETWORK)
10551055
c.argument('public_ip_sku', help='Public IP SKU. It is set to Basic by default. The public IP is supported to be created on edge zone only when it is \'Standard\'',
1056-
default=None, arg_type=get_enum_type(PublicIPAddressSkuName))
1056+
default=None, arg_type=get_enum_type(['Basic', 'Standard']))
10571057
c.argument('nic_delete_option', nargs='+', min_api='2021-03-01',
10581058
help='Specify what happens to the network interface when the VM is deleted. Use a singular '
10591059
'value to apply on all resources, or use <Name>=<Value> to configure '

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def build_nic_resource(_, name, location, tags, vm_name, subnet_id, private_ip_a
160160

161161
api_version = '2015-06-15'
162162
if application_security_groups:
163-
asg_ids = [{'id': x.id} for x in application_security_groups]
163+
asg_ids = [{'id': x['id']} for x in application_security_groups]
164164
nic_properties['ipConfigurations'][0]['properties']['applicationSecurityGroups'] = asg_ids
165165
api_version = '2017-09-01'
166166

@@ -983,7 +983,7 @@ def build_vmss_resource(cmd, name, computer_name_prefix, location, tags, overpro
983983

984984
if application_security_groups and cmd.supported_api_version(min_api='2018-06-01',
985985
operation_group='virtual_machine_scale_sets'):
986-
ip_config_properties['applicationSecurityGroups'] = [{'id': x.id} for x in application_security_groups]
986+
ip_config_properties['applicationSecurityGroups'] = [{'id': x['id']} for x in application_security_groups]
987987

988988
if ip_config_properties:
989989
ip_configuration = {

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

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@
2020
from azure.cli.core.commands.validators import (
2121
get_default_location_from_resource_group, validate_file_or_dict, validate_parameter_set, validate_tags)
2222
from azure.cli.core.util import (hash_string, DISALLOWED_USER_NAMES, get_default_admin_username)
23-
from azure.cli.command_modules.vm._vm_utils import (check_existence, get_storage_blob_uri, list_sku_info)
23+
from azure.cli.command_modules.vm._vm_utils import (check_existence, get_storage_blob_uri, list_sku_info,
24+
import_aaz_by_profile)
2425
from azure.cli.command_modules.vm._template_builder import StorageProfile
2526
from azure.cli.core import keys
2627
from azure.core.exceptions import ResourceNotFoundError
2728

2829
from ._client_factory import _compute_client_factory
2930
from ._actions import _get_latest_image_version
31+
32+
3033
logger = get_logger(__name__)
3134

3235

3336
def validate_asg_names_or_ids(cmd, namespace):
3437
from msrestazure.tools import resource_id, is_valid_resource_id
35-
from azure.cli.core.profiles import ResourceType
3638
from azure.cli.core.commands.client_factory import get_subscription_id
37-
ApplicationSecurityGroup = cmd.get_models('ApplicationSecurityGroup',
38-
resource_type=ResourceType.MGMT_NETWORK)
3939

4040
resource_group = namespace.resource_group_name
4141
subscription_id = get_subscription_id(cmd.cli_ctx)
@@ -53,7 +53,7 @@ def validate_asg_names_or_ids(cmd, namespace):
5353
namespace='Microsoft.Network', type='applicationSecurityGroups',
5454
name=val
5555
)
56-
ids.append(ApplicationSecurityGroup(id=val))
56+
ids.append({'id': val})
5757
setattr(namespace, 'application_security_groups', ids)
5858

5959

@@ -734,28 +734,31 @@ def _validate_vm_vmss_create_vnet(cmd, namespace, for_scale_set=False):
734734
logger.debug('no subnet specified. Attempting to find an existing Vnet and subnet...')
735735

736736
# if nothing specified, try to find an existing vnet and subnet in the target resource group
737-
client = get_network_client(cmd.cli_ctx).virtual_networks
737+
VnetList = import_aaz_by_profile(cmd.cli_ctx.cloud.profile, "network.vnet").List
738738

739-
# find VNET in target resource group that matches the VM's location with a matching subnet
740-
for vnet_match in (v for v in client.list(rg) if v.location == location and v.subnets):
739+
vnet_list = VnetList(cli_ctx=cmd.cli_ctx)(command_args={
740+
"resource_group": rg
741+
})
741742

743+
# find VNET in target resource group that matches the VM's location with a matching subnet
744+
for vnet_match in (v for v in vnet_list if bool(v['location'] == location and v['subnets'])):
742745
# 1 - find a suitable existing vnet/subnet
743746
result = None
744747
if not for_scale_set:
745-
result = next((s for s in vnet_match.subnets if s.name.lower() != 'gatewaysubnet'), None)
748+
result = next((s for s in vnet_match['subnets'] if s['name'].lower() != 'gatewaysubnet'), None)
746749
else:
747750
def _check_subnet(s):
748-
if s.name.lower() == 'gatewaysubnet':
751+
if s['name'].lower() == 'gatewaysubnet':
749752
return False
750-
subnet_mask = s.address_prefix.split('/')[-1]
753+
subnet_mask = s['addressPrefix'].split('/')[-1]
751754
return _subnet_capacity_check(subnet_mask, namespace.instance_count,
752755
not namespace.disable_overprovision)
753756

754-
result = next((s for s in vnet_match.subnets if _check_subnet(s)), None)
757+
result = next((s for s in vnet_match['subnets'] if _check_subnet(s)), None)
755758
if not result:
756759
continue
757-
namespace.subnet = result.name
758-
namespace.vnet_name = vnet_match.name
760+
namespace.subnet = result['name']
761+
namespace.vnet_name = vnet_match['name']
759762
namespace.vnet_type = 'existing'
760763
logger.debug("existing vnet '%s' and subnet '%s' found", namespace.vnet_name, namespace.subnet)
761764
return
@@ -981,18 +984,15 @@ def _validate_vm_vmss_create_public_ip(cmd, namespace):
981984
namespace.public_ip_address_type = 'new'
982985
logger.debug('new public IP address will be created')
983986

984-
from azure.cli.core.profiles import ResourceType
985-
PublicIPAddressSkuName, IPAllocationMethod = cmd.get_models('PublicIPAddressSkuName', 'IPAllocationMethod',
986-
resource_type=ResourceType.MGMT_NETWORK)
987987
# Use standard public IP address automatically when using zones.
988988
if hasattr(namespace, 'zone') and namespace.zone is not None:
989-
namespace.public_ip_sku = PublicIPAddressSkuName.standard.value
989+
namespace.public_ip_sku = 'Standard'
990990

991991
# Public-IP SKU is only exposed for VM. VMSS has no such needs so far
992992
if getattr(namespace, 'public_ip_sku', None):
993-
if namespace.public_ip_sku == PublicIPAddressSkuName.standard.value:
993+
if namespace.public_ip_sku == 'Standard':
994994
if not namespace.public_ip_address_allocation:
995-
namespace.public_ip_address_allocation = IPAllocationMethod.static.value
995+
namespace.public_ip_address_allocation = 'Static'
996996

997997

998998
def _validate_vmss_create_public_ip(cmd, namespace):
@@ -1505,12 +1505,19 @@ def process_vm_update_namespace(cmd, namespace):
15051505
# region VMSS Create Validators
15061506
def _get_default_address_pool(cli_ctx, resource_group, balancer_name, balancer_type):
15071507
option_name = '--backend-pool-name'
1508-
client = getattr(get_network_client(cli_ctx), balancer_type, None)
1509-
if not client:
1508+
1509+
if balancer_type == 'application_gateways':
1510+
client = import_aaz_by_profile(cli_ctx.cloud.profile, "network.application_gateway")
1511+
elif balancer_type == 'load_balancers':
1512+
client = import_aaz_by_profile(cli_ctx.cloud.profile, "network.lb")
1513+
else:
15101514
raise CLIError('unrecognized balancer type: {}'.format(balancer_type))
15111515

1512-
balancer = client.get(resource_group, balancer_name)
1513-
values = [x.name for x in balancer.backend_address_pools]
1516+
balancer = client.Show(cli_ctx=cli_ctx)(command_args={
1517+
'name': balancer_name,
1518+
'resource_group': resource_group
1519+
})
1520+
values = [x['name'] for x in balancer['backendAddressPools']]
15141521
if len(values) > 1:
15151522
raise CLIError("Multiple possible values found for '{0}': {1}\nSpecify '{0}' "
15161523
"explicitly.".format(option_name, ', '.join(values)))
@@ -1555,12 +1562,15 @@ def _validate_vmss_create_load_balancer_or_app_gateway(cmd, namespace):
15551562
if balancer_type == 'applicationGateway':
15561563

15571564
if namespace.application_gateway:
1558-
client = get_network_client(cmd.cli_ctx).application_gateways
1565+
client = import_aaz_by_profile(cmd.cli_ctx.cloud.profile, "network.application_gateway")
15591566
try:
15601567
rg = parse_resource_id(namespace.application_gateway).get(
15611568
'resource_group', namespace.resource_group_name)
15621569
ag_name = parse_resource_id(namespace.application_gateway)['name']
1563-
client.get(rg, ag_name)
1570+
client.Show(cli_ctx=cmd.cli_ctx)(command_args={
1571+
'name': ag_name,
1572+
'resource_group': rg
1573+
})
15641574
namespace.app_gateway_type = 'existing'
15651575
namespace.backend_pool_name = namespace.backend_pool_name or \
15661576
_get_default_address_pool(cmd.cli_ctx, rg, ag_name, 'application_gateways')
@@ -1603,11 +1613,11 @@ def _validate_vmss_create_load_balancer_or_app_gateway(cmd, namespace):
16031613
namespace.backend_pool_name = namespace.backend_pool_name or \
16041614
_get_default_address_pool(cmd.cli_ctx, rg, lb_name, 'load_balancers')
16051615
if not namespace.nat_pool_name:
1606-
if len(lb.inbound_nat_pools) > 1:
1616+
if len(lb['inboundNatPools']) > 1:
16071617
raise CLIError("Multiple possible values found for '{0}': {1}\nSpecify '{0}' explicitly.".format( # pylint: disable=line-too-long
1608-
'--nat-pool-name', ', '.join([n.name for n in lb.inbound_nat_pools])))
1609-
if lb.inbound_nat_pools:
1610-
namespace.nat_pool_name = lb.inbound_nat_pools[0].name
1618+
'--nat-pool-name', ', '.join([n['name'] for n in lb['inboundNatPools']])))
1619+
if lb['inboundNatPools']:
1620+
namespace.nat_pool_name = lb['inboundNatPools'][0]['name']
16111621
logger.debug("using specified existing load balancer '%s'", namespace.load_balancer)
16121622
else:
16131623
namespace.load_balancer_type = 'new'
@@ -1620,11 +1630,10 @@ def _validate_vmss_create_load_balancer_or_app_gateway(cmd, namespace):
16201630
logger.debug('new load balancer will be created')
16211631

16221632
if namespace.load_balancer_type == 'new' and namespace.single_placement_group is False and std_lb_is_available:
1623-
LBSkuName = cmd.get_models('LoadBalancerSkuName', resource_type=ResourceType.MGMT_NETWORK)
16241633
if namespace.load_balancer_sku is None:
1625-
namespace.load_balancer_sku = LBSkuName.standard.value
1634+
namespace.load_balancer_sku = 'Standard'
16261635
logger.debug("use Standard sku as single placement group is turned off")
1627-
elif namespace.load_balancer_sku == LBSkuName.basic.value:
1636+
elif namespace.load_balancer_sku == 'Basic':
16281637
if namespace.zones:
16291638
err = "'Standard' load balancer is required for zonal scale-sets"
16301639
elif namespace.instance_count > 100:
@@ -1635,17 +1644,14 @@ def _validate_vmss_create_load_balancer_or_app_gateway(cmd, namespace):
16351644
raise CLIError('usage error:{}'.format(err))
16361645

16371646

1638-
def get_network_client(cli_ctx):
1639-
from azure.cli.core.profiles import ResourceType
1640-
from azure.cli.core.commands.client_factory import get_mgmt_service_client
1641-
return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_NETWORK)
1642-
1643-
16441647
def get_network_lb(cli_ctx, resource_group_name, lb_name):
16451648
from azure.core.exceptions import HttpResponseError
1646-
network_client = get_network_client(cli_ctx)
1649+
LBShow = import_aaz_by_profile(cli_ctx.cloud.profile, "network.lb").Show
16471650
try:
1648-
return network_client.load_balancers.get(resource_group_name, lb_name)
1651+
return LBShow(cli_ctx=cli_ctx)({
1652+
"name": lb_name,
1653+
"resource_group": resource_group_name
1654+
})
16491655
except HttpResponseError:
16501656
return None
16511657

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import os
88
import re
9+
import importlib
910

1011
from azure.cli.core.commands.arm import ArmTemplateBuilder
1112

@@ -584,3 +585,8 @@ def display_region_recommendation(cmd, namespace):
584585
identified_region)
585586
from azure.cli.core import telemetry
586587
telemetry.set_region_identified(namespace.location, identified_region)
588+
589+
590+
def import_aaz_by_profile(profile, module_name):
591+
profile_module_name = profile.lower().replace('-', '_')
592+
return importlib.import_module(f"azure.cli.command_modules.vm.aaz.{profile_module_name}.{module_name}")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
#
5+
# Code generated by aaz-dev-tools
6+
# --------------------------------------------------------------------------------------------
7+
8+
# pylint: skip-file
9+
# flake8: noqa
10+
11+
from azure.cli.core.aaz import *
12+
13+
14+
class __CMDGroup(AAZCommandGroup):
15+
"""Manage Azure Network resources.
16+
"""
17+
pass
18+
19+
20+
__all__ = ["__CMDGroup"]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
#
5+
# Code generated by aaz-dev-tools
6+
# --------------------------------------------------------------------------------------------
7+
8+
# pylint: skip-file
9+
# flake8: noqa
10+
11+
from .__cmd_group import *
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
#
5+
# Code generated by aaz-dev-tools
6+
# --------------------------------------------------------------------------------------------
7+
8+
# pylint: skip-file
9+
# flake8: noqa
10+
11+
from azure.cli.core.aaz import *
12+
13+
14+
class __CMDGroup(AAZCommandGroup):
15+
"""Manage application-level routing and load balancing services.
16+
17+
To learn more about Application Gateway, visit https://learn.microsoft.com/en-us/azure/application-gateway/quick-create-cli.
18+
"""
19+
pass
20+
21+
22+
__all__ = ["__CMDGroup"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
#
5+
# Code generated by aaz-dev-tools
6+
# --------------------------------------------------------------------------------------------
7+
8+
# pylint: skip-file
9+
# flake8: noqa
10+
11+
from .__cmd_group import *
12+
from ._show import *

0 commit comments

Comments
 (0)