126126
127127
128128def _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
144157def _add_resource_group_cluster_name_subscription_id_args (_args_schema ):
@@ -195,46 +208,15 @@ def _build_arguments_schema(cls, *args, **kwargs):
195208
196209class 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
239221class AKSSafeguardsListCustom (List ):
240222
0 commit comments