Skip to content

Commit

Permalink
Merge pull request #39364 from kamilturek/f-aws-ses-receipt-rule-s3-a…
Browse files Browse the repository at this point in the history
…ction-iam-role-arn

r/aws_ses_receipt_rule: add `iam_role_arn` argument to `s3_action` configuration block
  • Loading branch information
ewbankkit authored Sep 19, 2024
2 parents fa9ad8a + 73f271f commit dbc0ac9
Show file tree
Hide file tree
Showing 39 changed files with 1,795 additions and 1,445 deletions.
3 changes: 3 additions & 0 deletions .changelog/39364.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_ses_receipt_rule: Add `iam_role_arn` argument to `s3_action` configuration block
```
64 changes: 39 additions & 25 deletions internal/service/ses/active_receipt_rule_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ses"
awstypes "github.com/aws/aws-sdk-go-v2/service/ses/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_ses_active_receipt_rule_set")
func ResourceActiveReceiptRuleSet() *schema.Resource {
// @SDKResource("aws_ses_active_receipt_rule_set", name="Active Receipt Rule Set")
func resourceActiveReceiptRuleSet() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceActiveReceiptRuleSetUpdate,
UpdateWithoutTimeout: resourceActiveReceiptRuleSetUpdate,
Expand Down Expand Up @@ -52,14 +54,14 @@ func resourceActiveReceiptRuleSetUpdate(ctx context.Context, d *schema.ResourceD
conn := meta.(*conns.AWSClient).SESClient(ctx)

ruleSetName := d.Get("rule_set_name").(string)

createOpts := &ses.SetActiveReceiptRuleSetInput{
input := &ses.SetActiveReceiptRuleSetInput{
RuleSetName: aws.String(ruleSetName),
}

_, err := conn.SetActiveReceiptRuleSet(ctx, createOpts)
_, err := conn.SetActiveReceiptRuleSet(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "setting active SES rule set: %s", err)
return sdkdiag.AppendErrorf(diags, "setting SES Active Receipt Rule Set (%s): %s", ruleSetName, err)
}

d.SetId(ruleSetName)
Expand All @@ -71,25 +73,17 @@ func resourceActiveReceiptRuleSetRead(ctx context.Context, d *schema.ResourceDat
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).SESClient(ctx)

describeOpts := &ses.DescribeActiveReceiptRuleSetInput{}

response, err := conn.DescribeActiveReceiptRuleSet(ctx, describeOpts)
if err != nil {
if errs.IsA[*awstypes.RuleSetDoesNotExistException](err) {
log.Printf("[WARN] SES Receipt Rule Set (%s) belonging to SES Active Receipt Rule Set not found, removing from state", d.Id())
d.SetId("")
return diags
}
return sdkdiag.AppendErrorf(diags, "reading SES Active Receipt Rule Set: %s", err)
}
output, err := findActiveReceiptRuleSet(ctx, conn)

if response.Metadata == nil {
log.Print("[WARN] No active Receipt Rule Set found")
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] SES Active Receipt Rule Set (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}

d.Set("rule_set_name", response.Metadata.Name)
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading SES Active Receipt Rule Set: %s", err)
}

arn := arn.ARN{
Partition: meta.(*conns.AWSClient).Partition,
Expand All @@ -99,6 +93,7 @@ func resourceActiveReceiptRuleSetRead(ctx context.Context, d *schema.ResourceDat
Resource: fmt.Sprintf("receipt-rule-set/%s", d.Id()),
}.String()
d.Set(names.AttrARN, arn)
d.Set("rule_set_name", output.Name)

return diags
}
Expand All @@ -107,13 +102,10 @@ func resourceActiveReceiptRuleSetDelete(ctx context.Context, d *schema.ResourceD
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).SESClient(ctx)

deleteOpts := &ses.SetActiveReceiptRuleSetInput{
RuleSetName: nil,
}
_, err := conn.SetActiveReceiptRuleSet(ctx, &ses.SetActiveReceiptRuleSetInput{})

_, err := conn.SetActiveReceiptRuleSet(ctx, deleteOpts)
if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting active SES rule set: %s", err)
return sdkdiag.AppendErrorf(diags, "deleting SES Active Receipt Rule Set: %s", err)
}

return diags
Expand Down Expand Up @@ -150,3 +142,25 @@ func resourceActiveReceiptRuleSetImport(ctx context.Context, d *schema.ResourceD

return []*schema.ResourceData{d}, nil
}

func findActiveReceiptRuleSet(ctx context.Context, conn *ses.Client) (*awstypes.ReceiptRuleSetMetadata, error) {
input := &ses.DescribeActiveReceiptRuleSetInput{}
output, err := conn.DescribeActiveReceiptRuleSet(ctx, input)

if errs.IsA[*awstypes.RuleSetDoesNotExistException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil || output.Metadata == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output.Metadata, nil
}
15 changes: 5 additions & 10 deletions internal/service/ses/active_receipt_rule_set_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ import (

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/ses"
"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/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKDataSource("aws_ses_active_receipt_rule_set")
func DataSourceActiveReceiptRuleSet() *schema.Resource {
// @SDKDataSource("aws_ses_active_receipt_rule_set", name="Active Receipt Rule Set")
func dataSourceActiveReceiptRuleSet() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceActiveReceiptRuleSetRead,

Expand All @@ -40,18 +38,14 @@ func dataSourceActiveReceiptRuleSetRead(ctx context.Context, d *schema.ResourceD
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).SESClient(ctx)

data, err := conn.DescribeActiveReceiptRuleSet(ctx, &ses.DescribeActiveReceiptRuleSetInput{})
data, err := findActiveReceiptRuleSet(ctx, conn)

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading SES Active Receipt Rule Set: %s", err)
}
if data == nil || data.Metadata == nil {
return sdkdiag.AppendErrorf(diags, "reading SES Active Receipt Rule Set: %s", tfresource.NewEmptyResultError(nil))
}

name := aws.ToString(data.Metadata.Name)
name := aws.ToString(data.Name)
d.SetId(name)
d.Set("rule_set_name", name)
arn := arn.ARN{
Partition: meta.(*conns.AWSClient).Partition,
Service: "ses",
Expand All @@ -60,6 +54,7 @@ func dataSourceActiveReceiptRuleSetRead(ctx context.Context, d *schema.ResourceD
Resource: fmt.Sprintf("receipt-rule-set/%s", name),
}.String()
d.Set(names.AttrARN, arn)
d.Set("rule_set_name", name)

return diags
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func testAccActiveReceiptRuleSetDataSource_basic(t *testing.T) {
},
ErrorCheck: acctest.ErrorCheck(t, names.SESServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckActiveReceiptRuleSetDestroy(ctx),

Steps: []resource.TestStep{
{
Config: testAccActiveReceiptRuleSetDataSourceConfig_basic(rName),
Expand Down
33 changes: 12 additions & 21 deletions internal/service/ses/active_receipt_rule_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ses"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfses "github.com/hashicorp/terraform-provider-aws/internal/service/ses"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

Expand Down Expand Up @@ -105,14 +104,17 @@ func testAccCheckActiveReceiptRuleSetDestroy(ctx context.Context) resource.TestC
continue
}

response, err := conn.DescribeActiveReceiptRuleSet(ctx, &ses.DescribeActiveReceiptRuleSetInput{})
_, err := tfses.FindActiveReceiptRuleSet(ctx, conn)

if tfresource.NotFound(err) {
continue
}

if err != nil {
return err
}

if response.Metadata != nil && (aws.ToString(response.Metadata.Name) == rs.Primary.ID) {
return fmt.Errorf("Active receipt rule set still exists")
}
return fmt.Errorf("SES Active Receipt Rule Set %s still exists", rs.Primary.ID)
}

return nil
Expand All @@ -121,27 +123,16 @@ func testAccCheckActiveReceiptRuleSetDestroy(ctx context.Context) resource.TestC

func testAccCheckActiveReceiptRuleSetExists(ctx context.Context, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
_, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("SES Active Receipt Rule Set not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("SES Active Receipt Rule Set name not set")
return fmt.Errorf("Not found: %s", n)
}

conn := acctest.Provider.Meta().(*conns.AWSClient).SESClient(ctx)

response, err := conn.DescribeActiveReceiptRuleSet(ctx, &ses.DescribeActiveReceiptRuleSetInput{})
if err != nil {
return err
}
_, err := tfses.FindActiveReceiptRuleSet(ctx, conn)

if response.Metadata != nil && (aws.ToString(response.Metadata.Name) != rs.Primary.ID) {
return fmt.Errorf("The active receipt rule set (%s) was not set to %s", aws.ToString(response.Metadata.Name), rs.Primary.ID)
}

return nil
return err
}
}

Expand Down
Loading

0 comments on commit dbc0ac9

Please sign in to comment.