Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DXCDT-247: Update email provider if already existing when creating it #377

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions internal/provider/resource_auth0_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,17 @@ func newEmail() *schema.Resource {
func createEmail(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

d.SetId(resource.UniqueId())

if emailProviderIsConfigured(api) {
return updateEmail(ctx, d, m)
}

email := expandEmail(d.GetRawConfig())
if err := api.Email.Create(email); err != nil {
return diag.FromErr(err)
}

d.SetId(resource.UniqueId())

return readEmail(ctx, d, m)
}

Expand Down Expand Up @@ -171,6 +175,15 @@ func deleteEmail(ctx context.Context, d *schema.ResourceData, m interface{}) dia
return nil
}

func emailProviderIsConfigured(api *management.Management) bool {
_, err := api.Email.Read()
if err, ok := err.(management.Error); ok && err.Status() == http.StatusNotFound {
return false
}

return true
}

func expandEmail(config cty.Value) *management.Email {
email := &management.Email{
Name: value.String(config.GetAttr("name")),
Expand Down
79 changes: 79 additions & 0 deletions internal/provider/resource_auth0_email_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package provider

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/auth0/go-auth0/management"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/stretchr/testify/assert"

"github.com/auth0/terraform-provider-auth0/internal/recorder"
)
Expand Down Expand Up @@ -113,6 +117,81 @@ 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"
}
}

resource "auth0_email" "no_conflict_email_provider" {
depends_on = [ 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"),
resource.TestCheckResourceAttr("auth0_email.no_conflict_email_provider", "name", "mailgun"),
resource.TestCheckResourceAttr("auth0_email.no_conflict_email_provider", "enabled", "false"),
resource.TestCheckResourceAttr("auth0_email.no_conflict_email_provider", "default_from_address", ""),
resource.TestCheckResourceAttr("auth0_email.no_conflict_email_provider", "credentials.0.domain", "example.com"),
resource.TestCheckResourceAttr("auth0_email.no_conflict_email_provider", "credentials.0.region", "eu"),
),
},
},
})
}

func TestEmailProviderIsConfigured(t *testing.T) {
t.Run("it returns true if the provider is configured", func(t *testing.T) {
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/v2/emails/provider" {
w.WriteHeader(http.StatusOK)
return
}
http.NotFound(w, r)
})
testServer := httptest.NewServer(testHandler)

api, err := management.New(testServer.URL, management.WithInsecure())
assert.NoError(t, err)

actual := emailProviderIsConfigured(api)
assert.True(t, actual)
})

t.Run("it returns false if the provider is not configured", func(t *testing.T) {
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/v2/emails/provider" {
http.NotFound(w, r)
return
}
http.NotFound(w, r)
})
testServer := httptest.NewServer(testHandler)

api, err := management.New(testServer.URL, management.WithInsecure())
assert.NoError(t, err)

actual := emailProviderIsConfigured(api)
assert.False(t, actual)
})
}
Loading