blob: 51864a50ac72c8ad398a36f42fb1d30cdcb4d772 [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 Baker166f4b82015-02-09 11:18:46 -08008 sys.path.append("/opt/xos")
Scott Baker76a840e2015-02-11 21:38:09 -08009 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
Scott Bakerb30aa082014-01-03 08:36:00 -080010
Scott Baker76a840e2015-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 Baker3d114402015-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):
Scott Bakere8144d72015-05-22 21:04:13 -070085 if getattr(Config(),"observer_cmi_hostname",None):
86 return getattr(Config(),"observer_cmi_hostname")
87
Scott Baker95703032014-01-06 14:00:17 -080088 if (hpc_service is None):
Scott Baker3d114402015-03-31 15:51:58 -070089 hpc_service = self.get_hpc_service()
Scott Baker95703032014-01-06 14:00:17 -080090
Scott Baker96c03e92015-03-31 14:42:49 -070091 if hpc_service.cmi_hostname:
92 return hpc_service.cmi_hostname
Scott Baker95703032014-01-06 14:00:17 -080093
Scott Baker96c03e92015-03-31 14:42:49 -070094 try:
95 slices = hpc_service.slices.all()
96 except:
97 # deal with buggy data model
98 slices = hpc_service.service.all()
Scott Baker5bee8932014-01-03 12:00:59 -080099
Scott Baker96c03e92015-03-31 14:42:49 -0700100 for slice in slices:
101 if slice.name.endswith("cmi"):
Tony Mack3de59e32015-08-19 11:58:18 -0400102 for instance in slice.instances.all():
103 if instance.node:
104 return instance.node.name
Scott Baker5bee8932014-01-03 12:00:59 -0800105
Tony Mack3de59e32015-08-19 11:58:18 -0400106 raise Exception("Failed to find a CMI instance")
Scott Bakerb30aa082014-01-03 08:36:00 -0800107
Scott Bakere99e3df2014-01-06 16:05:46 -0800108 @property
109 def client(self):
110 if self._client is None:
111 self._client = CmiClient(self.get_cmi_hostname())
112 return self._client
113
Scott Bakerb30aa082014-01-03 08:36:00 -0800114if __name__ == '__main__':
Scott Baker96c03e92015-03-31 14:42:49 -0700115 import django
116 django.setup()
Scott Bakerb30aa082014-01-03 08:36:00 -0800117
Scott Baker96c03e92015-03-31 14:42:49 -0700118 lib = HpcLibrary()
119
120 print "testing API connection to", lib.get_cmi_hostname()
Scott Bakere99e3df2014-01-06 16:05:46 -0800121 lib.client.cob.GetNewObjects()
122 lib.client.onev.ListAll("CDN")
Scott Baker95703032014-01-06 14:00:17 -0800123
124
125
Scott Bakerb30aa082014-01-03 08:36:00 -0800126