feat: opensearch客户端初始化 feat: 索引模板接口 ai: 开发准则 chore: TDD流水线脚本
This commit is contained in:
58
operations/cluster/cluster.go
Normal file
58
operations/cluster/cluster.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// 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
|
||||
}
|
||||
40
operations/cluster/cluster_test.go
Normal file
40
operations/cluster/cluster_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"es-demo/client"
|
||||
)
|
||||
|
||||
func TestGetInfo(t *testing.T) {
|
||||
// This is a unit test that doesn't require real connection
|
||||
// Integration tests should be run separately
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test in short mode")
|
||||
}
|
||||
|
||||
cfg := &client.Config{
|
||||
Endpoint: "https://example.com",
|
||||
Region: "us-east-1",
|
||||
AccessKey: "test-key",
|
||||
SecretKey: "test-secret",
|
||||
}
|
||||
|
||||
c, err := client.NewClient(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create client: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// This will fail without a real cluster, which is expected in unit tests
|
||||
_, err = GetInfo(ctx, c)
|
||||
// We just verify the method signature is correct
|
||||
// Real integration tests would check actual responses
|
||||
if err == nil {
|
||||
t.Log("GetInfo succeeded (unexpected in unit test)")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user