fix #49: add ilm policy to index+index-template create+modify (#50)

This commit is contained in:
T. von Dein
2026-06-25 07:52:54 +02:00
parent 75a94fade3
commit 825b3ce5d1
5 changed files with 64 additions and 43 deletions

View File

@@ -28,6 +28,7 @@ import (
"codeberg.org/scip/esctl/pkg/cfg"
"codeberg.org/scip/esctl/pkg/printer"
"github.com/alecthomas/repr"
"github.com/elastic/go-elasticsearch/v9/typedapi/cat/indices"
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
"github.com/elastic/go-elasticsearch/v9/typedapi/types/enums/healthstatus"
@@ -131,6 +132,9 @@ func IndexShow(conf *cfg.Config, indexpattern string) error {
return fmt.Errorf("failed to get index: %s", esErrorString(err))
}
slog.Debug("index show", "index", res)
repr.Println(res)
for name, index := range res {
fields := make([]string, len(index.Mappings.Properties))
idx := 0
@@ -139,7 +143,7 @@ func IndexShow(conf *cfg.Config, indexpattern string) error {
idx++
}
table := printer.NewTable(conf, 2, 5)
table := printer.NewTable(conf, 2, 7)
table.Addheaders("index property", "value")
ts, err := strconv.ParseInt(index.Settings.Index.CreationDate.(string), 10, 64)
@@ -155,9 +159,17 @@ func IndexShow(conf *cfg.Config, indexpattern string) error {
{"shards", *index.Settings.Index.NumberOfShards},
{"created", created.Format("2006-01-02 15:04:05")},
{"uuid", *index.Settings.Index.Uuid},
{"version", *index.Settings.Index.Version.Created},
{"fields", strings.Join(fields, ",")},
}
if index.Settings.Index.Lifecycle != nil {
table.Entries = append(table.Entries, [][]string{
{"ilm policy", *index.Settings.Index.Lifecycle.Name},
{"ilm rollover alias", *index.Settings.Index.Lifecycle.RolloverAlias},
}...)
}
if err := table.Print(); err != nil {
return err
}
@@ -178,10 +190,10 @@ func IndexCreate(conf *cfg.Config, index string, mappings []string) error {
}
if conf.Shards > 0 {
settings = settings.NumberOfShards(strconv.Itoa(conf.Shards))
settings.NumberOfShards(strconv.Itoa(conf.Shards))
}
if conf.Replicas > 0 {
settings = settings.NumberOfReplicas(strconv.Itoa(conf.Replicas))
settings.NumberOfReplicas(strconv.Itoa(conf.Replicas))
}
if len(mappings) > 0 {
@@ -208,6 +220,13 @@ func IndexCreate(conf *cfg.Config, index string, mappings []string) error {
create.Mappings(maps)
}
if conf.Policy != "" {
settings.Lifecycle(
esdsl.NewIndexSettingsLifecycle().
Name(conf.Policy).
RolloverAlias(index))
}
_, err := create.Settings(settings).
Do(context.Background())
@@ -281,6 +300,7 @@ func IndexAllocation(conf *cfg.Config, index string) error {
return nil
}
/*
func IndexModify(conf *cfg.Config, index string) error {
settings := esdsl.NewIndexSettings().NumberOfReplicas(strconv.Itoa(conf.Replicas))
@@ -294,6 +314,7 @@ func IndexModify(conf *cfg.Config, index string) error {
return nil
}
*/
func IndexFields(conf *cfg.Config, index string) error {
res, err := conf.DefaultCluster.ES().FieldCaps().