diff --git a/src/command_modules/azure-cli-acs/HISTORY.rst b/src/command_modules/azure-cli-acs/HISTORY.rst index e8054f1ee4b..a20d5573e02 100644 --- a/src/command_modules/azure-cli-acs/HISTORY.rst +++ b/src/command_modules/azure-cli-acs/HISTORY.rst @@ -5,6 +5,7 @@ Release History unreleased +++++++++++++++++++ * correct preview regions +* format default dns_name_prefix properly 2.0.13 (2017-08-15) +++++++++++++++++++ diff --git a/src/command_modules/azure-cli-acs/azure/cli/command_modules/acs/_params.py b/src/command_modules/azure-cli-acs/azure/cli/command_modules/acs/_params.py index e7909cc3eea..2b2d80ceeea 100644 --- a/src/command_modules/azure-cli-acs/azure/cli/command_modules/acs/_params.py +++ b/src/command_modules/azure-cli-acs/azure/cli/command_modules/acs/_params.py @@ -81,7 +81,7 @@ def _get_feature_in_preview_message(): # some admin names are prohibited in acs, such as root, admin, etc. Because we have no control on the orchestrators, so default to a safe name. register_cli_argument('acs', 'admin_username', options_list=('--admin-username',), default='azureuser', required=False) register_cli_argument('acs', 'api_version', options_list=('--api-version',), required=False, help=_get_feature_in_preview_message() + 'Use API version of ACS to perform az acs operations. Available options: 2017-01-31, 2017-07-01. Default to use the latest version for the location') -register_cli_argument('acs', 'dns_name_prefix', options_list=('--dns-prefix', '-d')) +register_cli_argument('acs', 'dns_name_prefix', options_list=('--dns-prefix', '-d'), help='default use the format of --, will trim the length and replace sensitive characters if needed') register_cli_argument('acs', 'container_service_name', options_list=('--name', '-n'), help='The name of the container service', completer=get_resource_name_completion_list('Microsoft.ContainerService/ContainerServices')) register_cli_argument('acs', 'ssh_key_value', required=False, help='SSH key file value or key file path.', type=file_type, default=os.path.join('~', '.ssh', 'id_rsa.pub'), completer=FilesCompleter()) diff --git a/src/command_modules/azure-cli-acs/azure/cli/command_modules/acs/custom.py b/src/command_modules/azure-cli-acs/azure/cli/command_modules/acs/custom.py index c557a385b89..413f7be3582 100644 --- a/src/command_modules/azure-cli-acs/azure/cli/command_modules/acs/custom.py +++ b/src/command_modules/azure-cli-acs/azure/cli/command_modules/acs/custom.py @@ -21,6 +21,7 @@ import webbrowser import stat import ssl +import re import yaml import dateutil.parser from dateutil.relativedelta import relativedelta @@ -370,6 +371,15 @@ def _get_subscription_id(): return sub_id +def _get_default_dns_prefix(name, resource_group_name, subscription_id): + # Use subscription id to provide uniqueness and prevent DNS name clashes + name_part = re.sub('[^A-Za-z0-9-]', '', name)[0:10] + if not name_part[0].isalpha(): + name_part = (str('a') + name_part)[0:10] + resource_group_part = re.sub('[^A-Za-z0-9-]', '', resource_group_name)[0:16] + return '{}-{}-{}'.format(name_part, resource_group_part, subscription_id[0:6]) + + # pylint: disable=too-many-locals # pylint: disable-msg=too-many-arguments def acs_create(resource_group_name, deployment_name, name, ssh_key_value, dns_name_prefix=None, @@ -482,8 +492,7 @@ def acs_create(resource_group_name, deployment_name, name, ssh_key_value, dns_na subscription_id = _get_subscription_id() if not dns_name_prefix: - # Use subscription id to provide uniqueness and prevent DNS name clashes - dns_name_prefix = '{}-{}-{}'.format(name, resource_group_name, subscription_id[0:6]) + dns_name_prefix = _get_default_dns_prefix(name, resource_group_name, subscription_id) register_providers() groups = _resource_client_factory().resource_groups diff --git a/src/command_modules/azure-cli-acs/azure/cli/command_modules/acs/tests/test_custom.py b/src/command_modules/azure-cli-acs/azure/cli/command_modules/acs/tests/test_custom.py index 6296054d004..5a2c67bda5b 100644 --- a/src/command_modules/azure-cli-acs/azure/cli/command_modules/acs/tests/test_custom.py +++ b/src/command_modules/azure-cli-acs/azure/cli/command_modules/acs/tests/test_custom.py @@ -14,7 +14,7 @@ from msrestazure.azure_exceptions import CloudError from azure.cli.command_modules.acs.custom import (merge_kubernetes_configurations, - _acs_browse_internal, _add_role_assignment) + _acs_browse_internal, _add_role_assignment, _get_default_dns_prefix) from azure.mgmt.containerservice.models import (ContainerServiceOrchestratorTypes, ContainerService, ContainerServiceOrchestratorProfile) @@ -22,6 +22,18 @@ class AcsCustomCommandTest(unittest.TestCase): + def test_get_default_dns_prefix(self): + name = 'test5678910' + resource_group_name = 'resource_group_with_underscore' + sub_id = '123456789' + + dns_name_prefix = _get_default_dns_prefix(name, resource_group_name, sub_id) + self.assertEqual(dns_name_prefix, "test567891-resourcegroupwit-123456") + + name = '1test5678910' + dns_name_prefix = _get_default_dns_prefix(name, resource_group_name, sub_id) + self.assertEqual(dns_name_prefix, "a1test5678-resourcegroupwit-123456") + def test_add_role_assignment_basic(self): role = 'Owner' sp = '1234567'