-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudit_log.go
102 lines (85 loc) · 3.17 KB
/
audit_log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package database
import (
"context"
"github.com/google/uuid"
"github.com/nais/api/internal/auditlogger/audittype"
"github.com/nais/api/internal/database/gensql"
"github.com/nais/api/internal/slug"
)
type AuditLogsRepo interface {
CreateAuditLogEntry(ctx context.Context, correlationID uuid.UUID, actor *string, targetType audittype.AuditLogsTargetType, targetIdentifier string, action audittype.AuditAction, message string) error
GetAuditLogsForCorrelationID(ctx context.Context, correlationID uuid.UUID, p Page) ([]*AuditLog, int, error)
GetAuditLogsForReconciler(ctx context.Context, reconcilerName string, p Page) ([]*AuditLog, int, error)
GetAuditLogsForTeam(ctx context.Context, teamSlug slug.Slug, p Page) ([]*AuditLog, int, error)
}
var _ AuditLogsRepo = (*database)(nil)
type AuditLog struct {
*gensql.AuditLog
}
func (d *database) GetAuditLogsForTeam(ctx context.Context, teamSlug slug.Slug, p Page) ([]*AuditLog, int, error) {
rows, err := d.querier.GetAuditLogsForTeam(ctx, gensql.GetAuditLogsForTeamParams{
TargetIdentifier: string(teamSlug),
Offset: int32(p.Offset),
Limit: int32(p.Limit),
})
if err != nil {
return nil, 0, err
}
entries := make([]*AuditLog, len(rows))
for i, row := range rows {
entries[i] = &AuditLog{AuditLog: row}
}
total, err := d.querier.GetAuditLogsForTeamCount(ctx, string(teamSlug))
if err != nil {
return nil, 0, err
}
return entries, int(total), nil
}
func (d *database) GetAuditLogsForReconciler(ctx context.Context, reconcilerName string, p Page) ([]*AuditLog, int, error) {
rows, err := d.querier.GetAuditLogsForReconciler(ctx, gensql.GetAuditLogsForReconcilerParams{
TargetIdentifier: reconcilerName,
Offset: int32(p.Offset),
Limit: int32(p.Limit),
})
if err != nil {
return nil, 0, err
}
entries := make([]*AuditLog, len(rows))
for i, row := range rows {
entries[i] = &AuditLog{AuditLog: row}
}
total, err := d.querier.GetAuditLogsForReconcilerCount(ctx, reconcilerName)
if err != nil {
return nil, 0, err
}
return entries, int(total), nil
}
func (d *database) CreateAuditLogEntry(ctx context.Context, correlationID uuid.UUID, actor *string, targetType audittype.AuditLogsTargetType, targetIdentifier string, action audittype.AuditAction, message string) error {
return d.querier.CreateAuditLog(ctx, gensql.CreateAuditLogParams{
CorrelationID: correlationID,
Actor: actor,
TargetType: string(targetType),
TargetIdentifier: targetIdentifier,
Action: string(action),
Message: message,
})
}
func (d *database) GetAuditLogsForCorrelationID(ctx context.Context, correlationID uuid.UUID, p Page) ([]*AuditLog, int, error) {
rows, err := d.querier.GetAuditLogsForCorrelationID(ctx, gensql.GetAuditLogsForCorrelationIDParams{
CorrelationID: correlationID,
Offset: int32(p.Offset),
Limit: int32(p.Limit),
})
if err != nil {
return nil, 0, err
}
entries := make([]*AuditLog, len(rows))
for i, row := range rows {
entries[i] = &AuditLog{AuditLog: row}
}
total, err := d.querier.GetAuditLogsForCorrelationIDCount(ctx, correlationID)
if err != nil {
return nil, 0, err
}
return entries, int(total), nil
}