Use only the token to authenticate to synnefo
[snf-image-creator] / image_creator / dialog_menu.py
index 741ae92..c8cfe27 100644 (file)
@@ -44,7 +44,8 @@ from image_creator.output.dialog import GaugeOutput, InfoBoxOutput
 from image_creator.kamaki_wrapper import Kamaki, ClientError
 from image_creator.help import get_help_file
 from image_creator.dialog_util import SMALL_WIDTH, WIDTH, \
-    update_background_title, confirm_reset, confirm_exit, Reset, extract_image
+    update_background_title, confirm_reset, confirm_exit, Reset, \
+    extract_image, extract_metadata_string
 
 CONFIGURATION_TASKS = [
     ("Partition table manipulation", ["FixPartitionTable"],
@@ -106,31 +107,16 @@ class metadata_monitor(object):
         d.msgbox(msg, title="Image Property Changes", width=SMALL_WIDTH)
 
 
-def extract_metadata_string(session):
-    metadata = ['%s=%s' % (k, v) for (k, v) in session['metadata'].items()]
-
-    if 'task_metadata' in session:
-        metadata.extend("%s=yes" % m for m in session['task_metadata'])
-
-    return '\n'.join(metadata) + '\n'
-
-
 def upload_image(session):
     d = session["dialog"]
     dev = session['device']
     size = dev.size
 
     if "account" not in session:
-        d.msgbox("You need to provide your ~okeanos login username before you "
+        d.msgbox("You need to provide your ~okeanos account before you "
                  "can upload images to pithos+", width=SMALL_WIDTH)
         return False
 
-    if "token" not in session:
-        d.msgbox("You need to provide your ~okeanos account authentication "
-                 "token before you can upload images to pithos+",
-                 width=SMALL_WIDTH)
-        return False
-
     while 1:
         init = session["upload"] if "upload" in session else ''
         (code, answer) = d.inputbox("Please provide a filename:", init=init,
@@ -155,7 +141,7 @@ def upload_image(session):
                 md5 = MD5(out)
                 session['checksum'] = md5.compute(session['snapshot'], size)
 
-            kamaki = Kamaki(session['account'], session['token'], out)
+            kamaki = Kamaki(session['account'], out)
             try:
                 # Upload image file
                 with open(session['snapshot'], 'rb') as f:
@@ -205,12 +191,6 @@ def register_image(session):
                  width=SMALL_WIDTH)
         return False
 
-    if "token" not in session:
-        d.msgbox("You need to provide your ~okeanos account authentication "
-                 "token before you can register an images to cyclades",
-                 width=SMALL_WIDTH)
-        return False
-
     if "pithos_uri" not in session:
         d.msgbox("You need to upload the image to pithos+ before you can "
                  "register it to cyclades", width=SMALL_WIDTH)
@@ -241,7 +221,7 @@ def register_image(session):
         try:
             out.output("Registering image with Cyclades...")
             try:
-                kamaki = Kamaki(session['account'], session['token'], out)
+                kamaki = Kamaki(session['account'], out)
                 kamaki.register(name, session['pithos_uri'], metadata)
                 out.success('done')
             except ClientError as e:
@@ -261,21 +241,20 @@ def kamaki_menu(session):
     d = session['dialog']
     default_item = "Account"
 
-    account = Kamaki.get_account()
-    if account:
-        session['account'] = account
-
-    token = Kamaki.get_token()
-    if token:
-        session['token'] = token
+    if 'account' not in session:
+        token = Kamaki.get_token()
+        if token:
+            session['account'] = Kamaki.get_account(token)
+            if not session['account']:
+                del session['account']
+                Kamaki.save_token('')  # The token was not valid. Remove it
 
     while 1:
-        account = session["account"] if "account" in session else "<none>"
-        token = session["token"] if "token" in session else "<none>"
+        account = session["account"]['username'] if "account" in session else \
+            "<none>"
         upload = session["upload"] if "upload" in session else "<none>"
 
-        choices = [("Account", "Change your ~okeanos username: %s" % account),
-                   ("Token", "Change your ~okeanos token: %s" % token),
+        choices = [("Account", "Change your ~okeanos account: %s" % account),
                    ("Upload", "Upload image to pithos+"),
                    ("Register", "Register the image to cyclades: %s" % upload)]
 
@@ -291,31 +270,23 @@ def kamaki_menu(session):
         if choice == "Account":
             default_item = "Account"
             (code, answer) = d.inputbox(
-                "Please provide your ~okeanos account e-mail address:",
-                init=session["account"] if "account" in session else '',
-                width=WIDTH)
+                "Please provide your ~okeanos token:",
+                init=session["account"]['auth_token'] if "account" in session
+                else '', width=WIDTH)
             if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
                 continue
             if len(answer) == 0 and "account" in session:
                     del session["account"]
             else:
-                session["account"] = answer.strip()
-                Kamaki.save_account(session['account'])
-                default_item = "Token"
-        elif choice == "Token":
-            default_item = "Token"
-            (code, answer) = d.inputbox(
-                "Please provide your ~okeanos account authetication token:",
-                init=session["token"] if "token" in session else '',
-                width=WIDTH)
-            if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
-                continue
-            if len(answer) == 0 and "token" in session:
-                del session["token"]
-            else:
-                session["token"] = answer.strip()
-                Kamaki.save_token(session['token'])
-                default_item = "Upload"
+                token = answer.strip()
+                session['account'] = Kamaki.get_account(token)
+                if session['account'] is not None:
+                    Kamaki.save_token(token)
+                    default_item = "Upload"
+                else:
+                    del session['account']
+                    d.msgbox("The token you provided is not valid!",
+                        width=SMALL_WIDTH)
         elif choice == "Upload":
             if upload_image(session):
                 default_item = "Register"