Skip to content

Commit 4a857d2

Browse files
committed
Restore -c/--cluster options and add PSS level support to safeguards commands
- Add -c/--cluster options back to safeguards create command - Update validation to handle both cluster names and full resource IDs - Fix pre_operations execution order in _execute_operations - Add --pss-level parameter support with Baseline/Restricted values - Update test to use -c/-g syntax and test PSS level functionality - Re-record test with live Azure resources
1 parent c2efe4d commit 4a857d2

4 files changed

Lines changed: 323 additions & 481 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def _build_arguments_schema(cls, *args, **kwargs):
5858

5959
_args_schema = cls._args_schema
6060
_args_schema.managed_cluster = AAZStrArg(
61-
options=["--managed-cluster"],
61+
options=["-c", "--cluster", "--managed-cluster"],
6262
arg_group="",
6363
help="The name or ID of the managed cluster.",
6464
required=False, # Either this or -g/-n is required, validated in _execute_operations
@@ -91,6 +91,9 @@ 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
95+
self.pre_operations()
96+
9497
# Check if Deployment Safeguards already exists before attempting create
9598
from azure.cli.core.util import send_raw_request
9699
from azure.cli.core.azclierror import HTTPError
@@ -141,7 +144,6 @@ def _execute_operations(self):
141144
)
142145

143146
# If we get here, resource doesn't exist - proceed with create
144-
self.pre_operations()
145147
yield self.DeploymentSafeguardsCreate(ctx=self.ctx)()
146148
self.post_operations()
147149

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

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@
126126

127127

128128
def _validate_and_set_managed_cluster_argument(ctx):
129+
from azure.mgmt.core.tools import is_valid_resource_id
130+
129131
args = ctx.args
130132
has_managed_cluster = has_value(args.managed_cluster)
131133
has_rg_and_cluster = has_value(
@@ -138,7 +140,18 @@ def _validate_and_set_managed_cluster_argument(ctx):
138140

139141
if not has_managed_cluster:
140142
# pylint: disable=line-too-long
141-
args.managed_cluster = f"subscriptions/{ctx.subscription_id}/resourceGroups/{args.resource_group}/providers/Microsoft.ContainerService/managedClusters/{args.cluster_name}"
143+
args.managed_cluster = f"/subscriptions/{ctx.subscription_id}/resourceGroups/{args.resource_group}/providers/Microsoft.ContainerService/managedClusters/{args.cluster_name}"
144+
else:
145+
# If managed_cluster is provided but is not a full resource ID, treat it as a cluster name
146+
# and require resource_group to be provided
147+
managed_cluster_value = args.managed_cluster.to_serialized_data()
148+
if not is_valid_resource_id(managed_cluster_value):
149+
# It's just a cluster name, need resource group
150+
if not has_value(args.resource_group):
151+
raise ArgumentUsageError(
152+
"When providing cluster name via -c/--cluster, you must also provide -g/--resource-group.")
153+
# Build the full resource ID
154+
args.managed_cluster = f"/subscriptions/{ctx.subscription_id}/resourceGroups/{args.resource_group}/providers/Microsoft.ContainerService/managedClusters/{managed_cluster_value}"
142155

143156

144157
def _add_resource_group_cluster_name_subscription_id_args(_args_schema):
@@ -195,46 +208,15 @@ def _build_arguments_schema(cls, *args, **kwargs):
195208

196209
class AKSSafeguardsCreateCustom(Create):
197210

198-
def pre_operations(self):
199-
from azure.cli.core.util import send_raw_request
200-
201-
# Validate and set managed cluster argument first
202-
_validate_and_set_managed_cluster_argument(self.ctx)
203-
204-
# Check if Deployment Safeguards already exists
205-
resource_group_name = self.ctx.args.resource_group
206-
cluster_name = self.ctx.args.cluster_name
207-
subscription_id = self.ctx.subscription_id
208-
209-
# Construct the URL to check if safeguards already exists
210-
safeguards_url = (
211-
"https://management.azure.com/subscriptions/{}/resourceGroups/{}/providers/"
212-
"Microsoft.ContainerService/managedClusters/{}/providers/Microsoft.ContainerService/"
213-
"deploymentSafeguards/default?api-version=2025-05-02-preview"
214-
).format(subscription_id, resource_group_name, cluster_name)
215-
216-
# Check if resource already exists
217-
resource_exists = False
218-
try:
219-
response = send_raw_request(self.ctx.cli_ctx, "GET", safeguards_url)
220-
if response.status_code == 200:
221-
resource_exists = True
222-
except Exception: # pylint: disable=broad-exception-caught
223-
# 404 or any error means resource doesn't exist
224-
pass
225-
226-
if resource_exists:
227-
raise ClientRequestError(
228-
"Deployment Safeguards instance already exists for this cluster. "
229-
"Please use 'az aks safeguards update' to modify the configuration, "
230-
"or 'az aks safeguards delete' to remove it before creating a new one."
231-
)
232-
233211
@classmethod
234212
def _build_arguments_schema(cls, *args, **kwargs):
235213
_args_schema = super()._build_arguments_schema(*args, **kwargs)
236214
return _add_resource_group_cluster_name_subscription_id_args(_args_schema)
237215

216+
def pre_operations(self):
217+
# Validate and set managed cluster argument
218+
_validate_and_set_managed_cluster_argument(self.ctx)
219+
238220

239221
class AKSSafeguardsListCustom(List):
240222

0 commit comments

Comments
 (0)