<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import copy <EOL> import datetime <EOL> import difflib <EOL> import json <EOL> from salt . ext . six . moves import zip <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> return '<STR_LIT>' if '<STR_LIT>' in __salt__ else False <EOL> def present ( name , pipeline_objects = None , <EOL> pipeline_objects_from_pillars = '<STR_LIT>' , <EOL> parameter_objects = None , <EOL> parameter_objects_from_pillars = '<STR_LIT>' , <EOL> parameter_values = None , <EOL> parameter_values_from_pillars = '<STR_LIT>' , <EOL> region = None , <EOL> key = None , keyid = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:name>' : name , '<STR_LIT:result>' : True , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : { } } <EOL> pipeline_objects = pipeline_objects or { } <EOL> parameter_objects = parameter_objects or { } <EOL> parameter_values = parameter_values or { } <EOL> present , old_pipeline_definition = _pipeline_present_with_definition ( <EOL> name , <EOL> _pipeline_objects ( pipeline_objects_from_pillars , pipeline_objects ) , <EOL> _parameter_objects ( parameter_objects_from_pillars , parameter_objects ) , <EOL> _parameter_values ( parameter_values_from_pillars , parameter_values ) , <EOL> region = region , <EOL> key = key , <EOL> keyid = keyid , <EOL> profile = profile , <EOL> ) <EOL> if present : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> return ret <EOL> if __opts__ [ '<STR_LIT:test>' ] : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> ret [ '<STR_LIT:result>' ] = None <EOL> return ret <EOL> result_create_pipeline = __salt__ [ '<STR_LIT>' ] ( <EOL> name , <EOL> name , <EOL> region = region , <EOL> key = key , <EOL> keyid = keyid , <EOL> profile = profile , <EOL> ) <EOL> if '<STR_LIT:error>' in result_create_pipeline : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( <EOL> name , result_create_pipeline [ '<STR_LIT:error>' ] ) <EOL> return ret <EOL> pipeline_id = result_create_pipeline [ '<STR_LIT:result>' ] <EOL> result_pipeline_definition = __salt__ [ '<STR_LIT>' ] ( <EOL> pipeline_id , <EOL> _pipeline_objects ( pipeline_objects_from_pillars , pipeline_objects ) , <EOL> parameter_objects = _parameter_objects ( parameter_objects_from_pillars , parameter_objects ) , <EOL> parameter_values = _parameter_values ( parameter_values_from_pillars , parameter_values ) , <EOL> region = region , <EOL> key = key , <EOL> keyid = keyid , <EOL> profile = profile , <EOL> ) <EOL> if '<STR_LIT:error>' in result_pipeline_definition : <EOL> if _immutable_fields_error ( result_pipeline_definition ) : <EOL> result_delete_pipeline = __salt__ [ '<STR_LIT>' ] ( <EOL> pipeline_id , <EOL> region = region , <EOL> key = key , <EOL> keyid = keyid , <EOL> profile = profile , <EOL> ) <EOL> if '<STR_LIT:error>' in result_delete_pipeline : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( <EOL> pipeline_id , result_delete_pipeline [ '<STR_LIT:error>' ] ) <EOL> return ret <EOL> result_create_pipeline = __salt__ [ '<STR_LIT>' ] ( <EOL> name , <EOL> name , <EOL> region = region , <EOL> key = key , <EOL> keyid = keyid , <EOL> profile = profile , <EOL> ) <EOL> if '<STR_LIT:error>' in result_create_pipeline : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( <EOL> name , result_create_pipeline [ '<STR_LIT:error>' ] ) <EOL> return ret <EOL> pipeline_id = result_create_pipeline [ '<STR_LIT:result>' ] <EOL> result_pipeline_definition = __salt__ [ '<STR_LIT>' ] ( <EOL> pipeline_id , <EOL> _pipeline_objects ( pipeline_objects_from_pillars , pipeline_objects ) , <EOL> parameter_objects = _parameter_objects ( parameter_objects_from_pillars , parameter_objects ) , <EOL> parameter_values = _parameter_values ( parameter_values_from_pillars , parameter_values ) , <EOL> region = region , <EOL> key = key , <EOL> keyid = keyid , <EOL> profile = profile , <EOL> ) <EOL> if '<STR_LIT:error>' in result_pipeline_definition : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( <EOL> name , result_pipeline_definition [ '<STR_LIT:error>' ] ) <EOL> return ret <EOL> result_activate_pipeline = __salt__ [ '<STR_LIT>' ] ( <EOL> pipeline_id , <EOL> region = region , <EOL> key = key , <EOL> keyid = keyid , <EOL> profile = profile , <EOL> ) <EOL> if '<STR_LIT:error>' in result_activate_pipeline : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( <EOL> name , result_pipeline_definition [ '<STR_LIT:error>' ] ) <EOL> return ret <EOL> pipeline_definition_result = __salt__ [ '<STR_LIT>' ] ( <EOL> pipeline_id , <EOL> version = '<STR_LIT>' , <EOL> region = region , <EOL> key = key , <EOL> keyid = keyid , <EOL> profile = profile , <EOL> ) <EOL> if '<STR_LIT:error>' in pipeline_definition_result : <EOL> new_pipeline_definition = { } <EOL> else : <EOL> new_pipeline_definition = _standardize ( pipeline_definition_result [ '<STR_LIT:result>' ] ) <EOL> if not old_pipeline_definition : <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> else : <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = _diff ( old_pipeline_definition , new_pipeline_definition ) <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> return ret <EOL> def _immutable_fields_error ( result_pipeline_definition ) : <EOL> '''<STR_LIT>''' <EOL> for e in result_pipeline_definition [ '<STR_LIT:error>' ] : <EOL> for e2 in e [ "<STR_LIT>" ] : <EOL> if "<STR_LIT>" in e2 : <EOL> return True <EOL> return False <EOL> def _pipeline_present_with_definition ( name , expected_pipeline_objects , <EOL> expected_parameter_objects , <EOL> expected_parameter_values , region , key , <EOL> keyid , profile ) : <EOL> '''<STR_LIT>''' <EOL> result_pipeline_id = __salt__ [ '<STR_LIT>' ] ( <EOL> name , <EOL> region = region , <EOL> key = key , <EOL> keyid = keyid , <EOL> profile = profile , <EOL> ) <EOL> if '<STR_LIT:error>' in result_pipeline_id : <EOL> return False , { } <EOL> pipeline_id = result_pipeline_id [ '<STR_LIT:result>' ] <EOL> pipeline_definition_result = __salt__ [ '<STR_LIT>' ] ( <EOL> pipeline_id , <EOL> version = '<STR_LIT>' , <EOL> region = region , <EOL> key = key , <EOL> keyid = keyid , <EOL> profile = profile , <EOL> ) <EOL> if '<STR_LIT:error>' in pipeline_definition_result : <EOL> return False , { } <EOL> pipeline_definition = _standardize ( pipeline_definition_result [ '<STR_LIT:result>' ] ) <EOL> pipeline_objects = pipeline_definition . get ( '<STR_LIT>' ) <EOL> parameter_objects = pipeline_definition . get ( '<STR_LIT>' ) <EOL> paramater_values = pipeline_definition . get ( '<STR_LIT>' ) <EOL> present = ( _recursive_compare ( _cleaned ( pipeline_objects ) , _cleaned ( expected_pipeline_objects ) ) and <EOL> _recursive_compare ( parameter_objects , expected_parameter_objects ) and <EOL> _recursive_compare ( paramater_values , expected_parameter_values ) ) <EOL> return present , pipeline_definition <EOL> def _cleaned ( _pipeline_objects ) : <EOL> """<STR_LIT>""" <EOL> pipeline_objects = copy . deepcopy ( _pipeline_objects ) <EOL> for pipeline_object in pipeline_objects : <EOL> if pipeline_object [ '<STR_LIT:id>' ] == '<STR_LIT>' : <EOL> for field_object in pipeline_object [ '<STR_LIT>' ] : <EOL> if field_object [ '<STR_LIT:key>' ] == '<STR_LIT>' : <EOL> start_date_time_string = field_object [ '<STR_LIT>' ] <EOL> start_date_time = datetime . datetime . strptime ( start_date_time_string , <EOL> "<STR_LIT>" ) <EOL> field_object [ '<STR_LIT>' ] = start_date_time . strftime ( "<STR_LIT>" ) <EOL> return pipeline_objects <EOL> def _recursive_compare ( v1 , v2 ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( v1 , list ) : <EOL> if v2 is None : <EOL> v2 = [ ] <EOL> if len ( v1 ) != len ( v2 ) : <EOL> return False <EOL> v1 . sort ( key = _id_or_key ) <EOL> v2 . sort ( key = _id_or_key ) <EOL> for x , y in zip ( v1 , v2 ) : <EOL> if not _recursive_compare ( x , y ) : <EOL> return False <EOL> return True <EOL> elif isinstance ( v1 , dict ) : <EOL> if v2 is None : <EOL> v2 = { } <EOL> v1 = dict ( v1 ) <EOL> v2 = dict ( v2 ) <EOL> if sorted ( v1 ) != sorted ( v2 ) : <EOL> return False <EOL> for k in v1 : <EOL> if not _recursive_compare ( v1 [ k ] , v2 [ k ] ) : <EOL> return False <EOL> return True <EOL> else : <EOL> return v1 == v2 <EOL> def _id_or_key ( list_item ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( list_item , dict ) : <EOL> if '<STR_LIT:id>' in list_item : <EOL> return list_item [ '<STR_LIT:id>' ] <EOL> if '<STR_LIT:key>' in list_item : <EOL> return list_item [ '<STR_LIT:key>' ] <EOL> return list_item <EOL> def _diff ( old_pipeline_definition , new_pipeline_definition ) : <EOL> '''<STR_LIT>''' <EOL> old_pipeline_definition . pop ( '<STR_LIT>' , None ) <EOL> new_pipeline_definition . pop ( '<STR_LIT>' , None ) <EOL> diff = difflib . unified_diff ( <EOL> json . dumps ( old_pipeline_definition , indent = <NUM_LIT:4> ) . splitlines ( <NUM_LIT:1> ) , <EOL> json . dumps ( new_pipeline_definition , indent = <NUM_LIT:4> ) . splitlines ( <NUM_LIT:1> ) , <EOL> ) <EOL> return '<STR_LIT>' . join ( diff ) <EOL> def _standardize ( structure ) : <EOL> '''<STR_LIT>''' <EOL> def mutating_helper ( structure ) : <EOL> if isinstance ( structure , list ) : <EOL> structure . sort ( key = _id_or_key ) <EOL> for each in structure : <EOL> mutating_helper ( each ) <EOL> elif isinstance ( structure , dict ) : <EOL> structure = dict ( structure ) <EOL> for k , v in structure . iteritems ( ) : <EOL> mutating_helper ( k ) <EOL> mutating_helper ( v ) <EOL> new_structure = copy . deepcopy ( structure ) <EOL> mutating_helper ( new_structure ) <EOL> return new_structure <EOL> def _pipeline_objects ( pipeline_objects_from_pillars , pipeline_object_overrides ) : <EOL> '''<STR_LIT>''' <EOL> from_pillars = copy . deepcopy ( __salt__ [ '<STR_LIT>' ] ( pipeline_objects_from_pillars ) ) <EOL> from_pillars . update ( pipeline_object_overrides ) <EOL> pipeline_objects = _standardize ( _dict_to_list_ids ( from_pillars ) ) <EOL> for pipeline_object in pipeline_objects : <EOL> pipeline_object [ '<STR_LIT>' ] = _properties_from_dict ( pipeline_object [ '<STR_LIT>' ] ) <EOL> return pipeline_objects <EOL> def _parameter_objects ( parameter_objects_from_pillars , parameter_object_overrides ) : <EOL> '''<STR_LIT>''' <EOL> from_pillars = copy . deepcopy ( __salt__ [ '<STR_LIT>' ] ( parameter_objects_from_pillars ) ) <EOL> from_pillars . update ( parameter_object_overrides ) <EOL> parameter_objects = _standardize ( _dict_to_list_ids ( from_pillars ) ) <EOL> for parameter_object in parameter_objects : <EOL> parameter_object [ '<STR_LIT>' ] = _properties_from_dict ( parameter_object [ '<STR_LIT>' ] ) <EOL> return parameter_objects <EOL> def _parameter_values ( parameter_values_from_pillars , parameter_value_overrides ) : <EOL> '''<STR_LIT>''' <EOL> from_pillars = copy . deepcopy ( __salt__ [ '<STR_LIT>' ] ( parameter_values_from_pillars ) ) <EOL> from_pillars . update ( parameter_value_overrides ) <EOL> parameter_values = _standardize ( from_pillars ) <EOL> return _properties_from_dict ( parameter_values , key_name = '<STR_LIT:id>' ) <EOL> def _dict_to_list_ids ( objects ) : <EOL> '''<STR_LIT>''' <EOL> list_with_ids = [ ] <EOL> for key , value in objects . iteritems ( ) : <EOL> element = { '<STR_LIT:id>' : key } <EOL> element . update ( value ) <EOL> list_with_ids . append ( element ) <EOL> return list_with_ids <EOL> def _properties_from_dict ( d , key_name = '<STR_LIT:key>' ) : <EOL> '''<STR_LIT>''' <EOL> fields = [ ] <EOL> for key , value in d . iteritems ( ) : <EOL> if isinstance ( value , dict ) : <EOL> fields . append ( { <EOL> key_name : key , <EOL> '<STR_LIT>' : value [ '<STR_LIT>' ] , <EOL> } ) <EOL> else : <EOL> fields . append ( { <EOL> key_name : key , <EOL> '<STR_LIT>' : value , <EOL> } ) <EOL> return fields <EOL> def absent ( name , region = None , key = None , keyid = None , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:name>' : name , '<STR_LIT:result>' : True , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : { } } <EOL> result_pipeline_id = __salt__ [ '<STR_LIT>' ] ( <EOL> name , <EOL> region = region , <EOL> key = key , <EOL> keyid = keyid , <EOL> profile = profile , <EOL> ) <EOL> if '<STR_LIT:error>' not in result_pipeline_id : <EOL> pipeline_id = result_pipeline_id [ '<STR_LIT:result>' ] <EOL> if __opts__ [ '<STR_LIT:test>' ] : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> ret [ '<STR_LIT:result>' ] = None <EOL> return ret <EOL> else : <EOL> __salt__ [ '<STR_LIT>' ] ( <EOL> pipeline_id , <EOL> region = region , <EOL> key = key , <EOL> keyid = keyid , <EOL> profile = profile , <EOL> ) <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { '<STR_LIT>' : pipeline_id } <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = None <EOL> else : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> return ret </s>
