<s> import json <EOL> import re <EOL> import sys <EOL> from oslo_db import exception as db_exc <EOL> import pecan <EOL> from pecan import rest <EOL> import six <EOL> from wsme . rest import json as wsme_json <EOL> from wsme import types as wsme_types <EOL> import wsmeext . pecan as wsme_pecan <EOL> from solum . api . controllers . v1 . datamodel import plan <EOL> from solum . api . handlers import plan_handler <EOL> from solum . common import exception <EOL> from solum . common import yamlutils <EOL> from solum import objects <EOL> from solum . openstack . common import log as logging <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def init_plan_v1 ( yml_input_plan ) : <EOL> if not yml_input_plan . get ( '<STR_LIT:name>' ) : <EOL> raise exception . BadRequest ( reason = "<STR_LIT>" ) <EOL> try : <EOL> pp = plan . Plan ( ** yml_input_plan ) <EOL> except ValueError as ve : <EOL> raise exception . BadRequest ( reason = six . text_type ( ve ) ) <EOL> try : <EOL> name_regex = re . compile ( r'<STR_LIT>' ) <EOL> assert name_regex . match ( pp . name ) , '<STR_LIT>' <EOL> except AssertionError as ae : <EOL> raise exception . BadRequest ( reason = six . text_type ( ae ) ) <EOL> return pp <EOL> def init_plan_by_version ( input_plan ) : <EOL> version = input_plan . get ( '<STR_LIT:version>' ) <EOL> if version is None : <EOL> raise exception . BadRequest ( <EOL> reason = '<STR_LIT>' ) <EOL> mod = sys . modules [ __name__ ] <EOL> if not hasattr ( mod , '<STR_LIT>' % version ) : <EOL> raise exception . BadRequest ( reason = '<STR_LIT>' <EOL> % version ) <EOL> return getattr ( mod , '<STR_LIT>' % version ) ( input_plan ) <EOL> def init_yml_plan_by_version ( ) : <EOL> try : <EOL> yml_input_plan = yamlutils . load ( pecan . request . body ) <EOL> except ValueError as excp : <EOL> LOG . error ( "<STR_LIT>" ) <EOL> raise exception . BadRequest ( reason = '<STR_LIT>' <EOL> + six . text_type ( excp ) ) <EOL> return init_plan_by_version ( yml_input_plan ) <EOL> def init_json_plan_by_version ( ) : <EOL> try : <EOL> json_input_plan = json . loads ( pecan . request . body ) <EOL> except ValueError as excp : <EOL> raise exception . BadRequest ( reason = '<STR_LIT>' <EOL> + six . text_type ( excp ) ) <EOL> return init_plan_by_version ( json_input_plan ) <EOL> def yaml_content ( m ) : <EOL> ref_content = m . refined_content ( ) <EOL> host_url = pecan . request . host_url <EOL> ref_content [ '<STR_LIT>' ] = '<STR_LIT>' % ( host_url , m . uuid ) <EOL> ref_content [ '<STR_LIT>' ] = ( '<STR_LIT>' % <EOL> ( host_url , m . trigger_id ) ) <EOL> return ref_content <EOL> class PlanController ( rest . RestController ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , plan_id ) : <EOL> super ( PlanController , self ) . __init__ ( ) <EOL> self . _id = plan_id <EOL> @ exception . wrap_pecan_controller_exception <EOL> @ pecan . expose ( ) <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> handler = plan_handler . PlanHandler ( pecan . request . security_context ) <EOL> if pecan . request . accept is not None and '<STR_LIT>' in pecan . request . accept : <EOL> plan_serialized = yamlutils . dump ( <EOL> yaml_content ( handler . get ( self . _id ) ) ) <EOL> else : <EOL> plan_model = plan . Plan . from_db_model ( <EOL> handler . get ( self . _id ) , pecan . request . host_url ) <EOL> plan_serialized = wsme_json . encode_result ( plan_model , plan . Plan ) <EOL> pecan . response . status = <NUM_LIT:200> <EOL> return plan_serialized <EOL> @ exception . wrap_pecan_controller_exception <EOL> @ pecan . expose ( ) <EOL> def put ( self ) : <EOL> """<STR_LIT>""" <EOL> handler = plan_handler . PlanHandler ( pecan . request . security_context ) <EOL> handler . get ( self . _id ) <EOL> if not pecan . request . body or len ( pecan . request . body ) < <NUM_LIT:1> : <EOL> raise exception . BadRequest ( reason = "<STR_LIT>" ) <EOL> if ( pecan . request . content_type is not None and <EOL> '<STR_LIT>' in pecan . request . content_type ) : <EOL> data = init_yml_plan_by_version ( ) <EOL> updated_plan_yml = yamlutils . dump ( yaml_content ( handler . update ( <EOL> self . _id , data . as_dict ( objects . registry . Plan ) ) ) ) <EOL> else : <EOL> data = init_json_plan_by_version ( ) <EOL> plan_obj = handler . update ( self . _id , <EOL> data . as_dict ( objects . registry . Plan ) ) <EOL> updated_plan_yml = wsme_json . encode_result ( plan . Plan . from_db_model ( <EOL> plan_obj , pecan . request . host_url ) , plan . Plan ) <EOL> pecan . response . status = <NUM_LIT:200> <EOL> return updated_plan_yml <EOL> @ exception . wrap_wsme_pecan_controller_exception <EOL> @ wsme_pecan . wsexpose ( status_code = <NUM_LIT> ) <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> p_handler = plan_handler . PlanHandler ( pecan . request . security_context ) <EOL> try : <EOL> p_handler . delete ( self . _id ) <EOL> except ( db_exc . DBError ) : <EOL> raise exception . PlanStillReferenced ( name = self . _id ) <EOL> class PlansController ( rest . RestController ) : <EOL> """<STR_LIT>""" <EOL> @ pecan . expose ( ) <EOL> def _lookup ( self , plan_id , * remainder ) : <EOL> if remainder and not remainder [ - <NUM_LIT:1> ] : <EOL> remainder = remainder [ : - <NUM_LIT:1> ] <EOL> return PlanController ( plan_id ) , remainder <EOL> @ exception . wrap_pecan_controller_exception <EOL> @ pecan . expose ( ) <EOL> def post ( self ) : <EOL> """<STR_LIT>""" <EOL> if not pecan . request . body or len ( pecan . request . body ) < <NUM_LIT:1> : <EOL> raise exception . BadRequest ( reason = "<STR_LIT>" ) <EOL> handler = plan_handler . PlanHandler ( pecan . request . security_context ) <EOL> if ( pecan . request . content_type is not None and <EOL> '<STR_LIT>' in pecan . request . content_type ) : <EOL> data = init_yml_plan_by_version ( ) <EOL> created_plan = yamlutils . dump ( yaml_content ( handler . create ( <EOL> data . as_dict ( objects . registry . Plan ) ) ) ) <EOL> else : <EOL> data = init_json_plan_by_version ( ) <EOL> plan_wsme = plan . Plan . from_db_model ( handler . create ( <EOL> data . as_dict ( objects . registry . Plan ) ) , pecan . request . host_url ) <EOL> created_plan = wsme_json . encode_result ( plan_wsme , plan . Plan ) <EOL> pecan . response . status = <NUM_LIT> <EOL> return created_plan <EOL> @ exception . wrap_pecan_controller_exception <EOL> @ pecan . expose ( ) <EOL> def get_all ( self ) : <EOL> """<STR_LIT>""" <EOL> handler = plan_handler . PlanHandler ( pecan . request . security_context ) <EOL> if pecan . request . accept is not None and '<STR_LIT>' in pecan . request . accept : <EOL> plan_serialized = yamlutils . dump ( [ yaml_content ( obj ) <EOL> for obj in handler . get_all ( ) <EOL> if obj and obj . raw_content ] ) <EOL> else : <EOL> plan_serialized = wsme_json . encode_result ( <EOL> [ plan . Plan . from_db_model ( obj , pecan . request . host_url ) <EOL> for obj in handler . get_all ( ) ] , <EOL> wsme_types . ArrayType ( plan . Plan ) ) <EOL> pecan . response . status = <NUM_LIT:200> <EOL> return plan_serialized </s>
