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/aws_sesv2_email_identity_mail_from_attributes: new resource #27672

Merged
Merged
3 changes: 3 additions & 0 deletions .changelog/27672.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_sesv2_email_identity_mail_from_attributes
```
11 changes: 6 additions & 5 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2069,11 +2069,12 @@ func New(_ context.Context) (*schema.Provider, error) {
"aws_ses_receipt_rule_set": ses.ResourceReceiptRuleSet(),
"aws_ses_template": ses.ResourceTemplate(),

"aws_sesv2_configuration_set": sesv2.ResourceConfigurationSet(),
"aws_sesv2_dedicated_ip_assignment": sesv2.ResourceDedicatedIPAssignment(),
"aws_sesv2_dedicated_ip_pool": sesv2.ResourceDedicatedIPPool(),
"aws_sesv2_email_identity": sesv2.ResourceEmailIdentity(),
"aws_sesv2_email_identity_feedback_attributes": sesv2.ResourceEmailIdentityFeedbackAttributes(),
"aws_sesv2_configuration_set": sesv2.ResourceConfigurationSet(),
"aws_sesv2_dedicated_ip_assignment": sesv2.ResourceDedicatedIPAssignment(),
"aws_sesv2_dedicated_ip_pool": sesv2.ResourceDedicatedIPPool(),
"aws_sesv2_email_identity": sesv2.ResourceEmailIdentity(),
"aws_sesv2_email_identity_feedback_attributes": sesv2.ResourceEmailIdentityFeedbackAttributes(),
"aws_sesv2_email_identity_mail_from_attributes": sesv2.ResourceEmailIdentityMailFromAttributes(),

"aws_sfn_activity": sfn.ResourceActivity(),
"aws_sfn_state_machine": sfn.ResourceStateMachine(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func testAccCheckEmailIdentityFeedbackAttributesExist(name string, emailForwardi
return create.Error(names.SESV2, create.ErrActionCheckingExistence, tfsesv2.ResNameEmailIdentity, rs.Primary.ID, err)
}
if out == nil || out.FeedbackForwardingStatus != emailForwardingEnabled {
return create.Error(names.SESV2, create.ErrActionCheckingExistence, tfsesv2.ResNameEmailIdentityFeedbackAttributes, rs.Primary.ID, err)
return create.Error(names.SESV2, create.ErrActionCheckingExistence, tfsesv2.ResNameEmailIdentityFeedbackAttributes, rs.Primary.ID, errors.New("feedback attributes not set"))
}

return nil
Expand Down
170 changes: 170 additions & 0 deletions internal/service/sesv2/email_identity_mail_from_attributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package sesv2

import (
"context"
"errors"
"log"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sesv2"
"github.com/aws/aws-sdk-go-v2/service/sesv2/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

var ErrMailFromRequired = errors.New("mail from domain is required if behavior on MX failure is REJECT_MESSAGE")

func ResourceEmailIdentityMailFromAttributes() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceEmailIdentityMailFromAttributesCreate,
ReadWithoutTimeout: resourceEmailIdentityMailFromAttributesRead,
UpdateWithoutTimeout: resourceEmailIdentityMailFromAttributesUpdate,
DeleteWithoutTimeout: resourceEmailIdentityMailFromAttributesDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"behavior_on_mx_failure": {
Type: schema.TypeString,
Optional: true,
Default: string(types.BehaviorOnMxFailureUseDefaultValue),
ValidateDiagFunc: enum.Validate[types.BehaviorOnMxFailure](),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this is really handy! Thanks, TIL! 🙌

},
"email_identity": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"mail_from_domain": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

const (
ResNameEmailIdentityMailFromAttributes = "Email Identity Mail From Attributes"
)

func resourceEmailIdentityMailFromAttributesCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).SESV2Client

in := &sesv2.PutEmailIdentityMailFromAttributesInput{
EmailIdentity: aws.String(d.Get("email_identity").(string)),
}

if v, ok := d.GetOk("mail_from_domain"); ok {
in.MailFromDomain = aws.String(v.(string))
}

if v, ok := d.GetOk("behavior_on_mx_failure"); ok {
in.BehaviorOnMxFailure = types.BehaviorOnMxFailure(v.(string))
}

if in.BehaviorOnMxFailure == types.BehaviorOnMxFailureRejectMessage && (in.MailFromDomain == nil || aws.ToString(in.MailFromDomain) == "") {
return create.DiagError(names.SESV2, create.ErrActionCreating, ResNameEmailIdentityMailFromAttributes, d.Get("email_identity").(string), ErrMailFromRequired)
}

out, err := conn.PutEmailIdentityMailFromAttributes(ctx, in)
if err != nil {
return create.DiagError(names.SESV2, create.ErrActionCreating, ResNameEmailIdentityMailFromAttributes, d.Get("email_identity").(string), err)
}

if out == nil {
return create.DiagError(names.SESV2, create.ErrActionCreating, ResNameEmailIdentityMailFromAttributes, d.Get("email_identity").(string), errors.New("empty output"))
}

d.SetId(d.Get("email_identity").(string))

return resourceEmailIdentityMailFromAttributesRead(ctx, d, meta)
}

func resourceEmailIdentityMailFromAttributesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).SESV2Client

out, err := FindEmailIdentityByID(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] SESV2 EmailIdentityMailFromAttributes (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return create.DiagError(names.SESV2, create.ErrActionReading, ResNameEmailIdentityMailFromAttributes, d.Id(), err)
}

d.Set("email_identity", d.Id())

if out.MailFromAttributes != nil {
d.Set("behavior_on_mx_failure", out.MailFromAttributes.BehaviorOnMxFailure)
d.Set("mail_from_domain", out.MailFromAttributes.MailFromDomain)
} else {
d.Set("behavior_on_mx_failure", nil)
d.Set("mail_from_domain", nil)
}

return nil
}

func resourceEmailIdentityMailFromAttributesUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).SESV2Client

update := false

in := &sesv2.PutEmailIdentityMailFromAttributesInput{
EmailIdentity: aws.String(d.Id()),
}

if d.HasChanges("behavior_on_mx_failure", "mail_from_domain") {
in.BehaviorOnMxFailure = types.BehaviorOnMxFailure((d.Get("behavior_on_mx_failure").(string)))
in.MailFromDomain = aws.String(d.Get("mail_from_domain").(string))

if in.BehaviorOnMxFailure == types.BehaviorOnMxFailureRejectMessage && (in.MailFromDomain == nil || aws.ToString(in.MailFromDomain) == "") {
return create.DiagError(names.SESV2, create.ErrActionUpdating, ResNameEmailIdentityMailFromAttributes, d.Get("email_identity").(string), ErrMailFromRequired)
}

update = true
}

if !update {
return nil
}

log.Printf("[DEBUG] Updating SESV2 EmailIdentityMailFromAttributes (%s): %#v", d.Id(), in)
_, err := conn.PutEmailIdentityMailFromAttributes(ctx, in)
if err != nil {
return create.DiagError(names.SESV2, create.ErrActionUpdating, ResNameEmailIdentityMailFromAttributes, d.Id(), err)
}

return resourceEmailIdentityMailFromAttributesRead(ctx, d, meta)
}

func resourceEmailIdentityMailFromAttributesDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).SESV2Client

log.Printf("[INFO] Deleting SESV2 EmailIdentityMailFromAttributes %s", d.Id())

_, err := conn.PutEmailIdentityMailFromAttributes(ctx, &sesv2.PutEmailIdentityMailFromAttributesInput{
EmailIdentity: aws.String(d.Id()),
})

if err != nil {
var nfe *types.NotFoundException
if errors.As(err, &nfe) {
return nil
}

return create.DiagError(names.SESV2, create.ErrActionDeleting, ResNameEmailIdentityMailFromAttributes, d.Id(), err)
}

return nil
}
Loading