Skip to content

Commit d463347

Browse files
committed
Move pre-existence check logic from AAZ-generated file to custom class
- Moved pre-existence check from _create.py to AKSSafeguardsCreateCustom.pre_operations() - This prevents the logic from being wiped when AAZ regenerates the command files - Custom logic now survives AAZ regeneration as it's in custom.py - Re-recorded tests with new implementation (13 minutes, passed)
1 parent d6a4894 commit d463347

File tree

3 files changed

+418
-341
lines changed

3 files changed

+418
-341
lines changed

src/azure-cli/azure/cli/command_modules/acs/aaz/latest/aks/safeguards/_create.py

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -91,59 +91,7 @@ def _build_arguments_schema(cls, *args, **kwargs):
9191
return cls._args_schema
9292

9393
def _execute_operations(self):
94-
# Call pre_operations first to allow custom class to set managed_cluster
9594
self.pre_operations()
96-
97-
# Check if Deployment Safeguards already exists before attempting create
98-
from azure.cli.core.util import send_raw_request
99-
from azure.cli.core.azclierror import HTTPError
100-
from knack.util import CLIError
101-
102-
# Get the resource URI - check if managed_cluster is set, otherwise build from -g/-n
103-
resource_uri = None
104-
105-
# If managed_cluster is not set, build from resource_group and cluster_name
106-
if has_value(self.ctx.args.managed_cluster):
107-
resource_uri = self.ctx.args.managed_cluster.to_serialized_data()
108-
else:
109-
# Access resource_group and cluster_name from arguments
110-
resource_group = getattr(self.ctx.args, "resource_group", None)
111-
cluster_name = getattr(self.ctx.args, "cluster_name", None)
112-
if resource_group and cluster_name:
113-
subscription = self.ctx.subscription_id
114-
resource_uri = f"/subscriptions/{subscription}/resourceGroups/{resource_group}/providers/Microsoft.ContainerService/managedClusters/{cluster_name}"
115-
116-
if not resource_uri:
117-
raise CLIError("Resource URI not found. Please provide either --managed-cluster or both --resource-group and --name.")
118-
119-
# Validate resource_uri format to prevent URL injection
120-
if not resource_uri.startswith('/subscriptions/'):
121-
raise CLIError(f"Invalid managed cluster resource ID format: {resource_uri}")
122-
123-
# Construct the GET URL to check if resource already exists
124-
safeguards_url = f"https://management.azure.com{resource_uri}/providers/Microsoft.ContainerService/deploymentSafeguards/default?api-version=2025-05-02-preview"
125-
126-
# Check if resource already exists
127-
resource_exists = False
128-
try:
129-
response = send_raw_request(self.ctx.cli_ctx, "GET", safeguards_url)
130-
if response.status_code == 200:
131-
resource_exists = True
132-
except HTTPError as ex:
133-
# 404 means resource doesn't exist, which is expected for create
134-
if ex.response.status_code != 404:
135-
# Re-raise if it's not a 404 - could be auth issue, network problem, etc.
136-
raise
137-
138-
# If resource exists, block the create
139-
if resource_exists:
140-
raise CLIError(
141-
f"Deployment Safeguards instance already exists for this cluster. "
142-
f"Please use 'az aks safeguards update' to modify the configuration, "
143-
f"or 'az aks safeguards delete' to remove it before creating a new one."
144-
)
145-
146-
# If we get here, resource doesn't exist - proceed with create
14795
yield self.DeploymentSafeguardsCreate(ctx=self.ctx)()
14896
self.post_operations()
14997

src/azure-cli/azure/cli/command_modules/acs/custom.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,42 @@ def _build_arguments_schema(cls, *args, **kwargs):
221221
return _add_resource_group_cluster_name_subscription_id_args(_args_schema)
222222

223223
def pre_operations(self):
224+
from azure.cli.core.util import send_raw_request
225+
from azure.cli.core.azclierror import HTTPError
226+
224227
# Validate and set managed cluster argument
225228
_validate_and_set_managed_cluster_argument(self.ctx)
226229

230+
# Check if Deployment Safeguards already exists before attempting create
231+
resource_uri = self.ctx.args.managed_cluster.to_serialized_data()
232+
233+
# Validate resource_uri format to prevent URL injection
234+
if not resource_uri.startswith('/subscriptions/'):
235+
raise CLIError(f"Invalid managed cluster resource ID format: {resource_uri}")
236+
237+
# Construct the GET URL to check if resource already exists
238+
safeguards_url = f"https://management.azure.com{resource_uri}/providers/Microsoft.ContainerService/deploymentSafeguards/default?api-version=2025-05-02-preview"
239+
240+
# Check if resource already exists
241+
resource_exists = False
242+
try:
243+
response = send_raw_request(self.ctx.cli_ctx, "GET", safeguards_url)
244+
if response.status_code == 200:
245+
resource_exists = True
246+
except HTTPError as ex:
247+
# 404 means resource doesn't exist, which is expected for create
248+
if ex.response.status_code != 404:
249+
# Re-raise if it's not a 404 - could be auth issue, network problem, etc.
250+
raise
251+
252+
# If resource exists, block the create
253+
if resource_exists:
254+
raise CLIError(
255+
f"Deployment Safeguards instance already exists for this cluster. "
256+
f"Please use 'az aks safeguards update' to modify the configuration, "
257+
f"or 'az aks safeguards delete' to remove it before creating a new one."
258+
)
259+
227260

228261
class AKSSafeguardsListCustom(List):
229262

0 commit comments

Comments
 (0)