Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
Refactor provider so it's not accessible globally any more
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Jan 26, 2022
1 parent 7a7ecd1 commit a43de93
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 74 deletions.
71 changes: 32 additions & 39 deletions auth0/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import (
"fmt"
"os"

"github.com/alexkappa/terraform-provider-auth0/version"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/meta"

"gopkg.in/auth0.v5"
"gopkg.in/auth0.v5/management"
)

var provider *schema.Provider
"github.com/alexkappa/terraform-provider-auth0/version"
)

func init() {
provider = &schema.Provider{
// Provider returns a *schema.Provider.
func Provider() *schema.Provider {
provider := &schema.Provider{
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Expand Down Expand Up @@ -69,45 +68,39 @@ func init() {
"auth0_action": newAction(),
"auth0_trigger_binding": newTriggerBinding(),
},
ConfigureFunc: Configure,
}
}

func Provider() *schema.Provider {
return provider
}

func Configure(data *schema.ResourceData) (interface{}, error) {

domain := data.Get("domain").(string)
id := data.Get("client_id").(string)
secret := data.Get("client_secret").(string)
debug := data.Get("debug").(bool)
provider.ConfigureFunc = ConfigureProvider(provider.TerraformVersion)

userAgent := fmt.Sprintf("Terraform-Provider-Auth0/%s (Go-Auth0-SDK/%s; Terraform-SDK/%s; Terraform/%s)",
Version(),
SDKVersion(),
TerraformSDKVersion(),
TerraformVersion())

return management.New(domain,
management.WithClientCredentials(id, secret),
management.WithDebug(debug),
management.WithUserAgent(userAgent))
return provider
}

func Version() string {
return version.ProviderVersion
}
// ConfigureProvider will configure the *schema.Provider so that *management.Management
// client is stored and passed into the subsequent resources as the meta parameter.
func ConfigureProvider(terraformVersion string) func(data *schema.ResourceData) (interface{}, error) {
return func(data *schema.ResourceData) (interface{}, error) {
domain := data.Get("domain").(string)
id := data.Get("client_id").(string)
secret := data.Get("client_secret").(string)
debug := data.Get("debug").(bool)

func SDKVersion() string {
return auth0.Version
}
providerVersion := version.ProviderVersion
sdkVersion := auth0.Version
terraformSDKVersion := meta.SDKVersionString()

func TerraformVersion() string {
return provider.TerraformVersion
}
userAgent := fmt.Sprintf(
"Terraform-Provider-Auth0/%s (Go-Auth0-SDK/%s; Terraform-SDK/%s; Terraform/%s)",
providerVersion,
sdkVersion,
terraformSDKVersion,
terraformVersion,
)

func TerraformSDKVersion() string {
return meta.SDKVersionString()
return management.New(
domain,
management.WithClientCredentials(id, secret),
management.WithDebug(debug),
management.WithUserAgent(userAgent),
)
}
}
45 changes: 11 additions & 34 deletions auth0/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,18 @@ import (
"gopkg.in/auth0.v5/management"
)

func providerWithWiremock() *schema.Provider {
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"auth0_client": newClient(),
"auth0_global_client": newGlobalClient(),
"auth0_client_grant": newClientGrant(),
"auth0_connection": newConnection(),
"auth0_custom_domain": newCustomDomain(),
"auth0_custom_domain_verification": newCustomDomainVerification(),
"auth0_resource_server": newResourceServer(),
"auth0_rule": newRule(),
"auth0_rule_config": newRuleConfig(),
"auth0_hook": newHook(),
"auth0_prompt": newPrompt(),
"auth0_prompt_custom_text": newPromptCustomText(),
"auth0_email": newEmail(),
"auth0_email_template": newEmailTemplate(),
"auth0_user": newUser(),
"auth0_tenant": newTenant(),
"auth0_role": newRole(),
"auth0_log_stream": newLogStream(),
"auth0_branding": newBranding(),
"auth0_guardian": newGuardian(),
"auth0_organization": newOrganization(),
"auth0_action": newAction(),
"auth0_trigger_binding": newTriggerBinding(),
},
ConfigureFunc: func(data *schema.ResourceData) (interface{}, error) {
return management.New(
"localhost:8080",
management.WithInsecure(),
management.WithDebug(true),
)
},
const wiremockHost = "localhost:8080"

func providerWithTestingConfiguration() *schema.Provider {
provider := Provider()
provider.ConfigureFunc = func(data *schema.ResourceData) (interface{}, error) {
return management.New(
wiremockHost,
management.WithInsecure(),
management.WithDebug(true),
)
}
return provider
}

func Auth0() (*management.Management, error) {
Expand Down
2 changes: 1 addition & 1 deletion auth0/resource_auth0_custom_domain_verification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestAccCustomDomainVerification(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: map[string]terraform.ResourceProvider{
"auth0": providerWithWiremock(),
"auth0": providerWithTestingConfiguration(),
},
Steps: []resource.TestStep{
{
Expand Down

0 comments on commit a43de93

Please sign in to comment.