1
- from app import app , iam_blueprint , iam_base_url
1
+ from app import app , iam_blueprint , iam_base_url , sla as sla
2
2
from flask import json , current_app , render_template , request , redirect , url_for , flash , session
3
3
import requests , json
4
4
import yaml
5
5
import io , os , sys
6
6
from fnmatch import fnmatch
7
7
from hashlib import md5
8
+ from functools import wraps
8
9
9
10
10
11
def to_pretty_json (value ):
@@ -21,6 +22,7 @@ def avatar(email, size):
21
22
22
23
toscaDir = app .config .get ('TOSCA_TEMPLATES_DIR' ) + "/"
23
24
tosca_pars_dir = app .config .get ('TOSCA_PARAMETERS_DIR' )
25
+ orchestratorUrl = app .config .get ('ORCHESTRATOR_URL' )
24
26
25
27
toscaTemplates = []
26
28
for path , subdirs , files in os .walk (toscaDir ):
@@ -34,7 +36,7 @@ def avatar(email, size):
34
36
toscaInfo = {}
35
37
for tosca in toscaTemplates :
36
38
with io .open ( toscaDir + tosca ) as stream :
37
- template = yaml .load (stream )
39
+ template = yaml .full_load (stream )
38
40
39
41
toscaInfo [tosca ] = {
40
42
"valid" : True ,
@@ -78,83 +80,53 @@ def avatar(email, size):
78
80
tosca_pars_file = os .path .join (fpath , fname )
79
81
with io .open (tosca_pars_file ) as pars_file :
80
82
toscaInfo [tosca ]['enable_config_form' ] = True
81
- pars_data = yaml .load (pars_file )
83
+ pars_data = yaml .full_load (pars_file )
82
84
toscaInfo [tosca ]['inputs' ] = pars_data ["inputs" ]
83
85
if "tabs" in pars_data :
84
86
toscaInfo [tosca ]['tabs' ] = pars_data ["tabs" ]
85
87
86
88
87
89
app .logger .debug ("Extracted TOSCA INFO: " + json .dumps (toscaInfo ))
88
90
89
- orchestratorUrl = app .config .get ('ORCHESTRATOR_URL' )
90
- slamUrl = app .config .get ('SLAM_URL' )
91
- cmdbUrl = app .config .get ('CMDB_URL' )
92
- slam_cert = app .config .get ('SLAM_CERT' )
91
+
92
+ def authorized_with_valid_token (f ):
93
+ @wraps (f )
94
+ def decorated_function (* args , ** kwargs ):
95
+
96
+
97
+ if not iam_blueprint .session .authorized or 'username' not in session :
98
+ return redirect (url_for ('login' ))
99
+
100
+ if iam_blueprint .session .token ['expires_in' ] < 20 :
101
+ app .logger .debug ("Force refresh token" )
102
+ iam_blueprint .session .get ('/userinfo' )
103
+
104
+ return f (* args , ** kwargs )
105
+
106
+ return decorated_function
93
107
94
108
@app .route ('/settings' )
109
+ @authorized_with_valid_token
95
110
def show_settings ():
96
- if not iam_blueprint .session .authorized :
97
- return redirect (url_for ('login' ))
98
111
return render_template ('settings.html' , orchestrator_url = orchestratorUrl , iam_url = iam_base_url )
99
112
100
113
@app .route ('/login' )
101
114
def login ():
102
115
session .clear ()
103
116
return render_template ('home.html' )
104
117
105
-
106
- def get_sla_extra_info (access_token , service_id ):
107
- headers = {'Authorization' : 'bearer %s' % (access_token )}
108
- url = cmdbUrl + "/service/id/" + service_id
109
- response = requests .get (url , headers = headers , timeout = 20 )
110
- response .raise_for_status ()
111
- app .logger .info (json .dumps (response .json ()['data' ]['service_type' ]))
112
-
113
- service_type = response .json ()['data' ]['service_type' ]
114
- sitename = response .json ()['data' ]['sitename' ]
115
- if 'properties' in response .json ()['data' ]:
116
- if 'gpu_support' in response .json ()['data' ]['properties' ]:
117
- service_type = service_type + " (gpu_support: " + str (response .json ()['data' ]['properties' ]['gpu_support' ]) + ")"
118
-
119
- return sitename , service_type
120
-
121
- def get_slas (access_token ):
122
-
123
- headers = {'Authorization' : 'bearer %s' % (access_token )}
124
-
125
- url = slamUrl + "/rest/slam/preferences/" + session ['organisation_name' ]
126
- verify = True
127
- if slam_cert :
128
- verify = slam_cert
129
- response = requests .get (url , headers = headers , timeout = 20 , verify = verify )
130
- app .logger .info ("SLA response status: " + str (response .status_code ))
131
-
132
- response .raise_for_status ()
133
- app .logger .info ("SLA response: " + json .dumps (response .json ()))
134
- slas = response .json ()['sla' ]
135
-
136
- for i in range (len (slas )):
137
- sitename , service_type = get_sla_extra_info (access_token ,slas [i ]['services' ][0 ]['service_id' ])
138
- slas [i ]['service_type' ]= service_type
139
- slas [i ]['sitename' ]= sitename
140
-
141
- return slas
142
-
143
118
@app .route ('/slas' )
119
+ @authorized_with_valid_token
144
120
def getslas ():
145
121
146
- if not iam_blueprint .session .authorized :
147
- return redirect (url_for ('login' ))
148
-
149
122
slas = {}
150
123
151
124
try :
152
125
access_token = iam_blueprint .token ['access_token' ]
153
- slas = get_slas (access_token )
126
+ slas = sla . get_slas (access_token )
154
127
155
128
except Exception as e :
156
129
flash ("Error retrieving SLAs list: \n " + str (e ), 'warning' )
157
- return redirect (url_for ('home' ))
158
130
159
131
return render_template ('sla.html' , slas = slas )
160
132
@@ -163,29 +135,24 @@ def getslas():
163
135
def home ():
164
136
if not iam_blueprint .session .authorized :
165
137
return redirect (url_for ('login' ))
166
- try :
167
- account_info = iam_blueprint .session .get ("/userinfo" )
138
+
139
+ account_info = iam_blueprint .session .get ("/userinfo" )
168
140
169
- if account_info .ok :
170
- account_info_json = account_info .json ()
171
- session ['username' ] = account_info_json ['name' ]
172
- session ['gravatar' ] = avatar (account_info_json ['email' ], 26 )
173
- session ['organisation_name' ] = account_info_json ['organisation_name' ]
174
- access_token = iam_blueprint .token ['access_token' ]
141
+ if account_info .ok :
142
+ account_info_json = account_info .json ()
143
+ session ['username' ] = account_info_json ['name' ]
144
+ session ['gravatar' ] = avatar (account_info_json ['email' ], 26 )
145
+ session ['organisation_name' ] = account_info_json ['organisation_name' ]
146
+ access_token = iam_blueprint .token ['access_token' ]
175
147
176
- return render_template ('portfolio.html' , templates = toscaInfo )
148
+ return render_template ('portfolio.html' , templates = toscaInfo )
177
149
178
- except Exception as e :
179
- app .logger .error ("Error: " + str (e ))
180
- return redirect (url_for ('logout' ))
181
150
182
151
@app .route ('/deployments' )
152
+ @authorized_with_valid_token
183
153
def showdeployments ():
184
154
185
- if not iam_blueprint .session .authorized :
186
- return redirect (url_for ('login' ))
187
- try :
188
- access_token = iam_blueprint .token ['access_token' ]
155
+ access_token = iam_blueprint .session .token ['access_token' ]
189
156
190
157
headers = {'Authorization' : 'bearer %s' % (access_token )}
191
158
@@ -199,17 +166,13 @@ def showdeployments():
199
166
deployments = response .json ()["content" ]
200
167
app .logger .debug ("Deployments: " + str (deployments ))
201
168
return render_template ('deployments.html' , deployments = deployments )
202
- except Exception as e :
203
- app .logger .error ("Error: " + str (e ))
204
- return redirect (url_for ('logout' ))
169
+
205
170
206
171
207
172
@app .route ('/template/<depid>' )
173
+ @authorized_with_valid_token
208
174
def deptemplate (depid = None ):
209
175
210
- if not iam_blueprint .session .authorized :
211
- return redirect (url_for ('login' ))
212
-
213
176
access_token = iam_blueprint .session .token ['access_token' ]
214
177
headers = {'Authorization' : 'bearer %s' % (access_token )}
215
178
@@ -224,11 +187,9 @@ def deptemplate(depid=None):
224
187
return render_template ('deptemplate.html' , template = template )
225
188
#
226
189
@app .route ('/delete/<depid>' )
190
+ @authorized_with_valid_token
227
191
def depdel (depid = None ):
228
192
229
- if not iam_blueprint .session .authorized :
230
- return redirect (url_for ('login' ))
231
-
232
193
access_token = iam_blueprint .session .token ['access_token' ]
233
194
headers = {'Authorization' : 'bearer %s' % (access_token )}
234
195
url = orchestratorUrl + "/deployments/" + depid
@@ -241,22 +202,14 @@ def depdel(depid=None):
241
202
242
203
243
204
@app .route ('/configure' )
205
+ @authorized_with_valid_token
244
206
def configure ():
245
- if not iam_blueprint .session .authorized :
246
- return redirect (url_for ('login' ))
247
207
248
208
access_token = iam_blueprint .session .token ['access_token' ]
249
209
250
-
251
-
252
210
selected_tosca = request .args ['selected_tosca' ]
253
211
254
- try :
255
- slas = get_slas (access_token )
256
-
257
- except Exception as e :
258
- flash ("Error retrieving SLAs list: \n " + str (e ), 'warning' )
259
- return redirect (url_for ('home' ))
212
+ slas = sla .get_slas (access_token )
260
213
261
214
return render_template ('createdep.html' ,
262
215
template = toscaInfo [selected_tosca ],
@@ -269,60 +222,53 @@ def add_sla_to_template(template, sla_id):
269
222
270
223
template ['topology_template' ]['policies' ] = [
271
224
{"deploy_on_specific_site" : {"type" : "tosca.policies.Placement" , "properties" : {"sla_id" : sla_id }}}]
272
- app .logger .info (yaml .dump (template , default_flow_style = False ))
225
+ app .logger .debug (yaml .dump (template , default_flow_style = False ))
273
226
274
227
return template
275
228
#
276
229
#
277
230
@app .route ('/submit' , methods = ['POST' ])
231
+ @authorized_with_valid_token
278
232
def createdep ():
279
233
280
- if not iam_blueprint .session .authorized :
281
- return redirect (url_for ('login' ))
282
-
283
234
access_token = iam_blueprint .session .token ['access_token' ]
284
235
285
236
app .logger .debug ("Form data: " + json .dumps (request .form .to_dict ()))
286
237
287
- try :
288
- with io .open ( toscaDir + request .args .get ('template' )) as stream :
289
- template = yaml .load (stream )
238
+ with io .open ( toscaDir + request .args .get ('template' )) as stream :
239
+ template = yaml .full_load (stream )
290
240
291
- form_data = request .form .to_dict ()
292
-
293
- params = {}
294
- if 'extra_opts.keepLastAttempt' in form_data :
295
- params ['keepLastAttempt' ] = 'true'
296
- else :
297
- params ['keepLastAttempt' ] = 'false'
241
+ form_data = request .form .to_dict ()
242
+
243
+ params = {}
244
+ if 'extra_opts.keepLastAttempt' in form_data :
245
+ params ['keepLastAttempt' ] = 'true'
246
+ else :
247
+ params ['keepLastAttempt' ] = 'false'
298
248
299
- if form_data ['extra_opts.schedtype' ] == "man" :
300
- template = add_sla_to_template (template , form_data ['extra_opts.selectedSLA' ])
249
+ if form_data ['extra_opts.schedtype' ] == "man" :
250
+ template = add_sla_to_template (template , form_data ['extra_opts.selectedSLA' ])
301
251
302
- inputs = { k :v for (k ,v ) in form_data .items () if not k .startswith ("extra_opts." ) }
252
+ inputs = { k :v for (k ,v ) in form_data .items () if not k .startswith ("extra_opts." ) }
303
253
304
- app .logger .debug ("Parameters: " + json .dumps (inputs ))
254
+ app .logger .debug ("Parameters: " + json .dumps (inputs ))
305
255
306
- payload = { "template" : yaml .dump (template ,default_flow_style = False , sort_keys = False ), "parameters" : inputs }
256
+ payload = { "template" : yaml .dump (template ,default_flow_style = False , sort_keys = False ), "parameters" : inputs }
307
257
308
258
309
- url = orchestratorUrl + "/deployments/"
310
- headers = {'Content-Type' : 'application/json' , 'Authorization' : 'bearer %s' % (access_token )}
311
- response = requests .post (url , json = payload , params = params , headers = headers )
259
+ url = orchestratorUrl + "/deployments/"
260
+ headers = {'Content-Type' : 'application/json' , 'Authorization' : 'bearer %s' % (access_token )}
261
+ response = requests .post (url , json = payload , params = params , headers = headers )
312
262
313
- if not response .ok :
314
- flash ("Error submitting deployment: \n " + response .text )
263
+ if not response .ok :
264
+ flash ("Error submitting deployment: \n " + response .text )
315
265
316
- return redirect (url_for ('showdeployments' ))
266
+ return redirect (url_for ('showdeployments' ))
317
267
318
- except Exception as e :
319
- flash ("Error submitting deployment:" + str (e ) + ". Please retry" )
320
- return redirect (url_for ('home' ))
321
268
322
269
323
270
@app .route ('/logout' )
324
271
def logout ():
325
272
session .clear ()
326
273
iam_blueprint .session .get ("/logout" )
327
- # del iam_blueprint.session.token
328
274
return redirect (url_for ('login' ))
0 commit comments