From 7c3728554637db272fbb201ed8947808a975ad1e Mon Sep 17 00:00:00 2001 From: Loic Baron Date: Wed, 3 Dec 2014 19:03:47 +0100 Subject: [PATCH] REST: sfa API using rest only GetVersion for now --- myslice/urls.py | 1 + rest/sfa_api.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 rest/sfa_api.py diff --git a/myslice/urls.py b/myslice/urls.py index 91511b7d..af9a8007 100644 --- a/myslice/urls.py +++ b/myslice/urls.py @@ -82,6 +82,7 @@ urls = [ # # RESTful interface (r'^rest/(?P[^/]+)/(?P[^/]+)?/?$', 'rest.get.dispatch'), + (r'^sfa/(?P[^/]+)/?$', 'rest.sfa_api.dispatch'), (r'^table/(?P[^/]+)/(?P[^/]+)?/?$', 'rest.get.dispatch'), (r'^datatable/(?P[^/]+)/(?P[^/]+)?/?$', 'rest.get.dispatch'), (r'^update/(?P[^/]+)/(?P[^/]+)?/?$', 'rest.update.dispatch'), diff --git a/rest/sfa_api.py b/rest/sfa_api.py new file mode 100644 index 00000000..a4082740 --- /dev/null +++ b/rest/sfa_api.py @@ -0,0 +1,82 @@ +from sfa.trust.certificate import Keypair, Certificate +from sfa.client.sfaserverproxy import SfaServerProxy +from sfa.client.return_value import ReturnValue + +from manifold.core.query import Query + +from django.shortcuts import render_to_response + +from unfold.loginrequired import LoginRequiredView + +from rest import ObjectRequest, error + +from string import join + +from django.http import HttpResponse +from rest import error +import os,json + +def dispatch(request, method): + platforms = list() + options = list() + rspec = '' + results = dict() + + if request.method == 'POST': + req_items = request.POST + elif request.method == 'GET': + req_items = request.GET + + for el in req_items.items(): + if el[0].startswith('rspec'): + rspec += el[1] + if el[0].startswith('platform'): + platforms += req_items.getlist('platform[]') + elif el[0].startswith('options'): + options += req_items.getlist('options[]') + + if len(platforms)==0: + platforms.append('myslice') + #results = {'method':method,'platforms':platforms,'rspec':rspec,'options':options} + + from manifoldapi.manifoldapi import execute_admin_query + for pf in platforms: + platform_query = Query().get('local:platform').filter_by('platform', '==', pf).select('config') + platform_result = execute_admin_query(request, platform_query) + platform = json.loads(platform_result[0]['config']) + print platform + if 'sm' in platform and len(platform['sm']) > 0: + print 'sm' + server_url = platform['sm'] + if 'rm' in platform and len(platform['rm']) > 0: + print 'rm' + server_url = platform['rm'] + if 'registry' in platform and len(platform['registry']) > 0: + print 'registry' + server_url = platform['registry'] + + pkey_path = os.path.abspath(platform['user_private_key']) + if not os.path.isfile(pkey_path) : + return HttpResponse(json.dumps({'error' : '-2'}), content_type="application/json") + pkey_file = open(pkey_path,'r') + pkey = pkey_file.read() + x = pkey.encode('latin1') + keypair = Keypair(string=x) + self_signed = Certificate(subject = platform['user']) + self_signed.set_pubkey(keypair) + self_signed.set_issuer(keypair, subject=platform['user'].encode('latin1')) + self_signed.sign() + sscert_path = self_signed.save_to_random_tmp_file() + print "path of tmp sscert: %s" % sscert_path + print server_url + server = SfaServerProxy(server_url, pkey_path, sscert_path) + os.remove(sscert_path) + + if method == "GetVersion": + print "this is the result of GetVersion:" + result = server.GetVersion() + else: + return HttpResponse(json.dumps({'error' : '-1','msg':'method not supported yet'}), content_type="application/json") + + results[pf] = result + return HttpResponse(json.dumps(results), content_type="application/json") -- 2.43.0