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