Skip to content

Commit

Permalink
[9/X] Refactor email resources to allow for empty fields (#344)
Browse files Browse the repository at this point in the history
* Refactor email resources to allow for empty fields

* [10/X] Refactor resource server resources to allow for empty fields (#345)

* Refactor resource server resources to allow for empty fields

* [11/X] Refactor tenant resource to allow for empty fields (#346)

* Refactor tenant resource to allow for empty fields

* [12/X] Refactor custom domain resource to allow for empty fields (#347)

* Refactor custom domain resource to allow for empty fields

* [13/X] Refactor user resource to allow for empty fields (#348)

* Refactor user resource to allow for empty fields

* [14/X] Refactor role and rule resources to allow for empty fields (#349)

* Refactor role and rule resources to allow for empty fields

* [15/X] Refactor log stream resource to allow for empty fields (#350)

* Refactor log stream resource to allow for empty fields

* [16/X] Refactor prompts resource to allow for empty fields (#351)

* Refactor prompts resource to allow for empty fields

* [17/X] Refactor hooks resource to allow for empty fields (#352)

Refactor hooks resource to allow for empty fields

* Fix small issue with log stream filters

* Adding user_id back in to tests

Co-authored-by: Will Vedder <[email protected]>

Co-authored-by: Will Vedder <[email protected]>

* Removing default audience string replace

* Removing unncessary check

Co-authored-by: Will Vedder <[email protected]>

Co-authored-by: Will Vedder <[email protected]>

Co-authored-by: Will Vedder <[email protected]>
  • Loading branch information
sergiught and willvedd authored Oct 7, 2022
1 parent f1d919a commit 11d81b0
Show file tree
Hide file tree
Showing 38 changed files with 6,605 additions and 3,461 deletions.
5 changes: 4 additions & 1 deletion docs/resources/resource_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ resource "auth0_resource_server" "my_resource_server" {
<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `identifier` (String) Unique identifier for the resource server. Used as the audience parameter for authorization calls. Cannot be changed once set.

### Optional

- `allow_offline_access` (Boolean) Indicates whether refresh tokens can be issued for this resource server.
- `enforce_policies` (Boolean) Indicates whether authorization polices are enforced.
- `identifier` (String) Unique identifier for the resource server. Used as the audience parameter for authorization calls. Cannot be changed once set.
- `name` (String) Friendly name for the resource server. Cannot include `<` or `>` characters.
- `options` (Map of String) Used to store additional metadata.
- `scopes` (Block Set) List of permissions (scopes) used by this resource server. (see [below for nested schema](#nestedblock--scopes))
Expand Down
45 changes: 24 additions & 21 deletions internal/provider/resource_auth0_custom_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"context"
"net/http"

"github.com/auth0/go-auth0"
"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-cty/cty"
"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"

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

func newCustomDomain() *schema.Resource {
Expand Down Expand Up @@ -77,36 +79,36 @@ func newCustomDomain() *schema.Resource {
}

func createCustomDomain(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
customDomain := expandCustomDomain(d)
api := m.(*management.Management)

customDomain := expandCustomDomain(d.GetRawConfig())
if err := api.CustomDomain.Create(customDomain); err != nil {
return diag.FromErr(err)
}

d.SetId(auth0.StringValue(customDomain.ID))
d.SetId(customDomain.GetID())

return readCustomDomain(ctx, d, m)
}

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 {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
return diag.FromErr(err)
}

result := multierror.Append(
d.Set("domain", customDomain.Domain),
d.Set("type", customDomain.Type),
d.Set("primary", customDomain.Primary),
d.Set("status", customDomain.Status),
d.Set("origin_domain_name", customDomain.OriginDomainName),
d.Set("domain", customDomain.GetDomain()),
d.Set("type", customDomain.GetType()),
d.Set("primary", customDomain.GetPrimary()),
d.Set("status", customDomain.GetStatus()),
d.Set("origin_domain_name", customDomain.GetOriginDomainName()),
)

if customDomain.Verification != nil {
Expand All @@ -120,21 +122,22 @@ func readCustomDomain(ctx context.Context, d *schema.ResourceData, m interface{}

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 {
if mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
return diag.FromErr(err)
}

d.SetId("")
return nil
}

func expandCustomDomain(d *schema.ResourceData) *management.CustomDomain {
func expandCustomDomain(config cty.Value) *management.CustomDomain {
return &management.CustomDomain{
Domain: String(d, "domain"),
Type: String(d, "type"),
Domain: value.String(config.GetAttr("domain")),
Type: value.String(config.GetAttr("type")),
}
}
14 changes: 7 additions & 7 deletions internal/provider/resource_auth0_custom_domain_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func newCustomDomainVerification() *schema.Resource {

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

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 {
Expand All @@ -74,7 +75,7 @@ func createCustomDomainVerification(ctx context.Context, d *schema.ResourceData,
// The cname_api_key field is only given once: when verification
// succeeds for the first time. Therefore, we set it on the resource in
// the creation routine only, and never touch it again.
if err := d.Set("cname_api_key", customDomainVerification.CNAMEAPIKey); err != nil {
if err := d.Set("cname_api_key", customDomainVerification.GetCNAMEAPIKey()); err != nil {
return resource.NonRetryableError(err)
}

Expand All @@ -89,20 +90,19 @@ func createCustomDomainVerification(ctx context.Context, d *schema.ResourceData,

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 {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
return diag.FromErr(err)
}

result := multierror.Append(
d.Set("custom_domain_id", customDomain.GetID()),
d.Set("origin_domain_name", customDomain.OriginDomainName),
d.Set("origin_domain_name", customDomain.GetOriginDomainName()),
)

return diag.FromErr(result.ErrorOrNil())
Expand Down
40 changes: 22 additions & 18 deletions internal/provider/resource_auth0_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (
"net/http"

"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-cty/cty"
"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/auth0/terraform-provider-auth0/internal/value"
)

func newEmail() *schema.Resource {
Expand Down Expand Up @@ -113,7 +116,7 @@ func newEmail() *schema.Resource {
func createEmail(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

email := expandEmail(d)
email := expandEmail(d.GetRawConfig())
if err := api.Email.Create(email); err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -149,7 +152,7 @@ func readEmail(ctx context.Context, d *schema.ResourceData, m interface{}) diag.
func updateEmail(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

email := expandEmail(d)
email := expandEmail(d.GetRawConfig())
if err := api.Email.Update(email); err != nil {
return diag.FromErr(err)
}
Expand All @@ -165,30 +168,31 @@ func deleteEmail(ctx context.Context, d *schema.ResourceData, m interface{}) dia
}

d.SetId("")

return nil
}

func expandEmail(d *schema.ResourceData) *management.Email {
func expandEmail(config cty.Value) *management.Email {
email := &management.Email{
Name: String(d, "name"),
Enabled: Bool(d, "enabled"),
DefaultFromAddress: String(d, "default_from_address"),
Name: value.String(config.GetAttr("name")),
Enabled: value.Bool(config.GetAttr("enabled")),
DefaultFromAddress: value.String(config.GetAttr("default_from_address")),
}

List(d, "credentials").Elem(func(d ResourceData) {
config.GetAttr("credentials").ForEachElement(func(_ cty.Value, config cty.Value) (stop bool) {
email.Credentials = &management.EmailCredentials{
APIUser: String(d, "api_user"),
APIKey: String(d, "api_key"),
AccessKeyID: String(d, "access_key_id"),
SecretAccessKey: String(d, "secret_access_key"),
Region: String(d, "region"),
Domain: String(d, "domain"),
SMTPHost: String(d, "smtp_host"),
SMTPPort: Int(d, "smtp_port"),
SMTPUser: String(d, "smtp_user"),
SMTPPass: String(d, "smtp_pass"),
APIUser: value.String(config.GetAttr("api_user")),
APIKey: value.String(config.GetAttr("api_key")),
AccessKeyID: value.String(config.GetAttr("access_key_id")),
SecretAccessKey: value.String(config.GetAttr("secret_access_key")),
Region: value.String(config.GetAttr("region")),
Domain: value.String(config.GetAttr("domain")),
SMTPHost: value.String(config.GetAttr("smtp_host")),
SMTPPort: value.Int(config.GetAttr("smtp_port")),
SMTPUser: value.String(config.GetAttr("smtp_user")),
SMTPPass: value.String(config.GetAttr("smtp_pass")),
}

return stop
})

return email
Expand Down
66 changes: 35 additions & 31 deletions internal/provider/resource_auth0_email_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (

"github.com/auth0/go-auth0"
"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-cty/cty"
"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"

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

func newEmailTemplate() *schema.Resource {
Expand Down Expand Up @@ -98,19 +101,20 @@ func newEmailTemplate() *schema.Resource {
}

func createEmailTemplate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
email := expandEmailTemplate(d)
api := m.(*management.Management)

email := expandEmailTemplate(d.GetRawConfig())

// The email template resource doesn't allow deleting templates, so in order
// to avoid conflicts, we first attempt to read the template. If it exists
// we'll try to update it, if not we'll try to create it.
if _, err := api.EmailTemplate.Read(auth0.StringValue(email.Template)); err == nil {
if _, err := api.EmailTemplate.Read(email.GetTemplate()); 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 {
if err := api.EmailTemplate.Update(email.GetTemplate(), email); err != nil {
return diag.FromErr(err)
}

d.SetId(auth0.StringValue(email.Template))
d.SetId(email.GetTemplate())

return nil
}
Expand All @@ -121,44 +125,44 @@ func createEmailTemplate(ctx context.Context, d *schema.ResourceData, m interfac
return diag.FromErr(err)
}

d.SetId(auth0.StringValue(email.Template))
d.SetId(email.GetTemplate())

return nil
return readEmailTemplate(ctx, d, m)
}

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 {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
return diag.FromErr(err)
}

d.SetId(auth0.StringValue(email.Template))
d.SetId(email.GetTemplate())

result := multierror.Append(
d.Set("template", email.Template),
d.Set("body", email.Body),
d.Set("from", email.From),
d.Set("result_url", email.ResultURL),
d.Set("subject", email.Subject),
d.Set("syntax", email.Syntax),
d.Set("url_lifetime_in_seconds", email.URLLifetimeInSecoonds),
d.Set("enabled", email.Enabled),
d.Set("template", email.GetTemplate()),
d.Set("body", email.GetBody()),
d.Set("from", email.GetFrom()),
d.Set("result_url", email.GetResultURL()),
d.Set("subject", email.GetSubject()),
d.Set("syntax", email.GetSyntax()),
d.Set("url_lifetime_in_seconds", email.GetURLLifetimeInSecoonds()),
d.Set("enabled", email.GetEnabled()),
d.Set("include_email_in_redirect", email.GetIncludeEmailInRedirect()),
)

return diag.FromErr(result.ErrorOrNil())
}

func updateEmailTemplate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
email := expandEmailTemplate(d)
api := m.(*management.Management)

email := expandEmailTemplate(d.GetRawConfig())
if err := api.EmailTemplate.Update(d.Id(), email); err != nil {
return diag.FromErr(err)
}
Expand All @@ -184,17 +188,17 @@ func deleteEmailTemplate(ctx context.Context, d *schema.ResourceData, m interfac
return nil
}

func expandEmailTemplate(d *schema.ResourceData) *management.EmailTemplate {
func expandEmailTemplate(config cty.Value) *management.EmailTemplate {
emailTemplate := &management.EmailTemplate{
Template: String(d, "template"),
Body: String(d, "body"),
From: String(d, "from"),
ResultURL: String(d, "result_url"),
Subject: String(d, "subject"),
Syntax: String(d, "syntax"),
URLLifetimeInSecoonds: Int(d, "url_lifetime_in_seconds"),
Enabled: Bool(d, "enabled"),
IncludeEmailInRedirect: Bool(d, "include_email_in_redirect"),
Template: value.String(config.GetAttr("template")),
Body: value.String(config.GetAttr("body")),
From: value.String(config.GetAttr("from")),
ResultURL: value.String(config.GetAttr("result_url")),
Subject: value.String(config.GetAttr("subject")),
Syntax: value.String(config.GetAttr("syntax")),
URLLifetimeInSecoonds: value.Int(config.GetAttr("url_lifetime_in_seconds")),
Enabled: value.Bool(config.GetAttr("enabled")),
IncludeEmailInRedirect: value.Bool(config.GetAttr("include_email_in_redirect")),
}

return emailTemplate
Expand Down
21 changes: 21 additions & 0 deletions internal/provider/resource_auth0_email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ func TestAccEmail(t *testing.T) {
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.0.region", "eu"),
),
},
{
Config: `
resource "auth0_email" "my_email_provider" {
name = "mailgun"
enabled = false
default_from_address = ""
credentials {
api_key = "MAILGUNXXXXXXXXXXXXXXX"
domain = "example.com"
region = "eu"
}
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "name", "mailgun"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "enabled", "false"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "default_from_address", ""),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.0.domain", "example.com"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.0.region", "eu"),
),
},
},
})
}
Loading

0 comments on commit 11d81b0

Please sign in to comment.