5 from django.shortcuts import render_to_response
6 from django.http import HttpResponse
8 from sfa.trust.certificate import Keypair, Certificate
9 from sfa.client.sfaserverproxy import SfaServerProxy
10 from sfa.client.return_value import ReturnValue
11 from sfa.util.xrn import Xrn, get_leaf, get_authority, hrn_to_urn, urn_to_hrn
13 from manifold.core.query import Query
14 from manifold.models import db
15 from manifold.models.platform import Platform
16 from manifold.models.user import User
18 from unfold.loginrequired import LoginRequiredView
20 from myslice.settings import logger
22 def dispatch(request, method):
23 Config = ConfigParser.ConfigParser()
24 Config.read(os.getcwd() + "/myslice/monitor.ini")
26 # hardcoded user to be replaced by auth
27 user_email = "loic.baron@lip6.fr"
29 # Get this as parameters
30 slice_hrn = "fed4fire.upmc.berlin"
31 urn = hrn_to_urn(slice_hrn, "slice")
32 #urn = hrn_to_urn("fed4fire.upmc.loic_baron", "user")
39 if request.method == 'POST':
40 req_items = request.POST
41 elif request.method == 'GET':
42 req_items = request.GET
44 for el in req_items.items():
45 if el[0].startswith('rspec'):
47 if el[0].startswith('platform'):
48 platforms += req_items.getlist('platform[]')
49 elif el[0].startswith('options'):
50 options += req_items.getlist('options[]')
53 platforms.append('myslice')
54 #results = {'method':method,'platforms':platforms,'rspec':rspec,'options':options}
56 from manifoldapi.manifoldapi import execute_admin_query
58 platform = get_platform_config(pf)
59 logger.debug("platform={}".format(platform))
60 if 'sm' in platform and len(platform['sm']) > 0:
62 server_url = platform['sm']
63 if 'rm' in platform and len(platform['rm']) > 0:
65 server_url = platform['rm']
66 if 'registry' in platform and len(platform['registry']) > 0:
67 logger.debug('registry')
68 server_url = platform['registry']
70 if not Config.has_option('monitor', 'cert') :
71 return HttpResponse(json.dumps({'error' : '-1'}), content_type="application/json")
73 cert = os.path.abspath(Config.get('monitor', 'cert'))
74 if not os.path.isfile(cert) :
75 return HttpResponse(json.dumps({'error' : '-1'}), content_type="application/json")
77 if not Config.has_option('monitor', 'pkey') :
78 return HttpResponse(json.dumps({'error' : '-2'}), content_type="application/json")
80 pkey = os.path.abspath(Config.get('monitor', 'pkey'))
81 if not os.path.isfile(pkey) :
82 return HttpResponse(json.dumps({'error' : '-2'}), content_type="application/json")
84 server = SfaServerProxy(server_url, pkey, cert)
86 # Get user config from Manifold
87 user_config = get_user_config(user_email, pf)
88 if 'delegated_user_credential' in user_config:
89 user_cred = user_config['delegated_user_credential']
93 #if 'delegated_slice_credentials' in user_config:
94 # for slice_name, cred in user_config['delegated_slice_credentials']:
95 # if slice_name == slice_param
97 if method == "GetVersion":
98 result = server.GetVersion()
99 elif method == "ListResources":
101 #api_options ['call_id'] = unique_call_id()
102 api_options['geni_rspec_version'] = {'type': 'GENI', 'version': '3'}
103 result = server.ListResources([user_cred], api_options)
104 elif method == "Describe":
106 #api_options ['call_id'] = unique_call_id()
107 api_options['geni_rspec_version'] = {'type': 'GENI', 'version': '3'}
108 result = server.Describe([urn] ,[object_cred], api_options)
111 return HttpResponse(json.dumps({'error' : '-3','msg':'method not supported yet'}), content_type="application/json")
115 return HttpResponse(json.dumps(results), content_type="application/json")
117 def get_user_account(user_email, platform_name):
119 Returns the user configuration for a given platform.
120 This function does not resolve references.
122 user = db.query(User).filter(User.email == user_email).one()
123 platform = db.query(Platform).filter(Platform.platform == platform_name).one()
124 accounts = [a for a in user.accounts if a.platform == platform]
126 raise Exception, "this account does not exist"
128 if accounts[0].auth_type == 'reference':
129 pf = json.loads(accounts[0].config)['reference_platform']
130 return get_user_account(user_email, pf)
134 def get_user_config(user_email, platform_name):
135 account = get_user_account(user_email, platform_name)
136 return json.loads(account.config) if account.config else {}
138 def get_platform_config(platform_name):
139 platform = db.query(Platform).filter(Platform.platform == platform_name).one()
140 return json.loads(platform.config) if platform.config else {}