Skip to content

Commit ad3573f

Browse files
committed
Merge branch 'romain-pix-cyber-ovh-logs'.
2 parents 2d1c884 + 69befee commit ad3573f

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

docs/tables/ovh_log_self.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Table: ovh_log_self
2+
3+
Get the logs from recent API calls made from your account.
4+
5+
The `ovh_log_self` table can be used to query information about your recent API calls.
6+
7+
## Examples
8+
9+
### List logs
10+
11+
```sql
12+
select
13+
id,
14+
date,
15+
account
16+
from
17+
ovh_log_self;
18+
```
19+
20+
### Get a log
21+
22+
```sql
23+
select
24+
id,
25+
date,
26+
account
27+
from
28+
ovh_log_self
29+
where
30+
id = 'XXXXXX';
31+
```

ovh/plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
3434
"ovh_cloud_storage_swift": tableOvhCloudStorageSwift(),
3535
"ovh_cloud_volume": tableOvhCloudVolume(),
3636
"ovh_cloud_volume_snapshot": tableOvhCloudVolumeSnapshot(),
37+
"ovh_log_self": tableOvhLog(),
3738
"ovh_refund": tableOvhRefund(),
3839
"ovh_refund_detail": tableOvhRefundDetails(),
3940
},

ovh/table_ovh_log_self.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package ovh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strconv"
7+
"time"
8+
9+
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
10+
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
11+
)
12+
13+
type Log struct {
14+
ID int `json:"logId"`
15+
Date time.Time `json:"date"`
16+
Account string `json:"account"`
17+
IP string `json:"ip"`
18+
Method string `json:"method"`
19+
Route string `json:"route"`
20+
Path string `json:"path"`
21+
}
22+
23+
func tableOvhLog() *plugin.Table {
24+
return &plugin.Table{
25+
Name: "ovh_log_self",
26+
Description: "Logs of your account.",
27+
List: &plugin.ListConfig{
28+
Hydrate: listLog,
29+
},
30+
Get: &plugin.GetConfig{
31+
KeyColumns: plugin.AllColumns([]string{"id"}),
32+
Hydrate: getLog,
33+
},
34+
HydrateConfig: []plugin.HydrateConfig{
35+
{Func: getLogInfo},
36+
},
37+
Columns: []*plugin.Column{
38+
{
39+
Name: "id",
40+
Type: proto.ColumnType_STRING,
41+
Description: "ID of the log.",
42+
},
43+
{
44+
Name: "date",
45+
Hydrate: getLogInfo,
46+
Type: proto.ColumnType_TIMESTAMP,
47+
Description: "Date of the log.",
48+
},
49+
{
50+
Name: "account",
51+
Hydrate: getLogInfo,
52+
Type: proto.ColumnType_STRING,
53+
Description: "User performing the action.",
54+
},
55+
{
56+
Name: "ip",
57+
Hydrate: getLogInfo,
58+
Type: proto.ColumnType_STRING,
59+
Description: "Origin IP of the action.",
60+
},
61+
{
62+
Name: "method",
63+
Hydrate: getLogInfo,
64+
Type: proto.ColumnType_STRING,
65+
Description: "Method requested.",
66+
},
67+
{
68+
Name: "route",
69+
Hydrate: getLogInfo,
70+
Type: proto.ColumnType_STRING,
71+
Description: "Route used for the action.",
72+
},
73+
{
74+
Name: "path",
75+
Hydrate: getLogInfo,
76+
Type: proto.ColumnType_STRING,
77+
Description: "Path used for the action with project and object IDs.",
78+
},
79+
},
80+
}
81+
}
82+
83+
func getLogInfo(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
84+
log := h.Item.(Log)
85+
86+
client, err := connect(ctx, d)
87+
if err != nil {
88+
plugin.Logger(ctx).Error("ovh_logs_self.getLogInfo", "connection_error", err)
89+
return nil, err
90+
}
91+
92+
err = client.Get(fmt.Sprintf("/me/api/logs/self/%s", strconv.Itoa(log.ID)), &log)
93+
94+
if err != nil {
95+
plugin.Logger(ctx).Error("ovh_logs_self.getLogInfo", err)
96+
return nil, err
97+
}
98+
99+
return log, nil
100+
}
101+
102+
func listLog(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
103+
client, err := connect(ctx, d)
104+
if err != nil {
105+
plugin.Logger(ctx).Error("ovh_logs_self.listLog", "connection_error", err)
106+
return nil, err
107+
}
108+
109+
var logsId []int
110+
err = client.Get("/me/api/logs/self", &logsId)
111+
112+
if err != nil {
113+
plugin.Logger(ctx).Error("ovh_logs_self.listLog", err)
114+
return nil, err
115+
}
116+
117+
for _, logId := range logsId {
118+
var log Log
119+
log.ID = logId
120+
d.StreamListItem(ctx, log)
121+
}
122+
123+
return nil, nil
124+
}
125+
126+
func getLog(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
127+
strId := d.EqualsQuals["id"].GetStringValue()
128+
var log Log
129+
intId, err := strconv.Atoi(strId)
130+
if err != nil {
131+
return nil, err
132+
}
133+
log.ID = intId
134+
return log, nil
135+
}

0 commit comments

Comments
 (0)