Skip to content

Commit

Permalink
[Network] az network lb create: Support cross-subscription resource…
Browse files Browse the repository at this point in the history
… ID (#28247)

* change arm.py to support cross sub resource existence check

* recover the test for `--subnet`

* change test region and fix test

* adjust vm module according to `resource_exists` change

* Update resource existence check

* Fix style & mark test as live_only
  • Loading branch information
ReaNAiveD authored Jan 30, 2024
1 parent 75fbb0c commit aa482cd
Show file tree
Hide file tree
Showing 5 changed files with 1,077 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/azure-cli-core/azure/cli/core/commands/arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,14 @@ def __new__(cls, val):
return str.__new__(cls, val)


def resource_exists(cli_ctx, resource_group, name, namespace, type, **_): # pylint: disable=redefined-builtin
def resource_exists(cli_ctx, subscription, resource_group, name, namespace, type,
**_): # pylint: disable=redefined-builtin
''' Checks if the given resource exists. '''
odata_filter = "resourceGroup eq '{}' and name eq '{}'" \
" and resourceType eq '{}/{}'".format(resource_group, name, namespace, type)
client = get_mgmt_service_client(cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES).resources
# Support cross subscription resource existence check
client = get_mgmt_service_client(
cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES, subscription_id=subscription).resources
existing = len(list(client.list(filter=odata_filter))) == 1
return existing

Expand Down
18 changes: 17 additions & 1 deletion src/azure-cli/azure/cli/command_modules/network/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ def _is_v2_sku(sku):
return 'v2' in sku


def _add_aux_subscription(aux_subscriptions, added_resource_id):
if added_resource_id and is_valid_resource_id(added_resource_id):
res_parts = parse_resource_id(added_resource_id)
aux_sub = res_parts['subscription']
if aux_sub and aux_sub not in aux_subscriptions:
aux_subscriptions.append(aux_sub)


def create_application_gateway(cmd, application_gateway_name, resource_group_name, location=None,
tags=None, no_wait=False, capacity=2,
cert_data=None, cert_password=None, key_vault_secret_id=None,
Expand Down Expand Up @@ -3945,6 +3953,13 @@ def _build_arguments_schema(cls, *args, **kwargs):


# region LoadBalancers
def _get_lb_create_aux_subscriptions(public_ip_address, subnet):
aux_subscriptions = []
_add_aux_subscription(aux_subscriptions, public_ip_address)
_add_aux_subscription(aux_subscriptions, subnet)
return aux_subscriptions


def create_load_balancer(cmd, load_balancer_name, resource_group_name, location=None, tags=None,
backend_pool_name=None, frontend_ip_name='LoadBalancerFrontEnd',
private_ip_address=None, public_ip_address=None,
Expand All @@ -3960,6 +3975,7 @@ def create_load_balancer(cmd, load_balancer_name, resource_group_name, location=
build_load_balancer_resource, build_public_ip_resource, build_vnet_resource)

DeploymentProperties = cmd.get_models('DeploymentProperties', resource_type=ResourceType.MGMT_RESOURCE_RESOURCES)
aux_subscriptions = _get_lb_create_aux_subscriptions(public_ip_address, subnet)

if public_ip_address is None:
logger.warning(
Expand Down Expand Up @@ -4024,7 +4040,7 @@ def create_load_balancer(cmd, load_balancer_name, resource_group_name, location=

# deploy ARM template
deployment_name = 'lb_deploy_' + random_string(32)
client = get_mgmt_service_client(cmd.cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES).deployments
client = get_mgmt_service_client(cmd.cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES, aux_subscriptions=aux_subscriptions).deployments
properties = DeploymentProperties(template=template, parameters={}, mode='incremental')
Deployment = cmd.get_models('Deployment', resource_type=ResourceType.MGMT_RESOURCE_RESOURCES)
deployment = Deployment(properties=properties)
Expand Down
Loading

0 comments on commit aa482cd

Please sign in to comment.