Skip to content
Merged
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
4 changes: 3 additions & 1 deletion src/azure-cli/azure/cli/command_modules/network/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -3308,7 +3308,9 @@
helps['network lb'] = """
type: group
short-summary: Manage and configure load balancers.
long-summary: To learn more about Azure Load Balancer visit https://docs.microsoft.com/azure/load-balancer/load-balancer-get-started-internet-arm-cli
long-summary: |
[Coming breaking change] In the coming release, the default behavior will be changed. When sku is Standard and in zone-redundant regions, the default 'zones' of 'frontendIPConfigurations' will display as 'zones:[1,2,3]' instead of 'zones:null'.
To learn more about Azure Load Balancer visit https://docs.microsoft.com/azure/load-balancer/load-balancer-get-started-internet-arm-cli
"""

helps['network lb wait'] = """
Expand Down
29 changes: 29 additions & 0 deletions src/azure-cli/azure/cli/command_modules/network/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from azure.cli.core.util import sdk_no_wait

from ._client_factory import network_client_factory
from .custom import lb_get


def list_network_resource_property(resource, prop):
Expand Down Expand Up @@ -66,3 +67,31 @@ def delete_func(cmd, resource_group_name, resource_name, item_name, no_wait=Fals
func_name = 'delete_network_resource_property_entry_{}_{}'.format(resource, prop)
setattr(sys.modules[__name__], func_name, delete_func)
return func_name


# workaround for : https://github.com/Azure/azure-cli/issues/17071
def delete_lb_resource_property_entry(resource, prop):
""" Factory method for creating delete functions. """

def delete_func(cmd, resource_group_name, resource_name, item_name, no_wait=False): # pylint: disable=unused-argument
client = getattr(network_client_factory(cmd.cli_ctx), resource)
item = lb_get(client, resource_group_name, resource_name)

if item.__getattribute__(prop) is not None:
keep_items = [x for x in item.__getattribute__(prop) if x.name.lower() != item_name.lower()]
else:
keep_items = None

with cmd.update_context(item) as c:
c.set_param(prop, keep_items)
if no_wait:
sdk_no_wait(no_wait, client.begin_create_or_update, resource_group_name, resource_name, item)
else:
result = sdk_no_wait(no_wait, client.begin_create_or_update,
resource_group_name, resource_name, item).result()
if next((x for x in getattr(result, prop) or [] if x.name.lower() == item_name.lower()), None):
raise CLIError("Failed to delete '{}' on '{}'".format(item_name, resource_name))

func_name = 'delete_network_resource_property_entry_{}_{}'.format(resource, prop)
Copy link
Member

Choose a reason for hiding this comment

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

Should be delete_lb_resource_property_entry_

Copy link
Member Author

Choose a reason for hiding this comment

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

good job. Will be fixed in another PR

setattr(sys.modules[__name__], func_name, delete_func)
return func_name
32 changes: 26 additions & 6 deletions src/azure-cli/azure/cli/command_modules/network/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
cf_network_virtual_appliances, cf_virtual_appliance_skus, cf_virtual_appliance_sites, cf_virtual_hub,
cf_virtual_hub_bgp_connection, cf_virtual_hub_bgp_connections)
from azure.cli.command_modules.network._util import (
list_network_resource_property, get_network_resource_property_entry, delete_network_resource_property_entry)
list_network_resource_property, get_network_resource_property_entry, delete_network_resource_property_entry,
delete_lb_resource_property_entry)
from azure.cli.command_modules.network._format import (
transform_local_gateway_table_output, transform_dns_record_set_output,
transform_dns_record_set_table_output, transform_dns_zone_table_output,
Expand Down Expand Up @@ -445,6 +446,12 @@ def load_command_table(self, _):

network_custom = CliCommandType(operations_tmpl='azure.cli.command_modules.network.custom#{}')

network_load_balancers_custom = CliCommandType(
operations_tmpl='azure.cli.command_modules.network.custom#{}',
client_factory=cf_load_balancers,
min_api='2020-08-01'
)

# endregion

# region NetworkRoot
Expand Down Expand Up @@ -865,7 +872,8 @@ def _make_singular(value):
g.command('delete', 'begin_delete')
g.custom_command('list', 'list_lbs')
g.wait_command('wait')
g.generic_update_command('update', setter_name='begin_create_or_update')
g.generic_update_command('update', getter_name='lb_get', getter_type=network_load_balancers_custom,
setter_name='begin_create_or_update')

property_map = {
'frontend_ip_configurations': 'frontend-ip',
Expand All @@ -878,24 +886,30 @@ def _make_singular(value):
with self.command_group('network lb {}'.format(alias), network_util) as g:
g.command('list', list_network_resource_property('load_balancers', subresource))
g.show_command('show', get_network_resource_property_entry('load_balancers', subresource))
g.command('delete', delete_network_resource_property_entry('load_balancers', subresource))
g.command('delete', delete_lb_resource_property_entry('load_balancers', subresource))

with self.command_group('network lb frontend-ip', network_lb_sdk) as g:
g.custom_command('create', 'create_lb_frontend_ip_configuration', validator=process_lb_frontend_ip_namespace)
g.generic_update_command('update', child_collection_prop_name='frontend_ip_configurations',
getter_name='lb_get',
getter_type=network_load_balancers_custom,
setter_name='begin_create_or_update',
custom_func_name='set_lb_frontend_ip_configuration',
validator=process_lb_frontend_ip_namespace)

with self.command_group('network lb inbound-nat-rule', network_lb_sdk) as g:
g.custom_command('create', 'create_lb_inbound_nat_rule')
g.generic_update_command('update', child_collection_prop_name='inbound_nat_rules',
getter_name='lb_get',
getter_type=network_load_balancers_custom,
setter_name='begin_create_or_update',
custom_func_name='set_lb_inbound_nat_rule')

with self.command_group('network lb inbound-nat-pool', network_lb_sdk) as g:
g.custom_command('create', 'create_lb_inbound_nat_pool')
g.generic_update_command('update', child_collection_prop_name='inbound_nat_pools',
getter_name='lb_get',
getter_type=network_load_balancers_custom,
setter_name='begin_create_or_update',
custom_func_name='set_lb_inbound_nat_pool')

Expand All @@ -911,7 +925,7 @@ def _make_singular(value):
with self.command_group('network lb address-pool', network_util, max_api='2020-03-01') as g:
g.command('list', list_network_resource_property('load_balancers', 'backend_address_pools'))
g.show_command('show', get_network_resource_property_entry('load_balancers', 'backend_address_pools'))
g.command('delete', delete_network_resource_property_entry('load_balancers', 'backend_address_pools'))
g.command('delete', delete_lb_resource_property_entry('load_balancers', 'backend_address_pools'))

with self.command_group('network lb address-pool address', network_lb_backend_pool_sdk, is_preview=True) as g:
g.custom_command('add', 'add_lb_backend_address_pool_address')
Expand All @@ -921,25 +935,31 @@ def _make_singular(value):
with self.command_group('network lb rule', network_lb_sdk) as g:
g.custom_command('create', 'create_lb_rule')
g.generic_update_command('update', child_collection_prop_name='load_balancing_rules',
getter_name='lb_get',
getter_type=network_load_balancers_custom,
setter_name='begin_create_or_update',
custom_func_name='set_lb_rule')

with self.command_group('network lb probe', network_lb_sdk) as g:
g.custom_command('create', 'create_lb_probe')
g.generic_update_command('update', child_collection_prop_name='probes',
getter_name='lb_get',
getter_type=network_load_balancers_custom,
setter_name='begin_create_or_update',
custom_func_name='set_lb_probe')

with self.command_group('network lb outbound-rule', network_lb_sdk, min_api='2018-07-01') as g:
g.custom_command('create', 'create_lb_outbound_rule', validator=process_lb_outbound_rule_namespace)
g.generic_update_command('update', child_collection_prop_name='outbound_rules',
getter_name='lb_get',
getter_type=network_load_balancers_custom,
setter_name='begin_create_or_update',
custom_func_name='set_lb_outbound_rule', validator=process_lb_outbound_rule_namespace)

with self.command_group('network lb outbound-rule', network_util, min_api='2018-07-01') as g:
g.command('list', list_network_resource_property('load_balancers', 'outbound_rules'))
g.show_command('show', get_network_resource_property_entry('load_balancers', 'outbound_rules'))
g.command('delete', delete_network_resource_property_entry('load_balancers', 'outbound_rules'))
g.command('delete', delete_lb_resource_property_entry('load_balancers', 'outbound_rules'))
# endregion

# region cross-region load balancer
Expand All @@ -961,7 +981,7 @@ def _make_singular(value):
with self.command_group('network cross-region-lb {}'.format(alias), network_util) as g:
g.command('list', list_network_resource_property('load_balancers', subresource))
g.show_command('show', get_network_resource_property_entry('load_balancers', subresource))
g.command('delete', delete_network_resource_property_entry('load_balancers', subresource))
g.command('delete', delete_lb_resource_property_entry('load_balancers', subresource))

with self.command_group('network cross-region-lb frontend-ip', network_lb_sdk) as g:
g.custom_command('create', 'create_cross_region_lb_frontend_ip_configuration', validator=process_cross_region_lb_frontend_ip_namespace)
Expand Down
33 changes: 25 additions & 8 deletions src/azure-cli/azure/cli/command_modules/network/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3287,7 +3287,7 @@ def create_lb_inbound_nat_rule(
backend_port, frontend_ip_name=None, floating_ip=None, idle_timeout=None, enable_tcp_reset=None):
InboundNatRule = cmd.get_models('InboundNatRule')
ncf = network_client_factory(cmd.cli_ctx)
lb = ncf.load_balancers.get(resource_group_name, load_balancer_name)
lb = lb_get(ncf.load_balancers, resource_group_name, load_balancer_name)
if not frontend_ip_name:
frontend_ip_name = _get_default_name(lb, 'frontend_ip_configurations', '--frontend-ip-name')
frontend_ip = get_property(lb.frontend_ip_configurations, frontend_ip_name) # pylint: disable=no-member
Expand All @@ -3303,6 +3303,21 @@ def create_lb_inbound_nat_rule(
return get_property(poller.result().inbound_nat_rules, item_name)


# workaround for : https://github.com/Azure/azure-cli/issues/17071
def lb_get(client, resource_group_name, load_balancer_name):
lb = client.get(resource_group_name, load_balancer_name)
return lb_get_operation(lb)


# workaround for : https://github.com/Azure/azure-cli/issues/17071
def lb_get_operation(lb):
for item in lb.frontend_ip_configurations:
if item.zones is not None and len(item.zones) >= 3:
Copy link
Member

@jsntcy jsntcy Feb 25, 2021

Choose a reason for hiding this comment

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

Is it possible that zones is [1, 2], [1, 2, 3, 4]?
Is it possible that lb.frontend_ip_configurations is None?

Copy link
Member Author

Choose a reason for hiding this comment

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

(1)'zones' don't support input more than 1
(2)'frontend_ip_configurations' will not be None when get. Because it will be set to default value even customers do not set any parameters

item.zones = None
Comment on lines +3315 to +3316
Copy link
Contributor

Choose a reason for hiding this comment

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

If user set zones [1, 2, 3] on purpose, the output of zones will be None?

Copy link
Member Author

Choose a reason for hiding this comment

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

I have a test and it does not influence.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can user set zones as [1, 2, 3] in current version? If they can what's the response now?

Copy link
Member Author

Choose a reason for hiding this comment

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

(1)'zones' don't support input more than 1


return lb


def set_lb_inbound_nat_rule(
cmd, instance, parent, item_name, protocol=None, frontend_port=None,
frontend_ip_name=None, backend_port=None, floating_ip=None, idle_timeout=None, enable_tcp_reset=None):
Expand All @@ -3329,7 +3344,7 @@ def create_lb_inbound_nat_pool(
floating_ip=None, idle_timeout=None):
InboundNatPool = cmd.get_models('InboundNatPool')
ncf = network_client_factory(cmd.cli_ctx)
lb = ncf.load_balancers.get(resource_group_name, load_balancer_name)
lb = lb_get(ncf.load_balancers, resource_group_name, load_balancer_name)
if not frontend_ip_name:
frontend_ip_name = _get_default_name(lb, 'frontend_ip_configurations', '--frontend-ip-name')
frontend_ip = get_property(lb.frontend_ip_configurations, frontend_ip_name) \
Expand Down Expand Up @@ -3380,7 +3395,7 @@ def create_lb_frontend_ip_configuration(
FrontendIPConfiguration, SubResource, Subnet = cmd.get_models(
'FrontendIPConfiguration', 'SubResource', 'Subnet')
ncf = network_client_factory(cmd.cli_ctx)
lb = ncf.load_balancers.get(resource_group_name, load_balancer_name)
lb = lb_get(ncf.load_balancers, resource_group_name, load_balancer_name)

if private_ip_address_allocation is None:
private_ip_address_allocation = 'static' if private_ip_address else 'dynamic'
Expand Down Expand Up @@ -3454,7 +3469,7 @@ def _process_vnet_name_and_id(vnet):
if not isinstance(addr, dict):
raise CLIError('Each address in config file must be a dictionary. Please see example as a reference.')
ncf = network_client_factory(cmd.cli_ctx)
lb = ncf.load_balancers.get(resource_group_name, load_balancer_name)
lb = lb_get(ncf.load_balancers, resource_group_name, load_balancer_name)
(BackendAddressPool,
LoadBalancerBackendAddress,
VirtualNetwork) = cmd.get_models('BackendAddressPool',
Expand Down Expand Up @@ -3495,7 +3510,7 @@ def _process_vnet_name_and_id(vnet):
def delete_lb_backend_address_pool(cmd, resource_group_name, load_balancer_name, backend_address_pool_name):
from azure.cli.core.commands import LongRunningOperation
ncf = network_client_factory(cmd.cli_ctx)
lb = ncf.load_balancers.get(resource_group_name, load_balancer_name)
lb = lb_get(ncf.load_balancers, resource_group_name, load_balancer_name)

def delete_basic_lb_backend_address_pool():
new_be_pools = [pool for pool in lb.backend_address_pools
Expand Down Expand Up @@ -3596,7 +3611,7 @@ def create_cross_region_lb_frontend_ip_configuration(
FrontendIPConfiguration, SubResource = cmd.get_models(
'FrontendIPConfiguration', 'SubResource')
ncf = network_client_factory(cmd.cli_ctx)
lb = ncf.load_balancers.get(resource_group_name, load_balancer_name)
lb = lb_get(ncf.load_balancers, resource_group_name, load_balancer_name)

new_config = FrontendIPConfiguration(
name=item_name,
Expand Down Expand Up @@ -3694,6 +3709,7 @@ def create_cross_region_lb_rule(
LoadBalancingRule = cmd.get_models('LoadBalancingRule')
ncf = network_client_factory(cmd.cli_ctx)
lb = cached_get(cmd, ncf.load_balancers.get, resource_group_name, load_balancer_name)
lb = lb_get_operation(lb)
if not frontend_ip_name:
frontend_ip_name = _get_default_name(lb, 'frontend_ip_configurations', '--frontend-ip-name')
if not backend_address_pool_name:
Expand Down Expand Up @@ -3788,7 +3804,7 @@ def create_lb_outbound_rule(cmd, resource_group_name, load_balancer_name, item_n
outbound_ports=None, enable_tcp_reset=None, idle_timeout=None):
OutboundRule, SubResource = cmd.get_models('OutboundRule', 'SubResource')
client = network_client_factory(cmd.cli_ctx).load_balancers
lb = client.get(resource_group_name, load_balancer_name)
lb = lb_get(client, resource_group_name, load_balancer_name)
rule = OutboundRule(
protocol=protocol, enable_tcp_reset=enable_tcp_reset, idle_timeout_in_minutes=idle_timeout,
backend_address_pool=SubResource(id=backend_address_pool),
Expand Down Expand Up @@ -3820,7 +3836,7 @@ def create_lb_probe(cmd, resource_group_name, load_balancer_name, item_name, pro
path=None, interval=None, threshold=None):
Probe = cmd.get_models('Probe')
ncf = network_client_factory(cmd.cli_ctx)
lb = ncf.load_balancers.get(resource_group_name, load_balancer_name)
lb = lb_get(ncf.load_balancers, resource_group_name, load_balancer_name)
new_probe = Probe(
protocol=protocol, port=port, interval_in_seconds=interval, number_of_probes=threshold,
request_path=path, name=item_name)
Expand Down Expand Up @@ -3848,6 +3864,7 @@ def create_lb_rule(
LoadBalancingRule = cmd.get_models('LoadBalancingRule')
ncf = network_client_factory(cmd.cli_ctx)
lb = cached_get(cmd, ncf.load_balancers.get, resource_group_name, load_balancer_name)
lb = lb_get_operation(lb)
if not frontend_ip_name:
frontend_ip_name = _get_default_name(lb, 'frontend_ip_configurations', '--frontend-ip-name')
if not backend_address_pool_name:
Expand Down
Loading