Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 additions & 0 deletions src/azure-cli/azure/cli/command_modules/network/_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import argparse
from knack.util import CLIError
from azure.cli.core.azclierror import UnrecognizedArgumentError
from msrestazure.tools import is_valid_resource_id
from ._validators import read_base_64_file


Expand All @@ -33,6 +34,11 @@ def get_action(self, values, option_string): # pylint: disable=no-self-use
d['name'] = v[0]
elif kl == 'ip-address':
d['ip_address'] = v[0]
elif kl == 'subnet':
if is_valid_resource_id(v[0]):
d['subnet'] = v[0]
else:
raise UnrecognizedArgumentError(f'{v[0]} is not valid subnet id')
else:
raise CLIError('key error: key must be one of name and ip-address.')
return d
Expand Down
13 changes: 11 additions & 2 deletions src/azure-cli/azure/cli/command_modules/network/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -3337,12 +3337,14 @@
short-summary: Create an address pool.
parameters:
- name: --backend-address
short-summary: Backend addresses information for backend address pool. If it's used, --vnet is also required.
short-summary: Backend addresses information for backend address pool. If it's used, --vnet is required or subnet is required.
long-summary: |
Usage: --backend-address name=addr1 ip-address=10.0.0.1 --vnet MyVnet
Usage1: --backend-address name=addr1 ip-address=10.0.0.1 --vnet MyVnet
Usage2: --backend-address name=addr1 ip-address=10.0.0.1 subnet==/subscriptions/000/resourceGroups/MyRg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/subnet1
name: Required. The name of the backend address.
ip-address: Required. Ip Address within the Virtual Network.
subnet: Id of the subnet.(Required if --vnet lacks)
Multiple backend addresses can be specified by using more than one `--backend-address` argument.
- name: --backend-addresses-config-file
Expand All @@ -3361,6 +3363,11 @@
"name": "address2",
"virtualNetwork": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_lb_address_pool_addresses000001/providers/Microsoft.Network/virtualNetworks/clitestvnet",
"ipAddress": "10.0.0.5"
},
{
"name": "address3",
"subnet": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyRg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/subnet2",
"ipAddress": "10.0.0.6"
}
]
examples:
Expand Down Expand Up @@ -3407,6 +3414,8 @@
examples:
- name: Add one backend address into the load balance backend address pool.
text: az network lb address-pool address add -g MyResourceGroup --lb-name MyLb --pool-name MyAddressPool -n MyAddress --vnet MyVnet --ip-address 10.0.0.1
- name: Add one backend address into the load balance backend address pool with subnet.
text: az network lb address-pool address add -g MyResourceGroup --lb-name MyLb --pool-name MyAddressPool -n MyAddress --subnet /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyRg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/subnet2 --ip-address 10.0.0.1
"""

helps['network lb address-pool address remove'] = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
validate_user_assigned_identity, validate_virtul_network_gateway, validate_private_dns_zone,
NWConnectionMonitorEndpointFilterItemAction, NWConnectionMonitorTestConfigurationHTTPRequestHeaderAction,
process_private_link_resource_id_argument, process_private_endpoint_connection_id_argument,
process_vnet_name_or_id, validate_trusted_client_cert)
process_vnet_name_or_id, validate_trusted_client_cert, validator_subnet_id)
from azure.mgmt.trafficmanager.models import MonitorProtocol, ProfileStatus
from azure.cli.command_modules.network._completers import (
subnet_completion_list, get_lb_subresource_completion_list, get_ag_subresource_completion_list,
Expand Down Expand Up @@ -974,6 +974,7 @@ def load_arguments(self, _):
c.argument('address_name', options_list=['--name', '-n'], help='Name of the backend address.')
c.argument('vnet', help='Name or Id of the virtual network.', validator=process_vnet_name_or_id)
c.argument('ip_address', help='Ip Address within the Virtual Network.')
c.argument('subnet', help='Id of the existing subnet.', validator=validator_subnet_id, min_api='2020-11-01')

with self.argument_context('network lb frontend-ip') as c:
c.argument('zone', zone_type, min_api='2017-06-01', max_api='2020-07-01')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from azure.cli.core.commands.client_factory import get_subscription_id, get_mgmt_service_client
from azure.cli.core.commands.validators import validate_parameter_set
from azure.cli.core.profiles import ResourceType
from azure.cli.core.azclierror import RequiredArgumentMissingError
from azure.cli.core.azclierror import RequiredArgumentMissingError, UnrecognizedArgumentError

logger = get_logger(__name__)

Expand Down Expand Up @@ -1963,6 +1963,12 @@ def process_vnet_name_or_id(cmd, namespace):
name=namespace.vnet)


def validator_subnet_id(namespace):
from azure.mgmt.core.tools import is_valid_resource_id
if namespace.subnet and not is_valid_resource_id(namespace.subnet):
raise UnrecognizedArgumentError(f'{namespace.subnet} in not valid')


def process_appgw_waf_policy_update(cmd, namespace): # pylint: disable=unused-argument
rule_group_name = namespace.rule_group_name
rules = namespace.rules
Expand Down
43 changes: 32 additions & 11 deletions src/azure-cli/azure/cli/command_modules/network/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2760,7 +2760,8 @@ def create_express_route_connection(cmd, resource_group_name, express_route_gate

propagated_route_tables = PropagatedRouteTable(
labels=labels,
ids=[SubResource(id=propagated_route_table) for propagated_route_table in propagated_route_tables]
ids=[SubResource(id=propagated_route_table) for propagated_route_table in
propagated_route_tables] if propagated_route_tables else None
)
routing_configuration = RoutingConfiguration(
associated_route_table=SubResource(id=associated_route_table),
Expand Down Expand Up @@ -3488,8 +3489,10 @@ def _process_vnet_name_and_id(vnet):
lb = lb_get(ncf.load_balancers, resource_group_name, load_balancer_name)
(BackendAddressPool,
LoadBalancerBackendAddress,
Subnet,
VirtualNetwork) = cmd.get_models('BackendAddressPool',
'LoadBalancerBackendAddress',
'Subnet',
'VirtualNetwork')
# Before 2020-03-01, service doesn't support the other rest method.
# We have to use old one to keep backward compatibility.
Expand All @@ -3509,12 +3512,22 @@ def _process_vnet_name_and_id(vnet):
if 'virtual_network' not in addr and vnet:
addr['virtual_network'] = vnet
# pylint: disable=line-too-long
try:
new_addresses = [LoadBalancerBackendAddress(name=addr['name'],
virtual_network=VirtualNetwork(id=_process_vnet_name_and_id(addr['virtual_network'])),
ip_address=addr['ip_address']) for addr in addresses_pool] if addresses_pool else None
except KeyError:
raise CLIError('Each backend address must have name, vnet and ip-address information.')
if cmd.supported_api_version(min_api='2020-11-01'):
try:
new_addresses = [LoadBalancerBackendAddress(name=addr['name'],
virtual_network=VirtualNetwork(id=_process_vnet_name_and_id(addr['virtual_network'])) if 'virtual_network' in addr else None,
subnet=Subnet(id=addr['subnet']) if 'subnet' in addr else None,
ip_address=addr['ip_address']) for addr in addresses_pool] if addresses_pool else None
except KeyError:
raise CLIError('Each backend address must have name, ip-address, (vnet | subnet) information.')
else:
try:
new_addresses = [LoadBalancerBackendAddress(name=addr['name'],
virtual_network=VirtualNetwork(id=_process_vnet_name_and_id(addr['virtual_network'])),
ip_address=addr['ip_address']) for addr in addresses_pool] if addresses_pool else None
except KeyError:
raise CLIError('Each backend address must have name, vnet and ip-address information.')

new_pool = BackendAddressPool(name=backend_address_pool_name,
load_balancer_backend_addresses=new_addresses)
return ncf.load_balancer_backend_address_pools.begin_create_or_update(resource_group_name,
Expand Down Expand Up @@ -3780,15 +3793,23 @@ def set_cross_region_lb_rule(


def add_lb_backend_address_pool_address(cmd, resource_group_name, load_balancer_name, backend_address_pool_name,
address_name, vnet, ip_address):
address_name, ip_address, vnet=None, subnet=None):
client = network_client_factory(cmd.cli_ctx).load_balancer_backend_address_pools
address_pool = client.get(resource_group_name, load_balancer_name, backend_address_pool_name)
(LoadBalancerBackendAddress,
Subnet,
VirtualNetwork) = cmd.get_models('LoadBalancerBackendAddress',
'Subnet',
'VirtualNetwork')
new_address = LoadBalancerBackendAddress(name=address_name,
virtual_network=VirtualNetwork(id=vnet) if vnet else None,
ip_address=ip_address if ip_address else None)
if cmd.supported_api_version(min_api='2020-11-01'):
new_address = LoadBalancerBackendAddress(name=address_name,
subnet=Subnet(id=subnet) if subnet else None,
virtual_network=VirtualNetwork(id=vnet) if vnet else None,
ip_address=ip_address if ip_address else None)
else:
new_address = LoadBalancerBackendAddress(name=address_name,
virtual_network=VirtualNetwork(id=vnet) if vnet else None,
ip_address=ip_address if ip_address else None)
if address_pool.load_balancer_backend_addresses is None:
address_pool.load_balancer_backend_addresses = []
address_pool.load_balancer_backend_addresses.append(new_address)
Expand Down
Loading