diff --git a/auth0/data_source_auth0_client.go b/auth0/data_source_auth0_client.go index e5c8138f5..a19d5246c 100644 --- a/auth0/data_source_auth0_client.go +++ b/auth0/data_source_auth0_client.go @@ -1,18 +1,18 @@ package auth0 import ( - "errors" - "fmt" + "context" "github.com/auth0/go-auth0" "github.com/auth0/go-auth0/management" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) func newDataClient() *schema.Resource { return &schema.Resource{ - Read: readDataClient, - Schema: newClientSchema(), + ReadContext: readDataClient, + Schema: newClientSchema(), } } @@ -23,30 +23,30 @@ func newClientSchema() map[string]*schema.Schema { return clientSchema } -func readDataClient(d *schema.ResourceData, m interface{}) error { +func readDataClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { clientId := auth0.StringValue(String(d, "client_id")) if clientId != "" { d.SetId(clientId) - return readClient(d, m) + return readClient(ctx, d, m) } // If not provided ID, perform looking of client by name name := auth0.StringValue(String(d, "name")) if name == "" { - return errors.New("no 'client_id' or 'name' was specified") + return diag.Errorf("no 'client_id' or 'name' was specified") } api := m.(*management.Management) clients, err := api.Client.List(management.IncludeFields("client_id", "name")) if err != nil { - return err + return diag.FromErr(err) } for _, client := range clients.Clients { if auth0.StringValue(client.Name) == name { clientId = auth0.StringValue(client.ClientID) d.SetId(clientId) - return readClient(d, m) + return readClient(ctx, d, m) } } - return fmt.Errorf("no client found with 'name' = '%s'", name) + return diag.Errorf("no client found with 'name' = '%s'", name) } diff --git a/auth0/data_source_auth0_global_client.go b/auth0/data_source_auth0_global_client.go index 53e8aa61c..9001ee3ee 100644 --- a/auth0/data_source_auth0_global_client.go +++ b/auth0/data_source_auth0_global_client.go @@ -1,19 +1,22 @@ package auth0 import ( + "context" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) func newDataGlobalClient() *schema.Resource { return &schema.Resource{ - Read: readDataGlobalClient, - Schema: newClientSchema(), + ReadContext: readDataGlobalClient, + Schema: newClientSchema(), } } -func readDataGlobalClient(d *schema.ResourceData, m interface{}) error { - if err := readGlobalClientID(d, m); err != nil { +func readDataGlobalClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + if err := readGlobalClientID(ctx, d, m); err != nil { return err } - return readClient(d, m) + return readClient(ctx, d, m) } diff --git a/auth0/data_source_auth0_tenant.go b/auth0/data_source_auth0_tenant.go index 98205b8ad..3bfde10f6 100644 --- a/auth0/data_source_auth0_tenant.go +++ b/auth0/data_source_auth0_tenant.go @@ -1,18 +1,20 @@ package auth0 import ( + "context" "fmt" "net/url" "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/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) func newDataTenant() *schema.Resource { return &schema.Resource{ - Read: readDataTenant, + ReadContext: readDataTenant, Schema: map[string]*schema.Schema{ "domain": { Type: schema.TypeString, @@ -26,12 +28,12 @@ func newDataTenant() *schema.Resource { } } -func readDataTenant(d *schema.ResourceData, m interface{}) error { +func readDataTenant(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) u, err := url.Parse(api.URI()) if err != nil { - return fmt.Errorf("unable to determine management API URL: %w", err) + return diag.FromErr(fmt.Errorf("unable to determine management API URL: %w", err)) } d.SetId(resource.UniqueId()) @@ -40,5 +42,5 @@ func readDataTenant(d *schema.ResourceData, m interface{}) error { result = multierror.Append(result, d.Set("domain", u.Hostname())) result = multierror.Append(result, d.Set("management_api_identifier", u.String())) - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } diff --git a/auth0/provider.go b/auth0/provider.go index 2b098c0e4..9c793700e 100644 --- a/auth0/provider.go +++ b/auth0/provider.go @@ -1,11 +1,13 @@ package auth0 import ( + "context" "fmt" "os" "github.com/auth0/go-auth0" "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/hashicorp/terraform-plugin-sdk/v2/meta" @@ -86,15 +88,17 @@ func Provider() *schema.Provider { }, } - provider.ConfigureFunc = ConfigureProvider(provider.TerraformVersion) + provider.ConfigureContextFunc = ConfigureProvider(provider.TerraformVersion) return provider } // 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) { +func ConfigureProvider( + terraformVersion string, +) func(ctx context.Context, data *schema.ResourceData) (interface{}, diag.Diagnostics) { + return func(ctx context.Context, data *schema.ResourceData) (interface{}, diag.Diagnostics) { providerVersion := version.ProviderVersion sdkVersion := auth0.Version terraformSDKVersion := meta.SDKVersionString() @@ -120,10 +124,15 @@ func ConfigureProvider(terraformVersion string) func(data *schema.ResourceData) authenticationOption = management.WithClientCredentials(clientID, clientSecret) } - return management.New(domain, + apiClient, err := management.New(domain, authenticationOption, management.WithDebug(debug), management.WithUserAgent(userAgent), ) + if err != nil { + return nil, diag.FromErr(err) + } + + return apiClient, nil } } diff --git a/auth0/provider_test.go b/auth0/provider_test.go index 1820e7ae0..f3bc95933 100644 --- a/auth0/provider_test.go +++ b/auth0/provider_test.go @@ -19,12 +19,16 @@ const wiremockHost = "localhost:8080" func providerWithTestingConfiguration() *schema.Provider { provider := Provider() - provider.ConfigureFunc = func(data *schema.ResourceData) (interface{}, error) { - return management.New( + provider.ConfigureContextFunc = func(_ context.Context, data *schema.ResourceData) (interface{}, diag.Diagnostics) { + apiClient, err := management.New( wiremockHost, management.WithInsecure(), management.WithDebug(true), ) + if err != nil { + return nil, diag.FromErr(err) + } + return apiClient, nil } return provider } diff --git a/auth0/resource_auth0_action.go b/auth0/resource_auth0_action.go index d5b6b900b..eb59a8c34 100644 --- a/auth0/resource_auth0_action.go +++ b/auth0/resource_auth0_action.go @@ -1,12 +1,14 @@ package auth0 import ( + "context" "fmt" "net/http" "strings" "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/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -16,12 +18,12 @@ import ( func newAction() *schema.Resource { return &schema.Resource{ - Create: createAction, - Read: readAction, - Update: updateAction, - Delete: deleteAction, + CreateContext: createAction, + ReadContext: readAction, + UpdateContext: updateAction, + DeleteContext: deleteAction, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "name": { @@ -127,25 +129,25 @@ func newAction() *schema.Resource { } } -func createAction(d *schema.ResourceData, m interface{}) error { +func createAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { action := expandAction(d) api := m.(*management.Management) if err := api.Action.Create(action); err != nil { - return err + return diag.FromErr(err) } d.SetId(action.GetID()) d.Partial(true) if err := deployAction(d, m); err != nil { - return err + return diag.FromErr(err) } d.Partial(false) - return readAction(d, m) + return readAction(ctx, d, m) } -func readAction(d *schema.ResourceData, m interface{}) error { +func readAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) action, err := api.Action.Read(d.Id()) if err != nil { @@ -155,7 +157,7 @@ func readAction(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } result := multierror.Append( @@ -169,23 +171,23 @@ func readAction(d *schema.ResourceData, m interface{}) error { result = multierror.Append(result, d.Set("version_id", action.DeployedVersion.GetID())) } - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updateAction(d *schema.ResourceData, m interface{}) error { +func updateAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { action := expandAction(d) api := m.(*management.Management) if err := api.Action.Update(d.Id(), action); err != nil { - return err + return diag.FromErr(err) } d.Partial(true) if err := deployAction(d, m); err != nil { - return err + return diag.FromErr(err) } d.Partial(false) - return readAction(d, m) + return readAction(ctx, d, m) } func deployAction(d *schema.ResourceData, m interface{}) error { @@ -235,7 +237,7 @@ func deployAction(d *schema.ResourceData, m interface{}) error { return nil } -func deleteAction(d *schema.ResourceData, m interface{}) error { +func deleteAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.Action.Delete(d.Id()); err != nil { if mErr, ok := err.(management.Error); ok { @@ -244,7 +246,7 @@ func deleteAction(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } return nil diff --git a/auth0/resource_auth0_attack_protection.go b/auth0/resource_auth0_attack_protection.go index 57cae20ea..c8fb36e49 100644 --- a/auth0/resource_auth0_attack_protection.go +++ b/auth0/resource_auth0_attack_protection.go @@ -1,9 +1,11 @@ package auth0 import ( + "context" "net/http" "github.com/auth0/go-auth0/management" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -11,12 +13,12 @@ import ( func newAttackProtection() *schema.Resource { return &schema.Resource{ - Create: createAttackProtection, - Read: readAttackProtection, - Update: updateAttackProtection, - Delete: deleteAttackProtection, + CreateContext: createAttackProtection, + ReadContext: readAttackProtection, + UpdateContext: updateAttackProtection, + DeleteContext: deleteAttackProtection, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "breached_password_detection": { @@ -207,12 +209,12 @@ func newAttackProtection() *schema.Resource { } } -func createAttackProtection(d *schema.ResourceData, m interface{}) error { +func createAttackProtection(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.SetId(resource.UniqueId()) - return updateAttackProtection(d, m) + return updateAttackProtection(ctx, d, m) } -func readAttackProtection(d *schema.ResourceData, m interface{}) error { +func readAttackProtection(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) ipThrottling, err := api.AttackProtection.GetSuspiciousIPThrottling() @@ -223,11 +225,11 @@ func readAttackProtection(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } if err = d.Set("suspicious_ip_throttling", flattenSuspiciousIPThrottling(ipThrottling)); err != nil { - return err + return diag.FromErr(err) } bruteForce, err := api.AttackProtection.GetBruteForceProtection() @@ -238,11 +240,11 @@ func readAttackProtection(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } if err = d.Set("brute_force_protection", flattenBruteForceProtection(bruteForce)); err != nil { - return err + return diag.FromErr(err) } breachedPasswords, err := api.AttackProtection.GetBreachedPasswordDetection() @@ -253,41 +255,41 @@ func readAttackProtection(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } if err = d.Set("breached_password_detection", flattenBreachedPasswordProtection(breachedPasswords)); err != nil { - return err + return diag.FromErr(err) } return nil } -func updateAttackProtection(d *schema.ResourceData, m interface{}) error { +func updateAttackProtection(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if ipt := expandSuspiciousIPThrottling(d); ipt != nil { if err := api.AttackProtection.UpdateSuspiciousIPThrottling(ipt); err != nil { - return err + return diag.FromErr(err) } } if bfp := expandBruteForceProtection(d); bfp != nil { if err := api.AttackProtection.UpdateBruteForceProtection(bfp); err != nil { - return err + return diag.FromErr(err) } } if bpd := expandBreachedPasswordDetection(d); bpd != nil { if err := api.AttackProtection.UpdateBreachedPasswordDetection(bpd); err != nil { - return err + return diag.FromErr(err) } } - return readAttackProtection(d, m) + return readAttackProtection(ctx, d, m) } -func deleteAttackProtection(d *schema.ResourceData, m interface{}) error { +func deleteAttackProtection(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.SetId("") return nil } diff --git a/auth0/resource_auth0_branding.go b/auth0/resource_auth0_branding.go index 3b35b8668..ac4fd257a 100644 --- a/auth0/resource_auth0_branding.go +++ b/auth0/resource_auth0_branding.go @@ -1,22 +1,24 @@ package auth0 import ( + "context" "net/http" "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/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) func newBranding() *schema.Resource { return &schema.Resource{ - Create: createBranding, - Read: readBranding, - Update: updateBranding, - Delete: deleteBranding, + CreateContext: createBranding, + ReadContext: readBranding, + UpdateContext: updateBranding, + DeleteContext: deleteBranding, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "colors": { @@ -80,12 +82,12 @@ func newBranding() *schema.Resource { } } -func createBranding(d *schema.ResourceData, m interface{}) error { +func createBranding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.SetId(resource.UniqueId()) - return updateBranding(d, m) + return updateBranding(ctx, d, m) } -func readBranding(d *schema.ResourceData, m interface{}) error { +func readBranding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) branding, err := api.Branding.Read() if err != nil { @@ -95,7 +97,7 @@ func readBranding(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } result := multierror.Append( @@ -111,42 +113,42 @@ func readBranding(d *schema.ResourceData, m interface{}) error { tenant, err := api.Tenant.Read() if err != nil { - return err + return diag.FromErr(err) } if tenant.Flags.EnableCustomDomainInEmails != nil && *tenant.Flags.EnableCustomDomainInEmails { if err := setUniversalLogin(d, m); err != nil { d.SetId("") - return err + return diag.FromErr(err) } } - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updateBranding(d *schema.ResourceData, m interface{}) error { +func updateBranding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) branding := buildBranding(d) if err := api.Branding.Update(branding); err != nil { - return err + return diag.FromErr(err) } universalLogin := buildBrandingUniversalLogin(d) if universalLogin.GetBody() != "" { if err := api.Branding.SetUniversalLogin(universalLogin); err != nil { - return err + return diag.FromErr(err) } } - return readBranding(d, m) + return readBranding(ctx, d, m) } -func deleteBranding(d *schema.ResourceData, m interface{}) error { +func deleteBranding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) tenant, err := api.Tenant.Read() if err != nil { - return err + return diag.FromErr(err) } if tenant.Flags.EnableCustomDomainInEmails != nil && *tenant.Flags.EnableCustomDomainInEmails { @@ -160,7 +162,7 @@ func deleteBranding(d *schema.ResourceData, m interface{}) error { } } - return err + return diag.FromErr(err) } func buildBranding(d *schema.ResourceData) *management.Branding { diff --git a/auth0/resource_auth0_client.go b/auth0/resource_auth0_client.go index 085214052..1faa965d1 100644 --- a/auth0/resource_auth0_client.go +++ b/auth0/resource_auth0_client.go @@ -1,12 +1,14 @@ package auth0 import ( + "context" "net/http" "strconv" "github.com/auth0/go-auth0" "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/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -15,12 +17,12 @@ import ( func newClient() *schema.Resource { return &schema.Resource{ - Create: createClient, - Read: readClient, - Update: updateClient, - Delete: deleteClient, + CreateContext: createClient, + ReadContext: readClient, + UpdateContext: updateClient, + DeleteContext: deleteClient, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "name": { @@ -580,21 +582,21 @@ func newClient() *schema.Resource { } } -func createClient(d *schema.ResourceData, m interface{}) error { +func createClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { client, err := expandClient(d) if err != nil { - return err + return diag.FromErr(err) } api := m.(*management.Management) if err := api.Client.Create(client); err != nil { - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(client.ClientID)) - return readClient(d, m) + return readClient(ctx, d, m) } -func readClient(d *schema.ResourceData, m interface{}) error { +func readClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) client, err := api.Client.Read(d.Id()) if err != nil { @@ -604,7 +606,7 @@ func readClient(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } result := multierror.Append( @@ -644,32 +646,32 @@ func readClient(d *schema.ResourceData, m interface{}) error { d.Set("signing_keys", client.SigningKeys), ) - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updateClient(d *schema.ResourceData, m interface{}) error { +func updateClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { client, err := expandClient(d) if err != nil { - return err + return diag.FromErr(err) } api := m.(*management.Management) if clientHasChange(client) { if err := api.Client.Update(d.Id(), client); err != nil { - return err + return diag.FromErr(err) } } d.Partial(true) if err := rotateClientSecret(d, m); err != nil { - return err + return diag.FromErr(err) } d.Partial(false) - return readClient(d, m) + return readClient(ctx, d, m) } -func deleteClient(d *schema.ResourceData, m interface{}) error { +func deleteClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.Client.Delete(d.Id()); err != nil { if mErr, ok := err.(management.Error); ok { diff --git a/auth0/resource_auth0_client_grant.go b/auth0/resource_auth0_client_grant.go index 04c598213..8cbe1acc5 100644 --- a/auth0/resource_auth0_client_grant.go +++ b/auth0/resource_auth0_client_grant.go @@ -1,22 +1,24 @@ package auth0 import ( + "context" "net/http" "github.com/auth0/go-auth0" "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/schema" ) func newClientGrant() *schema.Resource { return &schema.Resource{ - Create: createClientGrant, - Read: readClientGrant, - Update: updateClientGrant, - Delete: deleteClientGrant, + CreateContext: createClientGrant, + ReadContext: readClientGrant, + UpdateContext: updateClientGrant, + DeleteContext: deleteClientGrant, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "client_id": { @@ -38,19 +40,19 @@ func newClientGrant() *schema.Resource { } } -func createClientGrant(d *schema.ResourceData, m interface{}) error { +func createClientGrant(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { clientGrant := buildClientGrant(d) api := m.(*management.Management) if err := api.ClientGrant.Create(clientGrant); err != nil { - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(clientGrant.ID)) - return readClientGrant(d, m) + return readClientGrant(ctx, d, m) } -func readClientGrant(d *schema.ResourceData, m interface{}) error { +func readClientGrant(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) clientGrant, err := api.ClientGrant.Read(d.Id()) if err != nil { @@ -60,7 +62,7 @@ func readClientGrant(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } result := multierror.Append( @@ -69,22 +71,22 @@ func readClientGrant(d *schema.ResourceData, m interface{}) error { d.Set("scope", clientGrant.Scope), ) - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updateClientGrant(d *schema.ResourceData, m interface{}) error { +func updateClientGrant(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { clientGrant := buildClientGrant(d) clientGrant.Audience = nil clientGrant.ClientID = nil api := m.(*management.Management) if err := api.ClientGrant.Update(d.Id(), clientGrant); err != nil { - return err + return diag.FromErr(err) } - return readClientGrant(d, m) + return readClientGrant(ctx, d, m) } -func deleteClientGrant(d *schema.ResourceData, m interface{}) error { +func deleteClientGrant(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.ClientGrant.Delete(d.Id()); err != nil { if mErr, ok := err.(management.Error); ok { @@ -93,7 +95,7 @@ func deleteClientGrant(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } return nil diff --git a/auth0/resource_auth0_connection.go b/auth0/resource_auth0_connection.go index e1a095cbd..b27e24f77 100644 --- a/auth0/resource_auth0_connection.go +++ b/auth0/resource_auth0_connection.go @@ -9,6 +9,7 @@ import ( "github.com/auth0/go-auth0" "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/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) @@ -647,12 +648,12 @@ var connectionSchema = map[string]*schema.Schema{ func newConnection() *schema.Resource { return &schema.Resource{ - Create: createConnection, - Read: readConnection, - Update: updateConnection, - Delete: deleteConnection, + CreateContext: createConnection, + ReadContext: readConnection, + UpdateContext: updateConnection, + DeleteContext: deleteConnection, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: connectionSchema, SchemaVersion: 2, @@ -763,19 +764,19 @@ func connectionSchemaUpgradeV1( return state, nil } -func createConnection(d *schema.ResourceData, m interface{}) error { +func createConnection(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { connection := expandConnection(d) api := m.(*management.Management) if err := api.Connection.Create(connection); err != nil { - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(connection.ID)) - return readConnection(d, m) + return readConnection(ctx, d, m) } -func readConnection(d *schema.ResourceData, m interface{}) error { +func readConnection(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) connection, err := api.Connection.Read(d.Id()) if err != nil { @@ -785,7 +786,7 @@ func readConnection(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(connection.ID)) @@ -810,20 +811,20 @@ func readConnection(d *schema.ResourceData, m interface{}) error { result = multierror.Append(result, d.Set("show_as_button", connection.ShowAsButton)) } - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updateConnection(d *schema.ResourceData, m interface{}) error { +func updateConnection(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { connection := expandConnection(d) api := m.(*management.Management) if err := api.Connection.Update(d.Id(), connection); err != nil { - return err + return diag.FromErr(err) } - return readConnection(d, m) + return readConnection(ctx, d, m) } -func deleteConnection(d *schema.ResourceData, m interface{}) error { +func deleteConnection(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.Connection.Delete(d.Id()); err != nil { if mErr, ok := err.(management.Error); ok { diff --git a/auth0/resource_auth0_custom_domain.go b/auth0/resource_auth0_custom_domain.go index 4b41c7a3d..fc2656af8 100644 --- a/auth0/resource_auth0_custom_domain.go +++ b/auth0/resource_auth0_custom_domain.go @@ -1,22 +1,24 @@ package auth0 import ( + "context" "net/http" "github.com/auth0/go-auth0" "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/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) func newCustomDomain() *schema.Resource { return &schema.Resource{ - Create: createCustomDomain, - Read: readCustomDomain, - Delete: deleteCustomDomain, + CreateContext: createCustomDomain, + ReadContext: readCustomDomain, + DeleteContext: deleteCustomDomain, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "domain": { @@ -65,19 +67,19 @@ func newCustomDomain() *schema.Resource { } } -func createCustomDomain(d *schema.ResourceData, m interface{}) error { +func createCustomDomain(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { customDomain := buildCustomDomain(d) api := m.(*management.Management) if err := api.CustomDomain.Create(customDomain); err != nil { - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(customDomain.ID)) - return readCustomDomain(d, m) + return readCustomDomain(ctx, d, m) } -func readCustomDomain(d *schema.ResourceData, m interface{}) error { +func readCustomDomain(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) customDomain, err := api.CustomDomain.Read(d.Id()) if err != nil { @@ -87,7 +89,7 @@ func readCustomDomain(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } result := multierror.Append( @@ -103,10 +105,10 @@ func readCustomDomain(d *schema.ResourceData, m interface{}) error { })) } - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func deleteCustomDomain(d *schema.ResourceData, m interface{}) error { +func deleteCustomDomain(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.CustomDomain.Delete(d.Id()); err != nil { if mErr, ok := err.(management.Error); ok { diff --git a/auth0/resource_auth0_custom_domain_verification.go b/auth0/resource_auth0_custom_domain_verification.go index fe5d02cd7..2a0366202 100644 --- a/auth0/resource_auth0_custom_domain_verification.go +++ b/auth0/resource_auth0_custom_domain_verification.go @@ -1,23 +1,25 @@ package auth0 import ( + "context" "fmt" "log" "net/http" "time" "github.com/auth0/go-auth0/management" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) func newCustomDomainVerification() *schema.Resource { return &schema.Resource{ - Create: createCustomDomainVerification, - Read: readCustomDomainVerification, - Delete: deleteCustomDomainVerification, + CreateContext: createCustomDomainVerification, + ReadContext: readCustomDomainVerification, + DeleteContext: deleteCustomDomainVerification, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "custom_domain_id": { @@ -32,9 +34,9 @@ func newCustomDomainVerification() *schema.Resource { } } -func createCustomDomainVerification(d *schema.ResourceData, m interface{}) error { +func createCustomDomainVerification(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) - return resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { + err := resource.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { customDomainVerification, err := api.CustomDomain.Verify(d.Get("custom_domain_id").(string)) if err != nil { return resource.NonRetryableError(err) @@ -50,15 +52,17 @@ func createCustomDomainVerification(d *schema.ResourceData, m interface{}) error d.SetId(customDomainVerification.GetID()) - if err := readCustomDomainVerification(d, m); err != nil { - return resource.NonRetryableError(err) + if result := readCustomDomainVerification(ctx, d, m); result.HasError() { + return resource.NonRetryableError(fmt.Errorf("failed to read custom domain verification")) } return nil }) + + return diag.FromErr(err) } -func readCustomDomainVerification(d *schema.ResourceData, m interface{}) error { +func readCustomDomainVerification(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) customDomain, err := api.CustomDomain.Read(d.Id()) if err != nil { @@ -68,13 +72,13 @@ func readCustomDomainVerification(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } - return d.Set("custom_domain_id", customDomain.GetID()) + return diag.FromErr(d.Set("custom_domain_id", customDomain.GetID())) } -func deleteCustomDomainVerification(d *schema.ResourceData, m interface{}) error { +func deleteCustomDomainVerification(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.SetId("") return nil } diff --git a/auth0/resource_auth0_email.go b/auth0/resource_auth0_email.go index 3202c98bc..be5bef5dc 100644 --- a/auth0/resource_auth0_email.go +++ b/auth0/resource_auth0_email.go @@ -1,22 +1,24 @@ package auth0 import ( + "context" "net/http" "github.com/auth0/go-auth0" "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/schema" ) func newEmail() *schema.Resource { return &schema.Resource{ - Create: createEmail, - Read: readEmail, - Update: updateEmail, - Delete: deleteEmail, + CreateContext: createEmail, + ReadContext: readEmail, + UpdateContext: updateEmail, + DeleteContext: deleteEmail, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "name": { @@ -92,19 +94,19 @@ func newEmail() *schema.Resource { } } -func createEmail(d *schema.ResourceData, m interface{}) error { +func createEmail(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { email := buildEmail(d) api := m.(*management.Management) if err := api.Email.Create(email); err != nil { - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(email.Name)) - return readEmail(d, m) + return readEmail(ctx, d, m) } -func readEmail(d *schema.ResourceData, m interface{}) error { +func readEmail(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) email, err := api.Email.Read() if err != nil { @@ -114,7 +116,7 @@ func readEmail(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(email.Name)) @@ -140,20 +142,20 @@ func readEmail(d *schema.ResourceData, m interface{}) error { result = multierror.Append(result, d.Set("credentials", []map[string]interface{}{credentialsMap})) } - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updateEmail(d *schema.ResourceData, m interface{}) error { +func updateEmail(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { email := buildEmail(d) api := m.(*management.Management) if err := api.Email.Update(email); err != nil { - return err + return diag.FromErr(err) } - return readEmail(d, m) + return readEmail(ctx, d, m) } -func deleteEmail(d *schema.ResourceData, m interface{}) error { +func deleteEmail(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.Email.Delete(); err != nil { if mErr, ok := err.(management.Error); ok { diff --git a/auth0/resource_auth0_email_template.go b/auth0/resource_auth0_email_template.go index f6330a3e4..a78ab6147 100644 --- a/auth0/resource_auth0_email_template.go +++ b/auth0/resource_auth0_email_template.go @@ -1,23 +1,25 @@ package auth0 import ( + "context" "net/http" "github.com/auth0/go-auth0" "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/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) func newEmailTemplate() *schema.Resource { return &schema.Resource{ - Create: createEmailTemplate, - Read: readEmailTemplate, - Update: updateEmailTemplate, - Delete: deleteEmailTemplate, + CreateContext: createEmailTemplate, + ReadContext: readEmailTemplate, + UpdateContext: updateEmailTemplate, + DeleteContext: deleteEmailTemplate, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "template": { @@ -69,7 +71,7 @@ func newEmailTemplate() *schema.Resource { } } -func createEmailTemplate(d *schema.ResourceData, m interface{}) error { +func createEmailTemplate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { email := buildEmailTemplate(d) api := m.(*management.Management) @@ -79,7 +81,7 @@ func createEmailTemplate(d *schema.ResourceData, m interface{}) error { if _, err := api.EmailTemplate.Read(auth0.StringValue(email.Template)); err == nil { // We succeeded in reading the template, this means it was created previously. if err := api.EmailTemplate.Update(auth0.StringValue(email.Template), email); err != nil { - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(email.Template)) @@ -90,7 +92,7 @@ func createEmailTemplate(d *schema.ResourceData, m interface{}) error { // If we reached this point the template doesn't exist. // Therefore, it is safe to create it. if err := api.EmailTemplate.Create(email); err != nil { - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(email.Template)) @@ -98,7 +100,7 @@ func createEmailTemplate(d *schema.ResourceData, m interface{}) error { return nil } -func readEmailTemplate(d *schema.ResourceData, m interface{}) error { +func readEmailTemplate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) email, err := api.EmailTemplate.Read(d.Id()) if err != nil { @@ -108,7 +110,7 @@ func readEmailTemplate(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(email.Template)) @@ -124,20 +126,20 @@ func readEmailTemplate(d *schema.ResourceData, m interface{}) error { d.Set("enabled", email.Enabled), ) - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updateEmailTemplate(d *schema.ResourceData, m interface{}) error { +func updateEmailTemplate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { email := buildEmailTemplate(d) api := m.(*management.Management) if err := api.EmailTemplate.Update(d.Id(), email); err != nil { - return err + return diag.FromErr(err) } - return readEmailTemplate(d, m) + return readEmailTemplate(ctx, d, m) } -func deleteEmailTemplate(d *schema.ResourceData, m interface{}) error { +func deleteEmailTemplate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) emailTemplate := &management.EmailTemplate{ Template: auth0.String(d.Id()), diff --git a/auth0/resource_auth0_global_client.go b/auth0/resource_auth0_global_client.go index 49b994ff1..c184deac5 100644 --- a/auth0/resource_auth0_global_client.go +++ b/auth0/resource_auth0_global_client.go @@ -1,16 +1,17 @@ package auth0 import ( - "errors" + "context" "github.com/auth0/go-auth0/management" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) func newGlobalClient() *schema.Resource { client := newClient() - client.Create = createGlobalClient - client.Delete = deleteGlobalClient + client.CreateContext = createGlobalClient + client.DeleteContext = deleteGlobalClient exclude := []string{"client_secret_rotation_trigger"} @@ -40,26 +41,26 @@ func in(needle string, haystack []string) bool { return false } -func createGlobalClient(d *schema.ResourceData, m interface{}) error { - if err := readGlobalClientID(d, m); err != nil { +func createGlobalClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + if err := readGlobalClientID(ctx, d, m); err != nil { return err } - return updateClient(d, m) + return updateClient(ctx, d, m) } -func readGlobalClientID(d *schema.ResourceData, m interface{}) error { +func readGlobalClientID(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) clients, err := api.Client.List(management.Parameter("is_global", "true"), management.IncludeFields("client_id")) if err != nil { - return err + return diag.FromErr(err) } if len(clients.Clients) == 0 { - return errors.New("no auth0 global client found") + return diag.Errorf("no auth0 global client found") } d.SetId(clients.Clients[0].GetClientID()) return nil } -func deleteGlobalClient(d *schema.ResourceData, m interface{}) error { +func deleteGlobalClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { return nil } diff --git a/auth0/resource_auth0_guardian.go b/auth0/resource_auth0_guardian.go index 19bf497a9..22089aae0 100644 --- a/auth0/resource_auth0_guardian.go +++ b/auth0/resource_auth0_guardian.go @@ -1,8 +1,11 @@ package auth0 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/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -10,12 +13,12 @@ import ( func newGuardian() *schema.Resource { return &schema.Resource{ - Create: createGuardian, - Read: readGuardian, - Update: updateGuardian, - Delete: deleteGuardian, + CreateContext: createGuardian, + ReadContext: readGuardian, + UpdateContext: updateGuardian, + DeleteContext: deleteGuardian, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "policy": { @@ -103,27 +106,27 @@ func newGuardian() *schema.Resource { } } -func createGuardian(d *schema.ResourceData, m interface{}) error { +func createGuardian(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.SetId(resource.UniqueId()) - return updateGuardian(d, m) + return updateGuardian(ctx, d, m) } -func deleteGuardian(d *schema.ResourceData, m interface{}) error { +func deleteGuardian(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.Guardian.MultiFactor.Phone.Enable(false); err != nil { - return err + return diag.FromErr(err) } if err := api.Guardian.MultiFactor.Email.Enable(false); err != nil { - return err + return diag.FromErr(err) } if err := api.Guardian.MultiFactor.OTP.Enable(false); err != nil { - return err + return diag.FromErr(err) } d.SetId("") return nil } -func updateGuardian(d *schema.ResourceData, m interface{}) error { +func updateGuardian(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if d.HasChange("policy") { @@ -131,26 +134,26 @@ func updateGuardian(d *schema.ResourceData, m interface{}) error { if policy == "never" { // Passing empty array to set it to the "never" policy. if err := api.Guardian.MultiFactor.UpdatePolicy(&management.MultiFactorPolicies{}); err != nil { - return err + return diag.FromErr(err) } } else { if err := api.Guardian.MultiFactor.UpdatePolicy(&management.MultiFactorPolicies{policy}); err != nil { - return err + return diag.FromErr(err) } } } if err := updatePhoneFactor(d, api); err != nil { - return err + return diag.FromErr(err) } if err := updateEmailFactor(d, api); err != nil { - return err + return diag.FromErr(err) } if err := updateOTPFactor(d, api); err != nil { - return err + return diag.FromErr(err) } - return readGuardian(d, m) + return readGuardian(ctx, d, m) } func updatePhoneFactor(d *schema.ResourceData, api *management.Management) error { @@ -276,13 +279,13 @@ func updateTwilioOptions(opts Iterator, api *management.Management) error { ) } -func readGuardian(d *schema.ResourceData, m interface{}) error { +func readGuardian(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { var result *multierror.Error api := m.(*management.Management) messageTypes, err := api.Guardian.MultiFactor.Phone.MessageTypes() if err != nil { - return err + return diag.FromErr(err) } phoneData := make(map[string]interface{}) @@ -290,13 +293,13 @@ func readGuardian(d *schema.ResourceData, m interface{}) error { phoneProvider, err := api.Guardian.MultiFactor.Phone.Provider() if err != nil { - return err + return diag.FromErr(err) } phoneData["provider"] = phoneProvider.Provider policy, err := api.Guardian.MultiFactor.Policy() if err != nil { - return err + return diag.FromErr(err) } if len(*policy) == 0 { @@ -313,12 +316,12 @@ func readGuardian(d *schema.ResourceData, m interface{}) error { phoneProviderFlattenedOptions, err = flattenAuth0Options(api) } if err != nil { - return err + return diag.FromErr(err) } ok, err := factorShouldBeUpdated(d, "phone") if err != nil { - return err + return diag.FromErr(err) } if ok { phoneData["options"] = []interface{}{phoneProviderFlattenedOptions} @@ -329,7 +332,7 @@ func readGuardian(d *schema.ResourceData, m interface{}) error { factors, err := api.Guardian.MultiFactor.List() if err != nil { - return err + return diag.FromErr(err) } for _, factor := range factors { @@ -343,7 +346,7 @@ func readGuardian(d *schema.ResourceData, m interface{}) error { } } - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } func hasBlockPresentInNewState(d *schema.ResourceData, factor string) bool { diff --git a/auth0/resource_auth0_hook.go b/auth0/resource_auth0_hook.go index 5bcf264f9..c015e10a4 100644 --- a/auth0/resource_auth0_hook.go +++ b/auth0/resource_auth0_hook.go @@ -1,24 +1,26 @@ package auth0 import ( + "context" "net/http" "regexp" "github.com/auth0/go-auth0" "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/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) func newHook() *schema.Resource { return &schema.Resource{ - Create: createHook, - Read: readHook, - Update: updateHook, - Delete: deleteHook, + CreateContext: createHook, + ReadContext: readHook, + UpdateContext: updateHook, + DeleteContext: deleteHook, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "name": { @@ -70,23 +72,23 @@ func newHook() *schema.Resource { } } -func createHook(d *schema.ResourceData, m interface{}) error { +func createHook(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { hook := buildHook(d) api := m.(*management.Management) if err := api.Hook.Create(hook); err != nil { - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(hook.ID)) if err := upsertHookSecrets(d, m); err != nil { - return err + return diag.FromErr(err) } - return readHook(d, m) + return readHook(ctx, d, m) } -func readHook(d *schema.ResourceData, m interface{}) error { +func readHook(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) hook, err := api.Hook.Read(d.Id()) if err != nil { @@ -96,7 +98,7 @@ func readHook(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } result := multierror.Append( @@ -107,21 +109,21 @@ func readHook(d *schema.ResourceData, m interface{}) error { d.Set("enabled", hook.Enabled), ) - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updateHook(d *schema.ResourceData, m interface{}) error { +func updateHook(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { hook := buildHook(d) api := m.(*management.Management) if err := api.Hook.Update(d.Id(), hook); err != nil { - return err + return diag.FromErr(err) } if err := upsertHookSecrets(d, m); err != nil { - return err + return diag.FromErr(err) } - return readHook(d, m) + return readHook(ctx, d, m) } func upsertHookSecrets(d *schema.ResourceData, m interface{}) error { @@ -145,7 +147,7 @@ func toHookSecrets(val map[string]interface{}) management.HookSecrets { return hookSecrets } -func deleteHook(d *schema.ResourceData, m interface{}) error { +func deleteHook(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.Hook.Delete(d.Id()); err != nil { if mErr, ok := err.(management.Error); ok { @@ -154,7 +156,7 @@ func deleteHook(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } return nil diff --git a/auth0/resource_auth0_log_stream.go b/auth0/resource_auth0_log_stream.go index ec683d709..161f01ac2 100644 --- a/auth0/resource_auth0_log_stream.go +++ b/auth0/resource_auth0_log_stream.go @@ -1,23 +1,25 @@ package auth0 import ( + "context" "log" "net/http" "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/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) func newLogStream() *schema.Resource { return &schema.Resource{ - Create: createLogStream, - Read: readLogStream, - Update: updateLogStream, - Delete: deleteLogStream, + CreateContext: createLogStream, + ReadContext: readLogStream, + UpdateContext: updateLogStream, + DeleteContext: deleteLogStream, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "name": { @@ -180,12 +182,12 @@ func newLogStream() *schema.Resource { } } -func createLogStream(d *schema.ResourceData, m interface{}) error { +func createLogStream(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { logStream := buildLogStream(d) api := m.(*management.Management) if err := api.LogStream.Create(logStream); err != nil { - return err + return diag.FromErr(err) } d.SetId(logStream.GetID()) @@ -196,14 +198,14 @@ func createLogStream(d *schema.ResourceData, m interface{}) error { status := String(d, "status") if status != nil && status != logStream.Status { if err := api.LogStream.Update(logStream.GetID(), &management.LogStream{Status: status}); err != nil { - return err + return diag.FromErr(err) } } - return readLogStream(d, m) + return readLogStream(ctx, d, m) } -func readLogStream(d *schema.ResourceData, m interface{}) error { +func readLogStream(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) logStream, err := api.LogStream.Read(d.Id()) if err != nil { @@ -213,7 +215,7 @@ func readLogStream(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } result := multierror.Append( @@ -223,20 +225,20 @@ func readLogStream(d *schema.ResourceData, m interface{}) error { d.Set("sink", flattenLogStreamSink(logStream.Sink)), ) - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updateLogStream(d *schema.ResourceData, m interface{}) error { +func updateLogStream(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { logStream := buildLogStream(d) api := m.(*management.Management) if err := api.LogStream.Update(d.Id(), logStream); err != nil { - return err + return diag.FromErr(err) } - return readLogStream(d, m) + return readLogStream(ctx, d, m) } -func deleteLogStream(d *schema.ResourceData, m interface{}) error { +func deleteLogStream(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.LogStream.Delete(d.Id()); err != nil { if mErr, ok := err.(management.Error); ok { diff --git a/auth0/resource_auth0_organization.go b/auth0/resource_auth0_organization.go index 33e2c9c85..a707806a5 100644 --- a/auth0/resource_auth0_organization.go +++ b/auth0/resource_auth0_organization.go @@ -1,12 +1,14 @@ package auth0 import ( + "context" "fmt" "log" "net/http" "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/schema" "github.com/auth0/terraform-provider-auth0/auth0/internal/hash" @@ -14,12 +16,12 @@ import ( func newOrganization() *schema.Resource { return &schema.Resource{ - Create: createOrganization, - Read: readOrganization, - Update: updateOrganization, - Delete: deleteOrganization, + CreateContext: createOrganization, + ReadContext: readOrganization, + UpdateContext: updateOrganization, + DeleteContext: deleteOrganization, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "name": { @@ -81,22 +83,22 @@ func newOrganization() *schema.Resource { } } -func createOrganization(d *schema.ResourceData, m interface{}) error { +func createOrganization(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { organization := buildOrganization(d) api := m.(*management.Management) if err := api.Organization.Create(organization); err != nil { - return err + return diag.FromErr(err) } d.SetId(organization.GetID()) d.Partial(true) if err := assignOrganizationConnections(d, m); err != nil { - return fmt.Errorf("failed assigning organization connections. %w", err) + return diag.FromErr(fmt.Errorf("failed assigning organization connections. %w", err)) } d.Partial(false) - return readOrganization(d, m) + return readOrganization(ctx, d, m) } func assignOrganizationConnections(d *schema.ResourceData, m interface{}) (err error) { @@ -149,7 +151,7 @@ func assignOrganizationConnections(d *schema.ResourceData, m interface{}) (err e return nil } -func readOrganization(d *schema.ResourceData, m interface{}) error { +func readOrganization(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) organization, err := api.Organization.Read(d.Id()) if err != nil { @@ -159,12 +161,12 @@ func readOrganization(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } organizationConnectionList, err := api.Organization.Connections(d.Id()) if err != nil { - return err + return diag.FromErr(err) } result := multierror.Append( @@ -175,26 +177,26 @@ func readOrganization(d *schema.ResourceData, m interface{}) error { d.Set("connections", flattenOrganizationConnections(organizationConnectionList)), ) - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updateOrganization(d *schema.ResourceData, m interface{}) error { +func updateOrganization(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { organization := buildOrganization(d) api := m.(*management.Management) if err := api.Organization.Update(d.Id(), organization); err != nil { - return err + return diag.FromErr(err) } d.Partial(true) if err := assignOrganizationConnections(d, m); err != nil { - return fmt.Errorf("failed updating organization connections. %w", err) + return diag.FromErr(fmt.Errorf("failed updating organization connections. %w", err)) } d.Partial(false) - return readOrganization(d, m) + return readOrganization(ctx, d, m) } -func deleteOrganization(d *schema.ResourceData, m interface{}) error { +func deleteOrganization(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.Organization.Delete(d.Id()); err != nil { if mErr, ok := err.(management.Error); ok { diff --git a/auth0/resource_auth0_prompt.go b/auth0/resource_auth0_prompt.go index 926299cb9..f7811ef28 100644 --- a/auth0/resource_auth0_prompt.go +++ b/auth0/resource_auth0_prompt.go @@ -1,11 +1,13 @@ package auth0 import ( + "context" "net/http" "github.com/auth0/go-auth0" "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/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -13,12 +15,12 @@ import ( func newPrompt() *schema.Resource { return &schema.Resource{ - Create: createPrompt, - Read: readPrompt, - Update: updatePrompt, - Delete: deletePrompt, + CreateContext: createPrompt, + ReadContext: readPrompt, + UpdateContext: updatePrompt, + DeleteContext: deletePrompt, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "universal_login_experience": { @@ -36,12 +38,12 @@ func newPrompt() *schema.Resource { } } -func createPrompt(d *schema.ResourceData, m interface{}) error { +func createPrompt(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.SetId(resource.UniqueId()) - return updatePrompt(d, m) + return updatePrompt(ctx, d, m) } -func readPrompt(d *schema.ResourceData, m interface{}) error { +func readPrompt(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) prompt, err := api.Prompt.Read() if err != nil { @@ -51,7 +53,7 @@ func readPrompt(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } result := multierror.Append( @@ -59,20 +61,20 @@ func readPrompt(d *schema.ResourceData, m interface{}) error { d.Set("identifier_first", prompt.IdentifierFirst), ) - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updatePrompt(d *schema.ResourceData, m interface{}) error { +func updatePrompt(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { prompt := buildPrompt(d) api := m.(*management.Management) if err := api.Prompt.Update(prompt); err != nil { - return err + return diag.FromErr(err) } - return readPrompt(d, m) + return readPrompt(ctx, d, m) } -func deletePrompt(d *schema.ResourceData, m interface{}) error { +func deletePrompt(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.SetId("") return nil } diff --git a/auth0/resource_auth0_prompt_custom_text.go b/auth0/resource_auth0_prompt_custom_text.go index 2c712d847..73f4a954b 100644 --- a/auth0/resource_auth0_prompt_custom_text.go +++ b/auth0/resource_auth0_prompt_custom_text.go @@ -2,6 +2,7 @@ package auth0 import ( "bytes" + "context" "encoding/json" "fmt" "net/http" @@ -9,6 +10,7 @@ import ( "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/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -32,12 +34,12 @@ var ( func newPromptCustomText() *schema.Resource { return &schema.Resource{ - Create: createPromptCustomText, - Read: readPromptCustomText, - Update: updatePromptCustomText, - Delete: deletePromptCustomText, + CreateContext: createPromptCustomText, + ReadContext: readPromptCustomText, + UpdateContext: updatePromptCustomText, + DeleteContext: deletePromptCustomText, Importer: &schema.ResourceImporter{ - State: importPromptCustomText, + StateContext: importPromptCustomText, }, Schema: map[string]*schema.Schema{ "prompt": { @@ -60,7 +62,7 @@ func newPromptCustomText() *schema.Resource { } } -func importPromptCustomText(d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) { +func importPromptCustomText(ctx context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) { prompt, language, err := getPromptAndLanguage(d) if err != nil { return []*schema.ResourceData{}, err @@ -76,12 +78,12 @@ func importPromptCustomText(d *schema.ResourceData, _ interface{}) ([]*schema.Re return []*schema.ResourceData{d}, result.ErrorOrNil() } -func createPromptCustomText(d *schema.ResourceData, m interface{}) error { +func createPromptCustomText(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.SetId(d.Get("prompt").(string) + ":" + d.Get("language").(string)) - return updatePromptCustomText(d, m) + return updatePromptCustomText(ctx, d, m) } -func readPromptCustomText(d *schema.ResourceData, m interface{}) error { +func readPromptCustomText(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) customText, err := api.Prompt.CustomText(d.Get("prompt").(string), d.Get("language").(string)) if err != nil { @@ -91,43 +93,43 @@ func readPromptCustomText(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } body, err := marshalCustomTextBody(customText) if err != nil { - return err + return diag.FromErr(err) } - return d.Set("body", body) + return diag.FromErr(d.Set("body", body)) } -func updatePromptCustomText(d *schema.ResourceData, m interface{}) error { +func updatePromptCustomText(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) prompt, language, err := getPromptAndLanguage(d) if err != nil { - return err + return diag.FromErr(err) } if d.Get("body").(string) != "" { var body map[string]interface{} if err := json.Unmarshal([]byte(d.Get("body").(string)), &body); err != nil { - return err + return diag.FromErr(err) } if err := api.Prompt.SetCustomText(prompt, language, body); err != nil { - return err + return diag.FromErr(err) } } - return readPromptCustomText(d, m) + return readPromptCustomText(ctx, d, m) } -func deletePromptCustomText(d *schema.ResourceData, m interface{}) error { +func deletePromptCustomText(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { if err := d.Set("body", "{}"); err != nil { - return err + return diag.FromErr(err) } - if err := updatePromptCustomText(d, m); err != nil { + if err := updatePromptCustomText(ctx, d, m); err != nil { return err } diff --git a/auth0/resource_auth0_resource_server.go b/auth0/resource_auth0_resource_server.go index 2892bbfeb..e3a08ecb6 100644 --- a/auth0/resource_auth0_resource_server.go +++ b/auth0/resource_auth0_resource_server.go @@ -1,24 +1,26 @@ package auth0 import ( + "context" "fmt" "net/http" "github.com/auth0/go-auth0" "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/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) func newResourceServer() *schema.Resource { return &schema.Resource{ - Create: createResourceServer, - Read: readResourceServer, - Update: updateResourceServer, - Delete: deleteResourceServer, + CreateContext: createResourceServer, + ReadContext: readResourceServer, + UpdateContext: updateResourceServer, + DeleteContext: deleteResourceServer, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "name": { @@ -111,19 +113,19 @@ func newResourceServer() *schema.Resource { } } -func createResourceServer(d *schema.ResourceData, m interface{}) error { +func createResourceServer(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { resourceServer := expandResourceServer(d) api := m.(*management.Management) if err := api.ResourceServer.Create(resourceServer); err != nil { - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(resourceServer.ID)) - return readResourceServer(d, m) + return readResourceServer(ctx, d, m) } -func readResourceServer(d *schema.ResourceData, m interface{}) error { +func readResourceServer(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) resourceServer, err := api.ResourceServer.Read(d.Id()) if err != nil { @@ -133,7 +135,7 @@ func readResourceServer(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } result := multierror.Append( @@ -164,22 +166,22 @@ func readResourceServer(d *schema.ResourceData, m interface{}) error { d.Set("token_dialect", resourceServer.TokenDialect), ) - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updateResourceServer(d *schema.ResourceData, m interface{}) error { +func updateResourceServer(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { resourceServer := expandResourceServer(d) resourceServer.Identifier = nil api := m.(*management.Management) if err := api.ResourceServer.Update(d.Id(), resourceServer); err != nil { - return err + return diag.FromErr(err) } - return readResourceServer(d, m) + return readResourceServer(ctx, d, m) } -func deleteResourceServer(d *schema.ResourceData, m interface{}) error { +func deleteResourceServer(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.ResourceServer.Delete(d.Id()); err != nil { if mErr, ok := err.(management.Error); ok { diff --git a/auth0/resource_auth0_role.go b/auth0/resource_auth0_role.go index 31f681ddc..1e8f4a5ef 100644 --- a/auth0/resource_auth0_role.go +++ b/auth0/resource_auth0_role.go @@ -1,22 +1,24 @@ package auth0 import ( + "context" "net/http" "github.com/auth0/go-auth0" "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/schema" ) func newRole() *schema.Resource { return &schema.Resource{ - Create: createRole, - Update: updateRole, - Read: readRole, - Delete: deleteRole, + CreateContext: createRole, + UpdateContext: updateRole, + ReadContext: readRole, + DeleteContext: deleteRole, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "name": { @@ -47,11 +49,11 @@ func newRole() *schema.Resource { } } -func createRole(d *schema.ResourceData, m interface{}) error { +func createRole(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { role := expandRole(d) api := m.(*management.Management) if err := api.Role.Create(role); err != nil { - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(role.ID)) @@ -63,16 +65,16 @@ func createRole(d *schema.ResourceData, m interface{}) error { // See: https://www.terraform.io/docs/extend/writing-custom-providers.html d.Partial(true) if err := assignRolePermissions(d, m); err != nil { - return err + return diag.FromErr(err) } // We succeeded, disable partial mode. // This causes Terraform to save all fields again. d.Partial(false) - return readRole(d, m) + return readRole(ctx, d, m) } -func readRole(d *schema.ResourceData, m interface{}) error { +func readRole(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) role, err := api.Role.Read(d.Id()) if err != nil { @@ -82,7 +84,7 @@ func readRole(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } d.SetId(role.GetID()) @@ -97,7 +99,7 @@ func readRole(d *schema.ResourceData, m interface{}) error { for { permissionList, err := api.Role.Permissions(d.Id(), management.Page(page)) if err != nil { - return err + return diag.FromErr(err) } for _, permission := range permissionList.Permissions { permissions = append(permissions, permission) @@ -110,26 +112,26 @@ func readRole(d *schema.ResourceData, m interface{}) error { result = multierror.Append(result, d.Set("permissions", flattenRolePermissions(permissions))) - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updateRole(d *schema.ResourceData, m interface{}) error { +func updateRole(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { role := expandRole(d) api := m.(*management.Management) if err := api.Role.Update(d.Id(), role); err != nil { - return err + return diag.FromErr(err) } d.Partial(true) if err := assignRolePermissions(d, m); err != nil { - return err + return diag.FromErr(err) } d.Partial(false) - return readRole(d, m) + return readRole(ctx, d, m) } -func deleteRole(d *schema.ResourceData, m interface{}) error { +func deleteRole(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.Role.Delete(d.Id()); err != nil { if mErr, ok := err.(management.Error); ok { diff --git a/auth0/resource_auth0_rule.go b/auth0/resource_auth0_rule.go index 11504f74a..771d968a9 100644 --- a/auth0/resource_auth0_rule.go +++ b/auth0/resource_auth0_rule.go @@ -1,12 +1,14 @@ package auth0 import ( + "context" "net/http" "regexp" "github.com/auth0/go-auth0" "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/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) @@ -15,12 +17,12 @@ var ruleNameRegexp = regexp.MustCompile("^[^\\s-][\\w -]+[^\\s-]$") func newRule() *schema.Resource { return &schema.Resource{ - Create: createRule, - Read: readRule, - Update: updateRule, - Delete: deleteRule, + CreateContext: createRule, + ReadContext: readRule, + UpdateContext: updateRule, + DeleteContext: deleteRule, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "name": { @@ -48,19 +50,19 @@ func newRule() *schema.Resource { } } -func createRule(d *schema.ResourceData, m interface{}) error { +func createRule(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { rule := buildRule(d) api := m.(*management.Management) if err := api.Rule.Create(rule); err != nil { - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(rule.ID)) - return readRule(d, m) + return readRule(ctx, d, m) } -func readRule(d *schema.ResourceData, m interface{}) error { +func readRule(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) rule, err := api.Rule.Read(d.Id()) if err != nil { @@ -70,7 +72,7 @@ func readRule(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } result := multierror.Append( @@ -80,20 +82,20 @@ func readRule(d *schema.ResourceData, m interface{}) error { d.Set("enabled", rule.Enabled), ) - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updateRule(d *schema.ResourceData, m interface{}) error { +func updateRule(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { rule := buildRule(d) api := m.(*management.Management) if err := api.Rule.Update(d.Id(), rule); err != nil { - return err + return diag.FromErr(err) } - return readRule(d, m) + return readRule(ctx, d, m) } -func deleteRule(d *schema.ResourceData, m interface{}) error { +func deleteRule(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.Rule.Delete(d.Id()); err != nil { if mErr, ok := err.(management.Error); ok { @@ -102,7 +104,7 @@ func deleteRule(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } return nil diff --git a/auth0/resource_auth0_rule_config.go b/auth0/resource_auth0_rule_config.go index 218cf6b42..861d9f3b0 100644 --- a/auth0/resource_auth0_rule_config.go +++ b/auth0/resource_auth0_rule_config.go @@ -1,21 +1,23 @@ package auth0 import ( + "context" "net/http" "github.com/auth0/go-auth0" "github.com/auth0/go-auth0/management" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) func newRuleConfig() *schema.Resource { return &schema.Resource{ - Create: createRuleConfig, - Read: readRuleConfig, - Update: updateRuleConfig, - Delete: deleteRuleConfig, + CreateContext: createRuleConfig, + ReadContext: readRuleConfig, + UpdateContext: updateRuleConfig, + DeleteContext: deleteRuleConfig, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "key": { @@ -32,21 +34,21 @@ func newRuleConfig() *schema.Resource { } } -func createRuleConfig(d *schema.ResourceData, m interface{}) error { +func createRuleConfig(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { ruleConfig := buildRuleConfig(d) key := auth0.StringValue(ruleConfig.Key) ruleConfig.Key = nil api := m.(*management.Management) if err := api.RuleConfig.Upsert(key, ruleConfig); err != nil { - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(ruleConfig.Key)) - return readRuleConfig(d, m) + return readRuleConfig(ctx, d, m) } -func readRuleConfig(d *schema.ResourceData, m interface{}) error { +func readRuleConfig(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) ruleConfig, err := api.RuleConfig.Read(d.Id()) if err != nil { @@ -56,24 +58,24 @@ func readRuleConfig(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } - return d.Set("key", ruleConfig.Key) + return diag.FromErr(d.Set("key", ruleConfig.Key)) } -func updateRuleConfig(d *schema.ResourceData, m interface{}) error { +func updateRuleConfig(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { ruleConfig := buildRuleConfig(d) ruleConfig.Key = nil api := m.(*management.Management) if err := api.RuleConfig.Upsert(d.Id(), ruleConfig); err != nil { - return err + return diag.FromErr(err) } - return readRuleConfig(d, m) + return readRuleConfig(ctx, d, m) } -func deleteRuleConfig(d *schema.ResourceData, m interface{}) error { +func deleteRuleConfig(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.RuleConfig.Delete(d.Id()); err != nil { if mErr, ok := err.(management.Error); ok { diff --git a/auth0/resource_auth0_tenant.go b/auth0/resource_auth0_tenant.go index 6a524dff4..5bcfdee72 100644 --- a/auth0/resource_auth0_tenant.go +++ b/auth0/resource_auth0_tenant.go @@ -1,10 +1,12 @@ package auth0 import ( + "context" "net/http" "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/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -14,12 +16,12 @@ import ( func newTenant() *schema.Resource { return &schema.Resource{ - Create: createTenant, - Read: readTenant, - Update: updateTenant, - Delete: deleteTenant, + CreateContext: createTenant, + ReadContext: readTenant, + UpdateContext: updateTenant, + DeleteContext: deleteTenant, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "change_password": { @@ -241,12 +243,12 @@ func newTenant() *schema.Resource { } } -func createTenant(d *schema.ResourceData, m interface{}) error { +func createTenant(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.SetId(resource.UniqueId()) - return updateTenant(d, m) + return updateTenant(ctx, d, m) } -func readTenant(d *schema.ResourceData, m interface{}) error { +func readTenant(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) tenant, err := api.Tenant.Read() if err != nil { @@ -256,7 +258,7 @@ func readTenant(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } result := multierror.Append( @@ -279,20 +281,20 @@ func readTenant(d *schema.ResourceData, m interface{}) error { d.Set("universal_login", flattenTenantUniversalLogin(tenant.UniversalLogin)), ) - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func updateTenant(d *schema.ResourceData, m interface{}) error { +func updateTenant(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { tenant := buildTenant(d) api := m.(*management.Management) if err := api.Tenant.Update(tenant); err != nil { - return err + return diag.FromErr(err) } - return readTenant(d, m) + return readTenant(ctx, d, m) } -func deleteTenant(d *schema.ResourceData, m interface{}) error { +func deleteTenant(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.SetId("") return nil } diff --git a/auth0/resource_auth0_trigger_binding.go b/auth0/resource_auth0_trigger_binding.go index 125219893..37e8ebc7a 100644 --- a/auth0/resource_auth0_trigger_binding.go +++ b/auth0/resource_auth0_trigger_binding.go @@ -1,22 +1,24 @@ package auth0 import ( + "context" "net/http" "github.com/auth0/go-auth0" "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/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) func newTriggerBinding() *schema.Resource { return &schema.Resource{ - Create: createTriggerBinding, - Read: readTriggerBinding, - Update: updateTriggerBinding, - Delete: deleteTriggerBinding, + CreateContext: createTriggerBinding, + ReadContext: readTriggerBinding, + UpdateContext: updateTriggerBinding, + DeleteContext: deleteTriggerBinding, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "trigger": { @@ -60,20 +62,20 @@ func newTriggerBinding() *schema.Resource { } } -func createTriggerBinding(d *schema.ResourceData, m interface{}) error { +func createTriggerBinding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { id := d.Get("trigger").(string) triggerBindings := expandTriggerBindings(d) api := m.(*management.Management) if err := api.Action.UpdateBindings(id, triggerBindings); err != nil { - return err + return diag.FromErr(err) } d.SetId(id) - return readTriggerBinding(d, m) + return readTriggerBinding(ctx, d, m) } -func readTriggerBinding(d *schema.ResourceData, m interface{}) error { +func readTriggerBinding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) triggerBindings, err := api.Action.Bindings(d.Id()) if err != nil { @@ -83,23 +85,23 @@ func readTriggerBinding(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } - return d.Set("actions", flattenTriggerBindingActions(triggerBindings.Bindings)) + return diag.FromErr(d.Set("actions", flattenTriggerBindingActions(triggerBindings.Bindings))) } -func updateTriggerBinding(d *schema.ResourceData, m interface{}) error { +func updateTriggerBinding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { triggerBindings := expandTriggerBindings(d) api := m.(*management.Management) if err := api.Action.UpdateBindings(d.Id(), triggerBindings); err != nil { - return err + return diag.FromErr(err) } - return readTriggerBinding(d, m) + return readTriggerBinding(ctx, d, m) } -func deleteTriggerBinding(d *schema.ResourceData, m interface{}) error { +func deleteTriggerBinding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.Action.UpdateBindings(d.Id(), []*management.ActionBinding{}); err != nil { if mErr, ok := err.(management.Error); ok { @@ -108,7 +110,7 @@ func deleteTriggerBinding(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } return nil diff --git a/auth0/resource_auth0_user.go b/auth0/resource_auth0_user.go index fa8963f20..910b35049 100644 --- a/auth0/resource_auth0_user.go +++ b/auth0/resource_auth0_user.go @@ -1,6 +1,7 @@ package auth0 import ( + "context" "fmt" "net/http" "strings" @@ -8,6 +9,7 @@ import ( "github.com/auth0/go-auth0" "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/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -17,12 +19,12 @@ type validateUserFunc func(*management.User) error func newUser() *schema.Resource { return &schema.Resource{ - Create: createUser, - Read: readUser, - Update: updateUser, - Delete: deleteUser, + CreateContext: createUser, + ReadContext: readUser, + UpdateContext: updateUser, + DeleteContext: deleteUser, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ @@ -118,7 +120,7 @@ func newUser() *schema.Resource { } } -func readUser(d *schema.ResourceData, m interface{}) error { +func readUser(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) user, err := api.User.Read(d.Id()) if err != nil { @@ -128,7 +130,7 @@ func readUser(d *schema.ResourceData, m interface{}) error { return nil } } - return err + return diag.FromErr(err) } result := multierror.Append( @@ -149,19 +151,19 @@ func readUser(d *schema.ResourceData, m interface{}) error { userMeta, err := structure.FlattenJsonToString(user.UserMetadata) if err != nil { - return err + return diag.FromErr(err) } result = multierror.Append(result, d.Set("user_metadata", userMeta)) appMeta, err := structure.FlattenJsonToString(user.AppMetadata) if err != nil { - return err + return diag.FromErr(err) } result = multierror.Append(result, d.Set("app_metadata", appMeta)) roleList, err := api.User.Roles(d.Id()) if err != nil { - return err + return diag.FromErr(err) } result = multierror.Append( result, @@ -174,57 +176,57 @@ func readUser(d *schema.ResourceData, m interface{}) error { }()), ) - return result.ErrorOrNil() + return diag.FromErr(result.ErrorOrNil()) } -func createUser(d *schema.ResourceData, m interface{}) error { +func createUser(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { user, err := buildUser(d) if err != nil { - return err + return diag.FromErr(err) } api := m.(*management.Management) if err := api.User.Create(user); err != nil { - return err + return diag.FromErr(err) } d.SetId(auth0.StringValue(user.ID)) d.Partial(true) if err = assignUserRoles(d, m); err != nil { - return err + return diag.FromErr(err) } d.Partial(false) - return readUser(d, m) + return readUser(ctx, d, m) } -func updateUser(d *schema.ResourceData, m interface{}) error { +func updateUser(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { user, err := buildUser(d) if err != nil { - return err + return diag.FromErr(err) } if err = validateUser(user); err != nil { - return err + return diag.FromErr(err) } api := m.(*management.Management) if userHasChange(user) { if err := api.User.Update(d.Id(), user); err != nil { - return err + return diag.FromErr(err) } } d.Partial(true) if err = assignUserRoles(d, m); err != nil { - return fmt.Errorf("failed assigning user roles. %s", err) + return diag.Errorf("failed assigning user roles. %s", err) } d.Partial(false) - return readUser(d, m) + return readUser(ctx, d, m) } -func deleteUser(d *schema.ResourceData, m interface{}) error { +func deleteUser(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.User.Delete(d.Id()); err != nil { if mErr, ok := err.(management.Error); ok {