Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Network] Migrate network to track2 SDK #16245

Merged
merged 4 commits into from
Dec 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion scripts/sdk_process/patch_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ def find_autorest_generated_folder(module_prefix="azure.mgmt"):
track2_packages = [
'azure.mgmt.keyvault',
'azure.mgmt.storage',
'azure.mgmt.compute'
'azure.mgmt.compute',
'azure.mgmt.network'
Copy link
Member Author

Choose a reason for hiding this comment

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

Add package here to pass CI.

]
prefix = sys.argv[1] if len(sys.argv) >= 2 else "azure.mgmt"
for autorest_package in find_autorest_generated_folder(prefix):
Expand Down
7 changes: 6 additions & 1 deletion src/azure-cli-core/azure/cli/core/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ def _resolve_model(self):

doc_string = doc_string.replace('\r', '').replace('\n', ' ')
doc_string = re.sub(' +', ' ', doc_string)
model_name_regex = re.compile(r':return: (.*that returns )?(?P<model>[a-zA-Z]*)')

# pylint: disable=line-too-long
# In track1, the doc_string for return type is like ':return: An instance of LROPoller that returns ConnectionSharedKey or ClientRawResponse<ConnectionSharedKey>'
# In track2, the doc_string for return type is like ':return: An instance of LROPoller that returns either ConnectionSharedKey or the result of cls(response)'
# Add '(?:either )?' to match 'either' zero or one times to support track2.
model_name_regex = re.compile(r':return: (?:.*?that returns (?:either )?)?(?P<model>[a-zA-Z]*)')
Copy link
Member

@jiasli jiasli Dec 18, 2020

Choose a reason for hiding this comment

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

This is dark magic. Consider adding more comments.

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 catch. Originally I added comments, it seems when I resolved conflicts manually after rebasing dev, the comments was removed accidentally. I'll add comments here to explain why this change happened.

Copy link
Member

Choose a reason for hiding this comment

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

Could add more annotation or example? The regex is not clear.

model_path_regex = re.compile(r':rtype:.*(?P<path>azure.mgmt[a-zA-Z0-9_\.]*)')
try:
self._model_name = model_name_regex.search(doc_string).group('model')
Expand Down
3 changes: 2 additions & 1 deletion src/azure-cli-core/azure/cli/core/commands/arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ def get_provisioning_state(instance):
def handler(args):
from azure.cli.core.commands.client_factory import resolve_client_arg_name
from msrest.exceptions import ClientException
from azure.core.exceptions import HttpResponseError
import time

context_copy = copy.copy(context)
Expand Down Expand Up @@ -695,7 +696,7 @@ def handler(args):
custom_condition and bool(verify_property(instance, custom_condition)):
progress_indicator.end()
return None
except ClientException as ex:
except (ClientException, HttpResponseError) as ex:
progress_indicator.stop()
if getattr(ex, 'status_code', None) == 404:
if wait_for_deleted:
Expand Down
8 changes: 8 additions & 0 deletions src/azure-cli-core/azure/cli/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,14 @@ def augment_no_wait_handler_args(no_wait_enabled, handler, handler_args):
# support autorest 3
handler_args['polling'] = False

# Support track2 SDK.
# In track2 SDK, there is no parameter 'polling' in SDK, but just use '**kwargs'.
# So we check the name of the operation to see if it's a long running operation.
# The name of long running operation in SDK is like 'begin_xxx_xxx'.
op_name = handler.__name__
if op_name and op_name.startswith('begin_') and no_wait_enabled:
handler_args['polling'] = False


