Skip to content

Commit 30ff561

Browse files
committed
auditlogs: add Events filtering
1 parent 8b8b82e commit 30ff561

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

planetscale/audit_logs.go

+51-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,41 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"net/url"
78
"time"
89

910
"github.com/pkg/errors"
1011
)
1112

13+
// AuditLogEvent represents an audit log's event type
14+
type AuditLogEvent string
15+
16+
const (
17+
AuditLogEventBranchCreated AuditLogEvent = "branch.created"
18+
AuditLogEventBranchDeleted AuditLogEvent = "branch.deleted"
19+
AuditLogEventDatabaseCreated AuditLogEvent = "database.created"
20+
AuditLogEventDatabaseDeleted AuditLogEvent = "database.deleted"
21+
AuditLogEventDeployRequestApproved AuditLogEvent = "deploy_request.approved"
22+
AuditLogEventDeployRequestClosed AuditLogEvent = "deploy_request.closed"
23+
AuditLogEventDeployRequestCreated AuditLogEvent = "deploy_request.created"
24+
AuditLogEventDeployRequestDeleted AuditLogEvent = "deploy_request.deleted"
25+
AuditLogEventDeployRequestQueued AuditLogEvent = "deploy_request.queued"
26+
AuditLogEventDeployRequestUnqueued AuditLogEvent = "deploy_request.unqueued"
27+
AuditLogEventIntegrationCreated AuditLogEvent = "integration.created"
28+
AuditLogEventIntegrationDeleted AuditLogEvent = "integration.deleted"
29+
AuditLogEventOrganizationInvitationCreated AuditLogEvent = "organization_invitation.created"
30+
AuditLogEventOrganizationInvitationDeleted AuditLogEvent = "organization_invitation.deleted"
31+
AuditLogEventOrganizationMembershipCreated AuditLogEvent = "organization_membership.created"
32+
AuditLogEventOrganizationJoined AuditLogEvent = "organization.joined"
33+
AuditLogEventOrganizationRemovedMember AuditLogEvent = "organization.removed_member"
34+
AuditLogEventOrganizationDisabledSSO AuditLogEvent = "organization.disabled_sso"
35+
AuditLogEventOrganizationEnabledSSO AuditLogEvent = "organization.enabled_sso"
36+
AuditLogEventOrganizationUpdatedRole AuditLogEvent = "organization.updated_role"
37+
AuditLogEventServiceTokenCreated AuditLogEvent = "service_token.created"
38+
AuditLogEventServiceTokenDeleted AuditLogEvent = "service_token.deleted"
39+
AuditLogEventServiceTokenGrantedAccess AuditLogEvent = "service_token.granted_access"
40+
)
41+
1242
var _ AuditLogsService = &auditlogsService{}
1343

1444
// AuditLogsService is an interface for communicating with the PlanetScale
@@ -21,6 +51,9 @@ type AuditLogsService interface {
2151
// an organization.
2252
type ListAuditLogsRequest struct {
2353
Organization string
54+
55+
// Events can be used to filter out only the given audit log events.
56+
Events []AuditLogEvent
2457
}
2558

2659
// AuditLog represents a PlanetScale audit log.
@@ -68,7 +101,24 @@ func NewAuditLogsService(client *Client) *auditlogsService {
68101

69102
// List returns the audit logs for an organization.
70103
func (o *auditlogsService) List(ctx context.Context, listReq *ListAuditLogsRequest) ([]*AuditLog, error) {
71-
req, err := o.client.newRequest(http.MethodGet, auditlogsAPIPath(listReq.Organization), nil)
104+
if listReq.Organization == "" {
105+
return nil, errors.New("organization is not set")
106+
}
107+
108+
path := auditlogsAPIPath(listReq.Organization)
109+
110+
v := url.Values{}
111+
if len(listReq.Events) != 0 {
112+
for _, action := range listReq.Events {
113+
v.Add("filters[]", fmt.Sprintf("audit_action:%s", action))
114+
}
115+
}
116+
117+
if vals := v.Encode(); vals != "" {
118+
path += "?" + vals
119+
}
120+
121+
req, err := o.client.newRequest(http.MethodGet, path, nil)
72122
if err != nil {
73123
return nil, errors.Wrap(err, "error creating request for listing audit logs")
74124
}

planetscale/audit_logs_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ func TestAuditLogs_List(t *testing.T) {
5656

5757
auditLogs, err := client.AuditLogs.List(ctx, &ListAuditLogsRequest{
5858
Organization: testOrg,
59+
Events: []AuditLogEvent{
60+
AuditLogEventBranchDeleted,
61+
AuditLogEventOrganizationJoined,
62+
},
5963
})
6064

6165
want := []*AuditLog{

planetscale/integration_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,17 @@ func TestIntegration_AuditLogs_List(t *testing.T) {
129129

130130
auditLogs, err := client.AuditLogs.List(ctx, &ListAuditLogsRequest{
131131
Organization: org,
132+
Events: []AuditLogEvent{
133+
AuditLogEventBranchDeleted,
134+
AuditLogEventOrganizationJoined,
135+
},
132136
})
133137
if err != nil {
134138
t.Fatalf("get audit logs failed: %s", err)
135139
}
136140

137141
for _, l := range auditLogs {
138-
fmt.Printf("l = %+v\n", l)
142+
fmt.Printf("l. = %+v\n", l.AuditAction)
139143
}
140144
fmt.Printf("len(auditLogs) = %+v\n", len(auditLogs))
141145
}

0 commit comments

Comments
 (0)