From 152ca2d21bb9af0260c7f2842bfd0353c375889a Mon Sep 17 00:00:00 2001 From: Rajat Bajaj Date: Thu, 24 Oct 2024 19:53:47 +0530 Subject: [PATCH] Added a check on provider's configuration for required env vars (#1053) * Added a check on provider's configuration for reqd env vars * Added linting --- internal/provider/provider.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index f0c54c92..84c88340 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -1,8 +1,12 @@ package provider import ( + "context" + "fmt" "os" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/auth0/terraform-provider-auth0/internal/auth0/flow" "github.com/auth0/terraform-provider-auth0/internal/auth0/form" @@ -172,7 +176,29 @@ func New() *schema.Provider { }, } - provider.ConfigureContextFunc = config.ConfigureProvider(&provider.TerraformVersion) + provider.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) { + var diags diag.Diagnostics + + // Check required environment variables. + requiredEnvVars := []string{"AUTH0_DOMAIN", "AUTH0_CLIENT_ID", "AUTH0_CLIENT_SECRET"} + for _, varName := range requiredEnvVars { + value, exists := os.LookupEnv(varName) + if !exists || value == "" { + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: fmt.Sprintf("Missing environment variable: %s", varName), + Detail: fmt.Sprintf("The environment variable %s must be set and cannot be empty.", varName), + }) + } + } + + if len(diags) > 0 { + return nil, diags + } + + // Call the original configuration function if no errors. + return config.ConfigureProvider(&provider.TerraformVersion)(ctx, d) + } return provider }