Skip to content

Commit

Permalink
Added a check on provider's configuration for required env vars (#1053)
Browse files Browse the repository at this point in the history
* Added a check on provider's configuration for reqd env vars

* Added linting
  • Loading branch information
duedares-rvj authored Oct 24, 2024
1 parent 98823c3 commit 152ca2d
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 152ca2d

Please sign in to comment.