-
Notifications
You must be signed in to change notification settings - Fork 24
feat(logs) add example for logs API #4068
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
9 commits
Select commit
Hold shift + click to select a range
59946d1
feat(logs) add example for logs API
cgoetz-inovex ec08953
feat(logs) add waiters for logs API
cgoetz-inovex f99b57b
Update services/logs/wait/wait_test.go
cgoetz-inovex 6dcd541
Update services/logs/wait/wait_test.go
cgoetz-inovex e1685f6
Update services/logs/wait/wait_test.go
cgoetz-inovex 60daf3a
fix(logs) fix invalid refs after rename in GH UI
cgoetz-inovex 09cc812
update changelogs
GokceGK d88725f
Merge branch 'main' into feat/STACKITSDK-281-logs-service
GokceGK 1bf3f01
bump version from 0.2.0 to 0.3.0
GokceGK 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
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 github.com/stackitcloud/stackit-sdk-go/examples/logs | ||
|
|
||
| go 1.21 | ||
|
|
||
| require ( | ||
| github.com/stackitcloud/stackit-sdk-go/core v0.20.1 | ||
| github.com/stackitcloud/stackit-sdk-go/services/logs 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.20.1 h1:odiuhhRXmxvEvnVTeZSN9u98edvw2Cd3DcnkepncP3M= | ||
| github.com/stackitcloud/stackit-sdk-go/core v0.20.1/go.mod h1:fqto7M82ynGhEnpZU6VkQKYWYoFG5goC076JWXTUPRQ= | ||
| github.com/stackitcloud/stackit-sdk-go/services/logs v0.1.0 h1:Fck91pz2Oxk8dUd2lOdnsIMWSCzBzAHJp7ivqAJ59is= | ||
| github.com/stackitcloud/stackit-sdk-go/services/logs v0.1.0/go.mod h1:VM+++rhzI2/lvhyGKg0FCiEfnrADWykcdHLbECrl6T0= |
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,102 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
|
|
||
| "github.com/stackitcloud/stackit-sdk-go/core/config" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/utils" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/logs" | ||
| ) | ||
|
|
||
| func main() { | ||
| ctx := context.Background() | ||
|
|
||
| projectId := "PROJECT_ID" // the uuid of your STACKIT project | ||
| regionId := "eu01" | ||
|
|
||
| client, err := logs.NewAPIClient( | ||
| config.WithRegion(regionId), | ||
| ) | ||
| if err != nil { | ||
| log.Fatalf("[Logs API] Creating API client: %v\n", err) | ||
| } | ||
|
|
||
| // Create a Logs Instance | ||
| var createdInstance string | ||
| createInstancePayload := logs.CreateLogsInstancePayload{ | ||
| DisplayName: utils.Ptr("my-logs-instance"), | ||
| RetentionDays: utils.Ptr(int64(1)), | ||
| } | ||
| createResp, err := client.CreateLogsInstance(ctx, projectId, regionId). | ||
| CreateLogsInstancePayload(createInstancePayload). | ||
| Execute() | ||
| if err != nil { | ||
| log.Fatalf("[Logs API] Error when calling `CreateLogsInstance`: %v\n", err) | ||
| } | ||
| createdInstance = *createResp.Id | ||
| log.Printf("[Logs API] Created Logs Instance with ID \"%s\".\n", createdInstance) | ||
|
|
||
| // List Logs Instances | ||
| listResp, err := client.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.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(int64(7)), | ||
| } | ||
| updateResp, err := client.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: utils.Ptr("my-access-token"), | ||
| Permissions: &[]string{"read"}, | ||
| } | ||
| createTokenResp, err := client.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.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.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.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) | ||
| } |
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
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
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| v0.2.0 | ||
| v0.3.0 |
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
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,57 @@ | ||
| package wait | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/wait" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/logs" | ||
| ) | ||
|
|
||
| type APIClientInterface interface { | ||
| GetLogsInstanceExecute(ctx context.Context, projectId string, regionId string, instanceId string) (*logs.LogsInstance, error) | ||
| } | ||
|
|
||
| func CreateLogsInstanceWaitHandler(ctx context.Context, client APIClientInterface, projectID, region, instanceID string) *wait.AsyncActionHandler[logs.LogsInstance] { | ||
| handler := wait.New(func() (waitFinished bool, response *logs.LogsInstance, err error) { | ||
| instance, err := client.GetLogsInstanceExecute(ctx, projectID, region, instanceID) | ||
| if err != nil { | ||
| return false, nil, err | ||
| } | ||
| if instance.Id == nil || instance.Status == nil { | ||
| return false, nil, fmt.Errorf("get instance, project: %q, region: %q, instanceID: %q: missing id or status", projectID, region, instanceID) | ||
| } | ||
| if *instance.Id == instanceID && *instance.Status == logs.LOGSINSTANCESTATUS_ACTIVE { | ||
| return true, instance, nil | ||
| } | ||
| if *instance.Status == logs.LOGSINSTANCESTATUS_DELETING { | ||
| return true, nil, fmt.Errorf("creating log instance failed, instance is being deleted") | ||
| } | ||
| return false, nil, nil | ||
| }) | ||
| handler.SetTimeout(10 * time.Minute) | ||
| return handler | ||
| } | ||
|
|
||
| func DeleteLogsInstanceWaitHandler(ctx context.Context, client APIClientInterface, projectID, region, instanceID string) *wait.AsyncActionHandler[logs.LogsInstance] { | ||
| handler := wait.New(func() (waitFinished bool, response *logs.LogsInstance, err error) { | ||
| _, err = client.GetLogsInstanceExecute(ctx, projectID, region, instanceID) | ||
| // the instances is still gettable, e.g. not deleted, when the errors is null | ||
| if err == nil { | ||
| return false, nil, nil | ||
| } | ||
| var oapiError *oapierror.GenericOpenAPIError | ||
| if errors.As(err, &oapiError) { | ||
| if statusCode := oapiError.StatusCode; statusCode == http.StatusNotFound { | ||
| return true, nil, nil | ||
| } | ||
| } | ||
| return false, nil, err | ||
| }) | ||
| handler.SetTimeout(10 * time.Minute) | ||
| return handler | ||
| } | ||
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,169 @@ | ||
| package wait | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/uuid" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/utils" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/logs" | ||
| ) | ||
|
|
||
| type apiClientMock struct { | ||
| getFails bool | ||
| returnInstance bool | ||
| statusCode int | ||
| getLogsResponse *logs.LogsInstance | ||
| } | ||
|
|
||
| func (a *apiClientMock) GetLogsInstanceExecute(_ context.Context, _, _, _ string) (*logs.LogsInstance, error) { | ||
| if a.getFails { | ||
| return nil, &oapierror.GenericOpenAPIError{ | ||
| StatusCode: a.statusCode, | ||
| } | ||
| } | ||
| if !a.returnInstance { | ||
| return nil, nil | ||
| } | ||
| return a.getLogsResponse, nil | ||
| } | ||
|
|
||
| var projectId = uuid.NewString() | ||
| var instanceId = uuid.NewString() | ||
|
|
||
| const region = "eu01" | ||
|
|
||
| func TestCreateLogsInstanceWaitHandler(t *testing.T) { | ||
| tests := []struct { | ||
| description string | ||
| getFails bool | ||
| wantErr bool | ||
| wantResp bool | ||
| returnInstance bool | ||
| getLogsResponse *logs.LogsInstance | ||
| }{ | ||
| { | ||
| description: "create succeeded", | ||
| getFails: false, | ||
| wantErr: false, | ||
| wantResp: true, | ||
| returnInstance: true, | ||
| getLogsResponse: &logs.LogsInstance{ | ||
| Id: utils.Ptr(instanceId), | ||
| Status: utils.Ptr(logs.LOGSINSTANCESTATUS_ACTIVE), | ||
| }, | ||
| }, | ||
| { | ||
| description: "create failed with error", | ||
| getFails: true, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| returnInstance: true, | ||
| getLogsResponse: &logs.LogsInstance{ | ||
| Id: utils.Ptr(instanceId), | ||
| Status: utils.Ptr(logs.LOGSINSTANCESTATUS_ACTIVE), | ||
| }, | ||
| }, | ||
| { | ||
| description: "create without id", | ||
| getFails: false, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| returnInstance: true, | ||
| getLogsResponse: &logs.LogsInstance{ | ||
| Status: utils.Ptr(logs.LOGSINSTANCESTATUS_ACTIVE), | ||
| }, | ||
| }, | ||
| { | ||
| description: "create without status", | ||
| getFails: false, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| returnInstance: true, | ||
| getLogsResponse: &logs.LogsInstance{ | ||
| Id: utils.Ptr(instanceId), | ||
| }, | ||
| }, | ||
| { | ||
| description: "instance deleting", | ||
| getFails: false, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| returnInstance: true, | ||
| getLogsResponse: &logs.LogsInstance{ | ||
| Id: utils.Ptr(instanceId), | ||
| Status: utils.Ptr(logs.LOGSINSTANCESTATUS_DELETING), | ||
| }, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.description, func(t *testing.T) { | ||
| client := &apiClientMock{ | ||
| getFails: tt.getFails, | ||
| getLogsResponse: tt.getLogsResponse, | ||
| returnInstance: tt.returnInstance, | ||
| } | ||
| var instanceWanted *logs.LogsInstance | ||
| if tt.wantResp { | ||
| instanceWanted = tt.getLogsResponse | ||
| } | ||
|
|
||
| handler := CreateLogsInstanceWaitHandler(context.Background(), client, projectId, region, instanceId) | ||
|
|
||
| response, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) | ||
|
|
||
| if (err != nil) != tt.wantErr { | ||
| t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) | ||
| } | ||
| if !cmp.Equal(response, instanceWanted) { | ||
| t.Fatalf("handler gotRes = %v, want %v", response, instanceWanted) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDeleteLogsInstanceWaitHandler(t *testing.T) { | ||
| tests := []struct { | ||
| description string | ||
| getFails bool | ||
| wantErr bool | ||
| statusCode int | ||
| }{ | ||
| { | ||
| description: "delete succeeded", | ||
| getFails: true, | ||
| statusCode: http.StatusNotFound, | ||
| }, | ||
| { | ||
| description: "delete failed with error", | ||
| getFails: true, | ||
| wantErr: true, | ||
| statusCode: http.StatusInternalServerError, | ||
| }, | ||
| { | ||
| description: "delete still in progress", | ||
| getFails: false, | ||
| wantErr: true, | ||
| statusCode: http.StatusOK, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.description, func(t *testing.T) { | ||
| client := &apiClientMock{ | ||
| getFails: tt.getFails, | ||
| returnInstance: false, | ||
| statusCode: tt.statusCode, | ||
| getLogsResponse: nil, | ||
| } | ||
| handler := DeleteLogsInstanceWaitHandler(context.Background(), client, projectId, region, instanceId) | ||
| _, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.