def sdk_no_wait(no_wait, func, *args, **kwargs):
if no_wait:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Sat Nov 07 15:41:37 Pacific Standard Time 2020
#Fri Dec 11 12:04:49 China Standard Time 2020
Color=Red
Region=West US
feature-management.FalseFeature=false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def _ensure_subnet_service_endpoint(cli_ctx, subnet_id):
if not service_endpoint_exists:
web_service_endpoint = ServiceEndpointPropertiesFormat(service="Microsoft.Web")
subnet_obj.service_endpoints.append(web_service_endpoint)
poller = vnet_client.subnets.create_or_update(
poller = vnet_client.subnets.begin_create_or_update(
subnet_resource_group, subnet_vnet_name,
subnet_name, subnet_parameters=subnet_obj)
# Ensure subnet is updated to avoid update conflict
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,21 @@ def _ensure_route_table(cli_ctx, resource_group_name, ase_name, location, subnet
if not rt_found:
logger.info('Ensure Route Table...')
ase_route_table = RouteTable(location=location)
poller = network_client.route_tables.create_or_update(resource_group_name,
ase_route_table_name, ase_route_table)
poller = network_client.route_tables.begin_create_or_update(resource_group_name,
ase_route_table_name, ase_route_table)
LongRunningOperation(cli_ctx)(poller)

logger.info('Ensure Internet Route...')
internet_route = Route(address_prefix='0.0.0.0/0', next_hop_type='Internet')
poller = network_client.routes.create_or_update(resource_group_name, ase_route_table_name,
ase_route_name, internet_route)
poller = network_client.routes.begin_create_or_update(resource_group_name, ase_route_table_name,
ase_route_name, internet_route)
LongRunningOperation(cli_ctx)(poller)

rt = network_client.route_tables.get(resource_group_name, ase_route_table_name)
if not subnet_obj.route_table or subnet_obj.route_table.id != rt.id:
logger.info('Associate Route Table with Subnet...')
subnet_obj.route_table = rt
poller = network_client.subnets.create_or_update(
poller = network_client.subnets.begin_create_or_update(
vnet_resource_group, vnet_name,
subnet_name, subnet_parameters=subnet_obj)
LongRunningOperation(cli_ctx)(poller)
Expand Down Expand Up @@ -277,7 +277,8 @@ def _ensure_network_security_group(cli_ctx, resource_group_name, ase_name, locat
if not nsg_found:
logger.info('Ensure Network Security Group...')
ase_nsg = NetworkSecurityGroup(location=location)
poller = network_client.network_security_groups.create_or_update(resource_group_name, ase_nsg_name, ase_nsg)
poller = network_client.network_security_groups.begin_create_or_update(resource_group_name,
ase_nsg_name, ase_nsg)
LongRunningOperation(cli_ctx)(poller)

logger.info('Ensure Network Security Group Rules...')
Expand Down Expand Up @@ -334,7 +335,7 @@ def _ensure_network_security_group(cli_ctx, resource_group_name, ase_name, locat
if not subnet_obj.network_security_group or subnet_obj.network_security_group.id != nsg.id:
logger.info('Associate Network Security Group with Subnet...')
subnet_obj.network_security_group = NetworkSecurityGroup(id=nsg.id)
poller = network_client.subnets.create_or_update(
poller = network_client.subnets.begin_create_or_update(
vnet_resource_group, vnet_name,
subnet_name, subnet_parameters=subnet_obj)
LongRunningOperation(cli_ctx)(poller)
Expand Down Expand Up @@ -402,6 +403,6 @@ def _create_nsg_rule(cli_ctx, resource_group_name, network_security_group_name,
name=security_rule_name)

network_client = _get_network_client_factory(cli_ctx)
poller = network_client.security_rules.create_or_update(
poller = network_client.security_rules.begin_create_or_update(
resource_group_name, network_security_group_name, security_rule_name, settings)
LongRunningOperation(cli_ctx)(poller)
Original file line number Diff line number Diff line change
Expand Up @@ -3573,8 +3573,8 @@ def add_vnet_integration(cmd, name, resource_group_name, vnet, subnet, slot=None

if not delegated:
subnetObj.delegations = [Delegation(name="delegation", service_name="Microsoft.Web/serverFarms")]
vnet_client.subnets.create_or_update(vnet_resource_group, vnet, subnet,
subnet_parameters=subnetObj)
vnet_client.subnets.begin_create_or_update(vnet_resource_group, vnet, subnet,
subnet_parameters=subnetObj)

id_subnet = vnet_client.subnets.get(vnet_resource_group, vnet, subnet)
subnet_resource_id = id_subnet.id
Expand Down
14 changes: 7 additions & 7 deletions src/azure-cli/azure/cli/command_modules/container/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def _get_vnet_network_profile(cmd, location, resource_group_name, vnet, vnet_add
if not subnet.delegations:
logger.info('Adding ACI delegation to the existing subnet.')
subnet.delegations = [aci_delegation]
subnet = ncf.subnets.create_or_update(resource_group_name, vnet_name, subnet_name, subnet).result()
subnet = ncf.subnets.begin_create_or_update(resource_group_name, vnet_name, subnet_name, subnet).result()
else:
for delegation in subnet.delegations:
if delegation.service_name != aci_delegation_service_name:
Expand All @@ -313,19 +313,19 @@ def _get_vnet_network_profile(cmd, location, resource_group_name, vnet, vnet_add
vnet = _get_resource(ncf.virtual_networks, resource_group_name, vnet_name)
if not vnet:
logger.info('Creating new vnet "%s" in resource group "%s"', vnet_name, resource_group_name)
ncf.virtual_networks.create_or_update(resource_group_name,
vnet_name,
VirtualNetwork(name=vnet_name,
location=location,
address_space=AddressSpace(address_prefixes=[vnet_address_prefix])))
ncf.virtual_networks.begin_create_or_update(resource_group_name,
vnet_name,
VirtualNetwork(name=vnet_name,
location=location,
address_space=AddressSpace(address_prefixes=[vnet_address_prefix])))
subnet = Subnet(
name=subnet_name,
location=location,
address_prefix=subnet_address_prefix,
delegations=[aci_delegation])

logger.info('Creating new subnet "%s" in resource group "%s"', subnet_name, resource_group_name)
subnet = ncf.subnets.create_or_update(resource_group_name, vnet_name, subnet_name, subnet).result()
subnet = ncf.subnets.begin_create_or_update(resource_group_name, vnet_name, subnet_name, subnet).result()

NetworkProfile, ContainerNetworkInterfaceConfiguration, IPConfigurationProfile = cmd.get_models('NetworkProfile',
'ContainerNetworkInterfaceConfiguration',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def load_command_table(self, _):

with self.command_group('network nat gateway', nat_gateway_sdk) as g:
g.custom_command('create', 'create_nat_gateway', supports_no_wait=True)
g.command('delete', 'delete')
g.command('delete', 'begin_delete')
g.custom_command('list', 'list_nat_gateway')
g.show_command('show', 'get')
g.generic_update_command('update', custom_func_name='update_nat_gateway')
g.generic_update_command('update', setter_name='begin_create_or_update', custom_func_name='update_nat_gateway')
g.wait_command('wait')
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def create_nat_gateway(cmd, nat_gateway_name, resource_group_name,
public_ip_addresses=public_ip_addresses,
public_ip_prefixes=public_ip_prefixes)

return sdk_no_wait(no_wait, client.create_or_update, resource_group_name, nat_gateway_name, nat_gateway)
return sdk_no_wait(no_wait, client.begin_create_or_update, resource_group_name, nat_gateway_name, nat_gateway)


def update_nat_gateway(instance, cmd, public_ip_addresses=None,
Expand Down
8 changes: 8 additions & 0 deletions src/azure-cli/azure/cli/command_modules/network/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -2977,6 +2977,14 @@
text: az network cross-region-lb address-pool create -g MyResourceGroup --lb-name MyLb -n MyAddressPool --backend-addresses-config-file @config_file.json
"""

helps['network cross-region-lb address-pool delete'] = """
type: command
short-summary: Delete an address pool.
examples:
- name: Delete an address pool.
text: az network cross-region-lb address-pool delete -g MyResourceGroup --lb-name MyLb -n MyAddressPool
"""

helps['network cross-region-lb address-pool address'] = """
type: group
short-summary: Manage backend addresses of the cross-region load balance backend address pool.
Expand Down
7 changes: 4 additions & 3 deletions src/azure-cli/azure/cli/command_modules/network/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ def load_arguments(self, _):
c.argument('floating_ip', help='Enable floating IP.', arg_type=get_three_state_flag())
c.argument('idle_timeout', help='Idle timeout in minutes.', type=int)
c.argument('protocol', help='Network transport protocol.', arg_type=get_enum_type(TransportProtocol))
c.argument('private_ip_address_version', min_api='2019-04-01', help='The private IP address version to use.', default=IPVersion.ipv4.value if IPVersion else '')
c.argument('private_ip_address_version', min_api='2019-04-01', help='The private IP address version to use.', default=IPVersion.I_PV4.value if IPVersion else '')
Copy link
Member Author

Choose a reason for hiding this comment

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

name change by track2 SDK.

for item in ['backend_pool_name', 'backend_address_pool_name']:
c.argument(item, options_list='--backend-pool-name', help='The name of the backend address pool.', completer=get_lb_subresource_completion_list('backend_address_pools'))

Expand Down Expand Up @@ -1089,7 +1089,7 @@ def load_arguments(self, _):
c.argument('enable_ip_forwarding', options_list='--ip-forwarding', help='Enable IP forwarding.', arg_type=get_three_state_flag())

with self.argument_context('network nic create') as c:
c.argument('private_ip_address_version', min_api='2016-09-01', help='The private IP address version to use.', default=IPVersion.ipv4.value if IPVersion else '')
c.argument('private_ip_address_version', min_api='2016-09-01', help='The private IP address version to use.', default=IPVersion.I_PV4.value if IPVersion else '')
c.argument('network_interface_name', nic_type, options_list=['--name', '-n'], id_part=None)

public_ip_help = get_folded_parameter_help_string('public IP address', allow_none=True, default_none=True)
Expand Down Expand Up @@ -1924,7 +1924,8 @@ def load_arguments(self, _):
with self.argument_context('network vpn-connection shared-key') as c:
c.argument('connection_shared_key_name', options_list=['--name', '-n'], id_part='name')
c.argument('virtual_network_gateway_connection_name', options_list='--connection-name', metavar='NAME', id_part='name')
c.argument('key_length', type=int)
c.argument('key_length', type=int, help='The virtual network connection reset shared key length, should between 1 and 128.')
c.argument('value', help='The virtual network connection shared key value.')

with self.argument_context('network vrouter') as c:
c.argument('virtual_router_name', options_list=['--name', '-n'], help='The name of the Virtual Router.')
Expand Down
5 changes: 3 additions & 2 deletions src/azure-cli/azure/cli/command_modules/network/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ def delete_func(cmd, resource_group_name, resource_name, item_name, no_wait=Fals
with cmd.update_context(item) as c:
c.set_param(prop, keep_items)
if no_wait:
sdk_no_wait(no_wait, client.create_or_update, resource_group_name, resource_name, item)
sdk_no_wait(no_wait, client.begin_create_or_update, resource_group_name, resource_name, item)
else:
result = sdk_no_wait(no_wait, client.create_or_update, resource_group_name, resource_name, item).result()
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))

Expand Down
Loading