import * as Either from 'fp-ts/lib/Either' ; import * as TaskEither from 'fp-ts/lib/TaskEither' ; import { errors as EsErrors } from '@elastic/elasticsearch' ; import { ElasticsearchClient } from '../../../elasticsearch' ; import { catchRetryableEsClientErrors , RetryableEsClientError , } from './catch_retryable_es_client_errors' ; import { IndexNotFound } from './index' ; export interface AliasNotFound { type : 'alias_not_found_exception' ; } internal export interface RemoveIndexNotAConcreteIndex { type : 'remove_index_not_a_concrete_index' ; } internal export type AliasAction = { remove_index : { index : string ; } ; } | { remove : { index : string ; alias : string ; must_exist : boolean ; } ; } | { add : { index : string ; alias : string ; } ; } ; internal export interface UpdateAliasesParams { client : ElasticsearchClient ; aliasActions : AliasAction [ ] ; } /**
 * Calls the Update index alias API `_alias` with the provided alias actions.
 */ export const updateAliases = ( { client , aliasActions , } ) : TaskEither . TaskEither < IndexNotFound | AliasNotFound | RemoveIndexNotAConcreteIndex | RetryableEsClientError , 'update_aliases_succeeded' > => ( ) => { return client . indices . updateAliases ( { body : { actions : aliasActions , } , } , { maxRetries : 0 } ) . then ( ( ) => { return Either . right ( 'update_aliases_succeeded' as const ) ; } ) . catch ( ( err ) => { if ( err instanceof EsErrors . ResponseError ) { if ( err ?. body ?. error ?. type === 'index_not_found_exception' ) { return Either . left ( { type : 'index_not_found_exception' as const , index : err . body . error . index , } ) ; } else if ( err ?. body ?. error ?. type === 'illegal_argument_exception' && err ?. body ?. error ?. reason ?. match ( /The provided expression \[.+\] matches an alias, specify the corresponding concrete indices instead./ ) ) { return Either . left ( { type : 'remove_index_not_a_concrete_index' as const } ) ; } else if ( err ?. body ?. error ?. type === 'aliases_not_found_exception' || ( err ?. body ?. error ?. type === 'resource_not_found_exception' && err ?. body ?. error ?. reason ?. match ( /required alias \[.+\] does not exist/ ) ) ) { return Either . left ( { type : 'alias_not_found_exception' as const , } ) ; } } throw err ; } ) . catch ( catchRetryableEsClientErrors ) ; } ;