/* Copyright © 2026 Thomas von Dein This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package es import ( "context" "fmt" "log/slog" "regexp" "strings" "codeberg.org/scip/esctl/pkg/cfg" "codeberg.org/scip/esctl/pkg/printer" ) // FIXME: add filter support, see IndexCreate mapping func IndexAliasCreate(conf *cfg.Config, index, alias string) error { res, err := conf.DefaultCluster.ES().Indices.PutAlias(index, alias). Do(context.Background()) slog.Debug("create alias", "result", res) if err != nil { return fmt.Errorf("failed to create index alias: %s", esErrorString(err)) } return nil } func IndexAliasList(conf *cfg.Config) error { filter := regexp.Regexp{} if len(conf.Filter) > 0 { // we support just one filter here, for now filter = *regexp.MustCompile(conf.Filter[0]) } res, err := conf.DefaultCluster.ES().Indices.GetAlias(). Index("_all"). Do(context.Background()) if err != nil { return fmt.Errorf("failed to list index aliases: %s", esErrorString(err)) } slog.Debug("aliases list", "result", res) aliaslist := map[string][]string{} // index => []aliases for index, aliases := range res { if len(conf.Filter) > 0 { if !filter.MatchString(index) { continue } } for alias := range aliases.Aliases { aliaslist[index] = append(aliaslist[index], alias) } } table := printer.NewTable(conf, 2, len(aliaslist)) table.Addheaders("index", "alias") idx := 0 for index, aliases := range aliaslist { table.Entries[idx] = []any{ index, strings.Join(aliases, ","), } idx++ } table.Sort() if err := table.Print(); err != nil { return err } return nil } func IndexAliasDelete(conf *cfg.Config, index, alias string) error { res, err := conf.DefaultCluster.ES().Indices.DeleteAlias(index, alias). Do(context.Background()) slog.Debug("delete alias", "result", res) if err != nil { return fmt.Errorf("failed to delete index alias: %s", esErrorString(err)) } return nil } func IndexAliasRollover(conf *cfg.Config, alias string) error { res, err := RolloverAlias(conf, alias) if err != nil { return fmt.Errorf("failed to rollover index alias: %s", esErrorString(err)) } table := printer.NewTable(conf, 2, 5) table.Addheaders("rollover response", "value") table.Entries = [][]any{ {"acknowledged", res.Acknowledged}, {"rolled over", res.RolledOver}, {"shards acknowledged", res.ShardsAcknowledged}, {"old index", res.OldIndex}, {"new index", res.NewIndex}, } return table.Print() }