Skip to content

Commit

Permalink
[ACS] format default dns_name_prefix (Azure#4237)
Browse files Browse the repository at this point in the history
* format default dns_name_prefix in a way not breaking dns name rules

* add a help message for dns_name_prefix

* fix History.rst

* minor fix

* refactor and add test
  • Loading branch information
rjtsdl authored and tjprescott committed Aug 17, 2017
1 parent fcdf873 commit ec20314
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/command_modules/azure-cli-acs/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Release History
unreleased
+++++++++++++++++++
* correct preview regions
* format default dns_name_prefix properly

2.0.13 (2017-08-15)
+++++++++++++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <clustername>-<resourcegroupname>-<subid>, 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import webbrowser
import stat
import ssl
import re
import yaml
import dateutil.parser
from dateutil.relativedelta import relativedelta
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,26 @@
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)
from azure.cli.core.util import CLIError


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'
Expand Down

0 comments on commit ec20314

Please sign in to comment.