-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseable.go
More file actions
177 lines (161 loc) · 4.55 KB
/
Copy pathparseable.go
File metadata and controls
177 lines (161 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package tools
import (
"bytes"
"crypto/tls"
"encoding/json"
"io"
"log/slog"
"net/http"
"os"
"strconv"
)
// These variables must be set by main.go before calling RegisterParseableTools
var (
ParseableBaseURL string
ParseableUser string
ParseablePass string
)
// package-level HTTP client; initialized in init() to respect UNSECURE env var
var HTTPClient *http.Client
func init() {
// UNSECURE environment variable controls whether TLS verification is skipped.
// Accepts the same values as strconv.ParseBool (true/1/t etc.).
unsecureEnv := os.Getenv("UNSECURE")
if ok, _ := strconv.ParseBool(unsecureEnv); ok {
HTTPClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
slog.Info("UNSECURE=true: HTTP client will skip TLS verification")
} else {
HTTPClient = http.DefaultClient
}
}
func addBasicAuth(req *http.Request) {
req.SetBasicAuth(ParseableUser, ParseablePass)
}
func doParseableQuery(query string, streamName string, startTime string, endTime string) ([]map[string]interface{}, error) {
payload := map[string]string{
"query": query,
"streamName": streamName,
"startTime": startTime,
"endTime": endTime,
}
jsonPayload, _ := json.Marshal(payload)
url := ParseableBaseURL + parseableSQLPath
httpReq, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
if err != nil {
return nil, err
}
httpReq.Header.Set("Content-Type", "application/json")
addBasicAuth(httpReq)
resp, err := HTTPClient.Do(httpReq)
if err != nil {
return nil, err
}
defer func() {
if err := resp.Body.Close(); err != nil {
slog.Error("failed to close response body", "error", err)
}
}()
body, _ := io.ReadAll(resp.Body)
// Try to unmarshal as array of rows
var arrResult []map[string]interface{}
if err := json.Unmarshal(body, &arrResult); err != nil {
return nil, err
}
return arrResult, nil
}
func listParseableStreams() ([]map[string]interface{}, error) {
url := ParseableBaseURL + "/api/v1/logstream"
return doSimpleGetArray(url)
}
func getParseableSchema(stream string) (map[string]interface{}, error) {
url := ParseableBaseURL + "/api/v1/logstream/" + stream + "/schema"
httpReq, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
addBasicAuth(httpReq)
resp, err := HTTPClient.Do(httpReq)
if err != nil {
return nil, err
}
defer func() {
if err := resp.Body.Close(); err != nil {
slog.Error("failed to close response body", "error", err)
}
}()
var result map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return result, nil
}
func getParseableStats(streamName string) (map[string]interface{}, error) {
url := ParseableBaseURL + "/api/v1/logstream/" + streamName + "/stats"
stats, _, err := doSimpleGet(url)
return stats, err
}
func getParseableInfo(streamName string) (map[string]interface{}, error) {
url := ParseableBaseURL + "/api/v1/logstream/" + streamName + "/info"
info, _, err := doSimpleGet(url)
return info, err
}
func getParseableAbout() (map[string]interface{}, error) {
url := ParseableBaseURL + "/api/v1/about"
about, _, err := doSimpleGet(url)
return about, err
}
func getParseableRoles() (map[string]interface{}, error) {
url := ParseableBaseURL + "/api/v1/roles"
roles, _, err := doSimpleGet(url)
return roles, err
}
func getParseableUsers() ([]map[string]interface{}, error) {
url := ParseableBaseURL + "/api/v1/users"
return doSimpleGetArray(url)
}
func doSimpleGet(url string) (map[string]interface{}, map[string]interface{}, error) {
httpReq, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, nil, err
}
addBasicAuth(httpReq)
resp, err := HTTPClient.Do(httpReq)
if err != nil {
return nil, nil, err
}
defer func() {
if err := resp.Body.Close(); err != nil {
slog.Error("failed to close response body", "error", err)
}
}()
var response map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, nil, err
}
return response, nil, nil
}
func doSimpleGetArray(url string) ([]map[string]interface{}, error) {
httpReq, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
addBasicAuth(httpReq)
resp, err := HTTPClient.Do(httpReq)
if err != nil {
return nil, err
}
defer func() {
if err := resp.Body.Close(); err != nil {
slog.Error("failed to close response body", "error", err)
}
}()
var response []map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
return response, nil
}