|
| 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