Files
esctl/pkg/es/index_alias.go

130 lines
3.1 KiB
Go
Raw Permalink Normal View History

2026-05-19 13:58:08 +02:00
/*
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 <http://www.gnu.org/licenses/>.
*/
package es
import (
"context"
"fmt"
"log/slog"
"regexp"
"strings"
"codeberg.org/scip/esctl/pkg/cfg"
2026-05-21 14:00:35 +02:00
"codeberg.org/scip/esctl/pkg/printer"
2026-05-19 13:58:08 +02:00
)
// 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).
2026-05-21 14:00:35 +02:00
Do(context.Background())
2026-05-19 13:58:08 +02:00
slog.Debug("create alias", "result", res)
if err != nil {
return fmt.Errorf("failed to create index alias: %s", esErrorString(err))
2026-05-19 13:58:08 +02:00
}
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().
2026-05-19 13:58:08 +02:00
Index("_all").
2026-05-21 14:00:35 +02:00
Do(context.Background())
2026-05-19 13:58:08 +02:00
if err != nil {
return fmt.Errorf("failed to list index aliases: %s", esErrorString(err))
2026-05-19 13:58:08 +02:00
}
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))
2026-05-19 13:58:08 +02:00
table.Addheaders("index", "alias")
idx := 0
for index, aliases := range aliaslist {
table.Entries[idx] = []string{
2026-05-19 13:58:08 +02:00
index,
strings.Join(aliases, ","),
}
idx++
}
table.Sort()
if err := table.Print(); err != nil {
2026-05-19 13:58:08 +02:00
return err
}
return nil
}
2026-05-21 14:00:35 +02:00
func IndexAliasDelete(conf *cfg.Config, index, alias string) error {
res, err := conf.DefaultCluster.ES().Indices.DeleteAlias(index, alias).
2026-05-21 14:00:35 +02:00
Do(context.Background())
slog.Debug("delete alias", "result", res)
if err != nil {
return fmt.Errorf("failed to delete index alias: %s", esErrorString(err))
2026-05-21 14:00:35 +02:00
}
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 = [][]string{
{"acknowledged", fmt.Sprintf("%t", res.Acknowledged)},
{"rolled over", fmt.Sprintf("%t", res.RolledOver)},
{"shards acknowledged", fmt.Sprintf("%t", res.ShardsAcknowledged)},
{"old index", res.OldIndex},
{"new index", res.NewIndex},
}
return table.Print()
}