Skip to content

Commit

Permalink
Merge branch 'romain-pix-cyber-ovh-logs'.
Browse files Browse the repository at this point in the history
  • Loading branch information
francois2metz committed Dec 29, 2024
2 parents 2d1c884 + 69befee commit ad3573f
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/tables/ovh_log_self.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Table: ovh_log_self

Get the logs from recent API calls made from your account.

The `ovh_log_self` table can be used to query information about your recent API calls.

## Examples

### List logs

```sql
select
id,
date,
account
from
ovh_log_self;
```

### Get a log

```sql
select
id,
date,
account
from
ovh_log_self
where
id = 'XXXXXX';
```
1 change: 1 addition & 0 deletions ovh/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"ovh_cloud_storage_swift": tableOvhCloudStorageSwift(),
"ovh_cloud_volume": tableOvhCloudVolume(),
"ovh_cloud_volume_snapshot": tableOvhCloudVolumeSnapshot(),
"ovh_log_self": tableOvhLog(),
"ovh_refund": tableOvhRefund(),
"ovh_refund_detail": tableOvhRefundDetails(),
},
Expand Down
135 changes: 135 additions & 0 deletions ovh/table_ovh_log_self.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package ovh

import (
"context"
"fmt"
"strconv"
"time"

"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
)

type Log struct {
ID int `json:"logId"`
Date time.Time `json:"date"`
Account string `json:"account"`
IP string `json:"ip"`
Method string `json:"method"`
Route string `json:"route"`
Path string `json:"path"`
}

func tableOvhLog() *plugin.Table {
return &plugin.Table{
Name: "ovh_log_self",
Description: "Logs of your account.",
List: &plugin.ListConfig{
Hydrate: listLog,
},
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"id"}),
Hydrate: getLog,
},
HydrateConfig: []plugin.HydrateConfig{
{Func: getLogInfo},
},
Columns: []*plugin.Column{
{
Name: "id",
Type: proto.ColumnType_STRING,
Description: "ID of the log.",
},
{
Name: "date",
Hydrate: getLogInfo,
Type: proto.ColumnType_TIMESTAMP,
Description: "Date of the log.",
},
{
Name: "account",
Hydrate: getLogInfo,
Type: proto.ColumnType_STRING,
Description: "User performing the action.",
},
{
Name: "ip",
Hydrate: getLogInfo,
Type: proto.ColumnType_STRING,
Description: "Origin IP of the action.",
},
{
Name: "method",
Hydrate: getLogInfo,
Type: proto.ColumnType_STRING,
Description: "Method requested.",
},
{
Name: "route",
Hydrate: getLogInfo,
Type: proto.ColumnType_STRING,
Description: "Route used for the action.",
},
{
Name: "path",
Hydrate: getLogInfo,
Type: proto.ColumnType_STRING,
Description: "Path used for the action with project and object IDs.",
},
},
}
}

func getLogInfo(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
log := h.Item.(Log)

client, err := connect(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("ovh_logs_self.getLogInfo", "connection_error", err)
return nil, err
}

err = client.Get(fmt.Sprintf("/me/api/logs/self/%s", strconv.Itoa(log.ID)), &log)

if err != nil {
plugin.Logger(ctx).Error("ovh_logs_self.getLogInfo", err)
return nil, err
}

return log, nil
}

func listLog(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
client, err := connect(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("ovh_logs_self.listLog", "connection_error", err)
return nil, err
}

var logsId []int
err = client.Get("/me/api/logs/self", &logsId)

if err != nil {
plugin.Logger(ctx).Error("ovh_logs_self.listLog", err)
return nil, err
}

for _, logId := range logsId {
var log Log
log.ID = logId
d.StreamListItem(ctx, log)
}

return nil, nil
}

func getLog(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
strId := d.EqualsQuals["id"].GetStringValue()
var log Log
intId, err := strconv.Atoi(strId)
if err != nil {
return nil, err
}
log.ID = intId
return log, nil
}

0 comments on commit ad3573f

Please sign in to comment.