-
Notifications
You must be signed in to change notification settings - Fork 82
Feat/stackittpr 788 context logging #1704
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
Draft
cgoetz-inovex
wants to merge
6
commits into
mock-refactor
Choose a base branch
from
feat/STACKITTPR-788-context-logging
base: mock-refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4d9f6ab
fix(tests): fix compile, lint and runtime errors
cgoetz-inovex cea9000
feat(clients): add defaultConfigOptions, add response trace id logging
cgoetz-inovex 1b62e72
feat(log): add logging funcs in core to also log x-trace-id
cgoetz-inovex 1f3cbf1
feat(logging): setup response capture for all resources+data sources
cgoetz-inovex 323075a
do not merge this one
cgoetz-inovex 55bbcac
fix(machinetype): add to refactored data sources
cgoetz-inovex 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,214 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/datasource" | ||
| "github.com/hashicorp/terraform-plugin-framework/resource" | ||
| ) | ||
|
|
||
| // WrapDataSource wraps a data source so its Read method can capture the HTTP | ||
| // response used for provider logging. | ||
| func WrapDataSource(inner datasource.DataSource) datasource.DataSource { | ||
| return &wrappedDataSource{inner: inner} | ||
| } | ||
|
|
||
| type wrappedDataSource struct { | ||
| inner datasource.DataSource | ||
| } | ||
|
|
||
| var ( | ||
| _ datasource.DataSource = &wrappedDataSource{} | ||
| _ datasource.DataSourceWithConfigure = &wrappedDataSource{} | ||
| _ datasource.DataSourceWithConfigValidators = &wrappedDataSource{} | ||
| _ datasource.DataSourceWithValidateConfig = &wrappedDataSource{} | ||
| ) | ||
|
|
||
| func (w *wrappedDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
| w.inner.Metadata(ctx, req, resp) | ||
| } | ||
|
|
||
| func (w *wrappedDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
| w.inner.Schema(ctx, req, resp) | ||
| } | ||
|
|
||
| func (w *wrappedDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { //nolint:gocritic,tflogresponse // Framework signature requires values; wrapped method logs the response. | ||
| ctx = InitProviderContext(ctx) //nolint:tflogresponse // The wrapped CRUD method logs the response. | ||
| w.inner.Read(ctx, req, resp) | ||
| } | ||
|
|
||
| func (w *wrappedDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
| if inner, ok := w.inner.(datasource.DataSourceWithConfigure); ok { | ||
| inner.Configure(ctx, req, resp) | ||
| } | ||
| } | ||
|
|
||
| func (w *wrappedDataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator { | ||
| if inner, ok := w.inner.(datasource.DataSourceWithConfigValidators); ok { | ||
| return inner.ConfigValidators(ctx) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (w *wrappedDataSource) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { | ||
| if inner, ok := w.inner.(datasource.DataSourceWithValidateConfig); ok { | ||
| inner.ValidateConfig(ctx, req, resp) | ||
| } | ||
| } | ||
|
|
||
| // WrapResource wraps a resource so its CRUD methods can capture the HTTP | ||
| // response used for provider logging. | ||
| func WrapResource(inner resource.Resource) resource.Resource { | ||
| wrapped := &wrappedResource{inner: inner} | ||
| _, hasIdentity := inner.(resource.ResourceWithIdentity) | ||
| _, hasUpgradeIdentity := inner.(resource.ResourceWithUpgradeIdentity) | ||
|
|
||
| switch { | ||
| case hasIdentity && hasUpgradeIdentity: | ||
| return &wrappedResourceWithIdentityAndUpgrade{wrappedResourceWithIdentity: wrappedResourceWithIdentity{wrappedResource: wrapped}} | ||
| case hasIdentity: | ||
| return &wrappedResourceWithIdentity{wrappedResource: wrapped} | ||
| case hasUpgradeIdentity: | ||
| return &wrappedResourceWithUpgradeIdentity{wrappedResource: wrapped} | ||
| default: | ||
| return wrapped | ||
| } | ||
| } | ||
|
|
||
| type wrappedResource struct { | ||
| inner resource.Resource | ||
| } | ||
|
|
||
| var ( | ||
| _ resource.Resource = &wrappedResource{} | ||
| _ resource.ResourceWithConfigure = &wrappedResource{} | ||
| _ resource.ResourceWithConfigValidators = &wrappedResource{} | ||
| _ resource.ResourceWithImportState = &wrappedResource{} | ||
| _ resource.ResourceWithModifyPlan = &wrappedResource{} | ||
| _ resource.ResourceWithMoveState = &wrappedResource{} | ||
| _ resource.ResourceWithUpgradeState = &wrappedResource{} | ||
| _ resource.ResourceWithValidateConfig = &wrappedResource{} | ||
| ) | ||
|
|
||
| func (w *wrappedResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
| w.inner.Metadata(ctx, req, resp) | ||
| } | ||
|
|
||
| func (w *wrappedResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
| w.inner.Schema(ctx, req, resp) | ||
| } | ||
|
|
||
| func (w *wrappedResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { //nolint:gocritic,tflogresponse // Framework signature requires values; wrapped method logs the response. | ||
| ctx = InitProviderContext(ctx) //nolint:tflogresponse // The wrapped CRUD method logs the response. | ||
| w.inner.Create(ctx, req, resp) | ||
| } | ||
|
|
||
| func (w *wrappedResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { //nolint:gocritic,tflogresponse // Framework signature requires values; wrapped method logs the response. | ||
| ctx = InitProviderContext(ctx) //nolint:tflogresponse // The wrapped CRUD method logs the response. | ||
| w.inner.Read(ctx, req, resp) | ||
| } | ||
|
|
||
| func (w *wrappedResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { //nolint:gocritic,tflogresponse // Framework signature requires values; wrapped method logs the response. | ||
| ctx = InitProviderContext(ctx) //nolint:tflogresponse // The wrapped CRUD method logs the response. | ||
| w.inner.Update(ctx, req, resp) | ||
| } | ||
|
|
||
| func (w *wrappedResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { //nolint:gocritic,tflogresponse // Framework signature requires values; wrapped method logs the response. | ||
| ctx = InitProviderContext(ctx) //nolint:tflogresponse // The wrapped CRUD method logs the response. | ||
| w.inner.Delete(ctx, req, resp) | ||
| } | ||
|
|
||
| func (w *wrappedResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { | ||
| if inner, ok := w.inner.(resource.ResourceWithConfigure); ok { | ||
| inner.Configure(ctx, req, resp) | ||
| } | ||
| } | ||
|
|
||
| func (w *wrappedResource) ConfigValidators(ctx context.Context) []resource.ConfigValidator { | ||
| if inner, ok := w.inner.(resource.ResourceWithConfigValidators); ok { | ||
| return inner.ConfigValidators(ctx) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (w *wrappedResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { | ||
| if inner, ok := w.inner.(resource.ResourceWithImportState); ok { | ||
| inner.ImportState(ctx, req, resp) | ||
| return | ||
| } | ||
|
|
||
| resp.Diagnostics.AddError( | ||
| "Resource Import Not Implemented", | ||
| "This resource does not support import. Please contact the provider developer for additional information.", | ||
| ) | ||
| } | ||
|
|
||
| func (w *wrappedResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { //nolint:gocritic // Framework signature requires values. | ||
| if inner, ok := w.inner.(resource.ResourceWithModifyPlan); ok { | ||
| inner.ModifyPlan(ctx, req, resp) | ||
| } | ||
| } | ||
|
|
||
| func (w *wrappedResource) MoveState(ctx context.Context) []resource.StateMover { | ||
| if inner, ok := w.inner.(resource.ResourceWithMoveState); ok { | ||
| return inner.MoveState(ctx) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (w *wrappedResource) UpgradeState(ctx context.Context) map[int64]resource.StateUpgrader { | ||
| if inner, ok := w.inner.(resource.ResourceWithUpgradeState); ok { | ||
| return inner.UpgradeState(ctx) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (w *wrappedResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) { | ||
| if inner, ok := w.inner.(resource.ResourceWithValidateConfig); ok { | ||
| inner.ValidateConfig(ctx, req, resp) | ||
| } | ||
| } | ||
|
|
||
| type wrappedResourceWithIdentity struct { | ||
| *wrappedResource | ||
| } | ||
|
|
||
| var _ resource.ResourceWithIdentity = &wrappedResourceWithIdentity{} | ||
|
|
||
| func (w *wrappedResourceWithIdentity) IdentitySchema(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { | ||
| if inner, ok := w.inner.(resource.ResourceWithIdentity); ok { | ||
| inner.IdentitySchema(ctx, req, resp) | ||
| } | ||
| } | ||
|
|
||
| type wrappedResourceWithUpgradeIdentity struct { | ||
| *wrappedResource | ||
| } | ||
|
|
||
| var _ resource.ResourceWithUpgradeIdentity = &wrappedResourceWithUpgradeIdentity{} | ||
|
|
||
| func (w *wrappedResourceWithUpgradeIdentity) UpgradeIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { | ||
| if inner, ok := w.inner.(resource.ResourceWithUpgradeIdentity); ok { | ||
| return inner.UpgradeIdentity(ctx) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| type wrappedResourceWithIdentityAndUpgrade struct { | ||
| wrappedResourceWithIdentity | ||
| } | ||
|
|
||
| var _ resource.ResourceWithUpgradeIdentity = &wrappedResourceWithIdentityAndUpgrade{} | ||
|
|
||
| func (w *wrappedResourceWithIdentityAndUpgrade) UpgradeIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { | ||
| if inner, ok := w.inner.(resource.ResourceWithUpgradeIdentity); ok { | ||
| return inner.UpgradeIdentity(ctx) | ||
| } | ||
|
|
||
| return nil | ||
| } |
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,75 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/datasource" | ||
| "github.com/hashicorp/terraform-plugin-framework/resource" | ||
| sdkconfig "github.com/stackitcloud/stackit-sdk-go/core/config" | ||
| ) | ||
|
|
||
| type testDataSource struct { | ||
| readContext context.Context | ||
| } | ||
|
|
||
| func (d *testDataSource) Metadata(context.Context, datasource.MetadataRequest, *datasource.MetadataResponse) { | ||
| } | ||
| func (d *testDataSource) Schema(context.Context, datasource.SchemaRequest, *datasource.SchemaResponse) { | ||
| } | ||
| func (d *testDataSource) Read(ctx context.Context, _ datasource.ReadRequest, _ *datasource.ReadResponse) { //nolint:gocritic // Framework signature requires values. | ||
| d.readContext = ctx | ||
| } | ||
|
|
||
| type testResource struct { | ||
| contexts map[string]context.Context | ||
| } | ||
|
|
||
| func (r *testResource) Metadata(context.Context, resource.MetadataRequest, *resource.MetadataResponse) { | ||
| } | ||
| func (r *testResource) Schema(context.Context, resource.SchemaRequest, *resource.SchemaResponse) {} | ||
| func (r *testResource) Create(ctx context.Context, _ resource.CreateRequest, _ *resource.CreateResponse) { //nolint:gocritic // Framework signature requires values. | ||
| r.contexts["create"] = ctx | ||
| } | ||
| func (r *testResource) Read(ctx context.Context, _ resource.ReadRequest, _ *resource.ReadResponse) { //nolint:gocritic // Framework signature requires values. | ||
| r.contexts["read"] = ctx | ||
| } | ||
| func (r *testResource) Update(ctx context.Context, _ resource.UpdateRequest, _ *resource.UpdateResponse) { //nolint:gocritic // Framework signature requires values. | ||
| r.contexts["update"] = ctx | ||
| } | ||
| func (r *testResource) Delete(ctx context.Context, _ resource.DeleteRequest, _ *resource.DeleteResponse) { //nolint:gocritic // Framework signature requires values. | ||
| r.contexts["delete"] = ctx | ||
| } | ||
|
|
||
| func TestWrapDataSourceReadInitializesProviderContext(t *testing.T) { | ||
| inner := &testDataSource{} | ||
| wrapped := WrapDataSource(inner) | ||
|
|
||
| wrapped.Read(context.Background(), datasource.ReadRequest{}, &datasource.ReadResponse{}) | ||
|
|
||
| if inner.readContext.Value(sdkconfig.ContextHTTPResponse) == nil { | ||
| t.Error("Read context does not capture HTTP responses") | ||
| } | ||
| } | ||
|
|
||
| func TestWrapResourceDoesNotAddIdentitySupport(t *testing.T) { | ||
| if _, ok := WrapResource(&testResource{}).(resource.ResourceWithIdentity); ok { | ||
| t.Error("wrapper must not add identity support when the resource does not implement it") | ||
| } | ||
| } | ||
|
|
||
| func TestWrapResourceCRUDInitializesProviderContext(t *testing.T) { | ||
| inner := &testResource{contexts: make(map[string]context.Context)} | ||
| wrapped := WrapResource(inner) | ||
|
|
||
| wrapped.Create(context.Background(), resource.CreateRequest{}, &resource.CreateResponse{}) | ||
| wrapped.Read(context.Background(), resource.ReadRequest{}, &resource.ReadResponse{}) | ||
| wrapped.Update(context.Background(), resource.UpdateRequest{}, &resource.UpdateResponse{}) | ||
| wrapped.Delete(context.Background(), resource.DeleteRequest{}, &resource.DeleteResponse{}) | ||
|
|
||
| for _, operation := range []string{"create", "read", "update", "delete"} { | ||
| if inner.contexts[operation].Value(sdkconfig.ContextHTTPResponse) == nil { | ||
| t.Errorf("%s context does not capture HTTP responses", operation) | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we'll need some linting rule then so only these
LogInfo,LogWarn, ... functions are used then, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I'd add a linter to forbid direct
tflog.Info|Error|...calls in theservicespkg