2424import io
2525import os
2626import logging
27+ import copy
2728from requests .exceptions import Timeout
2829from werkzeug .middleware .proxy_fix import ProxyFix
2930from flask_dance .consumer import OAuth2ConsumerBlueprint
@@ -49,7 +50,7 @@ def create_app(oidc_blueprint=None):
4950 app = Flask (__name__ )
5051 app .wsgi_app = ProxyFix (app .wsgi_app , x_for = 1 , x_proto = 1 , x_host = 1 , x_port = 1 , x_prefix = 1 )
5152 app .secret_key = "8210f566-4981-11ea-92d1-f079596e599b"
52- app .config .from_json ( ' config.json' )
53+ app .config .from_file ( " config.json" , load = json . load )
5354 settings = Settings (app .config )
5455 if settings .vault_url :
5556 cred = VaultCredentials (settings .vault_url )
@@ -162,22 +163,25 @@ def home():
162163 if "filter" in session :
163164 template_filter = session ["filter" ]
164165
166+ templates = {}
167+ for name , tosca in toscaInfo .items ():
168+ if "parents" not in tosca ["metadata" ]:
169+ templates [name ] = tosca
170+
165171 if template_filter :
166172 session ["filter" ] = template_filter
167173 templates = {}
168174 for k , v in toscaInfo .items ():
169175 if 'description' and v ['description' ]:
170- if v ['description' ].find (template_filter ) != - 1 :
176+ if v ['description' ].find (template_filter ) != - 1 and "parents" not in tosca [ "metadata" ] :
171177 templates [k ] = v
172- else :
173- templates = toscaInfo
174178
175179 if settings .debug_oidc_token :
176180 session ["vos" ] = None
177181 session ['userid' ] = "a_very_long_user_id_00000000000000000000000000000000000000000000@egi.es"
178182 session ['username' ] = "username"
179183 session ['gravatar' ] = ""
180- return render_template ('portfolio.html' , templates = templates )
184+ return render_template ('portfolio.html' , templates = templates , parent = None )
181185 else :
182186 if not oidc_blueprint .session .authorized :
183187 return redirect (url_for ('login' ))
@@ -225,7 +229,7 @@ def home():
225229 else :
226230 session ['gravatar' ] = utils .avatar (account_info_json ['sub' ], 26 )
227231
228- return render_template ('portfolio.html' , templates = templates )
232+ return render_template ('portfolio.html' , templates = templates , parent = None )
229233 else :
230234 flash ("Error getting User info: \n " + account_info .text , 'error' )
231235 return render_template ('home.html' , oidc_name = settings .oidcName )
@@ -592,6 +596,9 @@ def infoutputs(infid=None):
592596 def configure ():
593597 selected_tosca = None
594598 inf_id = request .args .get ('inf_id' , None )
599+ childs = request .args .get ('childs' , None )
600+ if childs :
601+ childs = childs .split ("," )
595602
596603 inputs = {}
597604 infra_name = ""
@@ -619,6 +626,8 @@ def configure():
619626 inputs [input_name ] = input_value ["default" ]
620627 if 'filename' in data ['metadata' ] and data ['metadata' ]['filename' ]:
621628 selected_tosca = data ['metadata' ]['filename' ]
629+ if 'childs' in data ['metadata' ] and data ['metadata' ]['childs' ]:
630+ childs = data ['metadata' ]['childs' ]
622631 except Exception as ex :
623632 flash ("Error getting TOSCA template inputs: \n %s" % ex , "error" )
624633
@@ -635,7 +644,24 @@ def configure():
635644 flash ("InvalidTOSCA template name: %s" % selected_tosca , "error" )
636645 return redirect (url_for ('home' ))
637646
638- app .logger .debug ("Template: " + json .dumps (toscaInfo [selected_tosca ]))
647+ child_templates = {}
648+ selected_template = copy .deepcopy (toscaInfo [selected_tosca ])
649+ if "childs" in toscaInfo [selected_tosca ]["metadata" ]:
650+ if childs is not None :
651+ for child in childs :
652+ if child in toscaInfo :
653+ child_templates [child ] = toscaInfo [child ]
654+ if "inputs" in toscaInfo [child ]:
655+ selected_template ["inputs" ].update (toscaInfo [child ]["inputs" ])
656+ if "tabs" in toscaInfo [child ]:
657+ selected_template ["tabs" ].extend (toscaInfo [child ]["tabs" ])
658+ else :
659+ for child in toscaInfo [selected_tosca ]["metadata" ]["childs" ]:
660+ if child in toscaInfo :
661+ child_templates [child ] = toscaInfo [child ]
662+ return render_template ('portfolio.html' , templates = child_templates , parent = selected_tosca )
663+ else :
664+ app .logger .debug ("Template: " + json .dumps (toscaInfo [selected_tosca ]))
639665
640666 try :
641667 creds = cred .get_creds (get_cred_id (), 1 )
@@ -645,10 +671,10 @@ def configure():
645671 utils .get_project_ids (creds )
646672
647673 return render_template ('createdep.html' ,
648- template = toscaInfo [ selected_tosca ] ,
674+ template = selected_template ,
649675 selectedTemplate = selected_tosca ,
650676 creds = creds , input_values = inputs ,
651- infra_name = infra_name )
677+ infra_name = infra_name , child_templates = child_templates )
652678
653679 @app .route ('/vos' )
654680 def getvos ():
@@ -824,6 +850,14 @@ def add_ssh_keys_to_template(template):
824850
825851 return template
826852
853+ def _merge_templates (template , new_template ):
854+ for item in ["inputs" , "node_templates" , "outputs" ]:
855+ if item in new_template ["topology_template" ]:
856+ if item not in template ["topology_template" ]:
857+ template ["topology_template" ][item ] = {}
858+ template ["topology_template" ][item ].update (new_template ["topology_template" ][item ])
859+ return template
860+
827861 @app .route ('/submit' , methods = ['POST' ])
828862 @authorized_with_valid_token
829863 def createdep ():
@@ -832,6 +866,9 @@ def createdep():
832866
833867 app .logger .debug ("Form data: " + json .dumps (request .form .to_dict ()))
834868
869+ childs = []
870+ if 'extra_opts.childs' in form_data :
871+ childs = form_data ['extra_opts.childs' ].split ("," )
835872 cred_id = form_data ['extra_opts.selectedCred' ]
836873 cred_data = cred .get_cred (cred_id , get_cred_id ())
837874 access_token = oidc_blueprint .session .token ['access_token' ]
@@ -871,9 +908,14 @@ def createdep():
871908 with io .open (settings .toscaDir + request .args .get ('template' )) as stream :
872909 template = yaml .full_load (stream )
873910
911+ for child in childs :
912+ with io .open (settings .toscaDir + child ) as stream :
913+ template = _merge_templates (template , yaml .full_load (stream ))
914+
874915 if 'metadata' not in template :
875916 template ['metadata' ] = {}
876917 template ['metadata' ]['filename' ] = request .args .get ('template' )
918+ template ['metadata' ]['childs' ] = childs
877919
878920 if priv_network_id and pub_network_id :
879921 template = add_network_id_to_template (template , priv_network_id , pub_network_id )
0 commit comments