Skip to content

Commit

Permalink
chore: lint and code cleanup (#251)
Browse files Browse the repository at this point in the history
# 📥 Pull Request

## ❓ What are you trying to address

This pull request includes several changes to the codebase, mainly
focusing on linting and code cleanup by replacing the use of
`context.Background()` with `t.Context()` in test files and modifying
the `setConfig` and `mapConfig` methods to return the context.
  • Loading branch information
DariuszPorowski authored Feb 13, 2025
1 parent 2d2b7c1 commit 108d209
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ linters:
- mnd
- goheader
- cyclop
- exportloopref
- tenv

linters-settings:
exhaustive:
Expand Down
3 changes: 1 addition & 2 deletions internal/auth/token_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package auth_test

import (
"context"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
Expand Down Expand Up @@ -49,7 +48,7 @@ func TestUnit_TokenCredential_GetToken(t *testing.T) {
} else {
require.NoError(t, err)

token, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{})
token, err := cred.GetToken(t.Context(), policy.TokenRequestOptions{})
require.NoError(t, err)
assert.Equal(t, testCase.expected, token, "they should be equal")
}
Expand Down
3 changes: 1 addition & 2 deletions internal/framework/customtypes/url_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package customtypes_test

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework/attr"
Expand Down Expand Up @@ -42,7 +41,7 @@ func TestUnit_URLTypeValueFromTerraform(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

ctx := context.Background()
ctx := t.Context()

got, err := customtypes.URLType{}.ValueFromTerraform(ctx, testCase.in)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions internal/framework/customtypes/url_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package customtypes_test

import (
"context"
"fmt"
"net/url"
"testing"
Expand Down Expand Up @@ -95,7 +94,7 @@ func TestUnit_URLValidateAttribute(t *testing.T) {
resp := xattr.ValidateAttributeResponse{}

testCase.urlValue.ValidateAttribute(
context.Background(),
t.Context(),
xattr.ValidateAttributeRequest{Path: path.Root("test")},
&resp,
)
Expand Down Expand Up @@ -172,7 +171,7 @@ func TestUnit_URLValidateParameter(t *testing.T) {
resp := function.ValidateParameterResponse{}

testCase.urlValue.ValidateParameter(
context.Background(),
t.Context(),
function.ValidateParameterRequest{
Position: 0,
},
Expand Down
3 changes: 1 addition & 2 deletions internal/framework/customtypes/uuid_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package customtypes_test

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework/attr"
Expand Down Expand Up @@ -42,7 +41,7 @@ func TestUnit_UUIDTypeValueFromTerraform(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

ctx := context.Background()
ctx := t.Context()

got, err := customtypes.UUIDType{}.ValueFromTerraform(ctx, testCase.in)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions internal/framework/customtypes/uuid_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package customtypes_test

import (
"context"
"fmt"
"testing"

Expand Down Expand Up @@ -92,7 +91,7 @@ func TestUnit_UUIDValidateAttribute(t *testing.T) {
resp := xattr.ValidateAttributeResponse{}

testCase.uuidValue.ValidateAttribute(
context.Background(),
t.Context(),
xattr.ValidateAttributeRequest{Path: path.Root("test")},
&resp,
)
Expand Down Expand Up @@ -166,7 +165,7 @@ func TestUnit_UUIDValidateParameter(t *testing.T) {
resp := function.ValidateParameterResponse{}

testCase.uuidValue.ValidateParameter(
context.Background(),
t.Context(),
function.ValidateParameterRequest{
Position: 0,
},
Expand Down
3 changes: 1 addition & 2 deletions internal/framework/typeutils/dynamic_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package typeutils_test

import (
"context"
"math/big"
"testing"

Expand Down Expand Up @@ -443,7 +442,7 @@ func TestUnit_JSONToDynamic(t *testing.T) {

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
actual, err := typeutils.JSONToDynamic([]byte(tt.input), tt.expect.UnderlyingValue().Type(context.TODO()))
actual, err := typeutils.JSONToDynamic([]byte(tt.input), tt.expect.UnderlyingValue().Type(t.Context()))
require.NoError(t, err)
require.Equal(t, tt.expect, actual)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package validators_test

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
Expand Down Expand Up @@ -269,7 +268,7 @@ func TestUnit_PatternsIfAttributeIsOneOfValidator(t *testing.T) { //nolint:maint

resp := &validators.PatternsIfAttributeIsOneOfResponse{}

validators.PatternsIfAttributeIsOneOf(test.in, test.exceptedValues, test.patterns, test.message).Validate(context.TODO(), test.req, resp)
validators.PatternsIfAttributeIsOneOf(test.in, test.exceptedValues, test.patterns, test.message).Validate(t.Context(), test.req, resp)

if test.expError && resp.Diagnostics.HasError() {
d1 := validatordiag.InvalidAttributeValueDiagnostic(test.inPath, test.expErrorMessage, test.req.ConfigValue.ValueString())
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/transforms/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func getTmplFuncs() (template.FuncMap, error) {
return handler.Build(), nil
}

func SourceFileToPayload(ctx context.Context, srcPath types.String, tokens supertypes.MapValueOf[string]) (*string, *string, diag.Diagnostics) {
func SourceFileToPayload(ctx context.Context, srcPath types.String, tokens supertypes.MapValueOf[string]) (*string, *string, diag.Diagnostics) { //revive:disable-line:confusing-results
var diags diag.Diagnostics

source := srcPath.ValueString()
Expand Down
3 changes: 1 addition & 2 deletions internal/pkg/utils/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package utils_test

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -115,7 +114,7 @@ func TestUnit_IsErrNotFound(t *testing.T) {
}

func TestUnit_GetDiagsFromError(t *testing.T) {
ctx := context.Background()
ctx := t.Context()

t.Run("nil error", func(t *testing.T) {
diags := utils.GetDiagsFromError(ctx, nil, utils.OperationRead, nil)
Expand Down
100 changes: 51 additions & 49 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,14 @@ func (p *FabricProvider) Configure(ctx context.Context, req provider.ConfigureRe
return
}

p.setConfig(&ctx, &config, resp)
ctx = p.setConfig(ctx, &config, resp)
tflog.Debug(ctx, "Setting configuration")

if resp.Diagnostics.HasError() {
return
}

p.mapConfig(&ctx, &config, resp)
p.mapConfig(ctx, &config, resp)
tflog.Debug(ctx, "Mapping configuration")

if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -467,10 +467,10 @@ func (p *FabricProvider) Functions(_ context.Context) []func() function.Function
}
}

func (p *FabricProvider) setConfig(ctx *context.Context, config *pconfig.ProviderConfigModel, resp *provider.ConfigureResponse) {
timeout, diags := config.Timeout.ToStringValue(*ctx)
func (p *FabricProvider) setConfig(ctx context.Context, config *pconfig.ProviderConfigModel, resp *provider.ConfigureResponse) context.Context {
timeout, diags := config.Timeout.ToStringValue(ctx)
if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() {
return
return ctx
}

config.Timeout = timetypes.NewGoDurationValueFromStringMust(putils.GetStringValue(timeout, pconfig.GetEnvVarsTimeout(), pconfig.DefaultTimeout).ValueString())
Expand All @@ -482,14 +482,14 @@ func (p *FabricProvider) setConfig(ctx *context.Context, config *pconfig.Provide
"Either target apply the source of the value first, set the value statically in the configuration, or use the "+utils.ConvertStringSlicesToString(pconfig.GetEnvVarsTimeout(), false, false)+" environment variables.",
)

return
return ctx
}

*ctx = tflog.SetField(*ctx, "timeout", config.Timeout)
ctx = tflog.SetField(ctx, "timeout", config.Timeout)

endpoint, diags := config.Endpoint.ToStringValue(*ctx)
endpoint, diags := config.Endpoint.ToStringValue(ctx)
if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() {
return
return ctx
}

config.Endpoint = customtypes.NewURLValue(putils.GetStringValue(endpoint, pconfig.GetEnvVarsEndpoint(), pconfig.DefaultFabricEndpointURL).ValueString())
Expand All @@ -501,10 +501,10 @@ func (p *FabricProvider) setConfig(ctx *context.Context, config *pconfig.Provide
"Either target apply the source of the value first, set the value statically in the configuration, or use the "+utils.ConvertStringSlicesToString(pconfig.GetEnvVarsEndpoint(), false, false)+" environment variables.",
)

return
return ctx
}

*ctx = tflog.SetField(*ctx, "endpoint", config.Endpoint)
ctx = tflog.SetField(ctx, "endpoint", config.Endpoint)

config.Environment = putils.GetStringValue(config.Environment, pconfig.GetEnvVarsEnvironment(), "public")
environmentAllowedValues := map[string]bool{
Expand All @@ -521,87 +521,87 @@ func (p *FabricProvider) setConfig(ctx *context.Context, config *pconfig.Provide
"Either target apply the source of the value first, set the value statically in the configuration, or use the "+strings.Join(pconfig.GetEnvVarsEnvironment(), ",")+" environment variables.",
)

return
return ctx
}

*ctx = tflog.SetField(*ctx, "environment", config.Environment.ValueString())
ctx = tflog.SetField(ctx, "environment", config.Environment.ValueString())

tenantID, diags := config.TenantID.ToStringValue(*ctx)
tenantID, diags := config.TenantID.ToStringValue(ctx)
if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() {
return
return ctx
}

config.TenantID = customtypes.NewUUIDValue(putils.GetStringValue(tenantID, pconfig.GetEnvVarsTenantID(), "").ValueString())
*ctx = tflog.SetField(*ctx, "tenant_id", config.TenantID.ValueString())
ctx = tflog.SetField(ctx, "tenant_id", config.TenantID.ValueString())

config.AuxiliaryTenantIDs = putils.GetListStringValues(config.AuxiliaryTenantIDs, pconfig.GetEnvVarsAuxiliaryTenantIDs(), []string{})
*ctx = tflog.SetField(*ctx, "auxiliary_tenant_ids", config.AuxiliaryTenantIDs.String())
ctx = tflog.SetField(ctx, "auxiliary_tenant_ids", config.AuxiliaryTenantIDs.String())

clientID, diags := config.ClientID.ToStringValue(*ctx)
clientID, diags := config.ClientID.ToStringValue(ctx)
if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() {
return
return ctx
}

config.ClientID = customtypes.NewUUIDValue(putils.GetStringValue(clientID, pconfig.GetEnvVarsClientID(), "").ValueString())
*ctx = tflog.SetField(*ctx, "client_id", config.ClientID.ValueString())
ctx = tflog.SetField(ctx, "client_id", config.ClientID.ValueString())

config.ClientIDFilePath = putils.GetStringValue(config.ClientIDFilePath, pconfig.GetEnvVarsClientIDFilePath(), "")
*ctx = tflog.SetField(*ctx, "client_id_file_path", config.ClientIDFilePath.ValueString())
ctx = tflog.SetField(ctx, "client_id_file_path", config.ClientIDFilePath.ValueString())

config.ClientSecret = putils.GetStringValue(config.ClientSecret, pconfig.GetEnvVarsClientSecret(), "")
*ctx = tflog.SetField(*ctx, "client_secret", config.ClientSecret.ValueString())
*ctx = tflog.MaskFieldValuesWithFieldKeys(*ctx, "client_secret")
ctx = tflog.SetField(ctx, "client_secret", config.ClientSecret.ValueString())
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "client_secret")

config.ClientSecretFilePath = putils.GetStringValue(config.ClientSecretFilePath, pconfig.GetEnvVarsClientSecretFilePath(), "")
*ctx = tflog.SetField(*ctx, "client_secret_file_path", config.ClientSecretFilePath.ValueString())
ctx = tflog.SetField(ctx, "client_secret_file_path", config.ClientSecretFilePath.ValueString())

config.ClientCertificate = putils.GetStringValue(config.ClientCertificate, pconfig.GetEnvVarsClientCertificate(), "")
*ctx = tflog.SetField(*ctx, "client_certificate", config.ClientCertificate.ValueString())
*ctx = tflog.MaskFieldValuesWithFieldKeys(*ctx, "client_certificate")
ctx = tflog.SetField(ctx, "client_certificate", config.ClientCertificate.ValueString())
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "client_certificate")

config.ClientCertificateFilePath = putils.GetStringValue(config.ClientCertificateFilePath, pconfig.GetEnvVarsClientCertificateFilePath(), "")
*ctx = tflog.SetField(*ctx, "client_certificate_file_path", config.ClientCertificateFilePath.ValueString())
ctx = tflog.SetField(ctx, "client_certificate_file_path", config.ClientCertificateFilePath.ValueString())

config.ClientCertificatePassword = putils.GetStringValue(config.ClientCertificatePassword, pconfig.GetEnvVarsClientCertificatePassword(), "")
*ctx = tflog.SetField(*ctx, "client_certificate_password", config.ClientCertificatePassword.ValueString())
*ctx = tflog.MaskFieldValuesWithFieldKeys(*ctx, "client_certificate_password")
ctx = tflog.SetField(ctx, "client_certificate_password", config.ClientCertificatePassword.ValueString())
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "client_certificate_password")

config.OIDCRequestURL = putils.GetStringValue(config.OIDCRequestURL, pconfig.GetEnvVarsOIDCRequestURL(), "")
*ctx = tflog.SetField(*ctx, "oidc_request_url", config.OIDCRequestURL.ValueString())
*ctx = tflog.MaskFieldValuesWithFieldKeys(*ctx, "oidc_request_url")
ctx = tflog.SetField(ctx, "oidc_request_url", config.OIDCRequestURL.ValueString())
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "oidc_request_url")

config.OIDCRequestToken = putils.GetStringValue(config.OIDCRequestToken, pconfig.GetEnvVarsOIDCRequestToken(), "")
*ctx = tflog.SetField(*ctx, "oidc_request_token", config.OIDCRequestToken.ValueString())
*ctx = tflog.MaskFieldValuesWithFieldKeys(*ctx, "oidc_request_token")
ctx = tflog.SetField(ctx, "oidc_request_token", config.OIDCRequestToken.ValueString())
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "oidc_request_token")

config.OIDCToken = putils.GetStringValue(config.OIDCToken, pconfig.GetEnvVarsOIDCToken(), "")
*ctx = tflog.SetField(*ctx, "oidc_token", config.OIDCToken.ValueString())
*ctx = tflog.MaskFieldValuesWithFieldKeys(*ctx, "oidc_token")
ctx = tflog.SetField(ctx, "oidc_token", config.OIDCToken.ValueString())
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "oidc_token")

config.OIDCTokenFilePath = putils.GetStringValue(config.OIDCTokenFilePath, pconfig.GetEnvVarsOIDCTokenFilePath(), "")
*ctx = tflog.SetField(*ctx, "oidc_token_file_path", config.OIDCTokenFilePath.ValueString())
ctx = tflog.SetField(ctx, "oidc_token_file_path", config.OIDCTokenFilePath.ValueString())

config.AzureDevOpsServiceConnectionID = putils.GetStringValue(config.AzureDevOpsServiceConnectionID, pconfig.GetEnvVarsAzureDevOpsServiceConnectionID(), "")
*ctx = tflog.SetField(*ctx, "azure_devops_service_connection_id", config.AzureDevOpsServiceConnectionID.ValueString())
ctx = tflog.SetField(ctx, "azure_devops_service_connection_id", config.AzureDevOpsServiceConnectionID.ValueString())

config.Token = putils.GetStringValue(config.Token, pconfig.GetEnvVarsToken(), "")
*ctx = tflog.SetField(*ctx, "token", config.Token.ValueString())
*ctx = tflog.MaskFieldValuesWithFieldKeys(*ctx, "token")
ctx = tflog.SetField(ctx, "token", config.Token.ValueString())
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "token")

config.TokenFilePath = putils.GetStringValue(config.TokenFilePath, pconfig.GetEnvVarsTokenFilePath(), "")
*ctx = tflog.SetField(*ctx, "token_file_path", config.TokenFilePath.ValueString())
ctx = tflog.SetField(ctx, "token_file_path", config.TokenFilePath.ValueString())

config.UseOIDC = putils.GetBoolValue(config.UseOIDC, pconfig.GetEnvVarsUseOIDC(), false)
*ctx = tflog.SetField(*ctx, "use_oidc", config.UseOIDC.ValueBool())
ctx = tflog.SetField(ctx, "use_oidc", config.UseOIDC.ValueBool())

config.UseMSI = putils.GetBoolValue(config.UseMSI, pconfig.GetEnvVarsUseMSI(), false)
*ctx = tflog.SetField(*ctx, "use_msi", config.UseMSI.ValueBool())
ctx = tflog.SetField(ctx, "use_msi", config.UseMSI.ValueBool())

config.UseDevCLI = putils.GetBoolValue(config.UseDevCLI, pconfig.GetEnvVarsUseDevCLI(), false)
*ctx = tflog.SetField(*ctx, "use_dev_cli", config.UseDevCLI.ValueBool())
ctx = tflog.SetField(ctx, "use_dev_cli", config.UseDevCLI.ValueBool())

config.UseCLI = putils.GetBoolValue(config.UseCLI, pconfig.GetEnvVarsUseCLI(), false)
*ctx = tflog.SetField(*ctx, "use_cli", config.UseCLI.ValueBool())
ctx = tflog.SetField(ctx, "use_cli", config.UseCLI.ValueBool())

trueCount := 0
if config.UseOIDC.ValueBool() {
Expand Down Expand Up @@ -631,11 +631,11 @@ func (p *FabricProvider) setConfig(ctx *context.Context, config *pconfig.Provide
"\tuse_oidc: "+config.UseOIDC.String(),
)

return
return ctx
}

config.Preview = putils.GetBoolValue(config.Preview, pconfig.GetEnvVarsPreview(), false)
*ctx = tflog.SetField(*ctx, "preview", config.Preview.ValueBool())
ctx = tflog.SetField(ctx, "preview", config.Preview.ValueBool())

if config.Preview.ValueBool() {
resp.Diagnostics.AddWarning(
Expand All @@ -644,9 +644,11 @@ func (p *FabricProvider) setConfig(ctx *context.Context, config *pconfig.Provide
"Production use is not recommended. Use at your own risk!",
)
}

return ctx
}

func (p *FabricProvider) mapConfig(ctx *context.Context, config *pconfig.ProviderConfigModel, resp *provider.ConfigureResponse) {
func (p *FabricProvider) mapConfig(ctx context.Context, config *pconfig.ProviderConfigModel, resp *provider.ConfigureResponse) {
// Map the provider configuration model the configuration provider configuration
var err error

Expand All @@ -672,10 +674,10 @@ func (p *FabricProvider) mapConfig(ctx *context.Context, config *pconfig.Provide

var auxiliaryTenantIDs []string

resp.Diagnostics.Append(config.AuxiliaryTenantIDs.ElementsAs(*ctx, &auxiliaryTenantIDs, true)...)
resp.Diagnostics.Append(config.AuxiliaryTenantIDs.ElementsAs(ctx, &auxiliaryTenantIDs, true)...)
p.config.Auth.AuxiliaryTenantIDs = auxiliaryTenantIDs

clientID, diags := config.ClientID.ToStringValue(*ctx)
clientID, diags := config.ClientID.ToStringValue(ctx)
if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() {
return
}
Expand Down
Loading

0 comments on commit 108d209

Please sign in to comment.