@@ -9,40 +9,32 @@ import (
99
1010 "github.com/threatwinds/go-sdk/catcher"
1111 sdkos "github.com/threatwinds/go-sdk/os"
12- "github.com/threatwinds/go-sdk/plugins"
1312)
1413
1514func ConnectOpenSearch () error {
16- osUrl := plugins . PluginCfg ( "org.opensearch" , false ). Get ( "opensearch" ). String ()
15+ osCfg := LoadOpenSearchConfig ()
1716
18- err := sdkos .Connect ([]string {osUrl }, "" , "" )
17+ err := sdkos .Connect ([]string {osCfg . URL }, osCfg . User , osCfg . Pass )
1918 if err != nil {
2019 return catcher .Error ("failed to connect to OpenSearch" , err , map [string ]any {
21- "url" : osUrl ,
20+ "url" : osCfg .URL ,
21+ "user" : osCfg .User ,
2222 })
2323 }
2424
2525 catcher .Info ("Connected to OpenSearch" , map [string ]any {
26- "url" : osUrl ,
26+ "url" : osCfg . URL ,
2727 })
28-
2928 return nil
3029}
3130
32- type SQLResponse struct {
33- Schema []any `json:"schema"`
34- DataRows [][]any `json:"datarows"`
35- Total int `json:"total"`
36- }
37-
3831func (b * BackendClient ) ExecuteSQLQuery (ctx context.Context , sql string ) (SQLResult , error ) {
39- baseURL := plugins . PluginCfg ( "org.opensearch" , false ). Get ( "opensearch" ). String ()
40- sqlEndpoint := fmt . Sprintf ( "%s/_plugins/_sql" , baseURL )
32+ osCfg := LoadOpenSearchConfig ()
33+ client := NewOpenSearchHTTPClient ( )
4134
42- body := map [string ]string {
43- "query" : sql ,
44- }
35+ sqlEndpoint := fmt .Sprintf ("%s/_plugins/_sql" , osCfg .URL )
4536
37+ body := map [string ]string {"query" : sql }
4638 jsonBody , err := json .Marshal (body )
4739 if err != nil {
4840 return SQLResult {}, fmt .Errorf ("failed to marshal SQL body: %w" , err )
@@ -54,8 +46,9 @@ func (b *BackendClient) ExecuteSQLQuery(ctx context.Context, sql string) (SQLRes
5446 }
5547
5648 req .Header .Set ("Content-Type" , "application/json" )
49+ req .SetBasicAuth (osCfg .User , osCfg .Pass )
5750
58- resp , err := b . httpClient .Do (req )
51+ resp , err := client .Do (req )
5952 if err != nil {
6053 return SQLResult {}, fmt .Errorf ("SQL request failed: %w" , err )
6154 }
0 commit comments