Skip to content

Commit

Permalink
[1/2] DXCDT-446: Add new config struct to hold api client and global …
Browse files Browse the repository at this point in the history
…mutex (#573)
  • Loading branch information
sergiught authored May 10, 2023
1 parent c31648d commit 49f9644
Show file tree
Hide file tree
Showing 44 changed files with 305 additions and 214 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ builds:
flags:
- -trimpath
ldflags:
- '-s -w -X github.com/auth0/terraform-provider-auth0/internal/provider.version={{.Version}}'
- '-s -w -X github.com/auth0/terraform-provider-auth0/internal/config.version={{.Version}}'
goos:
- freebsd
- windows
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ build: ## Build the provider binary. Usage: "make build VERSION=0.2.0"
then \
echo "Please provide a version. Example: make build VERSION=0.2.0" && exit 1; \
fi
@go build -v -ldflags "-X github.com/auth0/terraform-provider-auth0/internal/provider.version=${VERSION}" -o "${BUILD_DIR}/${BINARY}_v$(VERSION)"
@go build -v -ldflags "-X github.com/auth0/terraform-provider-auth0/internal/config.version=${VERSION}" -o "${BUILD_DIR}/${BINARY}_v$(VERSION)"

install: build ## Install the provider as a terraform plugin. Usage: "make install VERSION=0.2.0"
@mkdir -p "${HOME}/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/$(VERSION)/${GO_OS}_${GO_ARCH}"
Expand Down
7 changes: 3 additions & 4 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"gopkg.in/dnaeon/go-vcr.v3/recorder"

"github.com/auth0/terraform-provider-auth0/internal/config"
"github.com/auth0/terraform-provider-auth0/internal/provider"
)

Expand Down Expand Up @@ -55,9 +56,7 @@ func testFactoriesWithHTTPRecordings(httpRecorder *recorder.Recorder) map[string
}
}

func configureTestProviderWithHTTPRecordings(
httpRecorder *recorder.Recorder,
) func(ctx context.Context, data *schema.ResourceData) (interface{}, diag.Diagnostics) {
func configureTestProviderWithHTTPRecordings(httpRecorder *recorder.Recorder) schema.ConfigureContextFunc {
return func(ctx context.Context, data *schema.ResourceData) (interface{}, diag.Diagnostics) {
domain := data.Get("domain").(string)
debug := data.Get("debug").(bool)
Expand Down Expand Up @@ -90,6 +89,6 @@ func configureTestProviderWithHTTPRecordings(
return nil, diag.FromErr(err)
}

return apiClient, nil
return config.New(apiClient), nil
}
}
12 changes: 7 additions & 5 deletions internal/auth0/action/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/auth0/terraform-provider-auth0/internal/config"
)

// NewResource will return a new auth0_action resource.
Expand Down Expand Up @@ -131,7 +133,7 @@ func NewResource() *schema.Resource {
}

func createAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

action := expandAction(d.GetRawConfig())
if err := api.Action.Create(action); err != nil {
Expand All @@ -148,7 +150,7 @@ func createAction(ctx context.Context, d *schema.ResourceData, m interface{}) di
}

func readAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

action, err := api.Action.Read(d.Id())
if err != nil {
Expand Down Expand Up @@ -177,7 +179,7 @@ func readAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag
}

func updateAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

diagnostics := preventErasingUnmanagedSecrets(d, api)
if diagnostics.HasError() {
Expand All @@ -197,7 +199,7 @@ func updateAction(ctx context.Context, d *schema.ResourceData, m interface{}) di
}

func deleteAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

if err := api.Action.Delete(d.Id()); err != nil {
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
Expand All @@ -217,7 +219,7 @@ func deployAction(ctx context.Context, d *schema.ResourceData, m interface{}) di
return nil
}

api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

err := retry.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *retry.RetryError {
action, err := api.Action.Read(d.Id())
Expand Down
10 changes: 6 additions & 4 deletions internal/auth0/action/resource_trigger_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/auth0/terraform-provider-auth0/internal/config"
)

