-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
jar-b
merged 13 commits into
hashicorp:main
from
kamilturek:f-aws-sesv2-email-identity-mail-from-attributes
Nov 11, 2022
+446
−6
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
855b721
Create new resource
kamilturek 048a9b7
Register the new resource
kamilturek 78a9033
Create acceptance tests
kamilturek 3dd18e9
Create a website page
kamilturek 967de8e
Add custom validation
kamilturek 207b5a9
Add a note about the validation
kamilturek 6f3566c
Add a changelog entry
kamilturek 16d04a3
Fix the basic usage example
kamilturek 130cd6c
r/aws_sesv2_email_identity_mail_from_attributes: use email_identity i…
jar-b 3162175
r/aws_sesv2_email_identity_mail_from_attributes: split disappears tests
jar-b abd4498
r/aws_sesv2_email_identity_mail_from_attributes: update nil check err
jar-b 8dfc37f
r/aws_sesv2_email_identity_feedback_attributes: update nil check err
jar-b 61a747a
r/aws_sesv2_email_identity_mail_from_attributes: use enum validate func
jar-b File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
internal/service/sesv2/email_identity_mail_from_attributes.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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](), | ||
}, | ||
"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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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! 🙌