7191720 S11 sysconfig doesn't allow . in the hostname
authornirmal27<Nirmal.Agarwal@oracle.com>
Thu, 23 Aug 2012 12:38:45 -0600
changeset 1767 b3b03b4b6c9f
parent 1766 71fac55960e3
child 1768 b1640ab6e595
7191720 S11 sysconfig doesn't allow . in the hostname
usr/src/cmd/system-config/network_type.py
--- a/usr/src/cmd/system-config/network_type.py	Thu Aug 23 00:17:59 2012 -0600
+++ b/usr/src/cmd/system-config/network_type.py	Thu Aug 23 12:38:45 2012 -0600
@@ -19,7 +19,7 @@
 #
 # CDDL HEADER END
 #
-# Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
 #
 
 '''
@@ -143,10 +143,10 @@
         scrollable_columns = SystemInfo.MAX_HOSTNAME_LEN + 1
         hostname_area = WindowArea(1, cols, y_loc, self.hostfield_offset,
                                    scrollable_columns=scrollable_columns)
-        self.hostname = EditField(hostname_area, 
+        self.hostname = EditField(hostname_area,
                                   window=self.center_win,
                                   text=self.sys_info.hostname,
-                                  validate=hostname_is_valid,
+                                  validate=self.validate,
                                   error_win=self.main_win.error_line)
         self.hostname.item_key = None
 
@@ -221,29 +221,46 @@
         LOGGER.info("Configuring NIC as: %s", self.nic_info.type)
         self.sys_info.hostname = self.hostname.get_text()
     
-    def validate(self):
-        '''Ensure hostname is set and a network type is chosen (unless no
-        NICs are present)
+    def validate(self, edit_field=None):
+        '''Ensure hostname is set and validated.
+        Make sure a network type is chosen (unless no NICs are present).
+        If edit_field is None, validate all parameters when user leaves 
+        the screen else validate each key press when user is in
+        process of typing hostname.
         
         '''
-        hostname_text = self.hostname.get_text()
-        if not hostname_text:
-            raise UIMessage(_("A Hostname is required."))
-        if len(hostname_text) < 2:
-            raise UIMessage(_("A Hostname must be at least two characters."))
-        if self.configure_network and self.have_nic:
-            item_key = self.center_win.get_active_object().item_key
-            if item_key not in self.net_type_dict:
-                raise UIMessage(_("Select the wired network configuration: "
-                                   "Automatically, Manually, or None."))
+        if edit_field is not None:
+            # key press validation
+            # return if edit_field is empty
+            hostname_text = edit_field.get_text()
+            if not hostname_text:
+                return
+            # validate the leading character
+            if not hostname_text[0].isalnum():
+                raise UIMessage(_("The Hostname must start with an alphanumeric "
+                                  "character."))
 
+            if not hostname_text.replace(u"-", "a").replace(u".", "a").isalnum():
+                raise UIMessage(_("The Hostname can only contain letters, "
+                                  "numbers, periods, and minus signs (-)."))
+        else:
+            # when the user tries to move forward to the next screen,
+            # check the following:
+            #  - the existence of a hostname
+            #  - the trailing character
+            #  - if the string is all digits
+            #  - network type is chosen
+            hostname_text = self.hostname.get_text()
+            if not hostname_text:
+                raise UIMessage(_("A Hostname is required."))
+            if not hostname_text[-1].isalnum():
+                raise UIMessage(_("The Hostname must end with an alphanumeric "
+                                  "character."))
+            if hostname_text.isdigit():
+                raise UIMessage(_("The Hostname cannot be all digits."))
 
-def hostname_is_valid(edit_field):
-    '''Check hostname for characters other than a-zA-Z0-9 and hyphens'''
-    user_str = edit_field.get_text()
-    if not user_str:
-        return True
-    test_str = user_str.replace(u"-", "a")
-    if not test_str.isalnum():
-        raise UIMessage(_("The Hostname can only contain letters, numbers, "
-                            "and minus signs (-)."))
+            if self.configure_network and self.have_nic:
+                item_key = self.center_win.get_active_object().item_key
+                if item_key not in self.net_type_dict:
+                    raise UIMessage(_("Select the wired network configuration:"
+                                      " Automatically, Manually, or None."))