Skip to content

Commit ae8b8b9

Browse files
authored
Merge pull request Azure#145 from haroonf/envdefaultlocation
Envdefaultlocation
2 parents 5bfb856 + 32771f5 commit ae8b8b9

5 files changed

Lines changed: 60 additions & 58 deletions

File tree

src/containerapp/HISTORY.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Release History
66
0.3.10
77
++++++
88
* 'az containerapp create': Fix bug with --image caused by assuming a value for --registry-server
9+
* 'az containerapp hostname bind': Remove location set automatically by resource group
10+
* 'az containerapp env create': Add location validation
911

1012
0.3.9
1113
++++++

src/containerapp/azext_containerapp/_params.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66

77
from knack.arguments import CLIArgumentType
88

9-
from azure.cli.core.commands.validators import get_default_location_from_resource_group
109
from azure.cli.core.commands.parameters import (resource_group_name_type, get_location_type,
1110
file_type,
1211
get_three_state_flag, get_enum_type, tags_type)
13-
# from azure.cli.core.commands.validators import get_default_location_from_resource_group
1412

1513
from ._validators import (validate_memory, validate_cpu, validate_managed_env_name_or_id, validate_registry_server,
1614
validate_registry_user, validate_registry_pass, validate_target_port, validate_ingress)
@@ -338,7 +336,7 @@ def load_arguments(self, _):
338336

339337
with self.argument_context('containerapp hostname list') as c:
340338
c.argument('name', id_part=None)
341-
c.argument('location', arg_type=get_location_type(self.cli_ctx), validator=get_default_location_from_resource_group)
339+
c.argument('location', arg_type=get_location_type(self.cli_ctx))
342340

343341
with self.argument_context('containerapp hostname delete') as c:
344342
c.argument('hostname', help='The custom domain name.')

src/containerapp/azext_containerapp/_up_utils.py

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
get_container_app_if_exists,
4444
trigger_workflow,
4545
_ensure_location_allowed,
46-
register_provider_if_needed
46+
register_provider_if_needed,
47+
validate_environment_location
4748
)
4849

4950
from ._constants import MAXIMUM_SECRET_LENGTH, LOG_ANALYTICS_RP, CONTAINER_APPS_RP, ACR_IMAGE_SUFFIX, MAXIMUM_CONTAINER_APP_NAME_LENGTH
@@ -810,58 +811,6 @@ def find_existing_acr(cmd, app: "ContainerApp"):
810811
return None, None
811812

812813

813-
def validate_environment_location(cmd, location):
814-
from ._constants import MAX_ENV_PER_LOCATION
815-
env_list = list_managed_environments(cmd)
816-
817-
locations = [loc["location"] for loc in env_list]
818-
locations = list(set(locations)) # remove duplicates
819-
820-
location_count = {}
821-
for loc in locations:
822-
location_count[loc] = len([e for e in env_list if e["location"] == loc])
823-
824-
disallowed_locations = []
825-
for _, value in enumerate(location_count):
826-
if location_count[value] > MAX_ENV_PER_LOCATION - 1:
827-
disallowed_locations.append(value)
828-
829-
res_locations = list_environment_locations(cmd)
830-
res_locations = [loc for loc in res_locations if loc not in disallowed_locations]
831-
832-
allowed_locs = ", ".join(res_locations)
833-
834-
if location:
835-
try:
836-
_ensure_location_allowed(cmd, location, CONTAINER_APPS_RP, "managedEnvironments")
837-
except Exception as e: # pylint: disable=broad-except
838-
raise ValidationError("You cannot create a Containerapp environment in location {}. List of eligible locations: {}.".format(location, allowed_locs)) from e
839-
840-
if len(res_locations) > 0:
841-
if not location:
842-
logger.warning("Creating environment on location {}.".format(res_locations[0]))
843-
return res_locations[0]
844-
if location in disallowed_locations:
845-
raise ValidationError("You have more than {} environments in location {}. List of eligible locations: {}.".format(MAX_ENV_PER_LOCATION, location, allowed_locs))
846-
return location
847-
else:
848-
raise ValidationError("You cannot create any more environments. Environments are limited to {} per location in a subscription. Please specify an existing environment using --environment.".format(MAX_ENV_PER_LOCATION))
849-
850-
851-
def list_environment_locations(cmd):
852-
from ._utils import providers_client_factory
853-
providers_client = providers_client_factory(cmd.cli_ctx, get_subscription_id(cmd.cli_ctx))
854-
resource_types = getattr(providers_client.get(CONTAINER_APPS_RP), 'resource_types', [])
855-
res_locations = []
856-
for res in resource_types:
857-
if res and getattr(res, 'resource_type', "") == "managedEnvironments":
858-
res_locations = getattr(res, 'locations', [])
859-
860-
res_locations = [res_loc.lower().replace(" ", "").replace("(", "").replace(")", "") for res_loc in res_locations if res_loc.strip()]
861-
862-
return res_locations
863-
864-
865814
def check_env_name_on_rg(cmd, managed_env, resource_group_name, location):
866815
if location:
867816
_ensure_location_allowed(cmd, location, CONTAINER_APPS_RP, "managedEnvironments")

