mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 11:44:18 +02:00
refactored IndexTemplateModify() (#38)
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
"codeberg.org/scip/esctl/pkg/es"
|
"codeberg.org/scip/esctl/pkg/es"
|
||||||
@@ -170,6 +171,10 @@ https://www.elastic.co/docs/reference/elasticsearch/index-settings
|
|||||||
return fmt.Errorf("no name specified")
|
return fmt.Errorf("no name specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !slices.Contains([]string{"standard", "timeseries", "logsdb", "lookup"}, conf.Mode) {
|
||||||
|
return errors.New("mode must be one of: standard, timeseries, logsdb or lookup")
|
||||||
|
}
|
||||||
|
|
||||||
mappings := args.Slice()[1:]
|
mappings := args.Slice()[1:]
|
||||||
|
|
||||||
if modify {
|
if modify {
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"slices"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -185,34 +184,13 @@ func IndexTemplateCreate(conf *cfg.Config, name string, mappings []string) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
if conf.Mode != "" {
|
if conf.Mode != "" {
|
||||||
if !slices.Contains([]string{"standard", "timeseries", "logsdb", "lookup"}, conf.Mode) {
|
|
||||||
return errors.New("mode must be one of: standard, timeseries, logsdb or lookup")
|
|
||||||
}
|
|
||||||
|
|
||||||
settings = settings.Mode(conf.Mode)
|
settings = settings.Mode(conf.Mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(mappings) > 0 {
|
if len(mappings) > 0 {
|
||||||
typemaps := esdsl.NewTypeMapping()
|
typemaps, err := modMappings(mappings)
|
||||||
|
if err != nil {
|
||||||
for _, mapping := range mappings {
|
return err
|
||||||
parts := strings.Split(mapping, ":")
|
|
||||||
if len(parts) != 2 {
|
|
||||||
return fmt.Errorf(
|
|
||||||
"invalid mapping %s, expect <name:type> (type: integer, text, date, keyword)",
|
|
||||||
mapping)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch parts[1] {
|
|
||||||
case "text":
|
|
||||||
typemaps.AddProperty(parts[0], esdsl.NewTextProperty())
|
|
||||||
case "integer":
|
|
||||||
typemaps.AddProperty(parts[0], esdsl.NewIntegerNumberProperty())
|
|
||||||
case "date":
|
|
||||||
typemaps.AddProperty(parts[0], esdsl.NewDateProperty())
|
|
||||||
case "keyword":
|
|
||||||
typemaps.AddProperty(parts[0], esdsl.NewKeywordProperty())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
maps.Mappings(typemaps)
|
maps.Mappings(typemaps)
|
||||||
@@ -227,40 +205,18 @@ func IndexTemplateCreate(conf *cfg.Config, name string, mappings []string) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(conf.Meta) > 0 {
|
if len(conf.Meta) > 0 {
|
||||||
metadata := map[string]json.RawMessage{}
|
metadata, err := modMeta(conf, nil)
|
||||||
|
if err != nil {
|
||||||
for _, meta := range conf.Meta {
|
return err
|
||||||
parts := strings.Split(meta, ":")
|
|
||||||
if len(parts) != 2 {
|
|
||||||
return errors.New("meta data must be in the form key:value")
|
|
||||||
}
|
|
||||||
|
|
||||||
msg, err := json.Marshal(parts[1])
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to json marshal metadata %s: %w", meta, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
metadata[parts[0]] = msg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
create.Meta_(esdsl.NewMetadata(metadata))
|
create.Meta_(esdsl.NewMetadata(metadata))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(conf.Settings) > 0 {
|
if len(conf.Settings) > 0 {
|
||||||
usersettings := map[string]json.RawMessage{}
|
usersettings, err := modSettings(conf)
|
||||||
|
if err != nil {
|
||||||
for _, meta := range conf.Settings {
|
return err
|
||||||
parts := strings.Split(meta, ":")
|
|
||||||
if len(parts) != 2 {
|
|
||||||
return errors.New("settings data must be in the form key:value")
|
|
||||||
}
|
|
||||||
|
|
||||||
msg, err := json.Marshal(parts[1])
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to json marshal metadata %s: %w", meta, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
usersettings[parts[0]] = msg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
settings = settings.IndexSettings(usersettings)
|
settings = settings.IndexSettings(usersettings)
|
||||||
@@ -315,15 +271,10 @@ func IndexTemplateModify(conf *cfg.Config, name string, mappings []string) error
|
|||||||
maps.Mappings(tpl.IndexTemplate.Template.Mappings)
|
maps.Mappings(tpl.IndexTemplate.Template.Mappings)
|
||||||
maps.Aliases(tpl.IndexTemplate.Template.Aliases)
|
maps.Aliases(tpl.IndexTemplate.Template.Aliases)
|
||||||
|
|
||||||
// pre fill meta, if any
|
|
||||||
metadata := map[string]json.RawMessage{}
|
|
||||||
for key, value := range tpl.IndexTemplate.Meta_ {
|
|
||||||
metadata[key] = value
|
|
||||||
}
|
|
||||||
|
|
||||||
// pre fill components
|
// pre fill components
|
||||||
modify.ComposedOf(tpl.IndexTemplate.ComposedOf...)
|
modify.ComposedOf(tpl.IndexTemplate.ComposedOf...)
|
||||||
|
|
||||||
|
// pre fill data stream config
|
||||||
if conf.Stream {
|
if conf.Stream {
|
||||||
modify.DataStream(esdsl.NewDataStreamVisibility())
|
modify.DataStream(esdsl.NewDataStreamVisibility())
|
||||||
|
|
||||||
@@ -338,34 +289,13 @@ func IndexTemplateModify(conf *cfg.Config, name string, mappings []string) error
|
|||||||
modify.AllowAutoCreate(conf.AutoCreate)
|
modify.AllowAutoCreate(conf.AutoCreate)
|
||||||
|
|
||||||
if conf.Mode != "" {
|
if conf.Mode != "" {
|
||||||
if !slices.Contains([]string{"standard", "timeseries", "logsdb", "lookup"}, conf.Mode) {
|
|
||||||
return errors.New("mode must be one of: standard, timeseries, logsdb or lookup")
|
|
||||||
}
|
|
||||||
|
|
||||||
*settings.Mode = conf.Mode
|
*settings.Mode = conf.Mode
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(mappings) > 0 {
|
if len(mappings) > 0 {
|
||||||
typemaps := esdsl.NewTypeMapping()
|
typemaps, err := modMappings(mappings)
|
||||||
|
if err != nil {
|
||||||
for _, mapping := range mappings {
|
return err
|
||||||
parts := strings.Split(mapping, ":")
|
|
||||||
if len(parts) != 2 {
|
|
||||||
return fmt.Errorf(
|
|
||||||
"invalid mapping %s, expect <name:type> (type: integer, text, date, keyword)",
|
|
||||||
mapping)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch parts[1] {
|
|
||||||
case "text":
|
|
||||||
typemaps.AddProperty(parts[0], esdsl.NewTextProperty())
|
|
||||||
case "integer":
|
|
||||||
typemaps.AddProperty(parts[0], esdsl.NewIntegerNumberProperty())
|
|
||||||
case "date":
|
|
||||||
typemaps.AddProperty(parts[0], esdsl.NewDateProperty())
|
|
||||||
case "keyword":
|
|
||||||
typemaps.AddProperty(parts[0], esdsl.NewKeywordProperty())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
maps.Mappings(typemaps)
|
maps.Mappings(typemaps)
|
||||||
@@ -380,39 +310,19 @@ func IndexTemplateModify(conf *cfg.Config, name string, mappings []string) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(conf.Meta) > 0 {
|
if len(conf.Meta) > 0 {
|
||||||
for _, meta := range conf.Meta {
|
// pre fill meta, if any
|
||||||
parts := strings.Split(meta, ":")
|
metadata, err := modMeta(conf, tpl.IndexTemplate.Meta_)
|
||||||
if len(parts) != 2 {
|
if err != nil {
|
||||||
return errors.New("meta data must be in the form key:value")
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
msg, err := json.Marshal(parts[1])
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to json marshal metadata %s: %w", meta, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
metadata[parts[0]] = msg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
modify.Meta_(esdsl.NewMetadata(metadata))
|
||||||
}
|
}
|
||||||
|
|
||||||
modify.Meta_(esdsl.NewMetadata(metadata))
|
|
||||||
|
|
||||||
if len(conf.Settings) > 0 {
|
if len(conf.Settings) > 0 {
|
||||||
usersettings := map[string]json.RawMessage{}
|
usersettings, err := modSettings(conf)
|
||||||
|
if err != nil {
|
||||||
for _, meta := range conf.Settings {
|
return err
|
||||||
parts := strings.Split(meta, ":")
|
|
||||||
if len(parts) != 2 {
|
|
||||||
return errors.New("settings data must be in the form key:value")
|
|
||||||
}
|
|
||||||
|
|
||||||
msg, err := json.Marshal(parts[1])
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to json marshal metadata %s: %w", meta, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
usersettings[parts[0]] = msg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
settings.IndexSettings = usersettings
|
settings.IndexSettings = usersettings
|
||||||
@@ -447,3 +357,73 @@ func IndexTemplateDelete(conf *cfg.Config, name string) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func modMappings(mappings []string) (types.TypeMappingVariant, error) {
|
||||||
|
typemaps := esdsl.NewTypeMapping()
|
||||||
|
|
||||||
|
for _, mapping := range mappings {
|
||||||
|
parts := strings.Split(mapping, ":")
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"invalid mapping %s, expect <name:type> (type: integer, text, date, keyword)",
|
||||||
|
mapping)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch parts[1] {
|
||||||
|
case "text":
|
||||||
|
typemaps.AddProperty(parts[0], esdsl.NewTextProperty())
|
||||||
|
case "integer":
|
||||||
|
typemaps.AddProperty(parts[0], esdsl.NewIntegerNumberProperty())
|
||||||
|
case "date":
|
||||||
|
typemaps.AddProperty(parts[0], esdsl.NewDateProperty())
|
||||||
|
case "keyword":
|
||||||
|
typemaps.AddProperty(parts[0], esdsl.NewKeywordProperty())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return typemaps, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func modMeta(conf *cfg.Config, meta types.Metadata) (map[string]json.RawMessage, error) {
|
||||||
|
metadata := map[string]json.RawMessage{}
|
||||||
|
|
||||||
|
for key, value := range meta {
|
||||||
|
metadata[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, meta := range conf.Meta {
|
||||||
|
parts := strings.Split(meta, ":")
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return nil, errors.New("meta data must be in the form key:value")
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, err := json.Marshal(parts[1])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to json marshal metadata %s: %w", meta, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata[parts[0]] = msg
|
||||||
|
}
|
||||||
|
|
||||||
|
return metadata, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func modSettings(conf *cfg.Config) (map[string]json.RawMessage, error) {
|
||||||
|
usersettings := map[string]json.RawMessage{}
|
||||||
|
|
||||||
|
for _, meta := range conf.Settings {
|
||||||
|
parts := strings.Split(meta, ":")
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return nil, errors.New("settings data must be in the form key:value")
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, err := json.Marshal(parts[1])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to json marshal metadata %s: %w", meta, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
usersettings[parts[0]] = msg
|
||||||
|
}
|
||||||
|
|
||||||
|
return usersettings, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user