confirmation when changing service levels, catch exceptions and show to user
smbaker [Tue, 28 Aug 2012 02:27:47 +0000 (19:27 -0700)]
apps/gacksadmin/action.php
apps/gacksadmin/gacksclient.php

index ea173b5..e62b004 100644 (file)
@@ -40,130 +40,177 @@ if ( ! $action ) {
 
 $gacksapi = new GACKSAPI();
 
-switch ($action) {
+try {
 
-    case 'update-account': {
-        $account_name = $_POST['account_name'];
-        $account_kind = $_POST['account_kind'];
+    switch ($action) {
 
-        $fields = array();
-        $fields['name'] = gacksid($account_name);
-        $fields['kind'] = $account_kind;
-        $fields['level'] = $_POST['level'];
-        $fields['multiplier'] = $_POST['multiplier'];
-        $fields['goodStanding'] = $_POST['goodStanding'];
-        $fields['billingContacts'] = $_POST['billingContacts'];
-        $fields['freezeUnreserved'] = ! $_POST['enableBestEffort'];
+        case 'update-account': {
+            $account_name = $_POST['account_name'];
+            $account_kind = $_POST['account_kind'];
 
-        $gacksapi->update_account($fields);
+            $fields = array();
+            $fields['name'] = gacksid($account_name);
+            $fields['kind'] = $account_kind;
+            $fields['level'] = $_POST['level'];
+            $fields['multiplier'] = $_POST['multiplier'];
+            $fields['goodStanding'] = $_POST['goodStanding'];
+            $fields['billingContacts'] = $_POST['billingContacts'];
+            $fields['freezeUnreserved'] = ! $_POST['enableBestEffort'];
 
-        drupal_set_message ('Account updated');
+            $gacksapi->update_account($fields);
 
-        plc_redirect(l_gacks_account($account_name, $account_kind));
-        break;
-    }
+            drupal_set_message ('Account updated');
 
-    case 'create-account': {
-        $account_name = $_POST['account_name'];
-        $account_kind = $_POST['account_kind'];
+            plc_redirect(l_gacks_account($account_name, $account_kind));
+            break;
+        }
 
-        $fields = array();
-        $fields['name'] = gacksid_guess_prefix($account_name);
-        $fields['kind'] = $account_kind;
-        $fields['level'] = $_POST['level'];
-        $fields['multiplier'] = $_POST['multiplier'];
-        $fields['freezeUreserved'] = ! $_POST['enableBestEffort'];
+        case 'create-account': {
+            $account_name = $_POST['account_name'];
+            $account_kind = $_POST['account_kind'];
 
-        $gacksapi->create_account($fields);
+            $fields = array();
+            $fields['name'] = gacksid_guess_prefix($account_name);
+            $fields['kind'] = $account_kind;
+            $fields['level'] = $_POST['level'];
+            $fields['multiplier'] = $_POST['multiplier'];
+            $fields['freezeUreserved'] = ! $_POST['enableBestEffort'];
 
-        drupal_set_message ('Account created');
+            $gacksapi->create_account($fields);
 
-        plc_redirect(l_gacks_account($account_name, $account_kind));
-        break;
-    }
+            drupal_set_message ('Account created');
 
-    case 'delete-account': {
-        $account_name = $_POST['account_name'];
-        $account_kind = $_POST['account_kind'];
+            plc_redirect(l_gacks_account($account_name, $account_kind));
+            break;
+        }
 
-        $gacksapi->delete_account(gacksid($account_name), $account_kind);
+        case 'delete-account': {
+            $account_name = $_POST['account_name'];
+            $account_kind = $_POST['account_kind'];
 
-        drupal_set_message ('Account deleted');
+            $gacksapi->delete_account(gacksid($account_name), $account_kind);
 
-        plc_redirect(l_gacks_accounts());
-        break;
-    }
+            drupal_set_message ('Account deleted');
 
-    case 'add-charge': {
-        $account_name = $_POST['account_name'];
-        $account_kind = $_POST['account_kind'];
-        $kind = intval($_POST['kind']);
-        $desc = $_POST['description'];
-        $amount = floatval($_POST['amount']);
-        $date = time();
+            plc_redirect(l_gacks_accounts());
+            break;
+        }
 
-        $gacksapi->add_charge(gacksid($account_name), $desc, $kind, $date, $amount, 1);
+        case 'add-charge': {
+            $account_name = $_POST['account_name'];
+            $account_kind = $_POST['account_kind'];
+            $kind = intval($_POST['kind']);
+            $desc = $_POST['description'];
+            $amount = floatval($_POST['amount']);
+            $date = time();
 
-        drupal_set_message ('Charge applied');
+            $gacksapi->add_charge(gacksid($account_name), $desc, $kind, $date, $amount, 1);
 
-        plc_redirect(l_gacks_account($account_name, $account_kind));
-        break;
-    }
+            drupal_set_message ('Charge applied');
 
-    case 'node-wizard': {
-        $account_name = $_POST['account_name'];
-        $resource_name = $_POST['resource_name'];
-        $resource_group = $_POST['resource_group'];
-        $expand = intval($_POST['expand']);
-        $amount = intval($_POST['amount']);
-        $image = $_POST['image'];
-        $gacksapi->hrn_object = "plc.vicci." . str_replace("_", ".", $account_name);
+            plc_redirect(l_gacks_account($account_name, $account_kind));
+            break;
+        }
 
-        $options = array("image" => $image);
+        case 'node-wizard': {
+            $account_name = $_POST['account_name'];
+            $resource_name = $_POST['resource_name'];
+            $resource_group = $_POST['resource_group'];
+            $expand = intval($_POST['expand']);
+            $amount = intval($_POST['amount']);
+            $image = $_POST['image'];
+            $gacksapi->hrn_object = "plc.vicci." . str_replace("_", ".", $account_name);
+
+            // XXX we need a permission check here.
+
+            $options = array("image" => $image);
+
+            $result = $gacksapi->pick($resource_name, $resource_group, $amount, $expand, $options);
+
+            $status = $result['status'];
+
+            if (!$status) {
+                drupal_set_message('Failed to Pick Nodes');
+                return;
+            } else {
+                print "added the following nodes:<br>";
+                foreach ($result['addSetNames'] as $name) {
+                    print " " . $name . "<br>";
+                }
+                print "<br>removed the following nodes:<br>";
+                foreach ($result['delSetNames'] as $name) {
+                    print " " .$name . "<br>";
+                }
+                if (array_key_exists("headNode", $result)) {
+                print "<br>head node (for Hadoop or MPI slices):<br>";
+                print $result['headNode'];
+                }
+            }
 
-        update_gacks_session(array("account_name" => $account_name));
+            drupal_set_message('Nodes Picked');
+            break;
+        }
 
-        // XXX we need a permission check here.
+        case 'set-account': {
+            $account_name = $_POST['account_name'];
+            $url = $_POST['url'];
 
-        $result = $gacksapi->pick($resource_name, $resource_group, $amount, $expand, $options);
+            // XXX we need a permission check here.
 
-        $status = $result['status'];
+            update_gacks_session(array("account_name" => $account_name));
+            Header('Location: ' . $url);
+            break;
+        }
 
-        if (!$status) {
-            drupal_set_message('Failed to Pick Nodes');
-            return;
-        } else {
-            print "added the following nodes:<br>";
-            foreach ($result['addSetNames'] as $name) {
-                print " " . $name . "<br>";
+        case 'set-service': {
+            $account_name = $_POST['account_name'];
+            $level = $_POST['level'];
+            $autoRenew = $_POST['autoRenew'];
+            $freezeUnreserved = $_POST['freezeUnreserved'];
+            $confirmed = $_POST['confirmed'];
+
+            // XXX we need a permission check here.
+
+            $fields = array();
+            $fields["name"] = gacksid($account_name);
+            $fields["kind"] = "user";
+
+            if (($level != NULL) and ($level!="")) {
+                $fields['level'] = $level;
+                if ($confirmed!='yes') {
+                    plc_redirect(l_gacks_confirm_service($account_name, $level, $autoRenew, $freezeUnreserved));
+                    return;
+                }
             }
-            print "<br>removed the following nodes:<br>";
-            foreach ($result['delSetNames'] as $name) {
-                print " " .$name . "<br>";
+
+            if (($autoRenew != NULL) and ($autoRenew != "")) {
+                $fields['autoRenew'] = $autoRenew;
             }
-            if (array_key_exists("headNode", $result)) {
-            print "<br>head node (for Hadoop or MPI slices):<br>";
-            print $result['headNode'];
+
+            if (($freezeUnreserved != NULL) and ($freezeUnreserved != "")) {
+                $fields['freezeUnreserved'] = $freezeUnreserved;
             }
-        }
 
-        drupal_set_message('Nodes Picked');
-        break;
-    }
+            $gacksapi->update_account($fields, "restricted");
 
-    case 'set-account': {
-        $account_name = $_POST['account_name'];
-        $url = $_POST['url'];
+            drupal_set_message ('Settings updated');
 
-        update_gacks_session(array("account_name" => $account_name));
-        Header('Location: ' . $url);
-        break;
-    }
+            plc_redirect(l_gacks_service_level());
+            break;
+        }
 
-    default: {
-        plc_error ("Unknown action $action in actions.php");\r
-        return;\r
+        default: {
+            plc_error ("Unknown action $action in actions.php");\r
+            return;\r
+        }
     }
+} catch (GacksClientException $e) {
+    // The action failed on the client side; let the user know something went
+    // wrong.
+    $msg = $e->getMessage();
+    $msg = "<pre>" . $msg . "</pre>";
+    drupal_set_message($msg, 'error');
+    // TODO: Tell the user what to do about it. Give him an email address.
 }
 
 ?>
index 99d8613..2e48458 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+class GacksClientException extends Exception {}
+
 class GACKSAPI
 {
   function GACKSAPI($server = 'vicci.org',
@@ -20,6 +22,10 @@ class GACKSAPI
 \r
     $this->errors[] = 'GACKSAPI error:  ' . $error_msg . ' in ' . $file . ' on line ' . $line;\r
     error_log(end($this->errors));\r
+\r
+    throw new GacksClientException($error_msg);\r
+\r
+    //drupal_set_message($error_msg . ' in ' . $file . ' on line ' . $line, 'error');\r
   }\r
 \r
   function error()\r