Skip to content

Commit 6928786

Browse files
authored
feat(auditlog): add auditlog example (#3603)
* Add auditlog example - how to paginate the data of auditlog - how to detect reached rate limit relates to STACKITSDK-201
1 parent 35edb3d commit 6928786

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

examples/auditlog/auditlog.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
// To fetch all audit log items within a specified time range, we must implement pagination, because the api returns only
37+
// up to 100 elements per request. We store the result of each request in `allItems`. The response includes a cursor,
38+
// if more elements are available. This cursor must be used to get the next set of elements.
39+
// The api has a rate limit, which can be reached when all elements will be fetched with pagination or if you do multiple request.
40+
// The rate limit should be taken care in this case.
41+
var allItems []auditlog.AuditLogEntryResponse
42+
for {
43+
if err != nil {
44+
var oapiErr *oapierror.GenericOpenAPIError
45+
if errors.As(err, &oapiErr) {
46+
// Check if rate limit is reached
47+
if oapiErr.StatusCode == http.StatusTooManyRequests {
48+
// In case you want to fetch all items, you may want to wait some time and retry the request.
49+
// In this example we just stop here the pagination.
50+
fmt.Fprintf(os.Stderr, "[Auditlog API] Too Many Requests: %v\n", string(oapiErr.Body))
51+
break
52+
}
53+
}
54+
fmt.Fprintf(os.Stderr, "[Auditlog API] List project audit log entries: %v\n", err)
55+
os.Exit(1)
56+
}
57+
// Break loop when response has no items
58+
if result == nil || result.Items == nil || len(*result.Items) == 0 {
59+
break
60+
}
61+
62+
// Append items to allItems
63+
allItems = append(allItems, *result.Items...)
64+
65+
// If cursor is not set, end of logs is reached
66+
if result.Cursor == nil {
67+
fmt.Printf("[Auditlog API] Successfully fetched all items.\n")
68+
break
69+
}
70+
71+
// Paginate to the next set of items
72+
listProjectLogsReq = listProjectLogsReq.Cursor(*result.Cursor)
73+
result, err = listProjectLogsReq.Execute()
74+
}
75+
76+
fmt.Printf("[Auditlog API] Number of project audit log entries: %v\n", len(allItems))
77+
}

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+
)

examples/auditlog/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
2+
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
3+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
4+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
5+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
6+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
7+
github.com/stackitcloud/stackit-sdk-go/core v0.17.3 h1:GsZGmRRc/3GJLmCUnsZswirr5wfLRrwavbnL/renOqg=
8+
github.com/stackitcloud/stackit-sdk-go/core v0.17.3/go.mod h1:HBCXJGPgdRulplDzhrmwC+Dak9B/x0nzNtmOpu+1Ahg=
9+
github.com/stackitcloud/stackit-sdk-go/services/auditlog v0.1.0 h1:Y8qhIcp5gWlA0Tj58lSrS48lEB3gH2x56XCpCyHSVfc=
10+
github.com/stackitcloud/stackit-sdk-go/services/auditlog v0.1.0/go.mod h1:vwP9edf2elr3SLz3lH7y5vqhgKIdtfS/xN7wsPTxcwM=

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)