handle the case where there is no data in db yet.
[monitor.git] / monitor / wrapper / plc.py
index 63f17a4..255e3c1 100644 (file)
@@ -15,15 +15,21 @@ import traceback
 from monitor import database
 
 try:
-       import config
+       from monitor import config
        debug = config.debug
 except:
        debug = False
 logger = logging.getLogger("monitor")
        
 class Auth:
-       def __init__(self):
-               self.auth = {'AuthMethod': "anonymous"}
+       def __init__(self, username=None, password=None):
+               if username==None and password==None:
+                       self.auth = {'AuthMethod': "anonymous"}
+               else:
+                       self.auth = {'Username' : username,
+                               'AuthMethod' : 'password',
+                                               'AuthString' : password}
+                       
 
 # NOTE: this host is used by default when there are no auth files.
 XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
@@ -122,7 +128,7 @@ def getTechEmails(loginbase):
        # get site details.
        s = api.GetSites(loginbase)[0]
        # get people at site
-       p = api.GetPersons(s['person_ids'])[0]
+       p = api.GetPersons(s['person_ids'])
        # pull out those with the right role.
        emails = [ person['email'] for person in filter(lambda x: 'tech' in x['roles'], p) ]
        return emails
@@ -135,7 +141,7 @@ def getPIEmails(loginbase):
        # get site details.
        s = api.GetSites(loginbase)[0]
        # get people at site
-       p = api.GetPersons(s['person_ids'])[0]
+       p = api.GetPersons(s['person_ids'])
        # pull out those with the right role.
        emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], p) ]
        return emails
@@ -174,19 +180,20 @@ def nodesDbg():
 Returns loginbase for given nodename
 '''
 def siteId(nodename):
-       api = xmlrpclib.Server(auth.server, verbose=False)
-       anon = {'AuthMethod': "anonymous"}
-       site_id = api.GetNodes (anon, {"hostname": nodename}, ['site_id'])
+       api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
+       site_id = api.GetNodes (auth.auth, {"hostname": nodename}, ['site_id'])
        if len(site_id) == 1:
-               loginbase = api.GetSites (anon, site_id[0], ["login_base"])
+               loginbase = api.GetSites (auth.auth, site_id[0], ["login_base"])
                return loginbase[0]['login_base']
+       else:
+               print "Not nodes returned!!!!"
 
 '''
 Returns list of slices for a site.
 '''
 def slices(loginbase):
        siteslices = []
-       api = xmlrpclib.Server(auth.server, verbose=False)
+       api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
        sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
        for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
                siteslices.append(slice['name'])
@@ -196,7 +203,7 @@ def slices(loginbase):
 Returns dict of PCU info of a given node.
 '''
 def getpcu(nodename):
-       api = xmlrpclib.Server(auth.server, verbose=False)
+       api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
        anon = {'AuthMethod': "anonymous"}
        nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
        if nodeinfo['pcu_ids']: