accounts and invoices use plc slice name instead of hrn
smbaker [Fri, 8 Jun 2012 01:04:20 +0000 (18:04 -0700)]
13 files changed:
apps/gacks/API.py
apps/gacks/gacksbackend.py
apps/gacks/gackscollector.py [changed mode: 0755->0644]
apps/gacks/gacksenforce.py
apps/gacks/gacksexcep.py
apps/gacks/gacksid.py [new file with mode: 0644]
apps/gacksadmin/account.php
apps/gacksadmin/action.php
apps/gacksadmin/gacks_functions.php
apps/gacksadmin/invoices.php
apps/gacksui/gacksInvoiceScreen.py
apps/gacksui/gacksui.py
rpm_builds/SPECS/gackscentral.spec.template

index cbb948f..2140e3d 100644 (file)
@@ -28,6 +28,7 @@ from gacksinvoice import GacksInvoiceManager, STATE_PENDING
 from gackspolicy import GacksPolicyDirectory
 from gacksenforce import GacksEnforcer
 from gacksnodepicker import GacksNodePicker
+from gacksid import str_to_gacksid
 
 from AuthenticatedApi import AuthenticatedApi
 
@@ -727,6 +728,8 @@ class RemoteApi(AuthenticatedApi):
     def get_account(self, authToken_str, name, kind):
         (authToken, callerGID, objectGID) = self.authenticateToken(authToken_str, [name, kind])
 
+        name = str_to_gacksid(name)
+
         # ... if we want to create accounts automatically ...
         #acct = self.accounts.get_account(name, kind, create_if_not_exist=True)
         #if acct.created:
@@ -747,6 +750,8 @@ class RemoteApi(AuthenticatedApi):
     def delete_account(self, authToken_str, name, kind):
         (authToken, callerGID, objectGID) = self.authenticateToken(authToken_str, [name, kind])
 
+        name = str_to_gacksid(name)
+
         self.acls.test_acl("accounts", objectGID.get_hrn(), "delete")
 
         self.accounts.delete_account(name, kind)
@@ -767,7 +772,7 @@ class RemoteApi(AuthenticatedApi):
 
         self.acls.test_acl("accounts", objectGID.get_hrn(), "create")
 
-        name = args["name"]
+        name = str_to_gacksid(args["name"])
         kind = args["kind"]
 
         acct = self.accounts.get_account(name, kind, create_if_not_exist=True)
@@ -785,7 +790,7 @@ class RemoteApi(AuthenticatedApi):
 
         self.acls.test_acl("accounts", objectGID.get_hrn(), "update")
 
-        name = args["name"]
+        name = str_to_gacksid(args["name"])
         kind = args["kind"]
 
         acct = self.accounts.get_account(name, kind, create_if_not_exist=False)
@@ -809,9 +814,14 @@ class RemoteApi(AuthenticatedApi):
             assert(date_filter=={} or date_filter==None)
             assert(parent_id==None)
             file("/tmp/foo.txt","a").write(str(filter)+"\n")
+
+            if "name" in filter:
+                filter["name"] = str_to_gacksid(filter["name"])
+
             invoice = self.invoices.get_invoice_prime(filter, lookup_objects=True)
         else:
             #file("/tmp/foo.txt","a").write(str((name,state,date_filter,parent_id))+"\n")
+            name = str_to_gacksid(name)
             invoice = self.invoices.get_invoice(name, state=state, date_filter=date_filter, parent_id=parent_id, lookup_objects=True)
 
         return invoice.as_dict()
@@ -821,6 +831,8 @@ class RemoteApi(AuthenticatedApi):
 
         self.acls.test_acl("invoices", objectGID.get_hrn(), "add")
 
+        account = str_to_gacksid(account)
+
         self.invoices.add_charge(account, object, kind_id, date, amount, state, parent_id)
         return 0;
 
index d38f8b8..7205aa7 100644 (file)
@@ -27,6 +27,7 @@ from gackspolicy import *
 from gacksinvoice import *
 from gacksclient import GacksClient
 from gackshandler_plc import GacksPLCHandler
+from gacksid import hrn_to_gacksid
 
 glo_daemon = False
 glo_test = False
@@ -67,7 +68,7 @@ class GacksBackend:
        logger.info("do_accounting_grouped on " + resDef.name)
        for allocatorHRN in allocators.keys():
            # we need the account to know the policy
-           account = self.accounts.get_account(allocatorHRN, "user")
+           account = self.accounts.get_account(hrn_to_gacksid(allocatorHRN), "user")
            if not account:
                logger.warning("no account: %s" % allocatorHRN)
                return
@@ -86,7 +87,7 @@ class GacksBackend:
                    logger.info("add charge %s %s %d %d" % (allocatorHRN, hostitem.name, KIND_SLOT_CHARGE, 1 * policy.cost))
 
                    try:
-                        self.invoices.add_charge(allocatorHRN, hostitem.name, KIND_SLOT_CHARGE, tResv, 1 * policy.cost)
+                        self.invoices.add_charge(hrn_to_gacksid(allocatorHRN), hostitem.name, KIND_SLOT_CHARGE, tResv, 1 * policy.cost)
                    except GacksInvoiceDuplicate, e:
                         logger.info(str(e))
 
