import { isEmpty } from 'lodash' ; import { SerializedActionWithAllocation } from '../../../../../../common/types' ; import { DataAllocationMetaFields } from '../../types' ; export const serializeMigrateAndAllocateActions = ( /**
 * Form metadata about what tier allocation strategy to use and custom node
 * allocation information.
 */ { dataTierAllocationType , allocationNodeAttribute } , /**
 * The new configuration merged with old configuration to ensure we don't lose
 * any fields.
 */ mergedActions = { } , /**
 * The actions from the policy for a given phase when it was loaded.
 */ originalActions = { } , /**
 * The number of replicas value to set in the allocate action.
 */ numberOfReplicas ? ) : SerializedActionWithAllocation => { const { allocate , migrate , ... otherActions } = mergedActions ; const actions = { ... otherActions } ; if ( allocate ) { const { include , exclude , require , number_of_replicas : __ , ... otherSettings } = allocate ; if ( ! isEmpty ( otherSettings ) ) { actions . allocate = { ... otherSettings } ; } } switch ( dataTierAllocationType ) { case 'node_attrs' : if ( allocationNodeAttribute ) { const [ name , value ] = allocationNodeAttribute . split ( ':' ) ; actions . allocate = { ... actions . allocate , require : { [ name ] : value , } , } ; } else { actions . migrate = originalActions . migrate ; } if ( ! isEmpty ( originalActions ?. allocate ?. include ) ) { actions . allocate = { ... actions . allocate , include : { ... originalActions ?. allocate ?. include } , } ; } if ( ! isEmpty ( originalActions ?. allocate ?. exclude ) ) { actions . allocate = { ... actions . allocate , exclude : { ... originalActions ?. allocate ?. exclude } , } ; } break ; case 'none' : actions . migrate = { ... originalActions ?. migrate , enabled : false , } ; break ; default : } if ( numberOfReplicas != null ) { actions . allocate = { ... actions . allocate , number_of_replicas : numberOfReplicas , } ; } return actions ; } ;