src/containerapp/azext_containerapp/_utils.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,3 +1463,55 @@ def is_registry_msi_system(identity):
14631463
if identity is None:
14641464
return False
14651465
return identity.lower() == "system"
1466+
1467+
1468+
def validate_environment_location(cmd, location):
1469+
from ._constants import MAX_ENV_PER_LOCATION
1470+
from .custom import list_managed_environments
1471+
env_list = list_managed_environments(cmd)
1472+
1473+
locations = [loc["location"] for loc in env_list]
1474+
locations = list(set(locations)) # remove duplicates
1475+
1476+
location_count = {}
1477+
for loc in locations:
1478+
location_count[loc] = len([e for e in env_list if e["location"] == loc])
1479+
1480+
disallowed_locations = []
1481+
for _, value in enumerate(location_count):
1482+
if location_count[value] > MAX_ENV_PER_LOCATION - 1:
1483+
disallowed_locations.append(value)
1484+
1485+
res_locations = list_environment_locations(cmd)
1486+
res_locations = [loc for loc in res_locations if loc not in disallowed_locations]
1487+
1488+
allowed_locs = ", ".join(res_locations)
1489+
1490+
if location:
1491+
try:
1492+
_ensure_location_allowed(cmd, location, CONTAINER_APPS_RP, "managedEnvironments")
1493+
except Exception as e: # pylint: disable=broad-except
1494+
raise ValidationError("You cannot create a Containerapp environment in location {}. List of eligible locations: {}.".format(location, allowed_locs)) from e
1495+
1496+
if len(res_locations) > 0:
1497+
if not location:
1498+
logger.warning("Creating environment on location %s.", res_locations[0])
1499+
return res_locations[0]
1500+
if location in disallowed_locations:
1501+
raise ValidationError("You have more than {} environments in location {}. List of eligible locations: {}.".format(MAX_ENV_PER_LOCATION, location, allowed_locs))
1502+
return location
1503+
else:
1504+
raise ValidationError("You cannot create any more environments. Environments are limited to {} per location in a subscription. Please specify an existing environment using --environment.".format(MAX_ENV_PER_LOCATION))
1505+
1506+
1507+
def list_environment_locations(cmd):
1508+
providers_client = providers_client_factory(cmd.cli_ctx, get_subscription_id(cmd.cli_ctx))
1509+
resource_types = getattr(providers_client.get(CONTAINER_APPS_RP), 'resource_types', [])
1510+
res_locations = []
1511+
for res in resource_types:
1512+
if res and getattr(res, 'resource_type', "") == "managedEnvironments":
1513+
res_locations = getattr(res, 'locations', [])
1514+
1515+
res_locations = [res_loc.lower().replace(" ", "").replace("(", "").replace(")", "") for res_loc in res_locations if res_loc.strip()]
1516+
1517+
return res_locations

src/containerapp/azext_containerapp/custom.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
validate_container_app_name, _update_weights, get_vnet_location, register_provider_if_needed,
6565
generate_randomized_cert_name, _get_name, load_cert_file, check_cert_name_availability,
6666
validate_hostname, patch_new_custom_domain, get_custom_domains, _validate_revision_name, set_managed_identity,
67-
create_acrpull_role_assignment, is_registry_msi_system, clean_null_values, _populate_secret_values)
67+
create_acrpull_role_assignment, is_registry_msi_system, clean_null_values, _populate_secret_values,
68+
validate_environment_location)
6869
from ._validators import validate_create
6970
from ._ssh_utils import (SSH_DEFAULT_ENCODING, WebSocketConnection, read_ssh, get_stdin_writer, SSH_CTRL_C_MSG,
7071
SSH_BACKUP_ENCODING)
@@ -917,7 +918,7 @@ def create_managed_environment(cmd,
917918
else:
918919
location = vnet_location
919920

920-
location = location or _get_location_from_resource_group(cmd.cli_ctx, resource_group_name)
921+
location = validate_environment_location(cmd, location)
921922

922923
register_provider_if_needed(cmd, CONTAINER_APPS_RP)
923924
_ensure_location_allowed(cmd, location, CONTAINER_APPS_RP, "managedEnvironments")

0 commit comments

Comments
 (0)