// NewTriggerBindingResource will return a new auth0_trigger_binding resource.
Expand Down Expand Up @@ -68,7 +70,7 @@ func NewTriggerBindingResource() *schema.Resource {
}

func createTriggerBinding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

id := d.Get("trigger").(string)
triggerBindings := expandTriggerBindings(d.GetRawConfig().GetAttr("actions"))
Expand All @@ -83,7 +85,7 @@ func createTriggerBinding(ctx context.Context, d *schema.ResourceData, m interfa
}

func readTriggerBinding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

triggerBindings, err := api.Action.Bindings(d.Id())
if err != nil {
Expand All @@ -104,7 +106,7 @@ func readTriggerBinding(ctx context.Context, d *schema.ResourceData, m interface

func updateTriggerBinding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
triggerBindings := expandTriggerBindings(d.GetRawConfig().GetAttr("actions"))
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()
if err := api.Action.UpdateBindings(d.Id(), triggerBindings); err != nil {
return diag.FromErr(err)
}
Expand All @@ -113,7 +115,7 @@ func updateTriggerBinding(ctx context.Context, d *schema.ResourceData, m interfa
}

func deleteTriggerBinding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()
if err := api.Action.UpdateBindings(d.Id(), []*management.ActionBinding{}); err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
Expand Down
7 changes: 4 additions & 3 deletions internal/auth0/attackprotection/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/auth0/terraform-provider-auth0/internal/config"
"github.com/auth0/terraform-provider-auth0/internal/value"
)

Expand Down Expand Up @@ -277,7 +278,7 @@ func createAttackProtection(ctx context.Context, d *schema.ResourceData, m inter
}

func readAttackProtection(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

breachedPasswords, err := api.AttackProtection.GetBreachedPasswordDetection()
if err != nil {
Expand All @@ -304,7 +305,7 @@ func readAttackProtection(ctx context.Context, d *schema.ResourceData, m interfa
}

func updateAttackProtection(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

if ipt := expandSuspiciousIPThrottling(d); ipt != nil {
if err := api.AttackProtection.UpdateSuspiciousIPThrottling(ipt); err != nil {
Expand All @@ -328,7 +329,7 @@ func updateAttackProtection(ctx context.Context, d *schema.ResourceData, m inter
}

func deleteAttackProtection(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

enabled := false

Expand Down
4 changes: 2 additions & 2 deletions internal/auth0/branding/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package branding
import (
"context"

"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/auth0/terraform-provider-auth0/internal/config"
internalSchema "github.com/auth0/terraform-provider-auth0/internal/schema"
)

Expand All @@ -29,7 +29,7 @@ func readBrandingForDataSource(ctx context.Context, data *schema.ResourceData, m
// This resource is not identified by an id in the Auth0 management API.
data.SetId(id.UniqueId())

api := meta.(*management.Management)
api := meta.(*config.Config).GetAPI()

branding, err := api.Branding.Read()
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions internal/auth0/branding/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/auth0/terraform-provider-auth0/internal/config"
"github.com/auth0/terraform-provider-auth0/internal/value"
)

Expand Down Expand Up @@ -104,7 +105,7 @@ func createBranding(ctx context.Context, d *schema.ResourceData, m interface{})
}

func readBranding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

branding, err := api.Branding.Read()
if err != nil {
Expand Down Expand Up @@ -138,7 +139,7 @@ func readBranding(ctx context.Context, d *schema.ResourceData, m interface{}) di
}

func updateBranding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

if branding := expandBranding(d.GetRawConfig()); branding.String() != "{}" {
if err := api.Branding.Update(branding); err != nil {
Expand All @@ -160,7 +161,7 @@ func updateBranding(ctx context.Context, d *schema.ResourceData, m interface{})
}

func deleteBranding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

if _, ok := d.GetOk("universal_login"); !ok {
d.SetId("")
Expand Down
9 changes: 5 additions & 4 deletions internal/auth0/branding/resource_theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/auth0/terraform-provider-auth0/internal/config"
"github.com/auth0/terraform-provider-auth0/internal/value"
)

Expand Down Expand Up @@ -462,7 +463,7 @@ func NewThemeResource() *schema.Resource {
}

func createBrandingTheme(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*management.Management)
api := meta.(*config.Config).GetAPI()

if existingBrandingTheme, err := api.BrandingTheme.Default(); err == nil {
data.SetId(existingBrandingTheme.GetID())
Expand All @@ -480,7 +481,7 @@ func createBrandingTheme(ctx context.Context, data *schema.ResourceData, meta in
}

func readBrandingTheme(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*management.Management)
api := meta.(*config.Config).GetAPI()

brandingTheme, err := api.BrandingTheme.Default()
if err != nil {
Expand All @@ -507,7 +508,7 @@ func readBrandingTheme(ctx context.Context, data *schema.ResourceData, meta inte
}

func updateBrandingTheme(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*management.Management)
api := meta.(*config.Config).GetAPI()

brandingTheme := expandBrandingTheme(data)
if err := api.BrandingTheme.Update(data.Id(), &brandingTheme); err != nil {
Expand All @@ -518,7 +519,7 @@ func updateBrandingTheme(ctx context.Context, data *schema.ResourceData, meta in
}

func deleteBrandingTheme(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*management.Management)
api := meta.(*config.Config).GetAPI()

if err := api.BrandingTheme.Delete(data.Id()); err != nil {
if mErr, ok := err.(management.Error); ok {
Expand Down
3 changes: 2 additions & 1 deletion internal/auth0/client/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/auth0/terraform-provider-auth0/internal/config"
internalSchema "github.com/auth0/terraform-provider-auth0/internal/schema"
)

Expand Down Expand Up @@ -44,7 +45,7 @@ func readClientForDataSource(ctx context.Context, d *schema.ResourceData, m inte
return diag.Errorf("One of 'client_id' or 'name' is required.")
}

api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

var page int
for {
Expand Down
4 changes: 3 additions & 1 deletion internal/auth0/client/global_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/auth0/go-auth0/management"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/auth0/terraform-provider-auth0/internal/config"
)

// NewGlobalResource will return a new auth0_global_client resource.
Expand Down Expand Up @@ -52,7 +54,7 @@ func createGlobalClient(ctx context.Context, d *schema.ResourceData, m interface
}

func readGlobalClientID(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

clients, err := api.Client.List(
management.Parameter("is_global", "true"),
Expand Down
11 changes: 6 additions & 5 deletions internal/auth0/client/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/auth0/terraform-provider-auth0/internal/config"
internalValidation "github.com/auth0/terraform-provider-auth0/internal/validation"
)

Expand Down Expand Up @@ -734,7 +735,7 @@ func NewResource() *schema.Resource {
}

func createClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

client := expandClient(d)
if err := api.Client.Create(client); err != nil {
Expand All @@ -747,7 +748,7 @@ func createClient(ctx context.Context, d *schema.ResourceData, m interface{}) di
}

func readClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

client, err := api.Client.Read(d.Id())
if err != nil {
Expand Down Expand Up @@ -800,7 +801,7 @@ func readClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag
}

func updateClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

client := expandClient(d)
if clientHasChange(client) {
Expand All @@ -819,7 +820,7 @@ func updateClient(ctx context.Context, d *schema.ResourceData, m interface{}) di
}

func deleteClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

if err := api.Client.Delete(d.Id()); err != nil {
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
Expand All @@ -837,7 +838,7 @@ func rotateClientSecret(d *schema.ResourceData, m interface{}) error {
return nil
}

api := m.(*management.Management)
api := m.(*config.Config).GetAPI()

client, err := api.Client.RotateSecret(d.Id())
if err != nil {
Expand Down
Loading

0 comments on commit 49f9644

Please sign in to comment.