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

r/ses_email_identity - move to sesv2 #21208

Closed
Closed
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
3 changes: 3 additions & 0 deletions internal/conns/conns.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ import (
"github.com/aws/aws-sdk-go/service/servicediscovery"
"github.com/aws/aws-sdk-go/service/servicequotas"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/aws/aws-sdk-go/service/sesv2"
"github.com/aws/aws-sdk-go/service/sfn"
"github.com/aws/aws-sdk-go/service/shield"
"github.com/aws/aws-sdk-go/service/signer"
Expand Down Expand Up @@ -373,6 +374,7 @@ type AWSClient struct {
ServerlessAppRepoConn *serverlessapplicationrepository.ServerlessApplicationRepository
ServiceQuotasConn *servicequotas.ServiceQuotas
SESConn *ses.SES
SESV2Conn *sesv2.SESV2
SFNConn *sfn.SFN
ShieldConn *shield.Shield
SignerConn *signer.Signer
Expand Down Expand Up @@ -624,6 +626,7 @@ func (c *Config) Client() (interface{}, error) {
ServerlessAppRepoConn: serverlessapplicationrepository.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["serverlessrepo"])})),
ServiceQuotasConn: servicequotas.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["servicequotas"])})),
SESConn: ses.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["ses"])})),
SESV2Conn: sesv2.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["sesv2"])})),
SFNConn: sfn.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["stepfunctions"])})),
SignerConn: signer.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["signer"])})),
SimpleDBConn: simpledb.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["sdb"])})),
Expand Down
41 changes: 16 additions & 25 deletions internal/service/ses/email_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/aws/aws-sdk-go/service/sesv2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
)
Expand Down Expand Up @@ -39,18 +39,18 @@ func ResourceEmailIdentity() *schema.Resource {
}

func resourceEmailIdentityCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).SESConn
conn := meta.(*conns.AWSClient).SESV2Conn

email := d.Get("email").(string)
email = strings.TrimSuffix(email, ".")

createOpts := &ses.VerifyEmailIdentityInput{
EmailAddress: aws.String(email),
createOpts := &sesv2.CreateEmailIdentityInput{
EmailIdentity: aws.String(email),
}

_, err := conn.VerifyEmailIdentity(createOpts)
_, err := conn.CreateEmailIdentity(createOpts)
if err != nil {
return fmt.Errorf("Error requesting SES email identity verification: %s", err)
return fmt.Errorf("error creating SES email identity: %w", err)
}

d.SetId(email)
Expand All @@ -59,30 +59,21 @@ func resourceEmailIdentityCreate(d *schema.ResourceData, meta interface{}) error
}

func resourceEmailIdentityRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).SESConn
conn := meta.(*conns.AWSClient).SESV2Conn

email := d.Id()
d.Set("email", email)

readOpts := &ses.GetIdentityVerificationAttributesInput{
Identities: []*string{
aws.String(email),
},
readOpts := &sesv2.GetEmailIdentityInput{
EmailIdentity: aws.String(email),
}

response, err := conn.GetIdentityVerificationAttributes(readOpts)
_, err := conn.GetEmailIdentity(readOpts)
if err != nil {
log.Printf("[WARN] Error fetching identity verification attributes for %s: %s", d.Id(), err)
log.Printf("[WARN] Error reading SES email identity %s: %s", d.Id(), err)
return err
}

_, ok := response.VerificationAttributes[email]
if !ok {
log.Printf("[WARN] Email not listed in response when fetching verification attributes for %s", d.Id())
d.SetId("")
return nil
}

arn := arn.ARN{
AccountID: meta.(*conns.AWSClient).AccountID,
Partition: meta.(*conns.AWSClient).Partition,
Expand All @@ -95,17 +86,17 @@ func resourceEmailIdentityRead(d *schema.ResourceData, meta interface{}) error {
}

func resourceEmailIdentityDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).SESConn
conn := meta.(*conns.AWSClient).SESV2Conn

email := d.Get("email").(string)

deleteOpts := &ses.DeleteIdentityInput{
Identity: aws.String(email),
deleteOpts := &sesv2.DeleteEmailIdentityInput{
EmailIdentity: aws.String(email),
}

_, err := conn.DeleteIdentity(deleteOpts)
_, err := conn.DeleteEmailIdentity(deleteOpts)
if err != nil {
return fmt.Errorf("Error deleting SES email identity: %s", err)
return fmt.Errorf("error deleting SES email identity: %w", err)
}

return nil
Expand Down
44 changes: 17 additions & 27 deletions internal/service/ses/email_identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/aws/aws-sdk-go/service/sesv2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
)

func TestAccSESEmailIdentity_basic(t *testing.T) {
email := acctest.DefaultEmailAddress
domain := acctest.RandomDomainName()
email := acctest.RandomEmailAddress(domain)
resourceName := "aws_ses_email_identity.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ses.EndpointsID),
ErrorCheck: acctest.ErrorCheck(t, sesv2.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckEmailIdentityDestroy,
Steps: []resource.TestStep{
Expand All @@ -41,12 +42,13 @@ func TestAccSESEmailIdentity_basic(t *testing.T) {
}

func TestAccSESEmailIdentity_trailingPeriod(t *testing.T) {
email := fmt.Sprintf("%s.", acctest.DefaultEmailAddress)
domain := acctest.RandomDomainName()
email := acctest.RandomEmailAddress(domain)
resourceName := "aws_ses_email_identity.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ses.EndpointsID),
ErrorCheck: acctest.ErrorCheck(t, sesv2.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckEmailIdentityDestroy,
Steps: []resource.TestStep{
Expand All @@ -67,27 +69,21 @@ func TestAccSESEmailIdentity_trailingPeriod(t *testing.T) {
}

func testAccCheckEmailIdentityDestroy(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).SESConn
conn := acctest.Provider.Meta().(*conns.AWSClient).SESV2Conn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_ses_email_identity" {
continue
}

email := rs.Primary.ID
params := &ses.GetIdentityVerificationAttributesInput{
Identities: []*string{
aws.String(email),
},
}

response, err := conn.GetIdentityVerificationAttributes(params)
if err != nil {
return err
params := &sesv2.GetEmailIdentityInput{
EmailIdentity: aws.String(email),
}

if response.VerificationAttributes[email] != nil {
return fmt.Errorf("SES Email Identity %s still exists. Failing!", email)
_, err := conn.GetEmailIdentity(params)
if err == nil {
return fmt.Errorf("SES Email Identity %s still exists", email)
}
}

Expand All @@ -106,23 +102,17 @@ func testAccCheckEmailIdentityExists(n string) resource.TestCheckFunc {
}

email := rs.Primary.ID
conn := acctest.Provider.Meta().(*conns.AWSClient).SESConn
conn := acctest.Provider.Meta().(*conns.AWSClient).SESV2Conn

params := &ses.GetIdentityVerificationAttributesInput{
Identities: []*string{
aws.String(email),
},
params := &sesv2.GetEmailIdentityInput{
EmailIdentity: aws.String(email),
}

response, err := conn.GetIdentityVerificationAttributes(params)
_, err := conn.GetEmailIdentity(params)
if err != nil {
return err
}

if response.VerificationAttributes[email] == nil {
return fmt.Errorf("SES Email Identity %s not found in AWS", email)
}

return nil
}
}
Expand Down