logs.go raw
1 package govultr
2
3 import (
4 "context"
5 "net/http"
6
7 "github.com/google/go-querystring/query"
8 )
9
10 const logsPath = "/v2/logs"
11
12 // LogsService is the interface to interact with the Logs endpoint
13 // Link: https://www.vultr.com/api/#tag/logs
14 type LogsService interface {
15 List(ctx context.Context, options LogsOptions) ([]Log, *LogsMeta, *http.Response, error)
16 }
17
18 // LogsServiceHandler handle interaction with the server methods for the Vultr API
19 type LogsServiceHandler struct {
20 client *Client
21 }
22
23 // Log represents a log entry
24 type Log struct {
25 ResourceID string `json:"resource_id"`
26 ResourceType string `json:"resource_type"`
27 Level string `json:"log_level"`
28 Message string `json:"message"`
29 Timestamp string `json:"timestamp"`
30 Metadata LogMetadata `json:"metadata"`
31 }
32
33 // LogMetadata represents a log entry's metadata
34 type LogMetadata struct {
35 UserID string `json:"user_id"`
36 IPAddress string `json:"ip_address"`
37 UserName string `json:"username"`
38 HTTPStatusCode int `json:"http_status_code"`
39 Method string `json:"method"`
40 RequestPath string `json:"request_path"`
41 RequestBody string `json:"request_body"`
42 QueryParameters string `json:"query_parameters"`
43 }
44
45 type logsBase struct {
46 Logs []Log `json:"logs"`
47 Meta LogsMeta `json:"meta"`
48 }
49
50 // LogsMeta represent pagination data for log entries
51 type LogsMeta struct {
52 ContinueTime string `json:"continue_time"`
53 ReturnedCount int `json:"returned_count"`
54 UnreturnedCount int `json:"unreturned_count"`
55 TotalCount int `json:"total_count"`
56 }
57
58 // LogsOptions represents the query params for the logs list
59 type LogsOptions struct {
60 StartTime string `url:"start_time"`
61 EndTime string `url:"end_time"`
62 LogLevel string `url:"log_level,omitempty"`
63 ResourceType string `url:"resource_type,omitempty"`
64 ResourceID string `url:"resource_id,omitempty"`
65 }
66
67 // List retrieves logs
68 func (l *LogsServiceHandler) List(ctx context.Context, options LogsOptions) ([]Log, *LogsMeta, *http.Response, error) { //nolint:gocritic,lll
69 req, err := l.client.NewRequest(ctx, http.MethodGet, logsPath, nil)
70 if err != nil {
71 return nil, nil, nil, err
72 }
73
74 params, err := query.Values(options)
75 if err != nil {
76 return nil, nil, nil, err
77 }
78
79 req.URL.RawQuery = params.Encode()
80
81 logs := new(logsBase)
82 resp, err := l.client.DoWithContext(ctx, req, logs)
83 if err != nil {
84 return nil, nil, resp, err
85 }
86
87 return logs.Logs, &logs.Meta, resp, nil
88 }
89