diff --git a/internal/provider/resource_auth0_email.go b/internal/provider/resource_auth0_email.go index 7269669ff..ddd36c8fc 100644 --- a/internal/provider/resource_auth0_email.go +++ b/internal/provider/resource_auth0_email.go @@ -4,10 +4,10 @@ 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" ) @@ -57,21 +57,18 @@ func newEmail() *schema.Resource { Type: schema.TypeString, Optional: true, Sensitive: true, - ForceNew: true, Description: "API Key for your email service. Will always be encrypted in our database.", }, "access_key_id": { Type: schema.TypeString, Optional: true, Sensitive: true, - ForceNew: true, Description: "AWS Access Key ID. Used only for AWS.", }, "secret_access_key": { Type: schema.TypeString, Optional: true, Sensitive: true, - ForceNew: true, Description: "AWS Secret Key. Will always be encrypted in our database. Used only for AWS.", }, "region": { @@ -104,7 +101,6 @@ func newEmail() *schema.Resource { Type: schema.TypeString, Optional: true, Sensitive: true, - ForceNew: true, Description: "SMTP password. Used only for SMTP.", }, }, @@ -115,59 +111,45 @@ func newEmail() *schema.Resource { } func createEmail(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - email := buildEmail(d) api := m.(*management.Management) + + email := expandEmail(d) if err := api.Email.Create(email); err != nil { return diag.FromErr(err) } - d.SetId(auth0.StringValue(email.Name)) + d.SetId(resource.UniqueId()) return readEmail(ctx, d, m) } func readEmail(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) + email, err := api.Email.Read() 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.Name)) - result := multierror.Append( - d.Set("name", email.Name), - d.Set("enabled", email.Enabled), - d.Set("default_from_address", email.DefaultFromAddress), + d.Set("name", email.GetName()), + d.Set("enabled", email.GetEnabled()), + d.Set("default_from_address", email.GetDefaultFromAddress()), + d.Set("credentials", flattenCredentials(email.GetCredentials(), d)), ) - if credentials := email.Credentials; credentials != nil { - credentialsMap := make(map[string]interface{}) - credentialsMap["api_user"] = credentials.APIUser - credentialsMap["api_key"] = d.Get("credentials.0.api_key") - credentialsMap["access_key_id"] = d.Get("credentials.0.access_key_id") - credentialsMap["secret_access_key"] = d.Get("credentials.0.secret_access_key") - credentialsMap["region"] = credentials.Region - credentialsMap["domain"] = credentials.Domain - credentialsMap["smtp_host"] = credentials.SMTPHost - credentialsMap["smtp_port"] = credentials.SMTPPort - credentialsMap["smtp_user"] = credentials.SMTPUser - credentialsMap["smtp_pass"] = d.Get("credentials.0.smtp_pass") - result = multierror.Append(result, d.Set("credentials", []map[string]interface{}{credentialsMap})) - } - return diag.FromErr(result.ErrorOrNil()) } func updateEmail(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - email := buildEmail(d) api := m.(*management.Management) + + email := expandEmail(d) if err := api.Email.Update(email); err != nil { return diag.FromErr(err) } @@ -177,19 +159,17 @@ func updateEmail(ctx context.Context, d *schema.ResourceData, m interface{}) dia 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 { - if mErr.Status() == http.StatusNotFound { - d.SetId("") - return nil - } - } + return diag.FromErr(err) } + d.SetId("") + return nil } -func buildEmail(d *schema.ResourceData) *management.Email { +func expandEmail(d *schema.ResourceData) *management.Email { email := &management.Email{ Name: String(d, "name"), Enabled: Bool(d, "enabled"), @@ -213,3 +193,24 @@ func buildEmail(d *schema.ResourceData) *management.Email { return email } + +func flattenCredentials(credentials *management.EmailCredentials, d *schema.ResourceData) []interface{} { + if credentials == nil { + return nil + } + + m := map[string]interface{}{ + "api_user": credentials.GetAPIUser(), + "api_key": d.Get("credentials.0.api_key").(string), + "access_key_id": d.Get("credentials.0.access_key_id").(string), + "secret_access_key": d.Get("credentials.0.secret_access_key").(string), + "region": credentials.GetRegion(), + "domain": credentials.GetDomain(), + "smtp_host": credentials.GetSMTPHost(), + "smtp_port": credentials.GetSMTPPort(), + "smtp_user": credentials.GetSMTPUser(), + "smtp_pass": d.Get("credentials.0.smtp_pass").(string), + } + + return []interface{}{m} +} diff --git a/test/data/recordings/TestAccEmail.yaml b/test/data/recordings/TestAccEmail.yaml index ed5c3578f..ffa77ada0 100644 --- a/test/data/recordings/TestAccEmail.yaml +++ b/test/data/recordings/TestAccEmail.yaml @@ -1,266 +1,230 @@ --- version: 1 interactions: -- request: - body: | - {"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"accessKeyId":"AKIAXXXXXXXXXXXXXXXX","secretAccessKey":"7e8c2148xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","region":"us-east-1"}} - form: {} - headers: - Content-Type: - - application/json - User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider - method: POST - response: - body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' - headers: - Content-Type: - - application/json; charset=utf-8 - status: 201 Created - code: 201 - duration: 1ms -- request: - body: | - null - form: {} - headers: - Content-Type: - - application/json - User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true - method: GET - response: - body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' - headers: - Content-Type: - - application/json; charset=utf-8 - status: 200 OK - code: 200 - duration: 1ms -- request: - body: | - null - form: {} - headers: - Content-Type: - - application/json - User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true - method: GET - response: - body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' - headers: - Content-Type: - - application/json; charset=utf-8 - status: 200 OK - code: 200 - duration: 1ms -- request: - body: | - null - form: {} - headers: - Content-Type: - - application/json - User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true - method: GET - response: - body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' - headers: - Content-Type: - - application/json; charset=utf-8 - status: 200 OK - code: 200 - duration: 1ms -- request: - body: "" - form: {} - headers: - Content-Type: - - application/json - User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider - method: DELETE - response: - body: "" - headers: - Content-Type: - - application/json; charset=utf-8 - status: 204 No Content - code: 204 - duration: 1ms -- request: - body: | - {"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"accessKeyId":"AKIAXXXXXXXXXXXXXXXY","secretAccessKey":"7e8c2148xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","region":"us-east-1"}} - form: {} - headers: - Content-Type: - - application/json - User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider - method: POST - response: - body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' - headers: - Content-Type: - - application/json; charset=utf-8 - status: 201 Created - code: 201 - duration: 1ms -- request: - body: | - null - form: {} - headers: - Content-Type: - - application/json - User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true - method: GET - response: - body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' - headers: - Content-Type: - - application/json; charset=utf-8 - status: 200 OK - code: 200 - duration: 1ms -- request: - body: | - null - form: {} - headers: - Content-Type: - - application/json - User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true - method: GET - response: - body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' - headers: - Content-Type: - - application/json; charset=utf-8 - status: 200 OK - code: 200 - duration: 1ms -- request: - body: | - null - form: {} - headers: - Content-Type: - - application/json - User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true - method: GET - response: - body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' - headers: - Content-Type: - - application/json; charset=utf-8 - status: 200 OK - code: 200 - duration: 1ms -- request: - body: "" - form: {} - headers: - Content-Type: - - application/json - User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider - method: DELETE - response: - body: "" - headers: - Content-Type: - - application/json; charset=utf-8 - status: 204 No Content - code: 204 - duration: 1ms -- request: - body: | - {"name":"mailgun","enabled":true,"default_from_address":"accounts@example.com","credentials":{"api_key":"MAILGUNXXXXXXXXXXXXXXX","region":"eu","domain":"example.com"}} - form: {} - headers: - Content-Type: - - application/json - User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider - method: POST - response: - body: '{"name":"mailgun","enabled":true,"default_from_address":"accounts@example.com","credentials":{"domain":"example.com","region":"eu"}}' - headers: - Content-Type: - - application/json; charset=utf-8 - status: 201 Created - code: 201 - duration: 1ms -- request: - body: | - null - form: {} - headers: - Content-Type: - - application/json - User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true - method: GET - response: - body: '{"name":"mailgun","enabled":true,"default_from_address":"accounts@example.com","credentials":{"domain":"example.com","region":"eu"}}' - headers: - Content-Type: - - application/json; charset=utf-8 - status: 200 OK - code: 200 - duration: 1ms -- request: - body: | - null - form: {} - headers: - Content-Type: - - application/json - User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true - method: GET - response: - body: '{"name":"mailgun","enabled":true,"default_from_address":"accounts@example.com","credentials":{"domain":"example.com","region":"eu"}}' - headers: - Content-Type: - - application/json; charset=utf-8 - status: 200 OK - code: 200 - duration: 1ms -- request: - body: "" - form: {} - headers: - Content-Type: - - application/json - User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider - method: DELETE - response: - body: "" - headers: - Content-Type: - - application/json; charset=utf-8 - status: 204 No Content - code: 204 - duration: 1ms + - request: + body: | + {"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"accessKeyId":"AKIAXXXXXXXXXXXXXXXX","secretAccessKey":"7e8c2148xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","region":"us-east-1"}} + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.10.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider + method: POST + response: + body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 201 Created + code: 201 + duration: 1ms + - request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.10.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true + method: GET + response: + body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms + - request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.10.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true + method: GET + response: + body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms + - request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.10.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true + method: GET + response: + body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms + - request: + body: | + {"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"accessKeyId":"AKIAXXXXXXXXXXXXXXXY","secretAccessKey":"7e8c2148xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","region":"us-east-1"}} + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.10.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider + method: PATCH + response: + body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms + - request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.10.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true + method: GET + response: + body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms + - request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.10.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true + method: GET + response: + body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms + - request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.10.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true + method: GET + response: + body: '{"name":"ses","enabled":true,"default_from_address":"accounts@example.com","credentials":{"region":"us-east-1"}}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms + - request: + body: | + {"name":"mailgun","enabled":true,"default_from_address":"accounts@example.com","credentials":{"api_key":"MAILGUNXXXXXXXXXXXXXXX","region":"eu","domain":"example.com"}} + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.10.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider + method: PATCH + response: + body: '{"name":"mailgun","enabled":true,"default_from_address":"accounts@example.com","credentials":{"domain":"example.com","region":"eu"}}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms + - request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.10.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true + method: GET + response: + body: '{"name":"mailgun","enabled":true,"default_from_address":"accounts@example.com","credentials":{"domain":"example.com","region":"eu"}}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms + - request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.10.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider?fields=name%2Cenabled%2Cdefault_from_address%2Ccredentials%2Csettings&include_fields=true + method: GET + response: + body: '{"name":"mailgun","enabled":true,"default_from_address":"accounts@example.com","credentials":{"domain":"example.com","region":"eu"}}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms + - request: + body: "" + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.10.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/emails/provider + method: DELETE + response: + body: "" + headers: + Content-Type: + - application/json; charset=utf-8 + status: 204 No Content + code: 204 + duration: 1ms