@@ -4,11 +4,41 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"net/http"
7
+ "net/url"
7
8
"time"
8
9
9
10
"github.com/pkg/errors"
10
11
)
11
12
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
+
12
42
var _ AuditLogsService = & auditlogsService {}
13
43
14
44
// AuditLogsService is an interface for communicating with the PlanetScale
@@ -21,6 +51,9 @@ type AuditLogsService interface {
21
51
// an organization.
22
52
type ListAuditLogsRequest struct {
23
53
Organization string
54
+
55
+ // Events can be used to filter out only the given audit log events.
56
+ Events []AuditLogEvent
24
57
}
25
58
26
59
// AuditLog represents a PlanetScale audit log.
@@ -68,7 +101,24 @@ func NewAuditLogsService(client *Client) *auditlogsService {
68
101
69
102
// List returns the audit logs for an organization.
70
103
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 )
72
122
if err != nil {
73
123
return nil , errors .Wrap (err , "error creating request for listing audit logs" )
74
124
}
0 commit comments