blob: a53c8e9dc5cce49b7260875e53151528cf6ab8fc [file] [log] [blame]
Scott Bakerb30aa082014-01-03 08:36:00 -08001import os
2import base64
Scott Bakere99e3df2014-01-06 16:05:46 -08003import string
Scott Bakerb30aa082014-01-03 08:36:00 -08004import sys
Scott Baker95703032014-01-06 14:00:17 -08005import xmlrpclib
Scott Bakerb30aa082014-01-03 08:36:00 -08006
7if __name__ == '__main__':
Scott Bakereaeabad2015-02-09 11:18:46 -08008 sys.path.append("/opt/xos")
Scott Baker86e132c2015-02-11 21:38:09 -08009 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
Scott Bakerb30aa082014-01-03 08:36:00 -080010
Scott Baker86e132c2015-02-11 21:38:09 -080011from xos.config import Config
Scott Bakerb30aa082014-01-03 08:36:00 -080012from core.models import Service
13from hpc.models import HpcService
14from requestrouter.models import RequestRouterService
15from util.logger import Logger, logging
16
17logger = Logger(level=logging.INFO)
18
Scott Baker95703032014-01-06 14:00:17 -080019class APIHelper:
20 def __init__(self, proxy, auth, method=None):
21 self.proxy = proxy
22 self.auth = auth
23 self.method = method
24
25 def __getattr__(self, name):
26 if name.startswith("_"):
27 return getattr(self, name)
28 else:
29 return APIHelper(self.proxy, self.auth, name)
30
31 def __call__(self, *args):
32 method = getattr(self.proxy, self.method)
33 return method(self.auth, *args)
34
35class CmiClient:
36 def __init__(self, hostname, port=8003, username="apiuser", password="apiuser"):
37 self.connect_api(hostname, port, username, password)
38
39 def connect_api(self, hostname, port=8003, username="apiuser", password="apiuser"):
40 #print "https://%s:%d/COAPI/" % (hostname, port)
41 cob = xmlrpclib.ServerProxy("https://%s:%d/COAPI/" % (hostname, port), allow_none=True)
42 cob_auth = {}
43 cob_auth["Username"] = username
44 cob_auth["AuthString"] = password
45 cob_auth["AuthMethod"] = "password"
46
47 onev = xmlrpclib.ServerProxy("https://%s:%d/ONEV_API/" % (hostname, port), allow_none=True)
48 onev_auth = {}
49 onev_auth["Username"] = username
50 onev_auth["AuthString"] = password
51 onev_auth["AuthMethod"] = "password"
52
53 self.cob = APIHelper(cob, cob_auth)
54 self.onev = APIHelper(onev, onev_auth)
55
Scott Bakerb30aa082014-01-03 08:36:00 -080056class HpcLibrary:
Scott Bakere99e3df2014-01-06 16:05:46 -080057 def __init__(self):
58 self._client = None
59
60 def make_account_name(self, x):
61 x=x.lower()
62 y = ""
63 for c in x:
64 if (c in (string.lowercase + string.digits)):
65 y = y + c
Scott Baker18ce7152014-01-06 16:59:36 -080066 return y[:20]
Scott Bakere99e3df2014-01-06 16:05:46 -080067
Scott Baker443c3df2015-03-31 15:51:58 -070068 def get_hpc_service(self):
69 hpc_service_name = getattr(Config(), "observer_hpc_service", None)
70 if hpc_service_name:
71 hpc_service = HpcService.objects.filter(name = hpc_service_name)
72 else:
73 hpc_service = HpcService.objects.all()
74
75 if not hpc_service:
76 if hpc_service_name:
77 raise Exception("No HPC Service with name %s" % hpc_service_name)
78 else:
79 raise Exception("No HPC Services")
80 hpc_service = hpc_service[0]
81
82 return hpc_service
83
Scott Baker95703032014-01-06 14:00:17 -080084 def get_cmi_hostname(self, hpc_service=None):
85 if (hpc_service is None):
Scott Baker443c3df2015-03-31 15:51:58 -070086 hpc_service = self.get_hpc_service()
Scott Baker95703032014-01-06 14:00:17 -080087
Scott Baker00795bb2015-03-31 14:42:49 -070088 if hpc_service.cmi_hostname:
89 return hpc_service.cmi_hostname
Scott Baker95703032014-01-06 14:00:17 -080090
Scott Baker00795bb2015-03-31 14:42:49 -070091 try:
92 slices = hpc_service.slices.all()
93 except:
94 # deal with buggy data model
95 slices = hpc_service.service.all()
Scott Baker5bee8932014-01-03 12:00:59 -080096
Scott Baker00795bb2015-03-31 14:42:49 -070097 for slice in slices:
98 if slice.name.endswith("cmi"):
99 for sliver in slice.slivers.all():
100 if sliver.node:
101 return sliver.node.name
Scott Baker5bee8932014-01-03 12:00:59 -0800102
Scott Baker00795bb2015-03-31 14:42:49 -0700103 raise Exception("Failed to find a CMI sliver")
Scott Bakerb30aa082014-01-03 08:36:00 -0800104
Scott Bakere99e3df2014-01-06 16:05:46 -0800105 @property
106 def client(self):
107 if self._client is None:
108 self._client = CmiClient(self.get_cmi_hostname())
109 return self._client
110
Scott Bakerb30aa082014-01-03 08:36:00 -0800111if __name__ == '__main__':
Scott Baker00795bb2015-03-31 14:42:49 -0700112 import django
113 django.setup()
Scott Bakerb30aa082014-01-03 08:36:00 -0800114
Scott Baker00795bb2015-03-31 14:42:49 -0700115 lib = HpcLibrary()
116
117 print "testing API connection to", lib.get_cmi_hostname()
Scott Bakere99e3df2014-01-06 16:05:46 -0800118 lib.client.cob.GetNewObjects()
119 lib.client.onev.ListAll("CDN")
Scott Baker95703032014-01-06 14:00:17 -0800120
121
122
Scott Bakerb30aa082014-01-03 08:36:00 -0800123