5 from time import mktime
8 from django.shortcuts import render_to_response
9 from django.http import HttpResponse
11 from sfa.trust.certificate import Keypair, Certificate
12 from sfa.client.sfaserverproxy import SfaServerProxy
13 from sfa.client.return_value import ReturnValue
14 from sfa.util.xrn import Xrn, get_leaf, get_authority, hrn_to_urn, urn_to_hrn
16 from manifold.core.query import Query
17 from manifold.models import db
18 from manifold.models.platform import Platform
19 from manifold.models.user import User
21 from unfold.loginrequired import LoginRequiredView
23 from myslice.settings import logger, config
25 from rest.json_encoder import MyEncoder
27 def dispatch(request, method):
40 if request.method == 'POST':
41 req_items = request.POST
42 elif request.method == 'GET':
43 req_items = request.GET
45 for el in req_items.items():
46 if el[0].startswith('rspec'):
48 elif el[0].startswith('platform'):
49 platforms = req_items.getlist('platform[]')
50 #elif el[0].startswith('options'):
51 # options += req_items.getlist('options[]')
52 elif el[0].startswith('hrn'):
54 elif el[0].startswith('urn'):
56 elif el[0].startswith('type'):
58 elif el[0].startswith('recursive'):
63 elif el[0].startswith('display'):
66 results = sfa_client(request, method, hrn=hrn, urn=urn, object_type=object_type, recursive=recursive, options=options, platforms=platforms)
67 if display == 'table':
68 return render_to_response('table-default.html', {'data' : data, 'fields' : columns, 'id' : '@component_id', 'options' : None})
70 return HttpResponse(json.dumps(results, cls=MyEncoder), content_type="application/json")
72 def get_user_account(user_email, platform_name):
74 Returns the user configuration for a given platform.
75 This function does not resolve references.
77 user = db.query(User).filter(User.email == user_email).one()
78 platform = db.query(Platform).filter(Platform.platform == platform_name).one()
79 accounts = [a for a in user.accounts if a.platform == platform]
81 raise Exception, "this account does not exist"
83 if accounts[0].auth_type == 'reference':
84 pf = json.loads(accounts[0].config)['reference_platform']
85 return get_user_account(user_email, pf)
89 def sfa_client(request, method, hrn=None, urn=None, object_type=None, rspec=None, recursive=None, options=None, platforms=None, admin=False):
91 Config = ConfigParser.ConfigParser()
92 monitor_file = os.path.abspath(os.path.dirname(__file__) + '/../myslice/monitor.ini')
93 Config.read(monitor_file)
96 user_email, admin_password = config.manifold_admin_user_password()
98 #logger.debug(request.session['user']['email'])
99 user_email = request.session['user']['email']
107 if object_type is None:
111 if recursive is None:
115 if platforms is None:
118 if method not in ['GetVersion','ListResources']:
121 hrn = urn_to_hrn(urn)
123 urn = hrn_to_urn(hrn, object_type)
126 raise Exception, "Provide urn OR hrn + type as parameters of method %s" % method
128 if len(platforms)==0:
129 platforms = get_platforms()
130 #platforms.append('myslice')
131 #results = {'method':method,'platforms':platforms,'rspec':rspec,'options':options}
138 api_options['geni_rspec_version'] = {'type': 'GENI', 'version': '3'}
139 api_options['list_leases'] = 'all'
141 from manifoldapi.manifoldapi import execute_admin_query
143 platform = get_platform_config(pf)
144 logger.debug("platform={}".format(platform))
145 if 'sm' in platform and len(platform['sm']) > 0:
148 server_url = platform['sm']
149 if 'rm' in platform and len(platform['rm']) > 0:
152 server_url = platform['rm']
153 if 'registry' in platform and len(platform['registry']) > 0:
154 logger.debug('registry')
156 server_url = platform['registry']
158 if not Config.has_option('monitor', 'cert') :
159 #return HttpResponse(json.dumps({'error' : '-1'}), content_type="application/json")
160 return {'error' : '-1', 'msg': 'monitor.ini has no cert configured'}
162 cert = os.path.abspath(Config.get('monitor', 'cert'))
163 if not os.path.isfile(cert) :
164 #return HttpResponse(json.dumps({'error' : '-1'}), content_type="application/json")
165 return {'error' : '-1', 'msg': 'check cert file at %s'%cert}
167 if not Config.has_option('monitor', 'pkey') :
168 #return HttpResponse(json.dumps({'error' : '-2'}), content_type="application/json")
169 return {'error' : '-2'}
171 pkey = os.path.abspath(Config.get('monitor', 'pkey'))
172 if not os.path.isfile(pkey) :
173 #return HttpResponse(json.dumps({'error' : '-2'}), content_type="application/json")
174 return {'error' : '-2'}
176 server = SfaServerProxy(server_url, pkey, cert)
179 # Get user config from Manifold
180 user_config = get_user_config(user_email, pf)
181 if 'delegated_user_credential' in user_config:
182 user_cred = user_config['delegated_user_credential']
183 elif 'user_credential' in user_config:
184 user_cred = user_config['user_credential']
189 if 'delegated_%s_credentials'%object_type in user_config:
190 for obj_name, cred in user_config['delegated_%s_credentials'%object_type].items():
193 elif '%s_credentials'%object_type in user_config:
194 for obj_name, cred in user_config['%s_credentials'%object_type].items():
199 if method == "GetVersion":
200 result = server.GetVersion()
204 if method == "ListResources":
205 result = server.ListResources([user_cred], api_options)
206 dict_result = xmltodict.parse(result['value'])
207 result['json'] = json.dumps(dict_result)
208 if isinstance(dict_result['rspec']['node'], list):
209 columns.extend(dict_result['rspec']['node'][0].keys())
211 columns.extend(dict_result['rspec']['node'].keys())
213 elif method == "Describe":
214 version = server.GetVersion()
215 logger.debug(version['geni_api'])
217 if version['geni_api'] == 2:
218 # ListResources(slice_hrn)
219 api_options['geni_slice_urn'] = urn
220 result = server.ListResources([object_cred], api_options)
221 dict_result = xmltodict.parse(result['value'])
222 # else GetVersion = v3
224 result = server.Describe([urn] ,[object_cred], api_options)
225 dict_result = xmltodict.parse(result['value']['geni_rspec'])
227 result['json'] = json.dumps(dict_result)
228 if 'rspec' in dict_result and 'node' in dict_result['rspec']:
229 if isinstance(dict_result['rspec']['node'], list):
230 columns.extend(dict_result['rspec']['node'][0].keys())
232 columns.extend(dict_result['rspec']['node'].keys())
234 elif method == 'Renew':
235 # Renew till 1 month from now
236 d = datetime.datetime.utcnow() + datetime.timedelta(365/12)
237 date = d.isoformat("T") + "Z"
238 result = server.Renew([urn] ,[object_cred], date, api_options)
239 elif method == 'Delete':
240 result = server.Delete([urn] ,[object_cred], api_options)
241 elif method == 'Allocate':
243 # CreateSliver(slice_hrn)
244 # else GetVersion = v3
245 api_options['call_id'] = unique_call_id()
246 # List of users comes from the Registry
247 api_options['sfa_users'] = sfa_users
248 api_options['geni_users'] = geni_users
249 result = server.Allocate([urn] ,[object_cred], rspec, api_options)
250 elif method == 'Provision':
252 # Nothing it is not supported by v2 AMs
253 api_options['call_id'] = unique_call_id()
254 # List of users comes from the Registry
255 api_options['sfa_users'] = sfa_users
256 api_options['geni_users'] = geni_users
257 result = server.Provision([urn] ,[object_cred], api_options)
258 elif method == 'Status':
259 result = server.Status([urn] ,[object_cred], api_options)
260 elif method == 'PerformOperationalAction':
262 # Nothing it is not supported by v2 AMs
263 result = server.PerformOperationalAction([urn] ,[object_cred], action, api_options)
264 elif method == 'Shutdown':
265 result = server.Shutdown(urn ,[object_cred], api_options)
267 #return HttpResponse(json.dumps({'error' : '-3','msg':'method not supported by AM'}), content_type="application/json")
268 logger.debug('method %s not handled by AM' % method)
273 record_dict = {'urn': urn, 'hrn': hrn, 'type': object_type}
276 api_options['recursive'] = recursive
277 result = server.List(hrn, user_cred, api_options)
279 result = filter_records(object_type, result)
280 elif method == "Resolve":
282 # details can be True or False
283 api_options['details']=True
284 result = server.Resolve(hrn, user_cred, api_options)
286 result = filter_records(object_type, result)
287 elif method == "Register":
288 # record_dict must be crafted
289 # auth_cred must be selected in the list of auth_creds from user's account
290 result = server.Register(record_dict, auth_cred)
291 elif method == "Update":
292 # record_dict must be crafted
293 # object_cred must be selected in the list of creds for the object type
294 # from user's account
295 result = server.Update(record_dict, object_cred)
296 elif method == "Remove":
298 # auth_cred must be selected in the list of auth_creds from user's account
299 # object_type is required
300 result = server.Remove(hrn, auth_cred, object_type)
302 #return HttpResponse(json.dumps({'error' : '-3','msg':'method not supported by Registry'}), content_type="application/json")
303 logger.debug('method %s not handled by Registry' % method)
308 if 'rspec' in dict_result and 'node' in dict_result['rspec']:
309 if isinstance(dict_result['rspec']['node'], list):
310 data = data + dict_result['rspec']['node']
312 data.append(dict_result['rspec']['node'])
315 logger.error(traceback.format_exc())
317 results[pf] = {'error':'-3', 'error_msg': str(e)}
319 results['columns'] = columns
322 def get_user_config(user_email, platform_name):
323 account = get_user_account(user_email, platform_name)
324 return json.loads(account.config) if account.config else {}
328 platforms = db.query(Platform).filter(Platform.gateway_type == 'sfa', Platform.disabled == 0).all()
330 ret.append(p.platform)
334 def get_platform_config(platform_name):
335 platform = db.query(Platform).filter(Platform.platform == platform_name).one()
336 return json.loads(platform.config) if platform.config else {}
338 def filter_records(type, records):
339 filtered_records = []
340 for record in records:
341 if (record['type'] == type) or (type == "all"):
342 filtered_records.append(record)
343 return filtered_records