From 946efb6b5476df4a139c0a37beb816535b9bf1c5 Mon Sep 17 00:00:00 2001 From: Mahan Zendedel DH Date: Thu, 11 Apr 2024 20:34:45 +0400 Subject: [PATCH] feat: add list index --- pkg/kaytu-es-sdk/index.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/pkg/kaytu-es-sdk/index.go b/pkg/kaytu-es-sdk/index.go index ea783d4..973566a 100644 --- a/pkg/kaytu-es-sdk/index.go +++ b/pkg/kaytu-es-sdk/index.go @@ -2,6 +2,8 @@ package kaytu import ( "context" + "encoding/json" + "fmt" "go.uber.org/zap" "io" ) @@ -30,3 +32,32 @@ func (c Client) CreateIndexIfNotExist(ctx context.Context, logger *zap.Logger, i return nil } + +func (c Client) ListIndices(ctx context.Context) ([]string, error) { + res, err := c.es.Cat.Indices() + defer CloseSafe(res) + if err != nil { + return nil, err + } else if err := CheckError(res); err != nil { + if IsIndexNotFoundErr(err) { + return nil, nil + } + return nil, err + } + + b, err := io.ReadAll(res.Body) + if err != nil { + return nil, fmt.Errorf("read response: %w", err) + } + + var response []map[string]string + if err := json.Unmarshal(b, &response); err != nil { + return nil, fmt.Errorf("unmarshal response: %w", err) + } + indices := make([]string, 0) + for _, index := range response { + indices = append(indices, index["index"]) + } + + return indices, nil +}