remove Dashboard Views from user admin
[plstackapi.git] / planetstack / hpc_observer / hpclib.py
index e91a468..a4fa3b9 100644 (file)
@@ -1,6 +1,8 @@
 import os
 import base64
+import string
 import sys
+import xmlrpclib
 
 if __name__ == '__main__':
     sys.path.append("/opt/planetstack")
@@ -14,7 +16,55 @@ from util.logger import Logger, logging
 
 logger = Logger(level=logging.INFO)
 
+class APIHelper:
+    def __init__(self, proxy, auth, method=None):
+        self.proxy = proxy
+        self.auth = auth
+        self.method = method
+
+    def __getattr__(self, name):
+        if name.startswith("_"):
+            return getattr(self, name)
+        else:
+            return APIHelper(self.proxy, self.auth, name)
+
+    def __call__(self, *args):
+        method = getattr(self.proxy, self.method)
+        return method(self.auth, *args)
+
+class CmiClient:
+    def __init__(self, hostname, port=8003, username="apiuser", password="apiuser"):
+        self.connect_api(hostname, port, username, password)
+
+    def connect_api(self, hostname, port=8003, username="apiuser", password="apiuser"):
+        #print "https://%s:%d/COAPI/" % (hostname, port)
+        cob = xmlrpclib.ServerProxy("https://%s:%d/COAPI/" % (hostname, port), allow_none=True)
+        cob_auth = {}
+        cob_auth["Username"] = username
+        cob_auth["AuthString"] = password
+        cob_auth["AuthMethod"] = "password"
+
+        onev = xmlrpclib.ServerProxy("https://%s:%d/ONEV_API/" % (hostname, port), allow_none=True)
+        onev_auth = {}
+        onev_auth["Username"] = username
+        onev_auth["AuthString"] = password
+        onev_auth["AuthMethod"] = "password"
+
+        self.cob = APIHelper(cob, cob_auth)
+        self.onev = APIHelper(onev, onev_auth)
+
 class HpcLibrary:
+    def __init__(self):
+        self._client = None
+
+    def make_account_name(self, x):
+        x=x.lower()
+        y = ""
+        for c in x:
+            if (c in (string.lowercase + string.digits)):
+                y = y + c
+        return y[:20]
+
     def extract_slice_info(self, service):
         """ Produce a dict that describes the slices for the CMI
 
@@ -34,7 +84,7 @@ class HpcLibrary:
             if not ("_" in name):
                 continue
 
-            if "coblitz" in name:
+            if ("coblitz" in name) or ("hpc" in name):
                 slicenames["coblitz"] = name
                 slicehosts["coblitz"] = [sliver.node.name for sliver in slice.slivers.all()]
             elif "cmi" in name:
@@ -62,14 +112,25 @@ class HpcLibrary:
 
         return mapping
 
-    def write_slices_file(self, hpc_service, rr_service):
+    def get_cmi_hostname(self, hpc_service=None):
+        if (hpc_service is None):
+            hpc_service = HpcService.objects.get()
+
+        slice_info = self.extract_slice_info(hpc_service)
+        return slice_info["hostname_cmi"]
+
+    def write_slices_file(self, hpc_service=None, rr_service=None):
+        if (hpc_service is None):
+            hpc_service = HpcService.objects.get()
+
+        if (rr_service is None):
+            rr_service = RequestRouterService.objects.get()
+
         mapping = self.extract_slice_info(hpc_service)
         rr_mapping = self.extract_slice_info(rr_service)
 
         mapping.update(rr_mapping)
 
-        print mapping
-
         fn = "/tmp/slices"
 
         f = open(fn, "w")
@@ -94,10 +155,21 @@ PUPPET_MASTER_HOSTNAME="%(hostname_cmi)s"
 PUPPET_MASTER_PORT="8140"
 """ % mapping)
 
+    @property
+    def client(self):
+        if self._client is None:
+            self._client = CmiClient(self.get_cmi_hostname())
+        return self._client
+
 if __name__ == '__main__':
-    hpc_service = HpcService.objects.get()
-    rr_service = RequestRouterService.objects.get()
+    print "testing write_slices_file"
     lib = HpcLibrary()
-    lib.write_slices_file(hpc_service, rr_service)
+    lib.write_slices_file()
+
+    print "testing API connection"
+    lib.client.cob.GetNewObjects()
+    lib.client.onev.ListAll("CDN")
+
+