<s> import logging <EOL> import yaml <EOL> from heatclient import exc as heatexceptions <EOL> import six <EOL> from swiftclient import exceptions as swiftexceptions <EOL> from tripleo_common . core import exception <EOL> from tripleo_common . utils import meta <EOL> from tripleo_common . utils import templates <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class PlanManager ( object ) : <EOL> def __init__ ( self , plan_storage_backend , heatclient ) : <EOL> self . plan_store = plan_storage_backend <EOL> self . heatclient = heatclient <EOL> def create_plan ( self , plan_name , plan_files ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . plan_store . create ( plan_name ) <EOL> except Exception : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> raise <EOL> plan_files = meta . add_file_metadata ( plan_files ) <EOL> return self . update_plan ( plan_name , plan_files ) <EOL> def delete_plan ( self , plan_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> stack = self . heatclient . stacks . get ( plan_name ) <EOL> if stack is not None : <EOL> raise exception . StackInUseError ( name = plan_name ) <EOL> except heatexceptions . HTTPNotFound : <EOL> try : <EOL> self . plan_store . delete ( plan_name ) <EOL> except swiftexceptions . ClientException as ce : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> if ce . http_status == <NUM_LIT> : <EOL> six . raise_from ( exception . PlanDoesNotExistError ( <EOL> name = plan_name ) , ce ) <EOL> except Exception : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> raise <EOL> def delete_file ( self , plan_name , filename ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . plan_store . delete_file ( plan_name , filename ) <EOL> except swiftexceptions . ClientException as ce : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> if ce . http_status == <NUM_LIT> : <EOL> six . raise_from ( exception . FileDoesNotExistError ( <EOL> name = filename ) , ce ) <EOL> except Exception : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> raise <EOL> def delete_temporary_environment ( self , plan_name ) : <EOL> """<STR_LIT>""" <EOL> plan = self . get_plan ( plan_name ) <EOL> for item in { k : v for ( k , v ) in plan . files . items ( ) if <EOL> v . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' ) == '<STR_LIT>' } : <EOL> self . plan_store . delete_file ( plan_name , item ) <EOL> def get_plan ( self , plan_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . plan_store . get ( plan_name ) <EOL> except swiftexceptions . ClientException as ce : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> if ce . http_status == <NUM_LIT> : <EOL> six . raise_from ( exception . PlanDoesNotExistError ( <EOL> name = plan_name ) , ce ) <EOL> except Exception : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> raise <EOL> def get_plan_list ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . plan_store . list ( ) <EOL> except Exception : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> raise <EOL> def get_deployment_parameters ( self , plan_name ) : <EOL> """<STR_LIT>""" <EOL> plan = self . get_plan ( plan_name ) <EOL> template , environment , files = templates . process_plan_data ( plan . files ) <EOL> try : <EOL> params = self . heatclient . stacks . validate ( <EOL> template = template , <EOL> files = files , <EOL> environment = environment , <EOL> show_nested = True ) <EOL> except heatexceptions . HTTPBadRequest as exc : <EOL> six . raise_from ( exception . HeatValidationFailedError ( msg = exc ) , exc ) <EOL> return params <EOL> def update_deployment_parameters ( self , plan_name , deployment_parameters ) : <EOL> """<STR_LIT>""" <EOL> plan = self . get_plan ( plan_name ) <EOL> deployment_params_file = '<STR_LIT>' <EOL> if not deployment_parameters . get ( '<STR_LIT>' ) : <EOL> deployment_parameters = { <EOL> '<STR_LIT>' : deployment_parameters <EOL> } <EOL> if deployment_params_file in plan . files : <EOL> plan . files . pop ( deployment_params_file ) <EOL> template , environment , files = templates . process_plan_data ( plan . files ) <EOL> environment = templates . deep_update ( environment , deployment_parameters ) <EOL> try : <EOL> self . heatclient . stacks . validate ( <EOL> template = template , <EOL> files = files , <EOL> environment = environment , <EOL> show_nested = True ) <EOL> except heatexceptions . HTTPBadRequest as exc : <EOL> six . raise_from ( exception . HeatValidationFailedError ( msg = exc ) , exc ) <EOL> env = yaml . safe_dump ( deployment_parameters , default_flow_style = False ) <EOL> plan . files [ deployment_params_file ] = { <EOL> '<STR_LIT>' : env , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> } <EOL> self . update_plan ( plan_name , plan . files ) <EOL> def update_plan ( self , plan_name , plan_files ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . plan_store . update ( plan_name , plan_files ) <EOL> except swiftexceptions . ClientException as ce : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> if ce . http_status == <NUM_LIT> : <EOL> six . raise_from ( exception . PlanDoesNotExistError ( <EOL> name = plan_name ) , ce ) <EOL> except Exception : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> raise <EOL> return self . get_plan ( plan_name ) <EOL> def validate_plan ( self , plan_name ) : <EOL> """<STR_LIT>""" <EOL> plan = self . get_plan ( plan_name ) <EOL> rt = { k : v for ( k , v ) in plan . files . items ( ) <EOL> if v . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' ) == '<STR_LIT>' } <EOL> if len ( rt ) > <NUM_LIT:1> : <EOL> raise exception . TooManyRootTemplatesError ( ) <EOL> template , environment , files = templates . process_plan_data ( plan . files ) <EOL> try : <EOL> self . heatclient . stacks . validate ( <EOL> template = template , <EOL> files = files , <EOL> environment = environment , <EOL> show_nested = True ) <EOL> except heatexceptions . HTTPBadRequest as exc : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> six . raise_from ( exception . HeatValidationFailedError ( msg = exc ) , exc ) <EOL> return True </s>