old mode 100755 (executable)
new mode 100644 (file)
index 0c3ae60..7dd5795
@@ -134,7 +134,7 @@ class ChargeProcessor(threading.Thread):
     def _process_charge(self, account, host, kind, date, amount):
         kind_id = 0
         if not self.invoice_manager.is_existing_charge(account, host, kind, date, amount):
-            self.invoice_manager.add_charge(account=account,
+            self.invoice_manager.add_charge(account= account,
                                             object = host,
                                             kind_id = kind,
                                             date = date,
index ee8a0d1..9ea2d07 100644 (file)
@@ -6,6 +6,7 @@ import time
 BUCKET_QUANTUM = (60*60)
 
 from gacksexcep import *
+from gacksid import *
 from gackshandle import *
 from gacksresource import *
 
@@ -84,7 +85,7 @@ class GacksEnforcer:
         print "check_bucket"
 
         # load and/or initialize the bucket
-        bucketAccount = self.accounts.get_account(account.name, "bucket", create_if_not_exist=True)
+        bucketAccount = self.accounts.get_account(hrn_to_gacksid(account.name), "bucket", create_if_not_exist=True)
         bucketAccount.inRate = policy.bucketInRate
         bucketAccount.maxBalance = policy.bucketMax
         if (bucketAccount.created):
@@ -179,7 +180,7 @@ class GacksEnforcer:
 
         self.check_same_time(handles)
 
-        account = self.accounts.get_account(name, "user")
+        account = self.accounts.get_account(hrn_to_gacksid(name), "user")
         if (account == None):
             raise GacksEnforcerNoAccount(name)
 
index 6e212b0..b0589cd 100644 (file)
@@ -108,3 +108,6 @@ class GacksAccountBadName(GacksError):
 
 class GacksInvoiceDuplicate(GacksError):
     pass
+
+class GacksNameAmbiguous(GacksError):
+    pass
diff --git a/apps/gacks/gacksid.py b/apps/gacks/gacksid.py
new file mode 100644 (file)
index 0000000..d5cc181
--- /dev/null
@@ -0,0 +1,21 @@
+from gacksexcep import *
+from sfa.util.plxrn import hrn_to_pl_slicename
+
+def slicename_to_gacksid(slicename):
+    return slicename
+
+def hrn_to_gacksid(hrn):
+    return hrn_to_pl_slicename(hrn)
+
+def str_to_gacksid(s):
+    if s.startswith("gacksid:"):
+        return s[8:]
+    elif s.startswith("hrn:"):
+        return hrn_to_gacksid(s[4:])
+    elif s.startswith("slice:"):
+        return slicename_to_gacksid(s[6:])
+    else:
+        raise GacksNameAmbiguous(s + " does not start with hrn: or slice: or gacksid:")
+
+def gacksid_hrn(s):
+    return "hrn:" + s
index 8bf65af..8792ac1 100644 (file)
@@ -32,7 +32,7 @@ if ( ! $account_name ) {
   return;\r
  }
 
-$account = $gacksapi->get_account($account_name, $account_kind);
+$account = $gacksapi->get_account(gacksid($account_name), $account_kind);
 
 $name = $account['name'];
 $kind = $account['kind'];
index f08e482..4747306 100644 (file)
@@ -46,7 +46,7 @@ switch ($action) {
         $account_kind = $_POST['account_kind'];
 
         $fields = array();
-        $fields['name'] = $account_name;
+        $fields['name'] = gacksid($account_name);
         $fields['kind'] = $account_kind;
         $fields['level'] = $_POST['level'];
         $fields['multiplier'] = $_POST['multiplier'];
@@ -65,7 +65,7 @@ switch ($action) {
         $account_kind = $_POST['account_kind'];
 
         $fields = array();
-        $fields['name'] = $account_name;
+        $fields['name'] = gacksid_guess_prefix($account_name);
         $fields['kind'] = $account_kind;
         $fields['level'] = $_POST['level'];
         $fields['multiplier'] = $_POST['multiplier'];
@@ -82,7 +82,7 @@ switch ($action) {
         $account_name = $_POST['account_name'];
         $account_kind = $_POST['account_kind'];
 
-        $gacksapi->delete_account($account_name, $account_kind);
+        $gacksapi->delete_account(gacksid($account_name), $account_kind);
 
         drupal_set_message ('Account deleted');
 
@@ -98,7 +98,7 @@ switch ($action) {
         $amount = floatval($_POST['amount']);
         $date = time();
 
-        $gacksapi->add_charge($account_name, $desc, $kind, $date, $amount, 1);
+        $gacksapi->add_charge(gacksid($account_name), $desc, $kind, $date, $amount, 1);
 
         drupal_set_message ('Charge applied');
 
index 60360ea..e1b307f 100644 (file)
@@ -23,6 +23,19 @@ function l_gacks_reservation_set ()    { return "/db/gacks/reservation_set.php";
 function l_gacks_invoice($name, $state, $start_date, $end_date, $parent) { return "/db/gacks/invoices.php?account_name=$name&state=$state&start_date=$start_date&end_date=$end_date&parent_id=$parent"; }
 function l_gacks_all_invoices ()    { return l_gacks_invoice(NULL, NULL, NULL, NULL, NULL); }
 
+function gacksid($x) { return "gacksid:" . $x; }
+
+function gacksid_guess_prefix($x) {
+    if ((strpos($x, ".") !== false) and (strpos($x, "_") == false)) {
+        return "hrn:" . $x;
+    } else if ((strpos($x, ".") == false) and (strpos($x, "_") == true)) {
+        return "slice:" . $x;
+    } else {
+        // XXX this doesn't display an error to the user
+        throw new Exception("unable to guess whether name is a slice or hrn");
+    }
+}
+
 function level_selectors($value)
 {
     $level_selectors = array();
index 3639a55..b03a6c1 100644 (file)
@@ -33,7 +33,7 @@ $date = $_GET['date'];
 $filter = array();
 
 if ($account_name) {
-   $filter["account"] = $account_name;
+   $filter["account"] = gacksid($account_name);
 }
 
 \rif ($state || $state=="0") {
index cdea99e..d808a41 100644 (file)
@@ -159,13 +159,13 @@ class InvoiceTableWidget(QWidget):
                 date_filter['end_date'] = end_date 
             
             # get the amount of pending charges that have not been invoiced
-            pending = self.gacks.client().get_invoice(self.gacks.get_authtoken(), account, 0)
+            pending = self.gacks.client().get_invoice(self.gacks.get_authtoken(), "hrn:" + account, 0)
             pendingAmount = 0;
             for pendingCharge in pending["charges"]:
                 pendingAmount += pendingCharge["amount"]
 
             # invoices are charges with state_id == 3
-           result = self.gacks.client().get_invoice(self.gacks.get_authtoken(), account, 3)
+            result = self.gacks.client().get_invoice(self.gacks.get_authtoken(), "hrn:" + account, 3)
 
             # make up a fake "pending" invoice that's the sum of all pending charges
             if (pendingAmount > 0):
@@ -191,9 +191,9 @@ class InvoiceTableWidget(QWidget):
         cred = self.gacks.get_cred('slice', self.account)
         # charges have their state_id set to 2
         if (invoice['id']=="(pending)"):
-            result = self.gacks.client().get_invoice(self.gacks.get_authtoken(), self.account, 0)
+            result = self.gacks.client().get_invoice(self.gacks.get_authtoken(), "hrn:" + self.account, 0)
         else:
-            result = self.gacks.client().get_invoice(self.gacks.get_authtoken(), self.account, 2, None, invoice['id'])
+            result = self.gacks.client().get_invoice(self.gacks.get_authtoken(), "hrn:" + self.account, 2, None, invoice['id'])
         charges = result['charges']
         self.chargeTable.clearContents()
         self.chargeTable.setRowCount(len(charges)+1)
index e508250..b471536 100644 (file)
@@ -130,8 +130,9 @@ class GacksRefreshThread(QThread):
         self.asapRecList = self.gacks.client().query_asap(id=self.id, allocatorHRN=self.allocator)
         self.emit(SIGNAL("receivedAsap(PyQt_PyObject)"), self.asapRecList)
 
-        self.account = self.gacks.client().get_account(self.gacks.get_authtoken(), self.id, "user")
-        self.emit(SIGNAL("receivedAccount(PyQt_PyObject)"), self.account)
+        if self.gacks.identity_okay():
+            self.account = self.gacks.client().get_account(self.gacks.get_authtoken(), "hrn:" + self.gacks.identity_hrn, "user")
+            self.emit(SIGNAL("receivedAccount(PyQt_PyObject)"), self.account)
 
         # this doesn't change, so we only have to do it once
         if not self.gacks.resources_checked:
index e2af3dc..be233a1 100644 (file)
@@ -50,6 +50,7 @@ install gacksaccount.py %{buildroot}/usr/local/gackscentral/bin
 install gacksaccountmanager.py %{buildroot}/usr/local/gackscentral/bin
 install gacksaccount_test.py %{buildroot}/usr/local/gackscentral/bin
 install gacksauth.py %{buildroot}/usr/local/gackscentral/bin
+install gacksid.py %{buildroot}/usr/local/gackscentral/bin
 install gacksreset.py %{buildroot}/usr/local/gackscentral/bin
 install gacksbackend.py %{buildroot}/usr/local/gackscentral/bin
 install gackscalendar.py %{buildroot}/usr/local/gackscentral/bin
@@ -141,6 +142,7 @@ fi
 /usr/local/gackscentral/bin/gacksaccountmanager.py
 /usr/local/gackscentral/bin/gacksaccount_test.py
 /usr/local/gackscentral/bin/gacksauth.py
+/usr/local/gackscentral/bin/gacksid.py
 /usr/local/gackscentral/bin/gacksreset.py
 /usr/local/gackscentral/bin/gacksbackend.py
 /usr/local/gackscentral/bin/gackscalendar.py