-
Notifications
You must be signed in to change notification settings - Fork 21
feat(auditlog): add auditlog example #3603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||
"github.com/stackitcloud/stackit-sdk-go/services/auditlog" | ||
) | ||
|
||
func main() { | ||
// Specify the project ID, startTime and endTime | ||
projectId := "PROJECT_ID" | ||
startTime := time.Now().Add(-time.Hour * 24) | ||
endTime := time.Now() | ||
limit := float32(100) // set pagination limit to avoid rate limit | ||
|
||
// Create a new API client, that uses default authentication and configuration | ||
auditlogClient, err := auditlog.NewAPIClient() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "[Auditlog API] Creating API client: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// List all audit logs of a project | ||
listProjectLogsReq := auditlogClient.ListProjectAuditLogEntries(context.Background(), projectId). | ||
StartTimeRange(startTime). | ||
EndTimeRange(endTime). | ||
Limit(limit) | ||
result, err := listProjectLogsReq.Execute() | ||
|
||
// To fetch all audit log items within a specified time range, we must implement pagination, because the api returns only | ||
// up to 100 elements per request. We store the result of each request in `allItems`. The response includes a cursor, | ||
// if more elements are available. This cursor must be used to get the next set of elements. | ||
// The api has a rate limit, which can be reached when all elements will be fetched with pagination or if you do multiple request. | ||
// The rate limit should be taken care in this case. | ||
var allItems []auditlog.AuditLogEntryResponse | ||
for { | ||
if err != nil { | ||
var oapiErr *oapierror.GenericOpenAPIError | ||
if errors.As(err, &oapiErr) { | ||
// Check if rate limit is reached | ||
if oapiErr.StatusCode == http.StatusTooManyRequests { | ||
rubenhoenle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// In case you want to fetch all items, you may want to wait some time and retry the request. | ||
// In this example we just stop here the pagination. | ||
fmt.Fprintf(os.Stderr, "[Auditlog API] Too Many Requests: %v\n", string(oapiErr.Body)) | ||
break | ||
} | ||
} | ||
fmt.Fprintf(os.Stderr, "[Auditlog API] List project audit log entries: %v\n", err) | ||
os.Exit(1) | ||
} | ||
// Break loop when response has no items | ||
if result == nil || result.Items == nil || len(*result.Items) == 0 { | ||
break | ||
} | ||
|
||
// Append items to allItems | ||
allItems = append(allItems, *result.Items...) | ||
|
||
// If cursor is not set, end of logs is reached | ||
if result.Cursor == nil { | ||
fmt.Printf("[Auditlog API] Successfully fetched all items.\n") | ||
break | ||
} | ||
|
||
// Paginate to the next set of items | ||
listProjectLogsReq = listProjectLogsReq.Cursor(*result.Cursor) | ||
result, err = listProjectLogsReq.Execute() | ||
} | ||
|
||
fmt.Printf("[Auditlog API] Number of project audit log entries: %v\n", len(allItems)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module auditlog | ||
|
||
go 1.21 | ||
|
||
require ( | ||
github.com/stackitcloud/stackit-sdk-go/core v0.17.3 | ||
github.com/stackitcloud/stackit-sdk-go/services/auditlog v0.1.0 | ||
) | ||
|
||
require ( | ||
github.com/golang-jwt/jwt/v5 v5.3.0 // indirect | ||
github.com/google/uuid v1.6.0 // indirect | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo= | ||
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= | ||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= | ||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= | ||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= | ||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/stackitcloud/stackit-sdk-go/core v0.17.3 h1:GsZGmRRc/3GJLmCUnsZswirr5wfLRrwavbnL/renOqg= | ||
github.com/stackitcloud/stackit-sdk-go/core v0.17.3/go.mod h1:HBCXJGPgdRulplDzhrmwC+Dak9B/x0nzNtmOpu+1Ahg= | ||
github.com/stackitcloud/stackit-sdk-go/services/auditlog v0.1.0 h1:Y8qhIcp5gWlA0Tj58lSrS48lEB3gH2x56XCpCyHSVfc= | ||
github.com/stackitcloud/stackit-sdk-go/services/auditlog v0.1.0/go.mod h1:vwP9edf2elr3SLz3lH7y5vqhgKIdtfS/xN7wsPTxcwM= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.