Skip to content

Commit

Permalink
feat: add list index
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahanmmi committed Apr 11, 2024
1 parent d8ccbb9 commit 946efb6
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pkg/kaytu-es-sdk/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package kaytu

import (
"context"
"encoding/json"
"fmt"
"go.uber.org/zap"
"io"
)
Expand Down Expand Up @@ -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
}

0 comments on commit 946efb6

Please sign in to comment.