Skip to content

Commit

Permalink
internal/proto6server: Ensure logging.InitContext is called before an…
Browse files Browse the repository at this point in the history
…y RPC logic (#314)

Verified with terraform-provider-corner. Creating unit testing for this setup requires something like hashicorp/terraform-plugin-log#61 upstream.

Previously:

```text
2022-05-03T13:38:24.417-0400 [DEBUG] sdk.framework: Calling provider defined Provider GetResources: new_logger_warning="This log was generated by an SDK subsystem logger that wasn't created before being used. Use tflog.NewSubsystem to create this logger before it is used." tf_provider_addr=registry.terraform.io/hashicorp/framework tf_req_id=44608b15-0686-e9f9-1d17-56ca3c63b336 tf_resource_type=framework_user tf_rpc=ApplyResourceChange
2022-05-03T13:38:24.417-0400 [DEBUG] sdk.framework: Called provider defined Provider GetResources: new_logger_warning="This log was generated by an SDK subsystem logger that wasn't created before being used. Use tflog.NewSubsystem to create this logger before it is used." tf_provider_addr=registry.terraform.io/hashicorp/framework tf_req_id=44608b15-0686-e9f9-1d17-56ca3c63b336 tf_resource_type=framework_user tf_rpc=ApplyResourceChange
2022-05-03T13:38:24.417-0400 [DEBUG] sdk.framework: Calling provider defined ResourceType GetSchema: new_logger_warning="This log was generated by an SDK subsystem logger that wasn't created before being used. Use tflog.NewSubsystem to create this logger before it is used." tf_provider_addr=registry.terraform.io/hashicorp/framework tf_req_id=44608b15-0686-e9f9-1d17-56ca3c63b336 tf_resource_type=framework_user tf_rpc=ApplyResourceChange
```

Now:

```text
2022-05-04T09:21:20.515-0400 [DEBUG] sdk.framework: Calling provider defined Provider GetResources
2022-05-04T09:21:20.515-0400 [DEBUG] sdk.framework: Called provider defined Provider GetResources
2022-05-04T09:21:20.515-0400 [DEBUG] sdk.framework: Calling provider defined ResourceType GetSchema
2022-05-04T09:21:20.515-0400 [DEBUG] sdk.framework: Called provider defined ResourceType GetSchema
```
  • Loading branch information
bflad committed May 4, 2022
1 parent 12176c9 commit 2529cb0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/314.txt
@@ -0,0 +1,3 @@
```release-note:bug
all: Prevented `This log was generated by an SDK subsystem logger that wasn't created before being used.` warning messages in logging
```
10 changes: 10 additions & 0 deletions internal/proto6server/serve.go
Expand Up @@ -107,6 +107,7 @@ func (r getProviderSchemaResponse) toTfprotov6() *tfprotov6.GetProviderSchemaRes

func (s *Server) GetProviderSchema(ctx context.Context, _ *tfprotov6.GetProviderSchemaRequest) (*tfprotov6.GetProviderSchemaResponse, error) {
ctx = s.registerContext(ctx)
ctx = logging.InitContext(ctx)
resp := new(getProviderSchemaResponse)

s.getProviderSchema(ctx, resp)
Expand Down Expand Up @@ -246,6 +247,7 @@ func (r validateProviderConfigResponse) toTfprotov6() *tfprotov6.ValidateProvide

func (s *Server) ValidateProviderConfig(ctx context.Context, req *tfprotov6.ValidateProviderConfigRequest) (*tfprotov6.ValidateProviderConfigResponse, error) {
ctx = s.registerContext(ctx)
ctx = logging.InitContext(ctx)
resp := &validateProviderConfigResponse{
// This RPC allows a modified configuration to be returned. This was
// previously used to allow a "required" provider attribute (as defined
Expand Down Expand Up @@ -359,6 +361,7 @@ func (r configureProviderResponse) toTfprotov6() *tfprotov6.ConfigureProviderRes

func (s *Server) ConfigureProvider(ctx context.Context, req *tfprotov6.ConfigureProviderRequest) (*tfprotov6.ConfigureProviderResponse, error) {
ctx = s.registerContext(ctx)
ctx = logging.InitContext(ctx)
resp := &configureProviderResponse{}

s.configureProvider(ctx, req, resp)
Expand Down Expand Up @@ -415,6 +418,7 @@ func (r validateResourceConfigResponse) toTfprotov6() *tfprotov6.ValidateResourc

func (s *Server) ValidateResourceConfig(ctx context.Context, req *tfprotov6.ValidateResourceConfigRequest) (*tfprotov6.ValidateResourceConfigResponse, error) {
ctx = s.registerContext(ctx)
ctx = logging.InitContext(ctx)
resp := &validateResourceConfigResponse{}

s.validateResourceConfig(ctx, req, resp)
Expand Down Expand Up @@ -543,6 +547,7 @@ func (r upgradeResourceStateResponse) toTfprotov6() *tfprotov6.UpgradeResourceSt

func (s *Server) UpgradeResourceState(ctx context.Context, req *tfprotov6.UpgradeResourceStateRequest) (*tfprotov6.UpgradeResourceStateResponse, error) {
ctx = s.registerContext(ctx)
ctx = logging.InitContext(ctx)
resp := &upgradeResourceStateResponse{}

s.upgradeResourceState(ctx, req, resp)
Expand Down Expand Up @@ -773,6 +778,7 @@ func (r readResourceResponse) toTfprotov6() *tfprotov6.ReadResourceResponse {

func (s *Server) ReadResource(ctx context.Context, req *tfprotov6.ReadResourceRequest) (*tfprotov6.ReadResourceResponse, error) {
ctx = s.registerContext(ctx)
ctx = logging.InitContext(ctx)
resp := &readResourceResponse{}

s.readResource(ctx, req, resp)
Expand Down Expand Up @@ -919,6 +925,7 @@ func (r planResourceChangeResponse) toTfprotov6() *tfprotov6.PlanResourceChangeR

func (s *Server) PlanResourceChange(ctx context.Context, req *tfprotov6.PlanResourceChangeRequest) (*tfprotov6.PlanResourceChangeResponse, error) {
ctx = s.registerContext(ctx)
ctx = logging.InitContext(ctx)
resp := &planResourceChangeResponse{}

s.planResourceChange(ctx, req, resp)
Expand Down Expand Up @@ -1243,6 +1250,7 @@ func normaliseRequiresReplace(ctx context.Context, rs []*tftypes.AttributePath)

func (s *Server) ApplyResourceChange(ctx context.Context, req *tfprotov6.ApplyResourceChangeRequest) (*tfprotov6.ApplyResourceChangeResponse, error) {
ctx = s.registerContext(ctx)
ctx = logging.InitContext(ctx)
resp := &applyResourceChangeResponse{
// default to the prior state, so the state won't change unless
// we choose to change it
Expand Down Expand Up @@ -1538,6 +1546,7 @@ func (r validateDataResourceConfigResponse) toTfprotov6() *tfprotov6.ValidateDat

func (s *Server) ValidateDataResourceConfig(ctx context.Context, req *tfprotov6.ValidateDataResourceConfigRequest) (*tfprotov6.ValidateDataResourceConfigResponse, error) {
ctx = s.registerContext(ctx)
ctx = logging.InitContext(ctx)
resp := &validateDataResourceConfigResponse{}

s.validateDataResourceConfig(ctx, req, resp)
Expand Down Expand Up @@ -1666,6 +1675,7 @@ func (r readDataSourceResponse) toTfprotov6() *tfprotov6.ReadDataSourceResponse

func (s *Server) ReadDataSource(ctx context.Context, req *tfprotov6.ReadDataSourceRequest) (*tfprotov6.ReadDataSourceResponse, error) {
ctx = s.registerContext(ctx)
ctx = logging.InitContext(ctx)
resp := &readDataSourceResponse{}

s.readDataSource(ctx, req, resp)
Expand Down
1 change: 1 addition & 0 deletions internal/proto6server/serve_import.go
Expand Up @@ -152,6 +152,7 @@ func (s *Server) importResourceState(ctx context.Context, req *tfprotov6.ImportR
// ImportResourceState satisfies the tfprotov6.ProviderServer interface.
func (s *Server) ImportResourceState(ctx context.Context, req *tfprotov6.ImportResourceStateRequest) (*tfprotov6.ImportResourceStateResponse, error) {
ctx = s.registerContext(ctx)
ctx = logging.InitContext(ctx)
resp := &importResourceStateResponse{}

s.importResourceState(ctx, req, resp)
Expand Down

0 comments on commit 2529cb0

Please sign in to comment.