5 from time import mktime
7 from django.shortcuts import render_to_response
8 from django.http import HttpResponse
10 from sfa.trust.certificate import Keypair, Certificate
11 from sfa.client.sfaserverproxy import SfaServerProxy
12 from sfa.client.return_value import ReturnValue
13 from sfa.util.xrn import Xrn, get_leaf, get_authority, hrn_to_urn, urn_to_hrn
15 from manifold.core.query import Query
16 from manifold.models import db
17 from manifold.models.platform import Platform
18 from manifold.models.user import User
20 from unfold.loginrequired import LoginRequiredView
22 from myslice.settings import logger
24 from rest.json_encoder import MyEncoder
26 def dispatch(request, method):
27 Config = ConfigParser.ConfigParser()
28 Config.read(os.getcwd() + "/myslice/monitor.ini")
30 #logger.debug(request.session['user']['email'])
31 user_email = request.session['user']['email']
41 if request.method == 'POST':
42 req_items = request.POST
43 elif request.method == 'GET':
44 req_items = request.GET
46 for el in req_items.items():
47 if el[0].startswith('rspec'):
49 elif el[0].startswith('platform'):
50 platforms += req_items.getlist('platform[]')
51 #elif el[0].startswith('options'):
52 # options += req_items.getlist('options[]')
53 elif el[0].startswith('hrn'):
55 elif el[0].startswith('urn'):
57 elif el[0].startswith('type'):
60 if method not in ['GetVersion','ListResources']:
65 urn = hrn_to_urn(hrn, object_type)
68 raise Exception, "Provide urn OR hrn + type as parameters of method %s" % method
71 platforms = get_platforms()
72 #platforms.append('myslice')
73 #results = {'method':method,'platforms':platforms,'rspec':rspec,'options':options}
77 api_options['geni_rspec_version'] = {'type': 'GENI', 'version': '3'}
79 from manifoldapi.manifoldapi import execute_admin_query
81 platform = get_platform_config(pf)
82 logger.debug("platform={}".format(platform))
83 if 'sm' in platform and len(platform['sm']) > 0:
86 server_url = platform['sm']
87 if 'rm' in platform and len(platform['rm']) > 0:
90 server_url = platform['rm']
91 if 'registry' in platform and len(platform['registry']) > 0:
92 logger.debug('registry')
94 server_url = platform['registry']
96 if not Config.has_option('monitor', 'cert') :
97 return HttpResponse(json.dumps({'error' : '-1'}), content_type="application/json")
99 cert = os.path.abspath(Config.get('monitor', 'cert'))
100 if not os.path.isfile(cert) :
101 return HttpResponse(json.dumps({'error' : '-1'}), content_type="application/json")
103 if not Config.has_option('monitor', 'pkey') :
104 return HttpResponse(json.dumps({'error' : '-2'}), content_type="application/json")
106 pkey = os.path.abspath(Config.get('monitor', 'pkey'))
107 if not os.path.isfile(pkey) :
108 return HttpResponse(json.dumps({'error' : '-2'}), content_type="application/json")
110 server = SfaServerProxy(server_url, pkey, cert)
113 # Get user config from Manifold
114 user_config = get_user_config(user_email, pf)
115 if 'delegated_user_credential' in user_config:
116 user_cred = user_config['delegated_user_credential']
121 if 'delegated_%s_credentials'%object_type in user_config:
122 for obj_name, cred in user_config['delegated_%s_credentials'%object_type].items():
127 if method == "GetVersion":
128 result = server.GetVersion()
132 if method == "ListResources":
133 result = server.ListResources([user_cred], api_options)
134 elif method == "Describe":
136 # ListResources(slice_hrn)
137 # else GetVersion = v3
138 result = server.Describe([urn] ,[object_cred], api_options)
139 elif method == 'Renew':
140 result = server.Renew([urn] ,[object_cred], api_options)
141 elif method == 'Delete':
142 result = server.Delete([urn] ,[object_cred], api_options)
143 elif method == 'Allocate':
145 # CreateSliver(slice_hrn)
146 # else GetVersion = v3
147 api_options['call_id'] = unique_call_id()
148 # List of users comes from the Registry
149 api_options['sfa_users'] = sfa_users
150 api_options['geni_users'] = geni_users
151 result = server.Allocate([urn] ,[object_cred], rspec, api_options)
152 elif method == 'Provision':
154 # Nothing it is not supported by v2 AMs
155 api_options['call_id'] = unique_call_id()
156 # List of users comes from the Registry
157 api_options['sfa_users'] = sfa_users
158 api_options['geni_users'] = geni_users
159 result = server.Provision([urn] ,[object_cred], api_options)
160 elif method == 'Status':
161 result = server.Status([urn] ,[object_cred], api_options)
162 elif method == 'PerformOperationalAction':
164 # Nothing it is not supported by v2 AMs
165 result = server.PerformOperationalAction([urn] ,[object_cred], action, api_options)
166 elif method == 'Shutdown':
167 result = server.Shutdown(urn ,[object_cred], api_options)
169 #return HttpResponse(json.dumps({'error' : '-3','msg':'method not supported by AM'}), content_type="application/json")
170 logger.debug('method %s not handled by AM' % method)
175 result = server.List(hrn, user_cred, options)
177 elif method == "Resolve":
179 # details can be True or False
180 options['details']=True
181 result = server.Resolve(hrn, user_cred, options)
183 elif method == "Register":
184 # record_dict must be crafted
185 # auth_cred must be selected in the list of auth_creds from user's account
186 result = server.Register(record_dict, auth_cred)
187 elif method == "Update":
188 # record_dict must be crafted
189 # object_cred must be selected in the list of creds for the object type
190 # from user's account
191 result = server.Update(record_dict, object_cred)
192 elif method == "Remove":
194 # auth_cred must be selected in the list of auth_creds from user's account
195 # object_type is required
196 result = server.Remove(hrn, auth_cred, object_type)
198 #return HttpResponse(json.dumps({'error' : '-3','msg':'method not supported by Registry'}), content_type="application/json")
199 logger.debug('method %s not handled by Registry' % method)
205 logger.error(traceback.format_exc())
207 results[pf] = {'error':'-3', 'error_msg': str(e)}
209 return HttpResponse(json.dumps(results, cls=MyEncoder), content_type="application/json")
211 def get_user_account(user_email, platform_name):
213 Returns the user configuration for a given platform.
214 This function does not resolve references.
216 user = db.query(User).filter(User.email == user_email).one()
217 platform = db.query(Platform).filter(Platform.platform == platform_name).one()
218 accounts = [a for a in user.accounts if a.platform == platform]
220 raise Exception, "this account does not exist"
222 if accounts[0].auth_type == 'reference':
223 pf = json.loads(accounts[0].config)['reference_platform']
224 return get_user_account(user_email, pf)
228 def get_user_config(user_email, platform_name):
229 account = get_user_account(user_email, platform_name)
230 return json.loads(account.config) if account.config else {}
234 platforms = db.query(Platform).all()
236 ret.append(p.platform)
240 def get_platform_config(platform_name):
241 platform = db.query(Platform).filter(Platform.platform == platform_name).one()
242 return json.loads(platform.config) if platform.config else {}