mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 04:54:18 +02:00
shards, aliases and filtering (#18)
This commit is contained in:
101
pkg/es/index_alias.go
Normal file
101
pkg/es/index_alias.go
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
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"
|
||||
)
|
||||
|
||||
// FIXME: add filter support, see IndexCreate mapping
|
||||
func IndexAliasCreate(conf *cfg.Config, index, alias string) error {
|
||||
create := conf.DefaultCluster.ES.Indices.PutAlias(index, alias).
|
||||
Header("content-type", "application/json").
|
||||
Header("accept", "application/json")
|
||||
|
||||
res, err := create.Do(context.Background())
|
||||
|
||||
slog.Debug("create alias", "result", res)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create index alias: %s", 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])
|
||||
}
|
||||
|
||||
create := conf.DefaultCluster.ES.Indices.GetAlias().
|
||||
Index("_all").
|
||||
Header("content-type", "application/json").
|
||||
Header("accept", "application/json")
|
||||
|
||||
res, err := create.Do(context.Background())
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to list index aliases: %s", 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 := NewTable(2, len(aliaslist))
|
||||
table.Addheaders("index", "alias")
|
||||
|
||||
idx := 0
|
||||
for index, aliases := range aliaslist {
|
||||
table.entries[idx] = []string{
|
||||
index,
|
||||
strings.Join(aliases, ","),
|
||||
}
|
||||
|
||||
idx++
|
||||
}
|
||||
|
||||
table.Sort()
|
||||
|
||||
if err := table.PrintMarkdown(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user