Skip to content

Fix context and defer issues in Configure methods #834

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/fixed-20250605-093625.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: fixed
body: Fix context and defer issues in Configure methods to avoid unnecessary resource allocation when ProviderData is nil
time: 2025-06-05T09:36:25.40303494Z
custom:
Issue: "819"
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,12 @@
}

func (r *AdminManagementApplicationResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ func (r *EnvironmentApplicationAdminResource) Schema(ctx context.Context, req re
}

func (r *EnvironmentApplicationAdminResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func (r *EnvironmentApplicationPackageInstallResource) Schema(ctx context.Contex
}

func (r *EnvironmentApplicationPackageInstallResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions internal/services/authorization/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ func (r *UserResource) Schema(ctx context.Context, req resource.SchemaRequest, r
}

func (r *UserResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions internal/services/connection/resource_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ func (d *Resource) ConfigValidators(ctx context.Context) []resource.ConfigValida
}

func (r *Resource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
5 changes: 2 additions & 3 deletions internal/services/connection/resource_connection_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,12 @@ func (r *ShareResource) Schema(ctx context.Context, req resource.SchemaRequest,
}

func (r *ShareResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
resp.Diagnostics.AddError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,12 @@ func (r *Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp
}

func (r *Resource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
5 changes: 2 additions & 3 deletions internal/services/data_record/resource_data_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,12 @@
}

func (r *DataRecordResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

clientApi := req.ProviderData.(*api.ProviderClient).Api
if clientApi == nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/services/dlp_policy/resource_dlp_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@
}

func (r *DataLossPreventionPolicyResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
resp.Diagnostics.AddError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ func (r *Resource) Metadata(ctx context.Context, req resource.MetadataRequest, r
}

func (r *Resource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
5 changes: 2 additions & 3 deletions internal/services/environment/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,12 @@ func (d *Resource) ConfigValidators(ctx context.Context) []resource.ConfigValida
}

func (r *Resource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,12 @@
}

func (r *environmentGroupRuleSetResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client := req.ProviderData.(*api.ProviderClient).Api
if client == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@
}

func (r *EnvironmentGroupResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,12 @@
}

func (r *EnvironmentSettingsResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client := req.ProviderData.(*api.ProviderClient).Api

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,11 @@ func (r *Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp
}

func (r *Resource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

if req.ProviderData == nil {
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions internal/services/licensing/resource_billing_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@
}

func (r *BillingPolicyResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@
}

func (r *BillingPolicyEnvironmentResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func (r *ManagedEnvironmentResource) Metadata(ctx context.Context, req resource.
}

func (r *ManagedEnvironmentResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
5 changes: 2 additions & 3 deletions internal/services/rest/resource_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,12 @@ func (r *DataverseWebApiResource) buildOperationSchema(description string) schem
}

func (r *DataverseWebApiResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions internal/services/solution/resource_solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ func (r *Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp
}

func (r *Resource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,12 @@ func (r *Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp

// Configure configures the resource with the provider client.
func (r *Resource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions internal/services/tenant_settings/resource_tenant_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,12 @@ func (r *TenantSettingsResource) Schema(ctx context.Context, req resource.Schema
}

func (r *TenantSettingsResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()
if req.ProviderData == nil {
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
ctx, exitContext := helpers.EnterRequestContext(ctx, r.TypeInfo, req)
defer exitContext()

client := req.ProviderData.(*api.ProviderClient).Api

Expand Down