from sfa.util.xrn import Xrn, get_leaf, get_authority, hrn_to_urn, urn_to_hrn
from manifold.core.query import Query
-from manifold.models import db
-from manifold.models.platform import Platform
-from manifold.models.user import User
+
+from manifoldapi.manifoldapi import execute_admin_query
from unfold.loginrequired import LoginRequiredView
-from myslice.settings import logger
+from myslice.settings import logger, config
from rest.json_encoder import MyEncoder
def dispatch(request, method):
- Config = ConfigParser.ConfigParser()
- Config.read(os.getcwd() + "/myslice/monitor.ini")
-
- #logger.debug(request.session['user']['email'])
- user_email = request.session['user']['email']
- platforms = list()
- options = dict()
- rspec = ''
- results = dict()
- urn = ''
hrn = ''
+ urn = ''
object_type = ''
- display = None
+ rspec = ''
recursive = False
+ options = dict()
+ platforms = list()
+
+ results = dict()
+ display = None
if request.method == 'POST':
req_items = request.POST
if el[0].startswith('rspec'):
rspec += el[1]
elif el[0].startswith('platform'):
- platforms += req_items.getlist('platform[]')
+ platforms = req_items.getlist('platform[]')
#elif el[0].startswith('options'):
# options += req_items.getlist('options[]')
elif el[0].startswith('hrn'):
elif el[0].startswith('display'):
display = el[1]
+ results = sfa_client(request, method, hrn=hrn, urn=urn, object_type=object_type, recursive=recursive, options=options, platforms=platforms)
+ if display == 'table':
+ return render_to_response('table-default.html', {'data' : data, 'fields' : columns, 'id' : '@component_id', 'options' : None})
+ else:
+ return HttpResponse(json.dumps(results, cls=MyEncoder), content_type="application/json")
+
+def get_user_account(request, user_email, platform_name):
+ """
+ Returns the user configuration for a given platform.
+ This function does not resolve references.
+ """
+ user_query = Query().get('local:user').filter_by('email', '==', user_email).select('user_id')
+ user_details = execute_admin_query(request, user_query)
+ platform_query = Query().get('local:platform').filter_by('platform', '==', platform_name).select('platform_id')
+ platform_details = execute_admin_query(request, platform_query)
+
+ account_query = Query().get('local:account').filter_by('platform_id','==',platform_details[0]['platform_id']).filter_by('user_id', '==', user_details[0]['user_id']).select('user_id','platform_id','auth_type','config')
+ accounts = execute_admin_query(request, account_query)
+
+ if not accounts:
+ raise Exception, "this account does not exist"
+
+ if accounts[0]['auth_type'] == 'reference':
+ pf = json.loads(accounts[0]['config'])['reference_platform']
+ return get_user_account(request, user_email, pf)
+
+ return accounts[0]
+
+def sfa_client(request, method, hrn=None, urn=None, object_type=None, rspec=None, recursive=None, options=None, platforms=None, admin=False):
+
+ Config = ConfigParser.ConfigParser()
+ monitor_file = os.path.abspath(os.path.dirname(__file__) + '/../myslice/monitor.ini')
+ Config.read(monitor_file)
+
+ if admin:
+ user_email, admin_password = config.manifold_admin_user_password()
+ else:
+ #logger.debug(request.session['user']['email'])
+ user_email = request.session['user']['email']
+
+ results = dict()
+
+ if hrn is None:
+ hrn = ''
+ if urn is None:
+ urn = ''
+ if object_type is None:
+ object_type = ''
+ if rspec is None:
+ rspec = ''
+ if recursive is None:
+ recursive = False
+ if options is None:
+ options = dict()
+ if platforms is None:
+ platforms = list()
+
if method not in ['GetVersion','ListResources']:
try:
if not hrn:
raise Exception, "Provide urn OR hrn + type as parameters of method %s" % method
if len(platforms)==0:
- platforms = get_platforms()
+ platforms = get_platforms(request)
#platforms.append('myslice')
#results = {'method':method,'platforms':platforms,'rspec':rspec,'options':options}
api_options['geni_rspec_version'] = {'type': 'GENI', 'version': '3'}
api_options['list_leases'] = 'all'
server_am = False
- from manifoldapi.manifoldapi import execute_admin_query
for pf in platforms:
- platform = get_platform_config(pf)
+ platform = get_platform_config(request, pf)
logger.debug("platform={}".format(platform))
if 'sm' in platform and len(platform['sm']) > 0:
logger.debug('sm')
server_url = platform['registry']
if not Config.has_option('monitor', 'cert') :
- return HttpResponse(json.dumps({'error' : '-1'}), content_type="application/json")
+ #return HttpResponse(json.dumps({'error' : '-1'}), content_type="application/json")
+ return {'error' : '-1', 'msg': 'monitor.ini has no cert configured'}
cert = os.path.abspath(Config.get('monitor', 'cert'))
if not os.path.isfile(cert) :
- return HttpResponse(json.dumps({'error' : '-1'}), content_type="application/json")
+ #return HttpResponse(json.dumps({'error' : '-1'}), content_type="application/json")
+ return {'error' : '-1', 'msg': 'check cert file at %s'%cert}
if not Config.has_option('monitor', 'pkey') :
- return HttpResponse(json.dumps({'error' : '-2'}), content_type="application/json")
+ #return HttpResponse(json.dumps({'error' : '-2'}), content_type="application/json")
+ return {'error' : '-2'}
pkey = os.path.abspath(Config.get('monitor', 'pkey'))
if not os.path.isfile(pkey) :
- return HttpResponse(json.dumps({'error' : '-2'}), content_type="application/json")
+ #return HttpResponse(json.dumps({'error' : '-2'}), content_type="application/json")
+ return {'error' : '-2'}
server = SfaServerProxy(server_url, pkey, cert)
try:
# Get user config from Manifold
- user_config = get_user_config(user_email, pf)
+ user_config = get_user_config(request, user_email, pf)
if 'delegated_user_credential' in user_config:
user_cred = user_config['delegated_user_credential']
+ elif 'user_credential' in user_config:
+ user_cred = user_config['user_credential']
else:
+ logger.error("no user credentials for user = ", user_email)
user_cred = {}
if object_type:
for obj_name, cred in user_config['delegated_%s_credentials'%object_type].items():
if obj_name == hrn:
object_cred = cred
+ elif '%s_credentials'%object_type in user_config:
+ for obj_name, cred in user_config['%s_credentials'%object_type].items():
+ if obj_name == hrn:
+ object_cred = cred
+ else:
+ logger.error("no credentials for object")
+ logger.error(object_type)
+ logger.error(object_name)
+ object_cred = {}
# Both AM & Registry
if method == "GetVersion":
if server_am:
if method == "ListResources":
result = server.ListResources([user_cred], api_options)
+ logger.debug(result)
dict_result = xmltodict.parse(result['value'])
- result['json'] = json.dumps(dict_result)
+ result['parsed'] = dict_result
if isinstance(dict_result['rspec']['node'], list):
columns.extend(dict_result['rspec']['node'][0].keys())
else:
# else GetVersion = v3
else:
result = server.Describe([urn] ,[object_cred], api_options)
- dict_result = xmltodict.parse(result['value']['geni_rspec'])
+ if isinstance(result, dict):
+ if result['value'] != 0:
+ dict_result = xmltodict.parse(result['value']['geni_rspec'])
- result['json'] = json.dumps(dict_result)
- if isinstance(dict_result['rspec']['node'], list):
- columns.extend(dict_result['rspec']['node'][0].keys())
- else:
- columns.extend(dict_result['rspec']['node'].keys())
+ result['parsed'] = dict_result
+ if 'rspec' in dict_result and 'node' in dict_result['rspec']:
+ if isinstance(dict_result['rspec']['node'], list):
+ columns.extend(dict_result['rspec']['node'][0].keys())
+ else:
+ columns.extend(dict_result['rspec']['node'].keys())
elif method == 'Renew':
# Renew till 1 month from now
elif method == 'Delete':
result = server.Delete([urn] ,[object_cred], api_options)
elif method == 'Allocate':
- # if GetVersion = v2
- # CreateSliver(slice_hrn)
- # else GetVersion = v3
api_options['call_id'] = unique_call_id()
# List of users comes from the Registry
api_options['sfa_users'] = sfa_users
api_options['geni_users'] = geni_users
- result = server.Allocate([urn] ,[object_cred], rspec, api_options)
+ # if GetVersion = v2
+ version = server.GetVersion()
+ if version['geni_api'] == 2:
+ result = server.CreateSliver([urn] ,[object_cred], rspec, api_options)
+ # else GetVersion = v3
+ else:
+ result = server.Allocate([urn] ,[object_cred], rspec, api_options)
elif method == 'Provision':
# if GetVersion = v2
# Nothing it is not supported by v2 AMs
- api_options['call_id'] = unique_call_id()
- # List of users comes from the Registry
- api_options['sfa_users'] = sfa_users
- api_options['geni_users'] = geni_users
- result = server.Provision([urn] ,[object_cred], api_options)
+ version = server.GetVersion()
+ if version['geni_api'] == 3:
+ api_options['call_id'] = unique_call_id()
+ # List of users comes from the Registry
+ api_options['sfa_users'] = sfa_users
+ api_options['geni_users'] = geni_users
+ result = server.Provision([urn] ,[object_cred], api_options)
elif method == 'Status':
result = server.Status([urn] ,[object_cred], api_options)
elif method == 'PerformOperationalAction':
# if GetVersion = v2
# Nothing it is not supported by v2 AMs
- result = server.PerformOperationalAction([urn] ,[object_cred], action, api_options)
+ version = server.GetVersion()
+ if version['geni_api'] == 3:
+ result = server.PerformOperationalAction([urn] ,[object_cred], action, api_options)
elif method == 'Shutdown':
result = server.Shutdown(urn ,[object_cred], api_options)
else:
record_dict = {'urn': urn, 'hrn': hrn, 'type': object_type}
if method == "List":
# hrn is required
- options['recursive'] = recursive
- result = server.List(hrn, user_cred, options)
+ api_options['recursive'] = recursive
+ result = server.List(hrn, user_cred, api_options)
if object_type:
result = filter_records(object_type, result)
elif method == "Resolve":
# hrn is required
# details can be True or False
- options['details']=True
- result = server.Resolve(hrn, user_cred, options)
+ api_options['details']=True
+ result = server.Resolve(hrn, user_cred, api_options)
if object_type:
result = filter_records(object_type, result)
elif method == "Register":
results[pf] = result
if dict_result:
- if isinstance(dict_result['rspec']['node'], list):
- data = data + dict_result['rspec']['node']
- else:
- data.append(dict_result['rspec']['node'])
+ if 'rspec' in dict_result and 'node' in dict_result['rspec']:
+ if isinstance(dict_result['rspec']['node'], list):
+ data = data + dict_result['rspec']['node']
+ else:
+ data.append(dict_result['rspec']['node'])
except Exception,e:
import traceback
logger.error(traceback.format_exc())
logger.error(e)
results[pf] = {'error':'-3', 'error_msg': str(e)}
- if display == 'table':
- return render_to_response('table-default.html', {'data' : data, 'fields' : columns, 'id' : '@component_id', 'options' : None})
- else:
- results['columns'] = columns
- return HttpResponse(json.dumps(results, cls=MyEncoder), content_type="application/json")
-def get_user_account(user_email, platform_name):
- """
- Returns the user configuration for a given platform.
- This function does not resolve references.
- """
- user = db.query(User).filter(User.email == user_email).one()
- platform = db.query(Platform).filter(Platform.platform == platform_name).one()
- accounts = [a for a in user.accounts if a.platform == platform]
- if not accounts:
- raise Exception, "this account does not exist"
+ results['columns'] = columns
+ return results
- if accounts[0].auth_type == 'reference':
- pf = json.loads(accounts[0].config)['reference_platform']
- return get_user_account(user_email, pf)
+def get_user_config(request, user_email, platform_name):
+ account = get_user_account(request, user_email, platform_name)
+ return json.loads(account['config']) if account['config'] else {}
- return accounts[0]
-
-def get_user_config(user_email, platform_name):
- account = get_user_account(user_email, platform_name)
- return json.loads(account.config) if account.config else {}
-
-def get_platforms():
+def get_platforms(request):
ret = list()
- platforms = db.query(Platform).filter(Platform.gateway_type == 'sfa', Platform.disabled == 0).all()
+ platform_query = Query().get('local:platform').filter_by('gateway_type', '==', 'sfa').filter_by('disabled','==',0).select('platform')
+ platforms = execute_admin_query(request, platform_query)
+
for p in platforms:
- ret.append(p.platform)
+ ret.append(p['platform'])
return ret
+def get_platform_config(request, platform_name):
+ platform_query = Query().get('local:platform').filter_by('platform', '==', platform_name).select('platform', 'config')
+ platforms = execute_admin_query(request, platform_query)
-def get_platform_config(platform_name):
- platform = db.query(Platform).filter(Platform.platform == platform_name).one()
- return json.loads(platform.config) if platform.config else {}
+ return json.loads(platforms[0]['config']) if platforms[0]['config'] else {}
def filter_records(type, records):
filtered_records = []