From 52a0cc3059b3afdaa7adda3bffb4f796f81582e7 Mon Sep 17 00:00:00 2001 From: Bruno Soares da Silva Date: Fri, 29 Aug 2014 17:19:49 -0300 Subject: [PATCH] -> Removed registration based on LDAP. -> Added registration based on Labora Scheduler API. --- portal/database.py | 57 +++++++++++++++++++++++ portal/lsapiclient.py | 106 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 portal/database.py create mode 100644 portal/lsapiclient.py diff --git a/portal/database.py b/portal/database.py new file mode 100644 index 00000000..376d18b0 --- /dev/null +++ b/portal/database.py @@ -0,0 +1,57 @@ +import sys +import psycopg2 +import psycopg2.extras + +class Database(): + + def __init__( self, DbConfigurations ): + self.connection = None + self.connect( DbConfigurations['dbHost'], DbConfigurations['dbUser'], + DbConfigurations['dbPassword'], DbConfigurations['dbName'] ) + + + def connect( self, host, user, password, database ): + result = True + try: + self.connection = psycopg2.connect( "host=" + host + " dbname=" + database + " user=" + + user + " password=" + password ) + except psycopg2.DatabaseError, e: + result = False + + return result + + + def close( self ): + if self.connection: + self.connection.close() + self.connection = None + + + def query( self, queryString ): + result = True + try: + cursor = self.connection.cursor() + cursor.execute( queryString ) + self.connection.commit() + except Exception, e: + result = False + self.rollback() + + return result + + + def rollback( self ): + if self.connection: + self.connection.rollback() + + + def fetchRows( self, queryString ): + rows = None + try: + cursor = self.connection.cursor( cursor_factory = psycopg2.extras.RealDictCursor ) + cursor.execute( queryString ) + rows = cursor.fetchall() + except Exception, e: + pass + + return rows diff --git a/portal/lsapiclient.py b/portal/lsapiclient.py new file mode 100644 index 00000000..494adb0d --- /dev/null +++ b/portal/lsapiclient.py @@ -0,0 +1,106 @@ +import sys +import urllib +import urllib2 +import json +import socket +from urlparse import urlparse +from database import Database + +class LaboraSchedulerClient: + """ + A simple rest shell to a Labora Scheduler instance + This class can receive Labora API calls to the underlying testbed + """ + + direct_calls = [ 'get_testbed_info', 'get_users', 'add_user', 'delete_user', 'update_user', + 'get_user_id_by_username' ] + + def __init__ ( self, organization ): + self.url, self.key = self.getOrganizationConfigs( organization ) + + + def __getattr__(self, name): + + def func(*args, **kwds): + actual_name = None + + if name in LaboraSchedulerClient.direct_calls: + actual_name = name + + if not actual_name: + raise Exception, "Method %s not found in Labora Scheduler"%(name) + return + + if not self.url or not self.key: + raise Exception, "Missing Labora Scheduler island url and/or key." + return + + address = self.url + "?method=" + actual_name + "&key=" + self.key + + # get the direct_call parameters + method_parameters = [] + + if actual_name == "get_users": + method_parameters.extend(['filter']) + elif actual_name == "update_user": + method_parameters.extend(['user_id', 'new_user_data']) + elif actual_name == "delete_user": + method_parameters.extend(['user_id']) + elif actual_name == "get_user_id_by_username": + method_parameters.extend(['username']) + elif actual_name == "add_user": + method_parameters.extend(['username', 'email', 'password', 'name', 'gidnumber', + 'homedirectory']) + + for parameter in args: + if isinstance(parameter, (frozenset, list, set, tuple, dict)): + for key_name in parameter.keys(): + + if key_name in method_parameters: + param_value = parameter[key_name] + + if param_value == None: + continue + + if isinstance(param_value, (frozenset, list, set, tuple, dict)): + param_value = json.dumps(param_value) + + param_value = urllib.quote(param_value.encode('utf-8')) + + address += "&" + key_name + "=" + param_value + + api_call = urllib2.urlopen(address) + api_call = json.load(api_call) + + if not api_call['call_status']: + result = api_call['method_result'] + else: + result = False + + + return result + + return func + + + def getOrganizationConfigs( self, organization ): + ls_url = None + ls_key = None + + databaseConfig = { + 'dbHost' : '10.128.11.200', + 'dbUser' : '', + 'dbPassword' : '', + 'dbName' : 'LaboraSchedulerNOC' + } + + databaseConnection = Database( databaseConfig ) + + query = "SELECT * FROM islands WHERE domain ='" + organization + "'" + orgConfig = databaseConnection.fetchRows( query ) + + if orgConfig: + ls_url = orgConfig[0]["ls_url"] + ls_key = orgConfig[0]["ls_key"] + + return ls_url, ls_key -- 2.43.0