Skip to content

Commit

Permalink
[AKS] aks create/update: add --vnet-subnet_id validationChenyzh vnet …
Browse files Browse the repository at this point in the history
…validator (Azure#1243)

* Adding VNetSubnet Validator, raise Error if we got an invalid resource id

* addining valid test case

* adding null / empty value test

* Format fixing

* fixing style issues

* remove white space at tail

* Update _params.py

remove tailing space

* Update _params.py

add line for lint

Co-authored-by: Chenyi Zhang <Chenyi.Zhang@microsoft.com>
Co-authored-by: Chenyi Zhang <chenyzh@microsoft.com>
Co-authored-by: Feiyue Yu <fey@microsoft.com>
  • Loading branch information
4 people authored and ManuInNZ committed Apr 11, 2020
1 parent 14cd572 commit d2bc277
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/aks-preview/azext_aks_preview/_client_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def get_auth_management_client(cli_ctx, scope=None, **_):
matched = re.match('/subscriptions/(?P<subscription>[^/]*)/', scope)
if matched:
subscription_id = matched.groupdict()['subscription']
else:
raise CLIError("{} does not contain subscription Id.".format(scope))
return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_AUTHORIZATION, subscription_id=subscription_id)


Expand Down
5 changes: 3 additions & 2 deletions src/aks-preview/azext_aks_preview/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
validate_nodepool_name, validate_vm_set_type, validate_load_balancer_sku,
validate_load_balancer_outbound_ips, validate_load_balancer_outbound_ip_prefixes,
validate_taints, validate_priority, validate_eviction_policy, validate_spot_max_price, validate_acr, validate_user,
validate_load_balancer_outbound_ports, validate_load_balancer_idle_timeout, validate_nodepool_tags, validate_nodepool_labels)
validate_load_balancer_outbound_ports, validate_load_balancer_idle_timeout, validate_nodepool_tags,
validate_nodepool_labels, validate_vnet_subnet_id)
from ._consts import CONST_OUTBOUND_TYPE_LOAD_BALANCER, \
CONST_OUTBOUND_TYPE_USER_DEFINED_ROUTING, CONST_SCALE_SET_PRIORITY_REGULAR, CONST_SCALE_SET_PRIORITY_SPOT, \
CONST_SPOT_EVICTION_POLICY_DELETE, CONST_SPOT_EVICTION_POLICY_DEALLOCATE
Expand Down Expand Up @@ -80,7 +81,7 @@ def load_arguments(self, _):
c.argument('no_ssh_key', options_list=['--no-ssh-key', '-x'])
c.argument('pod_cidr')
c.argument('service_cidr')
c.argument('vnet_subnet_id')
c.argument('vnet_subnet_id', type=str, validator=validate_vnet_subnet_id)
c.argument('workspace_resource_id')
c.argument('skip_subnet_role_assignment', action='store_true')
c.argument('enable_cluster_autoscaler', action='store_true')
Expand Down
9 changes: 9 additions & 0 deletions src/aks-preview/azext_aks_preview/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ def validate_user(namespace):
raise CLIError("--user can only be clusterUser or clusterMonitoringUser")


def validate_vnet_subnet_id(namespace):
if namespace.vnet_subnet_id is not None:
if namespace.vnet_subnet_id == '':
return
from msrestazure.tools import is_valid_resource_id
if not is_valid_resource_id(namespace.vnet_subnet_id):
raise CLIError("--vnet-subnet-id is not a valid Azure resource ID.")


def validate_load_balancer_outbound_ports(namespace):
"""validate load balancer profile outbound allocated ports"""
if namespace.load_balancer_outbound_ports is not None:
Expand Down
5 changes: 2 additions & 3 deletions src/aks-preview/azext_aks_preview/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -1465,9 +1465,8 @@ def aks_kollect(cmd, # pylint: disable=too-many-statements,too-many-locals
if not prompt_y_n('Do you want to see analysis results now?', default="n"):
print(f"You can run 'az aks kanalyze -g {resource_group_name} -n {name}' "
f"anytime to check the analysis results.")
return

display_diagnostics_report(temp_kubeconfig_path)
else:
display_diagnostics_report(temp_kubeconfig_path)

return

Expand Down
31 changes: 31 additions & 0 deletions src/aks-preview/azext_aks_preview/tests/latest/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,34 @@ class Namespace:
def __init__(self, api_server_authorized_ip_ranges=None, cluster_autoscaler_profile=None):
self.api_server_authorized_ip_ranges = api_server_authorized_ip_ranges
self.cluster_autoscaler_profile = cluster_autoscaler_profile


class TestVNetSubnetId(unittest.TestCase):
def test_invalid_vnet_subnet_id(self):
invalid_vnet_subnet_id = "dummy subnet id"
namespace = VnetSubnetIdNamespace(invalid_vnet_subnet_id)
err = ("--vnet-subnet-id is not a valid Azure resource ID.")

with self.assertRaises(CLIError) as cm:
validators.validate_vnet_subnet_id(namespace)
self.assertEqual(str(cm.exception), err)

def test_valid_vnet_subnet_id(self):
invalid_vnet_subnet_id = "/subscriptions/testid/resourceGroups/MockedResourceGroup/providers/Microsoft.Network/virtualNetworks/MockedNetworkId/subnets/MockedSubNetId"
namespace = VnetSubnetIdNamespace(invalid_vnet_subnet_id)
validators.validate_vnet_subnet_id(namespace)

def test_none_vnet_subnet_id(self):
invalid_vnet_subnet_id = None
namespace = VnetSubnetIdNamespace(invalid_vnet_subnet_id)
validators.validate_vnet_subnet_id(namespace)

def test_empty_vnet_subnet_id(self):
invalid_vnet_subnet_id = ""
namespace = VnetSubnetIdNamespace(invalid_vnet_subnet_id)
validators.validate_vnet_subnet_id(namespace)


class VnetSubnetIdNamespace:
def __init__(self, vnet_subnet_id):
self.vnet_subnet_id = vnet_subnet_id

0 comments on commit d2bc277

Please sign in to comment.