/* 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 . */ package es import ( "context" "fmt" "codeberg.org/scip/esctl/pkg/cfg" "github.com/elastic/go-elasticsearch/v9" "github.com/elastic/go-elasticsearch/v9/typedapi/cat/indices" "github.com/elastic/go-elasticsearch/v9/typedapi/cat/tasks" "github.com/elastic/go-elasticsearch/v9/typedapi/ccr/stats" "github.com/elastic/go-elasticsearch/v9/typedapi/cluster/health" clusterstats "github.com/elastic/go-elasticsearch/v9/typedapi/cluster/stats" "github.com/elastic/go-elasticsearch/v9/typedapi/core/info" "github.com/elastic/go-elasticsearch/v9/typedapi/ilm/explainlifecycle" "github.com/elastic/go-elasticsearch/v9/typedapi/ilm/getlifecycle" "github.com/elastic/go-elasticsearch/v9/typedapi/types/enums/bytes" ) const ( ResponseHealth = iota ResponseInfo ResponseCcr ResponseStats ResponseIndices ResponseTasks ResponseExplain ResponseLifecycle ResponseHealthReport ) type apiResponse struct { error error info *info.Response health *health.Response healthreport *HealthReport ccr *stats.Response stats *clusterstats.Response indices *indices.Response indicesbytes *indices.Response tasks *tasks.Response lifecycle *getlifecycle.Response explainlifecycle *explainlifecycle.Response which int } func getApiData(conf *cfg.Config, es *elasticsearch.TypedClient, reschan chan apiResponse, which string) { apiRes := apiResponse{} var arerr error switch which { case "health": res, err := es.Cluster.Health(). Do(context.Background()) apiRes.health = res apiRes.which = ResponseHealth arerr = err case "healthreport": report, err := getHealthReport(conf) apiRes.healthreport = report apiRes.which = ResponseHealthReport arerr = err case "info": res, err := es.Info(). Do(context.Background()) apiRes.info = res apiRes.which = ResponseInfo arerr = err case "ccr": res, err := es.Ccr.Stats(). Do(context.Background()) apiRes.ccr = res apiRes.which = ResponseCcr arerr = err case "stats": res, err := es.Cluster.Stats(). Do(context.Background()) apiRes.stats = res apiRes.which = ResponseStats arerr = err case "indices": res, err := es.Cat.Indices(). Do(context.Background()) apiRes.indices = &res apiRes.which = ResponseIndices arerr = err case "indicesbytes": res, err := es.Cat. Indices(). Bytes(bytes.Bytes{Name: "b"}). Do(context.Background()) apiRes.indicesbytes = &res apiRes.which = ResponseIndices arerr = err case "tasks": res, err := es.Cat.Tasks(). Do(context.Background()) apiRes.tasks = &res apiRes.which = ResponseTasks arerr = err case "explain": res, err := es.Ilm. ExplainLifecycle("_all"). Do(context.Background()) apiRes.explainlifecycle = res apiRes.which = ResponseExplain arerr = err case "policies": res, err := es.Ilm. GetLifecycle(). Do(context.Background()) apiRes.lifecycle = &res apiRes.which = ResponseLifecycle arerr = err } if arerr != nil { apiRes.error = fmt.Errorf("failed to get data from API: %w", arerr) } reschan <- apiRes }