Add {enable, disable}_guestfs methods in image cls
[snf-image-creator] / image_creator / dialog_menu.py
index 9bc5c40..351f0b9 100644 (file)
@@ -68,6 +68,8 @@ CONFIGURATION_TASKS = [
     ("File injection", ["EnforcePersonality"], ["windows", "linux"])
 ]
 
+SYSPREP_PARAM_MAXLEN = 20
+
 
 class MetadataMonitor(object):
     """Monitors image metadata chages"""
@@ -397,9 +399,9 @@ def kamaki_menu(session):
                 if len(Kamaki.get_clouds()):
                     default_item = "Cloud"
                 else:
-                    default_time = "Add/Edit"
+                    default_item = "Add/Edit"
             else:
-                default_time = "Delete"
+                default_item = "Delete"
         elif choice == "Cloud":
             default_item = "Cloud"
             clouds = Kamaki.get_clouds()
@@ -620,36 +622,70 @@ def exclude_tasks(session):
 
 
 def sysprep_params(session):
-
+    """Collect the needed sysprep parameters"""
     d = session['dialog']
     image = session['image']
 
     available = image.os.sysprep_params
-    needed = image.os.needed_sysprep_params()
+    needed = image.os.needed_sysprep_params
 
     if len(needed) == 0:
         return True
 
-    fields = []
-    for param in needed:
-        default = available[param.name] if param.name in available else ""
-        fields.append(("%s: " % param.description, default, param.length))
+    def print_form(names, extra_button=False):
+        """print the dialog form providing sysprep_params"""
+        fields = []
+        for name in names:
+            param = needed[name]
+            default = str(available[name]) if name in available else ""
+            fields.append(("%s: " % param.description, default,
+                           SYSPREP_PARAM_MAXLEN))
+
+        kwargs = {}
+        if extra_button:
+            kwargs['extra_button'] = 1
+            kwargs['extra_label'] = "Advanced"
+
+        txt = "Please provide the following system preparation parameters:"
+        return d.form(txt, height=13, width=WIDTH, form_height=len(fields),
+                      fields=fields, **kwargs)
+
+    def check_params(names, values):
+        """check if the provided sysprep parameters have leagal values"""
+        for i in range(len(names)):
+            param = needed[names[i]]
+            try:
+                normalized = param.type(values[i])
+                if param.validate(normalized):
+                    image.os.sysprep_params[names[i]] = normalized
+                    continue
+            except ValueError:
+                pass
 
-    txt = "Please provide the following system preparation parameters:"
-    code, output = d.form(txt, height=13, width=WIDTH, form_height=len(fields),
-                          fields=fields)
+            d.msgbox("Invalid value for parameter: `%s'" % names[i],
+                     width=SMALL_WIDTH)
+            return False
+        return True
 
-    if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
-        return False
+    simple_names = [k for k, v in needed.items() if v.default is None]
+    advanced_names = [k for k, v in needed.items() if v.default is not None]
 
-    sysprep_params = {}
-    for i in range(len(fields)):
-        if needed[i].validator(output[i]):
-            image.os.sysprep_params[needed[i].name] = output[i]
-        else:
-            d.msgbox("The value you provided for parameter: %s is not valid" %
-                     name, width=SMALL_WIDTH)
+    while 1:
+        code, output = print_form(simple_names, extra_button=True)
+
+        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
             return False
+        if code == d.DIALOG_EXTRA:
+            while 1:
+                code, output = print_form(advanced_names)
+                if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
+                    break
+                if check_params(advanced_names, output):
+                    break
+            continue
+
+        if check_params(simple_names, output):
+            break
 
     return True