Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Scott Baker | e99e3df | 2014-01-06 16:05:46 -0800 | [diff] [blame] | 3 | import string |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 4 | import sys |
Scott Baker | 9570303 | 2014-01-06 14:00:17 -0800 | [diff] [blame] | 5 | import xmlrpclib |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 6 | |
| 7 | if __name__ == '__main__': |
Scott Baker | eaeabad | 2015-02-09 11:18:46 -0800 | [diff] [blame] | 8 | sys.path.append("/opt/xos") |
Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 9 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 10 | |
Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 11 | from xos.config import Config |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 12 | from core.models import Service |
| 13 | from hpc.models import HpcService |
| 14 | from requestrouter.models import RequestRouterService |
| 15 | from util.logger import Logger, logging |
| 16 | |
| 17 | logger = Logger(level=logging.INFO) |
| 18 | |
Scott Baker | 9570303 | 2014-01-06 14:00:17 -0800 | [diff] [blame] | 19 | class 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 | |
| 35 | class 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 Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 56 | class HpcLibrary: |
Scott Baker | e99e3df | 2014-01-06 16:05:46 -0800 | [diff] [blame] | 57 | 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 Baker | 18ce715 | 2014-01-06 16:59:36 -0800 | [diff] [blame] | 66 | return y[:20] |
Scott Baker | e99e3df | 2014-01-06 16:05:46 -0800 | [diff] [blame] | 67 | |
Scott Baker | 443c3df | 2015-03-31 15:51:58 -0700 | [diff] [blame] | 68 | 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 Baker | 9570303 | 2014-01-06 14:00:17 -0800 | [diff] [blame] | 84 | def get_cmi_hostname(self, hpc_service=None): |
| 85 | if (hpc_service is None): |
Scott Baker | 443c3df | 2015-03-31 15:51:58 -0700 | [diff] [blame] | 86 | hpc_service = self.get_hpc_service() |
Scott Baker | 9570303 | 2014-01-06 14:00:17 -0800 | [diff] [blame] | 87 | |
Scott Baker | 00795bb | 2015-03-31 14:42:49 -0700 | [diff] [blame] | 88 | if hpc_service.cmi_hostname: |
| 89 | return hpc_service.cmi_hostname |
Scott Baker | 9570303 | 2014-01-06 14:00:17 -0800 | [diff] [blame] | 90 | |
Scott Baker | 00795bb | 2015-03-31 14:42:49 -0700 | [diff] [blame] | 91 | try: |
| 92 | slices = hpc_service.slices.all() |
| 93 | except: |
| 94 | # deal with buggy data model |
| 95 | slices = hpc_service.service.all() |
Scott Baker | 5bee893 | 2014-01-03 12:00:59 -0800 | [diff] [blame] | 96 | |
Scott Baker | 00795bb | 2015-03-31 14:42:49 -0700 | [diff] [blame] | 97 | 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 Baker | 5bee893 | 2014-01-03 12:00:59 -0800 | [diff] [blame] | 102 | |
Scott Baker | 00795bb | 2015-03-31 14:42:49 -0700 | [diff] [blame] | 103 | raise Exception("Failed to find a CMI sliver") |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 104 | |
Scott Baker | e99e3df | 2014-01-06 16:05:46 -0800 | [diff] [blame] | 105 | @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 Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 111 | if __name__ == '__main__': |
Scott Baker | 00795bb | 2015-03-31 14:42:49 -0700 | [diff] [blame] | 112 | import django |
| 113 | django.setup() |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 114 | |
Scott Baker | 00795bb | 2015-03-31 14:42:49 -0700 | [diff] [blame] | 115 | lib = HpcLibrary() |
| 116 | |
| 117 | print "testing API connection to", lib.get_cmi_hostname() |
Scott Baker | e99e3df | 2014-01-06 16:05:46 -0800 | [diff] [blame] | 118 | lib.client.cob.GetNewObjects() |
| 119 | lib.client.onev.ListAll("CDN") |
Scott Baker | 9570303 | 2014-01-06 14:00:17 -0800 | [diff] [blame] | 120 | |
| 121 | |
| 122 | |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 123 | |