Skip to content

Commit 382e084

Browse files
author
Jenny Liu
committed
Add pre-existence check to safeguards create and fix duplicate HISTORY entry
- Add validation in _execute_operations() to check if Deployment Safeguards already exists before creating - Block duplicate creates with helpful error message guiding users to update or delete - Supports both -g/-n and --managed-cluster URI syntax - Fix duplicate 19.0.0b16 headers in HISTORY.rst - Update HISTORY.rst with new pre-existence check feature
1 parent b9e6577 commit 382e084

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/aks-preview/HISTORY.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ Pending
1414

1515
19.0.0b17
1616
+++++++
17+
* `az aks safeguards create`: Add pre-existence check to prevent duplicate resource creation and guide users to use update command instead.
1718
* `az aks safeguards`: Fix verb tense in help text and examples to use first-person imperative verbs per Azure CLI guidelines.
1819

1920
19.0.0b16
2021
+++++++
2122
* `az aks bastion`: Correctly configure `$KUBECONFIG` values for tunneling traffic into a private AKS cluster.
22-
23-
19.0.0b16
24-
+++++++
2523
* Update --enable-container-network-logs DCR to ContainerNetworkLogs instead of RetinaNetworkFlowLogs
2624

2725
19.0.0b15

src/aks-preview/azext_aks_preview/aaz/latest/aks/safeguards/_create.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,49 @@ def _build_arguments_schema(cls, *args, **kwargs):
8787
return cls._args_schema
8888

8989
def _execute_operations(self):
90+
# Check if Deployment Safeguards already exists BEFORE attempting create
91+
from azure.cli.core.util import send_raw_request
92+
from knack.util import CLIError
93+
94+
# Get the resource URI - check if managed_cluster is set, otherwise build from -g/-n
95+
resource_uri = self.ctx.args.managed_cluster
96+
97+
# If managed_cluster is "Undefined" or not set, build from resource_group and cluster_name
98+
if not resource_uri or str(resource_uri) == "Undefined":
99+
# Access raw data which has resource_group and cluster_name from -g/-n
100+
data = self.ctx.args._data
101+
if 'resource_group' in data and 'cluster_name' in data:
102+
subscription = self.ctx.subscription_id
103+
resource_uri = f"/subscriptions/{subscription}/resourceGroups/{data['resource_group']}/providers/Microsoft.ContainerService/managedClusters/{data['cluster_name']}"
104+
105+
if not resource_uri or str(resource_uri) == "Undefined":
106+
raise CLIError("Resource URI not found. Please provide either --managed-cluster or both --resource-group and --name.")
107+
108+
# Construct the GET URL to check if resource already exists
109+
safeguards_url = f"https://management.azure.com{resource_uri}/providers/Microsoft.ContainerService/deploymentSafeguards/default?api-version=2025-05-02-preview"
110+
111+
# Check if resource already exists
112+
resource_exists = False
113+
try:
114+
response = send_raw_request(self.ctx.cli_ctx, "GET", safeguards_url)
115+
if response.status_code == 200:
116+
resource_exists = True
117+
except Exception as ex:
118+
# Any exception (404, etc) means resource doesn't exist - that's fine for create
119+
error_str = str(ex).lower()
120+
if "404" not in error_str and "not found" not in error_str and "resourcenotfound" not in error_str:
121+
# If it's not a "not found" error, it might be a real problem - but let the create operation handle it
122+
pass
123+
124+
# If resource exists, block the create
125+
if resource_exists:
126+
raise CLIError(
127+
f"Deployment Safeguards instance already exists for this cluster. "
128+
f"Please use 'az aks safeguards update' to modify the configuration, "
129+
f"or 'az aks safeguards delete' to remove it before creating a new one."
130+
)
131+
132+
# If we get here, resource doesn't exist - proceed with create
90133
self.pre_operations()
91134
yield self.DeploymentSafeguardsCreate(ctx=self.ctx)()
92135
self.post_operations()

0 commit comments

Comments
 (0)