@@ -731,6 +731,95 @@ func DeleteAlias(ctx context.Context, apiClient *clients.ApiClient, aliasName st
731731 return diagutil .FrameworkDiagsFromSDK (diags )
732732}
733733
734+ // AliasAction represents a single action in an atomic alias update operation
735+ type AliasAction struct {
736+ Type string // "add" or "remove"
737+ Index string
738+ Alias string
739+ IsWriteIndex bool
740+ Filter map [string ]interface {}
741+ IndexRouting string
742+ IsHidden bool
743+ Routing string
744+ SearchRouting string
745+ }
746+
747+ // UpdateAliasesAtomic performs atomic alias updates using multiple actions
748+ func UpdateAliasesAtomic (ctx context.Context , apiClient * clients.ApiClient , actions []AliasAction ) fwdiags.Diagnostics {
749+ esClient , err := apiClient .GetESClient ()
750+ if err != nil {
751+ return fwdiags.Diagnostics {
752+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
753+ }
754+ }
755+
756+ var aliasActions []map [string ]interface {}
757+
758+ for _ , action := range actions {
759+ if action .Type == "remove" {
760+ aliasActions = append (aliasActions , map [string ]interface {}{
761+ "remove" : map [string ]interface {}{
762+ "index" : action .Index ,
763+ "alias" : action .Alias ,
764+ },
765+ })
766+ } else if action .Type == "add" {
767+ addDetails := map [string ]interface {}{
768+ "index" : action .Index ,
769+ "alias" : action .Alias ,
770+ }
771+
772+ if action .IsWriteIndex {
773+ addDetails ["is_write_index" ] = true
774+ }
775+ if action .Filter != nil {
776+ addDetails ["filter" ] = action .Filter
777+ }
778+ if action .IndexRouting != "" {
779+ addDetails ["index_routing" ] = action .IndexRouting
780+ }
781+ if action .SearchRouting != "" {
782+ addDetails ["search_routing" ] = action .SearchRouting
783+ }
784+ if action .Routing != "" {
785+ addDetails ["routing" ] = action .Routing
786+ }
787+ if action .IsHidden {
788+ addDetails ["is_hidden" ] = action .IsHidden
789+ }
790+
791+ aliasActions = append (aliasActions , map [string ]interface {}{
792+ "add" : addDetails ,
793+ })
794+ }
795+ }
796+
797+ requestBody := map [string ]interface {}{
798+ "actions" : aliasActions ,
799+ }
800+
801+ aliasBytes , err := json .Marshal (requestBody )
802+ if err != nil {
803+ return fwdiags.Diagnostics {
804+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
805+ }
806+ }
807+
808+ res , err := esClient .Indices .UpdateAliases (
809+ bytes .NewReader (aliasBytes ),
810+ esClient .Indices .UpdateAliases .WithContext (ctx ),
811+ )
812+ if err != nil {
813+ return fwdiags.Diagnostics {
814+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
815+ }
816+ }
817+ defer res .Body .Close ()
818+
819+ diags := diagutil .CheckError (res , "Unable to update aliases atomically" )
820+ return diagutil .FrameworkDiagsFromSDK (diags )
821+ }
822+
734823func PutIngestPipeline (ctx context.Context , apiClient * clients.ApiClient , pipeline * models.IngestPipeline ) diag.Diagnostics {
735824 var diags diag.Diagnostics
736825 pipelineBytes , err := json .Marshal (pipeline )
0 commit comments