1010from azure .cli .core .aaz import AAZResourceGroupNameArg , AAZStrArg , has_value
1111from azure .cli .core .azclierror import ArgumentUsageError , CLIError , HTTPError
1212from azure .cli .core .util import send_raw_request
13+ from azure .mgmt .core .tools import is_valid_resource_id
1314
1415from azext_aks_preview .aaz .latest .aks .safeguards ._create import Create
1516from azext_aks_preview .aaz .latest .aks .safeguards ._delete import Delete
@@ -40,6 +41,28 @@ def _validate_and_set_managed_cluster_argument(ctx):
4041 f"/subscriptions/{ ctx .subscription_id } /resourceGroups/{ args .resource_group } /"
4142 f"providers/Microsoft.ContainerService/managedClusters/{ args .cluster_name } "
4243 )
44+ else :
45+ # If managed_cluster is provided but is not a full resource ID, treat it as a cluster name
46+ # and require resource_group to be provided
47+ managed_cluster_value = args .managed_cluster .to_serialized_data ()
48+
49+ # Normalize resource ID: add leading slash if missing for backward compatibility
50+ if managed_cluster_value and not managed_cluster_value .startswith ('/' ):
51+ managed_cluster_value = f"/{ managed_cluster_value } "
52+
53+ if not is_valid_resource_id (managed_cluster_value ):
54+ # It's just a cluster name, need resource group
55+ if not has_value (args .resource_group ):
56+ raise ArgumentUsageError (
57+ "When providing cluster name via -c/--cluster, you must also provide -g/--resource-group."
58+ )
59+ # Build the full resource ID
60+ managed_cluster_value = (
61+ f"/subscriptions/{ ctx .subscription_id } /resourceGroups/{ args .resource_group } /"
62+ f"providers/Microsoft.ContainerService/managedClusters/{ managed_cluster_value .lstrip ('/' )} "
63+ )
64+
65+ args .managed_cluster = managed_cluster_value
4366
4467
4568def _add_resource_group_cluster_name_args (_args_schema ):
@@ -60,7 +83,7 @@ def _add_resource_group_cluster_name_args(_args_schema):
6083 "or both --resource-group and --name, but not both." ,
6184 required = False ,
6285 )
63- _args_schema .managed_cluster .required = False
86+ _args_schema .managed_cluster ._required = False
6487 return _args_schema
6588
6689
0 commit comments