|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/stackitcloud/stackit-sdk-go/core/config" |
| 8 | + "github.com/stackitcloud/stackit-sdk-go/core/utils" |
| 9 | + "github.com/stackitcloud/stackit-sdk-go/services/logs" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + ctx := context.Background() |
| 14 | + |
| 15 | + projectId := "PROJECT_ID" // the uuid of your STACKIT project |
| 16 | + regionId := "eu01" |
| 17 | + |
| 18 | + client, err := logs.NewAPIClient( |
| 19 | + config.WithRegion(regionId), |
| 20 | + ) |
| 21 | + if err != nil { |
| 22 | + log.Fatalf("[Logs API] Creating API client: %v\n", err) |
| 23 | + } |
| 24 | + |
| 25 | + // Create a Logs Instance |
| 26 | + var createdInstance string |
| 27 | + createInstancePayload := logs.CreateLogsInstancePayload{ |
| 28 | + DisplayName: utils.Ptr("my-logs-instance"), |
| 29 | + RetentionDays: utils.Ptr(int64(1)), |
| 30 | + } |
| 31 | + createResp, err := client.CreateLogsInstance(ctx, projectId, regionId). |
| 32 | + CreateLogsInstancePayload(createInstancePayload). |
| 33 | + Execute() |
| 34 | + if err != nil { |
| 35 | + log.Fatalf("[Logs API] Error when calling `CreateLogsInstance`: %v\n", err) |
| 36 | + } |
| 37 | + createdInstance = *createResp.Id |
| 38 | + log.Printf("[Logs API] Created Logs Instance with ID \"%s\".\n", createdInstance) |
| 39 | + |
| 40 | + // List Logs Instances |
| 41 | + listResp, err := client.ListLogsInstances(ctx, projectId, regionId).Execute() |
| 42 | + if err != nil { |
| 43 | + log.Fatalf("[Logs API] Error when calling `ListLogsInstances`: %v\n", err) |
| 44 | + } |
| 45 | + log.Printf("[Logs API] Retrieved %d Logs Instances.\n", len(*listResp.Instances)) |
| 46 | + |
| 47 | + // Get the created Logs Instance |
| 48 | + getResp, err := client.GetLogsInstance(ctx, projectId, regionId, createdInstance).Execute() |
| 49 | + if err != nil { |
| 50 | + log.Fatalf("[Logs API] Error when calling `GetLogsInstance`: %v\n", err) |
| 51 | + } |
| 52 | + log.Printf("[Logs API] Retrieved Logs Instance with ID \"%s\" and Display Name \"%s\".\n", *getResp.Id, *getResp.DisplayName) |
| 53 | + |
| 54 | + // Update the created Logs Instance |
| 55 | + updatePayload := logs.UpdateLogsInstancePayload{ |
| 56 | + DisplayName: utils.Ptr("my-updated-logs-instance"), |
| 57 | + RetentionDays: utils.Ptr(int64(7)), |
| 58 | + } |
| 59 | + updateResp, err := client.UpdateLogsInstance(ctx, projectId, regionId, createdInstance). |
| 60 | + UpdateLogsInstancePayload(updatePayload). |
| 61 | + Execute() |
| 62 | + if err != nil { |
| 63 | + log.Fatalf("[Logs API] Error when calling `UpdateLogsInstance`: %v\n", err) |
| 64 | + } |
| 65 | + log.Printf("[Logs API] Updated Logs Instance with ID \"%s\" to Display Name \"%s\".\n", *updateResp.Id, *updateResp.DisplayName) |
| 66 | + |
| 67 | + // Create an Access Token |
| 68 | + createTokenPayload := logs.CreateAccessTokenPayload{ |
| 69 | + DisplayName: utils.Ptr("my-access-token"), |
| 70 | + Permissions: &[]string{"read"}, |
| 71 | + } |
| 72 | + createTokenResp, err := client.CreateAccessToken(ctx, projectId, regionId, createdInstance). |
| 73 | + CreateAccessTokenPayload(createTokenPayload). |
| 74 | + Execute() |
| 75 | + if err != nil { |
| 76 | + log.Fatalf("[Logs API] Error when calling `CreateAccessToken`: %v\n", err) |
| 77 | + } |
| 78 | + log.Printf("[Logs API] Created Access Token with ID \"%s\".\n", *createTokenResp.Id) |
| 79 | + |
| 80 | + // Add Access Token to Logs Instance |
| 81 | + err = client.UpdateAccessToken(ctx, projectId, regionId, createdInstance, *createTokenResp.Id). |
| 82 | + // needs at least an empty payload |
| 83 | + UpdateAccessTokenPayload(logs.UpdateAccessTokenPayload{}). |
| 84 | + Execute() |
| 85 | + if err != nil { |
| 86 | + log.Fatalf("[Logs API] Error when calling `UpdateAccessToken`: %v\n", err) |
| 87 | + } |
| 88 | + |
| 89 | + // Delete all Access Tokens from Logs Instance |
| 90 | + tokenList, err := client.DeleteAllAccessTokens(ctx, projectId, regionId, createdInstance).Execute() |
| 91 | + if err != nil { |
| 92 | + log.Fatalf("[Logs API] Error when calling `DeleteAllAccessTokens`: %v\n", err) |
| 93 | + } |
| 94 | + log.Printf("[Logs API] Deleted %d Access Tokens from Logs Instance with ID \"%s\".\n", len(*tokenList.Tokens), createdInstance) |
| 95 | + |
| 96 | + // Delete the created Logs Instance |
| 97 | + err = client.DeleteLogsInstance(ctx, projectId, regionId, createdInstance).Execute() |
| 98 | + if err != nil { |
| 99 | + log.Fatalf("[Logs API] Error when calling `DeleteLogsInstance`: %v\n", err) |
| 100 | + } |
| 101 | + log.Printf("[Logs API] Deleted Logs Instance with ID \"%s\".\n", createdInstance) |
| 102 | +} |
0 commit comments