Skip to content

Commit 8eb3b38

Browse files
Jenny LiuJenny Liu
authored andcommitted
Fix safeguards -g/-n support and update test recordings
1 parent 948caa1 commit 8eb3b38

File tree

5 files changed

+64
-41
lines changed

5 files changed

+64
-41
lines changed

src/aks-preview/azext_aks_preview/aks_safeguards_custom.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from azure.cli.core.aaz import AAZResourceGroupNameArg, AAZStrArg, has_value
1111
from azure.cli.core.azclierror import ArgumentUsageError, CLIError, HTTPError
1212
from azure.cli.core.util import send_raw_request
13+
from azure.mgmt.core.tools import is_valid_resource_id
1314

1415
from azext_aks_preview.aaz.latest.aks.safeguards._create import Create
1516
from 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

4568
def _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 # pylint: disable=protected-access
6487
return _args_schema
6588

6689

src/aks-preview/azext_aks_preview/tests/latest/recordings/test_aks_deployment_safeguards_argument_validation.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ interactions:
9797
body: '{"location": "westus2", "sku": {"name": "Base", "tier": "Free"}, "identity":
9898
{"type": "SystemAssigned"}, "kind": "Base", "properties": {"kubernetesVersion":
9999
"", "dnsPrefix": "aksval-lqt-cli-000001-26fe00", "agentPoolProfiles": [{"count":
100-
3, "vmSize": "standard_a8_v2", "osDiskSizeGB": 0, "workloadRuntime": "OCIContainer",
100+
3, "vmSize": "Standard_DS2_v2", "osDiskSizeGB": 0, "workloadRuntime": "OCIContainer",
101101
"osType": "Linux", "enableAutoScaling": false, "type": "VirtualMachineScaleSets",
102102
"mode": "System", "orchestratorVersion": "", "upgradeSettings": {}, "upgradeSettingsBlueGreen":
103103
{}, "enableNodePublicIP": false, "scaleSetPriority": "Regular", "scaleSetEvictionPolicy":
@@ -140,7 +140,7 @@ interactions:
140140
\ \"fqdn\": \"aksval-lqt-cli-000001-26fe00-8wql3q5r.hcp.westus2.staging.azmk8s.io\",\n
141141
\ \"azurePortalFQDN\": \"aksval-lqt-cli-000001-26fe00-8wql3q5r.portal.hcp.westus2.staging.azmk8s.io\",\n
142142
\ \"agentPoolProfiles\": [\n {\n \"name\": \"nodepool1\",\n \"count\":
143-
3,\n \"vmSize\": \"standard_a8_v2\",\n \"osDiskSizeGB\": 256,\n \"osDiskType\":
143+
3,\n \"vmSize\": \"Standard_DS2_v2\",\n \"osDiskSizeGB\": 256,\n \"osDiskType\":
144144
\"Managed\",\n \"kubeletDiskType\": \"OS\",\n \"workloadRuntime\": \"OCIContainer\",\n
145145
\ \"maxPods\": 250,\n \"type\": \"VirtualMachineScaleSets\",\n \"enableAutoScaling\":
146146
false,\n \"scaleDownMode\": \"Delete\",\n \"provisioningState\": \"Creating\",\n
@@ -1229,7 +1229,7 @@ interactions:
12291229
\ \"fqdn\": \"aksval-lqt-cli-000001-26fe00-8wql3q5r.hcp.westus2.staging.azmk8s.io\",\n
12301230
\ \"azurePortalFQDN\": \"aksval-lqt-cli-000001-26fe00-8wql3q5r.portal.hcp.westus2.staging.azmk8s.io\",\n
12311231
\ \"agentPoolProfiles\": [\n {\n \"name\": \"nodepool1\",\n \"count\":
1232-
3,\n \"vmSize\": \"standard_a8_v2\",\n \"osDiskSizeGB\": 256,\n \"osDiskType\":
1232+
3,\n \"vmSize\": \"Standard_DS2_v2\",\n \"osDiskSizeGB\": 256,\n \"osDiskType\":
12331233
\"Managed\",\n \"kubeletDiskType\": \"OS\",\n \"workloadRuntime\": \"OCIContainer\",\n
12341234
\ \"maxPods\": 250,\n \"type\": \"VirtualMachineScaleSets\",\n \"enableAutoScaling\":
12351235
false,\n \"scaleDownMode\": \"Delete\",\n \"provisioningState\": \"Succeeded\",\n
@@ -1331,7 +1331,7 @@ interactions:
13311331
User-Agent:
13321332
- AZURECLI/2.79.0 azsdk-python-core/1.35.0 Python/3.12.12 (Linux-6.8.0-1030-azure-x86_64-with-glibc2.41)
13331333
method: PUT
1334-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/cli-000001/providers/Microsoft.ContainerService/managedClusters/aksval-000002/providers/Microsoft.ContainerService/deploymentSafeguards/default?api-version=2025-05-02-preview
1334+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/cli-000001/providers/Microsoft.ContainerService/managedClusters/aksval-000002/providers/Microsoft.ContainerService/deploymentSafeguards/default?api-version=2025-07-01
13351335
response:
13361336
body:
13371337
string: '{"eTag":"8734c920-5908-49de-b248-97ac4460c414","id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli-000001/providers/Microsoft.ContainerService/managedClusters/aksval-000002/providers/Microsoft.ContainerService/deploymentSafeguards/default","name":"default","properties":{"level":"Warn","podSecurityStandardsLevel":"Privileged","provisioningState":"Creating","systemExcludedNamespaces":["aks-command","kube-system","calico-system","tigera-system","gatekeeper-system","azappconfig-system","azureml","dapr-system","dataprotection-microsoft","flux-system","acstor","sc-system","azure-extensions-usage-system","app-routing-system","aks-periscope","aks-istio-system","aks-istio-ingress","aks-istio-egress"]},"type":"Microsoft.ContainerService/deploymentSafeguards"}'
@@ -1531,7 +1531,7 @@ interactions:
15311531
User-Agent:
15321532
- AZURECLI/2.79.0 azsdk-python-core/1.35.0 Python/3.12.12 (Linux-6.8.0-1030-azure-x86_64-with-glibc2.41)
15331533
method: GET
1534-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/cli-000001/providers/Microsoft.ContainerService/managedClusters/aksval-000002/providers/Microsoft.ContainerService/deploymentSafeguards/default?api-version=2025-05-02-preview
1534+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/cli-000001/providers/Microsoft.ContainerService/managedClusters/aksval-000002/providers/Microsoft.ContainerService/deploymentSafeguards/default?api-version=2025-07-01
15351535
response:
15361536
body:
15371537
string: '{"eTag":"64ab911c-c024-4310-aa14-a94c344ee710","id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli-000001/providers/Microsoft.ContainerService/managedClusters/aksval-000002/providers/Microsoft.ContainerService/deploymentSafeguards/default","name":"default","properties":{"level":"Warn","podSecurityStandardsLevel":"Privileged","provisioningState":"Succeeded","systemExcludedNamespaces":["aks-command","kube-system","calico-system","tigera-system","gatekeeper-system","azappconfig-system","azureml","dapr-system","dataprotection-microsoft","flux-system","acstor","sc-system","azure-extensions-usage-system","app-routing-system","aks-periscope","aks-istio-system","aks-istio-ingress","aks-istio-egress"]},"type":"Microsoft.ContainerService/deploymentSafeguards"}'
@@ -1581,7 +1581,7 @@ interactions:
15811581
User-Agent:
15821582
- AZURECLI/2.79.0 azsdk-python-core/1.35.0 Python/3.12.12 (Linux-6.8.0-1030-azure-x86_64-with-glibc2.41)
15831583
method: DELETE
1584-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli-000001/providers/Microsoft.ContainerService/managedClusters/aksval-000002/providers/Microsoft.ContainerService/deploymentSafeguards/default?api-version=2025-05-02-preview
1584+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli-000001/providers/Microsoft.ContainerService/managedClusters/aksval-000002/providers/Microsoft.ContainerService/deploymentSafeguards/default?api-version=2025-07-01
15851585
response:
15861586
body:
15871587
string: ''

0 commit comments

Comments
 (0)