Skip to content

Commit

Permalink
Replace resource funcs to use context.Context and Diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Apr 5, 2022
1 parent 30c08ae commit 84e1bbc
Show file tree
Hide file tree
Showing 29 changed files with 490 additions and 423 deletions.
20 changes: 10 additions & 10 deletions auth0/data_source_auth0_client.go
Original file line number Diff line number Diff line change
@@ -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(),
}
}

Expand All @@ -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)
}
13 changes: 8 additions & 5 deletions auth0/data_source_auth0_global_client.go
Original file line number Diff line number Diff line change
@@ -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)
}
10 changes: 6 additions & 4 deletions auth0/data_source_auth0_tenant.go
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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())
Expand All @@ -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())
}
17 changes: 13 additions & 4 deletions auth0/provider.go
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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()
Expand All @@ -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
}
}
8 changes: 6 additions & 2 deletions auth0/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
38 changes: 20 additions & 18 deletions auth0/resource_auth0_action.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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": {
Expand Down Expand Up @@ -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 {
Expand All @@ -155,7 +157,7 @@ func readAction(d *schema.ResourceData, m interface{}) error {
return nil
}
}
return err
return diag.FromErr(err)
}

result := multierror.Append(
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -244,7 +246,7 @@ func deleteAction(d *schema.ResourceData, m interface{}) error {
return nil
}
}
return err
return diag.FromErr(err)
}

return nil
Expand Down
Loading

0 comments on commit 84e1bbc

Please sign in to comment.