From 9a27027f062e69125870471860c1ae1d2e6f5f98 Mon Sep 17 00:00:00 2001 From: Jialun Cai Date: Thu, 6 Feb 2025 00:39:57 +0000 Subject: [PATCH] Emit error message when using `--asg-ids` alone without `--allowed-host-ports` --- .../cli/command_modules/acs/_validators.py | 14 +++++ .../acs/tests/latest/test_validators.py | 59 +++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/src/azure-cli/azure/cli/command_modules/acs/_validators.py b/src/azure-cli/azure/cli/command_modules/acs/_validators.py index 845d9a06f94..b154359dc69 100644 --- a/src/azure-cli/azure/cli/command_modules/acs/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/acs/_validators.py @@ -783,13 +783,27 @@ def validate_allowed_host_ports(namespace): def validate_application_security_groups(namespace): + is_nodepool_operation = False if hasattr((namespace), "nodepool_asg_ids"): + is_nodepool_operation = True asg_ids = namespace.nodepool_asg_ids + host_ports = namespace.nodepool_allowed_host_ports else: asg_ids = namespace.asg_ids + host_ports = namespace.allowed_host_ports + if not asg_ids: return + if not host_ports: + if is_nodepool_operation: + raise ArgumentUsageError( + '--nodepool-asg-ids must be used with --nodepool-allowed-host-ports' + ) + raise ArgumentUsageError( + '--asg-ids must be used with --allowed-host-ports' + ) + from azure.mgmt.core.tools import is_valid_resource_id for asg in asg_ids: if not is_valid_resource_id(asg): diff --git a/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_validators.py b/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_validators.py index 38fe3f4e1d5..e6bdee5b341 100644 --- a/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_validators.py +++ b/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_validators.py @@ -725,6 +725,7 @@ def test_invalid_application_security_groups(self): namespace = SimpleNamespace( **{ "asg_ids": "invalid", + "allowed_host_ports": ["80/tcp", "443/tcp", "8080-8090/tcp", "53/udp"], } ) with self.assertRaises(InvalidArgumentValueError): @@ -732,10 +733,52 @@ def test_invalid_application_security_groups(self): namespace ) + def test_application_security_groups_without_allowed_host_ports(self): + asg_ids = [ + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.Network/applicationSecurityGroups/asg1", + ] + namespace = SimpleNamespace( + **{ + "asg_ids": asg_ids, + "allowed_host_ports": [], + } + ) + with self.assertRaises(ArgumentUsageError): + validators.validate_application_security_groups( + namespace + ) + + def test_nodepool_application_security_groups_without_allowed_host_ports(self): + asg_ids = [ + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.Network/applicationSecurityGroups/asg1", + ] + namespace = SimpleNamespace( + **{ + "nodepool_asg_ids": asg_ids, + "nodepool_allowed_host_ports": [], + } + ) + with self.assertRaises(ArgumentUsageError): + validators.validate_application_security_groups( + namespace + ) + def test_empty_application_security_groups(self): namespace = SimpleNamespace( **{ "asg_ids": "", + "allowed_host_ports": [], + } + ) + validators.validate_application_security_groups( + namespace + ) + + def test_empty_nodepool_application_security_groups(self): + namespace = SimpleNamespace( + **{ + "nodepool_asg_ids": "", + "nodepool_allowed_host_ports": [], } ) validators.validate_application_security_groups( @@ -750,6 +793,22 @@ def test_multiple_application_security_groups(self): namespace = SimpleNamespace( **{ "asg_ids": asg_ids, + "allowed_host_ports": ["80/tcp", "443/tcp", "8080-8090/tcp", "53/udp"], + } + ) + validators.validate_application_security_groups( + namespace + ) + + def test_multiple_nodepool_application_security_groups(self): + asg_ids = [ + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.Network/applicationSecurityGroups/asg1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg2/providers/Microsoft.Network/applicationSecurityGroups/asg2", + ] + namespace = SimpleNamespace( + **{ + "nodepool_asg_ids": asg_ids, + "nodepool_allowed_host_ports": ["80/tcp", "443/tcp", "8080-8090/tcp", "53/udp"], } ) validators.validate_application_security_groups(