-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (114 loc) · 3.25 KB
/
main.go
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
package main
import (
"context"
"encoding/csv"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"net/url"
"strings"
"github.com/mashiike/prepalert/plugin"
"github.com/mashiike/prepalert/provider"
)
type ProviderParameter struct {
Endpoint string `json:"endpoint"`
}
type Provider struct {
endpoints map[string]*url.URL
}
func (p *Provider) ValidateProviderParameter(ctx context.Context, pp *provider.ProviderParameter) error {
slog.InfoContext(ctx, "call ValidateProviderParameter", "provider", pp.Name)
if pp.Type != "http" {
return fmt.Errorf("invalid provider type name %q: required plugin name http", pp.Type)
}
var cfg ProviderParameter
if err := json.Unmarshal(pp.Params, &cfg); err != nil {
return fmt.Errorf("failed to unmarshal provider params: %w", err)
}
if cfg.Endpoint == "" {
return fmt.Errorf("endpoint is required")
}
u, err := url.Parse(cfg.Endpoint)
if err != nil {
return fmt.Errorf("failed to parse endpoint: %w", err)
}
p.endpoints[pp.Name] = u
return nil
}
func (p *Provider) GetQuerySchema(ctx context.Context) (*plugin.Schema, error) {
slog.InfoContext(ctx, "call GetQuerySchema")
return &plugin.Schema{
Attributes: []plugin.AttributeSchema{
{
Name: "fields",
},
{
Name: "limit",
},
},
}, nil
}
type QueryParameter struct {
Fields []string `json:"fields"`
Limit int `json:"limit"`
}
func (p *Provider) RunQuery(ctx context.Context, req *plugin.RunQueryRequest) (*plugin.RunQueryResponse, error) {
slog.InfoContext(ctx, "call RunQuery")
var qp QueryParameter
if err := json.Unmarshal(req.QueryParameters, &qp); err != nil {
return nil, fmt.Errorf("failed to unmarshal query params: %w", err)
}
if len(qp.Fields) == 0 {
qp.Fields = []string{"all"}
}
if qp.Limit <= 0 {
qp.Limit = 100
}
endpoint, ok := p.endpoints[req.ProviderParameters.Name]
if !ok {
return nil, fmt.Errorf("provider name %q not found", req.ProviderParameters.Name)
}
c := *endpoint
c.RawQuery = url.Values{
"fields": []string{strings.Join(qp.Fields, ",")},
"limit": []string{fmt.Sprintf("%d", qp.Limit)},
}.Encode()
httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, c.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
httpReq.Header.Set("Accept", "text/csv")
httpReq.Header.Set("User-Agent", "prepalert-example-plugin")
resp, err := http.DefaultClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to request: %s", resp.Status)
}
reader := csv.NewReader(resp.Body)
reader.Comma = ','
columns, err := reader.Read()
if err != nil {
return nil, fmt.Errorf("failed to read csv: %w", err)
}
rows, err := reader.ReadAll()
if err != nil {
return nil, fmt.Errorf("failed to read csv: %w", err)
}
slog.InfoContext(ctx, "success to request", "columns", len(columns), "rows", len(rows))
return &plugin.RunQueryResponse{
Name: req.QueryName,
Query: fmt.Sprintf("GET %s", c.String()),
Columns: columns,
Rows: rows,
}, nil
}
func main() {
slog.SetDefault(slog.Default().With("plugin", "example-http-csv-plugin"))
plugin.ServePlugin(plugin.WithProviderPlugin(&Provider{
endpoints: make(map[string]*url.URL),
}))
}