Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | import string |
| 4 | import sys |
| 5 | import xmlrpclib |
| 6 | |
| 7 | if __name__ == '__main__': |
| 8 | sys.path.append("/opt/xos") |
| 9 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
| 10 | |
| 11 | from xos.config import Config |
| 12 | from core.models import Service |
| 13 | from services.hpc.models import HpcService |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 14 | from xos.logger import Logger, logging |
| 15 | |
| 16 | logger = Logger(level=logging.INFO) |
| 17 | |
| 18 | class APIHelper: |
| 19 | def __init__(self, proxy, auth, method=None): |
| 20 | self.proxy = proxy |
| 21 | self.auth = auth |
| 22 | self.method = method |
| 23 | |
| 24 | def __getattr__(self, name): |
| 25 | if name.startswith("_"): |
| 26 | return getattr(self, name) |
| 27 | else: |
| 28 | return APIHelper(self.proxy, self.auth, name) |
| 29 | |
| 30 | def __call__(self, *args): |
| 31 | method = getattr(self.proxy, self.method) |
| 32 | return method(self.auth, *args) |
| 33 | |
| 34 | class CmiClient: |
| 35 | def __init__(self, hostname, port=8003, username="apiuser", password="apiuser"): |
| 36 | self.connect_api(hostname, port, username, password) |
| 37 | |
| 38 | def connect_api(self, hostname, port=8003, username="apiuser", password="apiuser"): |
| 39 | #print "https://%s:%d/COAPI/" % (hostname, port) |
| 40 | cob = xmlrpclib.ServerProxy("https://%s:%d/COAPI/" % (hostname, port), allow_none=True) |
| 41 | cob_auth = {} |
| 42 | cob_auth["Username"] = username |
| 43 | cob_auth["AuthString"] = password |
| 44 | cob_auth["AuthMethod"] = "password" |
| 45 | |
| 46 | onev = xmlrpclib.ServerProxy("https://%s:%d/ONEV_API/" % (hostname, port), allow_none=True) |
| 47 | onev_auth = {} |
| 48 | onev_auth["Username"] = username |
| 49 | onev_auth["AuthString"] = password |
| 50 | onev_auth["AuthMethod"] = "password" |
| 51 | |
| 52 | self.cob = APIHelper(cob, cob_auth) |
| 53 | self.onev = APIHelper(onev, onev_auth) |
| 54 | |
| 55 | class HpcLibrary: |
| 56 | def __init__(self): |
| 57 | self._client = None |
| 58 | |
| 59 | def make_account_name(self, x): |
| 60 | x=x.lower() |
| 61 | y = "" |
| 62 | for c in x: |
| 63 | if (c in (string.lowercase + string.digits)): |
| 64 | y = y + c |
| 65 | return y[:20] |
| 66 | |
| 67 | def get_hpc_service(self): |
| 68 | hpc_service_name = getattr(Config(), "observer_hpc_service", None) |
| 69 | if hpc_service_name: |
| 70 | hpc_service = HpcService.objects.filter(name = hpc_service_name) |
| 71 | else: |
| 72 | hpc_service = HpcService.objects.all() |
| 73 | |
| 74 | if not hpc_service: |
| 75 | if hpc_service_name: |
| 76 | raise Exception("No HPC Service with name %s" % hpc_service_name) |
| 77 | else: |
| 78 | raise Exception("No HPC Services") |
| 79 | hpc_service = hpc_service[0] |
| 80 | |
| 81 | return hpc_service |
| 82 | |
| 83 | def get_cmi_hostname(self, hpc_service=None): |
| 84 | if getattr(Config(),"observer_cmi_hostname",None): |
| 85 | return getattr(Config(),"observer_cmi_hostname") |
| 86 | |
| 87 | if (hpc_service is None): |
| 88 | hpc_service = self.get_hpc_service() |
| 89 | |
| 90 | if hpc_service.cmi_hostname: |
| 91 | return hpc_service.cmi_hostname |
| 92 | |
| 93 | try: |
| 94 | slices = hpc_service.slices.all() |
| 95 | except: |
| 96 | # deal with buggy data model |
| 97 | slices = hpc_service.service.all() |
| 98 | |
| 99 | for slice in slices: |
| 100 | if slice.name.endswith("cmi"): |
| 101 | for instance in slice.instances.all(): |
| 102 | if instance.node: |
| 103 | return instance.node.name |
| 104 | |
| 105 | raise Exception("Failed to find a CMI instance") |
| 106 | |
| 107 | @property |
| 108 | def client(self): |
| 109 | if self._client is None: |
| 110 | self._client = CmiClient(self.get_cmi_hostname()) |
| 111 | return self._client |
| 112 | |
| 113 | if __name__ == '__main__': |
| 114 | import django |
| 115 | django.setup() |
| 116 | |
| 117 | lib = HpcLibrary() |
| 118 | |
| 119 | print "testing API connection to", lib.get_cmi_hostname() |
| 120 | lib.client.cob.GetNewObjects() |
| 121 | lib.client.onev.ListAll("CDN") |
| 122 | |
| 123 | |
| 124 | |
| 125 | |