Skip to content

Commit 3d5ec2a

Browse files
committed
Add auditlog example
- how to paginate the data of auditlog - how to detect reached rate limit
1 parent 35edb3d commit 3d5ec2a

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

examples/auditlog/auditlog.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"net/http"
8+
"os"
9+
"time"
10+
11+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
12+
"github.com/stackitcloud/stackit-sdk-go/services/auditlog"
13+
)
14+
15+
func main() {
16+
// Specify the project ID, startTime and endTIme
17+
projectId := "PROJECT_ID"
18+
startTime := time.Now().Add(-time.Hour * 24)
19+
endTime := time.Now()
20+
limit := float32(100) // set pagination limit to avoid rate limit
21+
22+
// Create a new API client, that uses default authentication and configuration
23+
auditlogClient, err := auditlog.NewAPIClient()
24+
if err != nil {
25+
fmt.Fprintf(os.Stderr, "[Auditlog API] Creating API client: %v\n", err)
26+
os.Exit(1)
27+
}
28+
29+
// List all audit logs of a project
30+
listProjectLogsReq := auditlogClient.ListProjectAuditLogEntries(context.Background(), projectId).
31+
StartTimeRange(startTime).
32+
EndTimeRange(endTime).
33+
Limit(limit)
34+
result, err := listProjectLogsReq.Execute()
35+
36+
var allItems []auditlog.AuditLogEntryResponse
37+
for {
38+
if err != nil {
39+
var oapiErr *oapierror.GenericOpenAPIError
40+
if errors.As(err, &oapiErr) {
41+
// reached rate limit
42+
if oapiErr.StatusCode == http.StatusTooManyRequests {
43+
fmt.Fprintf(os.Stderr, "[Auditlog API] Too Many Requests: %v\n", string(oapiErr.Body))
44+
break
45+
}
46+
}
47+
fmt.Fprintf(os.Stderr, "[Auditlog API] List project audit log entries: %v\n", err)
48+
os.Exit(1)
49+
}
50+
// Break loop when response has no items
51+
if result == nil || result.Items == nil || len(*result.Items) == 0 {
52+
break
53+
}
54+
55+
// Append items to allItems
56+
allItems = append(allItems, *result.Items...)
57+
58+
// If cursor is not set, end of logs is reached
59+
if result.Cursor == nil {
60+
fmt.Printf("[Auditlog API] Successfully all items.\n")
61+
break
62+
}
63+
64+
// Paginate to the next set of items
65+
listProjectLogsReq = listProjectLogsReq.Cursor(*result.Cursor)
66+
result, err = listProjectLogsReq.Execute()
67+
}
68+
69+
fmt.Printf("[Auditlog API] Number of project audit log entries: %v\n", len(allItems))
70+
}

examples/auditlog/go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module auditlog
2+
3+
go 1.21
4+
5+
require (
6+
github.com/stackitcloud/stackit-sdk-go/core v0.17.3
7+
github.com/stackitcloud/stackit-sdk-go/services/auditlog v0.1.0
8+
)
9+
10+
require (
11+
github.com/golang-jwt/jwt/v5 v5.3.0 // indirect
12+
github.com/google/uuid v1.6.0 // indirect
13+
)

go.work

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ go 1.21
22

33
use (
44
./core
5+
./examples/auditlog
56
./examples/authentication
67
./examples/authorization
78
./examples/backgroundrefresh

0 commit comments

Comments
 (0)