59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
// Package cluster provides cluster-level operations for OpenSearch administration.
|
|
package cluster
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
"es-demo/client"
|
|
|
|
"github.com/opensearch-project/opensearch-go/v2/opensearchapi"
|
|
)
|
|
|
|
// Info represents essential information about the OpenSearch cluster.
|
|
type Info struct {
|
|
Name string `json:"name"`
|
|
ClusterName string `json:"cluster_name"`
|
|
ClusterUUID string `json:"cluster_uuid"`
|
|
Version struct {
|
|
Number string `json:"number"`
|
|
BuildType string `json:"build_type"`
|
|
BuildHash string `json:"build_hash"`
|
|
BuildDate string `json:"build_date"`
|
|
BuildSnapshot bool `json:"build_snapshot"`
|
|
LuceneVersion string `json:"lucene_version"`
|
|
MinimumWireCompatibilityVersion string `json:"minimum_wire_compatibility_version"`
|
|
MinimumIndexCompatibilityVersion string `json:"minimum_index_compatibility_version"`
|
|
} `json:"version"`
|
|
Tagline string `json:"tagline"`
|
|
}
|
|
|
|
// GetInfo retrieves essential information about the OpenSearch cluster.
|
|
// It returns the cluster information or an error if the request fails.
|
|
func GetInfo(ctx context.Context, c *client.Client) (*Info, error) {
|
|
req := opensearchapi.InfoRequest{}
|
|
|
|
res, err := req.Do(ctx, c.GetClient())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to execute info request: %w", err)
|
|
}
|
|
defer func() {
|
|
if closeErr := res.Body.Close(); closeErr != nil {
|
|
fmt.Fprintf(os.Stderr, "warning: failed to close response body: %v\n", closeErr)
|
|
}
|
|
}()
|
|
|
|
if res.IsError() {
|
|
return nil, fmt.Errorf("request failed with status: %s", res.Status())
|
|
}
|
|
|
|
var info Info
|
|
if err := json.NewDecoder(res.Body).Decode(&info); err != nil {
|
|
return nil, fmt.Errorf("failed to decode cluster info: %w", err)
|
|
}
|
|
|
|
return &info, nil
|
|
}
|