|
| 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 | +} |
0 commit comments