mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
feat: audit log api (#3898)
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
package codersdk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
@ -29,9 +31,9 @@ const (
|
||||
type AuditDiff map[string]AuditDiffField
|
||||
|
||||
type AuditDiffField struct {
|
||||
Old any
|
||||
New any
|
||||
Secret bool
|
||||
Old any `json:"old,omitempty"`
|
||||
New any `json:"new,omitempty"`
|
||||
Secret bool `json:"secret"`
|
||||
}
|
||||
|
||||
type AuditLog struct {
|
||||
@ -54,3 +56,67 @@ type AuditLog struct {
|
||||
|
||||
User *User `json:"user"`
|
||||
}
|
||||
|
||||
type AuditLogResponse struct {
|
||||
AuditLogs []AuditLog `json:"audit_logs"`
|
||||
}
|
||||
|
||||
type AuditLogCountResponse struct {
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
|
||||
// AuditLogs retrieves audit logs from the given page.
|
||||
func (c *Client) AuditLogs(ctx context.Context, page Pagination) (AuditLogResponse, error) {
|
||||
res, err := c.Request(ctx, http.MethodGet, "/api/v2/audit", nil, page.asRequestOption())
|
||||
if err != nil {
|
||||
return AuditLogResponse{}, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return AuditLogResponse{}, readBodyAsError(res)
|
||||
}
|
||||
|
||||
var logRes AuditLogResponse
|
||||
err = json.NewDecoder(res.Body).Decode(&logRes)
|
||||
if err != nil {
|
||||
return AuditLogResponse{}, err
|
||||
}
|
||||
|
||||
return logRes, nil
|
||||
}
|
||||
|
||||
// AuditLogCount returns the count of all audit logs in the product.
|
||||
func (c *Client) AuditLogCount(ctx context.Context) (AuditLogCountResponse, error) {
|
||||
res, err := c.Request(ctx, http.MethodGet, "/api/v2/audit/count", nil)
|
||||
if err != nil {
|
||||
return AuditLogCountResponse{}, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return AuditLogCountResponse{}, readBodyAsError(res)
|
||||
}
|
||||
|
||||
var logRes AuditLogCountResponse
|
||||
err = json.NewDecoder(res.Body).Decode(&logRes)
|
||||
if err != nil {
|
||||
return AuditLogCountResponse{}, err
|
||||
}
|
||||
|
||||
return logRes, nil
|
||||
}
|
||||
|
||||
func (c *Client) CreateTestAuditLog(ctx context.Context) error {
|
||||
res, err := c.Request(ctx, http.MethodPost, "/api/v2/audit/testgenerate", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusNoContent {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user