Skip to content

Commit 59946d1

Browse files
committed
feat(logs) add example for logs API
1 parent 89bde19 commit 59946d1

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

examples/logs/go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/stackitcloud/stackit-sdk-go/examples/logs
2+
3+
go 1.21
4+
5+
require (
6+
github.com/stackitcloud/stackit-sdk-go/core v0.20.1
7+
github.com/stackitcloud/stackit-sdk-go/services/logs 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/logs/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.20.1 h1:odiuhhRXmxvEvnVTeZSN9u98edvw2Cd3DcnkepncP3M=
8+
github.com/stackitcloud/stackit-sdk-go/core v0.20.1/go.mod h1:fqto7M82ynGhEnpZU6VkQKYWYoFG5goC076JWXTUPRQ=
9+
github.com/stackitcloud/stackit-sdk-go/services/logs v0.1.0 h1:Fck91pz2Oxk8dUd2lOdnsIMWSCzBzAHJp7ivqAJ59is=
10+
github.com/stackitcloud/stackit-sdk-go/services/logs v0.1.0/go.mod h1:VM+++rhzI2/lvhyGKg0FCiEfnrADWykcdHLbECrl6T0=

examples/logs/logs.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}

go.work

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use (
1414
./examples/kms
1515
./examples/loadbalancer
1616
./examples/logme
17+
./examples/logs
1718
./examples/mariadb
1819
./examples/middleware
1920
./examples/mongodbflex

0 commit comments

Comments
 (0)