diff --git a/docs/tables/ovh_log_self.md b/docs/tables/ovh_log_self.md new file mode 100644 index 0000000..60e114a --- /dev/null +++ b/docs/tables/ovh_log_self.md @@ -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'; +``` diff --git a/ovh/plugin.go b/ovh/plugin.go index d184f07..cc944b9 100644 --- a/ovh/plugin.go +++ b/ovh/plugin.go @@ -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(), }, diff --git a/ovh/table_ovh_log_self.go b/ovh/table_ovh_log_self.go new file mode 100644 index 0000000..b2a8e59 --- /dev/null +++ b/ovh/table_ovh_log_self.go @@ -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 +}