text: # --------------------------------------------------------------------------------------------
  # Copyright (c) Microsoft Corporation. All rights reserved.
  # Licensed under the MIT License. See License.txt in the project root for license information.
  # --------------------------------------------------------------------------------------------
  
  import os
  import os.path
  import re
  
  from azure.cli.core.util import CLIError
  import azure.cli.core.azlogging as azlogging
  import azure.cli.core.keys as keys
  
  logger = azlogging.get_az_logger(__name__)
  
  
  def validate_ssh_key(namespace):
      string_or_file = (namespace.ssh_key_value or
                        os.path.join(os.path.expanduser('~'), '.ssh', 'id_rsa.pub'))
      content = string_or_file
      if os.path.exists(string_or_file):
          logger.info('Use existing SSH public key file: %s', string_or_file)
          with open(string_or_file, 'r') as f:
              content = f.read()
      elif not keys.is_valid_ssh_rsa_public_key(content):
          if namespace.generate_ssh_keys:
              # figure out appropriate file names:
              # 'base_name'(with private keys), and 'base_name.pub'(with public keys)
              public_key_filepath = string_or_file
              if public_key_filepath[-4:].lower() == '.pub':
                  private_key_filepath = public_key_filepath[:-4]
              else:
                  private_key_filepath = public_key_filepath + '.private'
              content = keys.generate_ssh_keys(private_key_filepath, public_key_filepath)
              logger.warning("SSH key files '%s' and '%s' have been generated under ~/.ssh to "
                             "allow SSH access to the VM. If using machines without "
                             "permanent storage like Azure Cloud Shell without an attached "
                             "file share, back up your keys to a safe location",
                             private_key_filepath, public_key_filepath)
          else:
              raise CLIError('An RSA key file or key value must be supplied to SSH Key Value. '
                             'You can use --generate-ssh-keys to let CLI generate one for you')
      namespace.ssh_key_value = content
  
  
  def validate_list_of_integers(string):
      # extract comma separated list of integers
      return list(map(int, string.split(',')))
  
  
  def validate_create_parameters(namespace):
      if not namespace.name:
          raise CLIError('--name has no value')
      if namespace.dns_name_prefix is not None and not namespace.dns_name_prefix:
          raise CLIError('--dns-prefix has no value')
  
  
  def validate_k8s_version(namespace):
      """Validates a string as a possible Kubernetes version."""
      k8s_release_regex = re.compile(r'^[v|V]?(\d+\.\d+\.\d+.*)$')
      found = k8s_release_regex.findall(namespace.kubernetes_version)
      if found:
          namespace.kubernetes_version = found[0]
      else:
          raise CLIError('--kubernetes-version should be the full version number, '
                         'such as "1.7.7" or "1.8.1"')
  
  
  def validate_k8s_client_version(namespace):
      """Validates a string as a possible Kubernetes version."""
      k8s_release_regex = re.compile(r'^[v|V]?(\d+\.\d+\.\d+.*|latest)$')
      found = k8s_release_regex.findall(namespace.client_version)
      if found:
          namespace.client_version = found[0]
      else:
          raise CLIError('--client-version should be the full version number '
                         '(such as "1.7.7" or "1.8.1") or "latest"')
  
  
  def validate_linux_host_name(namespace):
      """Validates a string as a legal host name component.
  
      This validation will also occur server-side in the ARM API, but that may take
      a minute or two before the user sees it. So it's more user-friendly to validate
      in the CLI pre-flight.
      """
      # https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
      rfc1123_regex = re.compile(r'^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$')  # pylint:disable=line-too-long
      found = rfc1123_regex.findall(namespace.name)
      if not found:
          raise CLIError('--name cannot exceed 63 characters and can only contain '
                         'letters, numbers, or dashes (-).')
  