Skip to content

Commit

Permalink
Merge pull request #39325 from hashicorp/f-aws_sesv2_account_suppress…
Browse files Browse the repository at this point in the history
…ion_attributes

r/aws_sesv2_account_suppression_attributes: New resource
  • Loading branch information
ewbankkit authored Sep 16, 2024
2 parents 81a5e59 + 6974f49 commit 58d9fb8
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/39325.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_sesv2_account_suppression_attributes
```
158 changes: 158 additions & 0 deletions internal/service/sesv2/account_suppression_attributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package sesv2

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/service/sesv2"
awstypes "github.com/aws/aws-sdk-go-v2/service/sesv2/types"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkResource(name="Account Suppression Attributes")
func newAccountSuppressionAttributesResource(context.Context) (resource.ResourceWithConfigure, error) {
r := &accountSuppressionAttributesResource{}

return r, nil
}

type accountSuppressionAttributesResource struct {
framework.ResourceWithConfigure
framework.WithNoOpDelete
framework.WithImportByID
}

func (*accountSuppressionAttributesResource) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) {
response.TypeName = "aws_sesv2_account_suppression_attributes"
}

func (r *accountSuppressionAttributesResource) Schema(ctx context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) {
response.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
names.AttrID: framework.IDAttribute(),
"suppressed_reasons": schema.SetAttribute{
CustomType: fwtypes.NewSetTypeOf[fwtypes.StringEnum[awstypes.SuppressionListReason]](ctx),
Required: true,
ElementType: fwtypes.StringEnumType[awstypes.SuppressionListReason](),
},
},
}
}

func (r *accountSuppressionAttributesResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
var data accountSuppressionAttributesResourceModel
response.Diagnostics.Append(request.Plan.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

conn := r.Meta().SESV2Client(ctx)

id := r.Meta().AccountID
input := &sesv2.PutAccountSuppressionAttributesInput{}
response.Diagnostics.Append(fwflex.Expand(ctx, data, input)...)
if response.Diagnostics.HasError() {
return
}

_, err := conn.PutAccountSuppressionAttributes(ctx, input)

if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("creating SESv2 Account Suppression Attributes (%s)", id), err.Error())

return
}

// Set values for unknowns.
data.ID = fwflex.StringValueToFramework(ctx, id)

response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}

func (r *accountSuppressionAttributesResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
var data accountSuppressionAttributesResourceModel
response.Diagnostics.Append(request.State.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

conn := r.Meta().SESV2Client(ctx)

suppressionAttributes, err := findAccountSuppressionAttributes(ctx, conn)

if tfresource.NotFound(err) {
response.Diagnostics.Append(fwdiag.NewResourceNotFoundWarningDiagnostic(err))
response.State.RemoveResource(ctx)

return
}

if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("reading SESv2 Account Suppression Attributes (%s)", data.ID.ValueString()), err.Error())

return
}

response.Diagnostics.Append(fwflex.Flatten(ctx, suppressionAttributes, &data)...)
if response.Diagnostics.HasError() {
return
}

response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}

func (r *accountSuppressionAttributesResource) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) {
var new accountSuppressionAttributesResourceModel
response.Diagnostics.Append(request.Plan.Get(ctx, &new)...)
if response.Diagnostics.HasError() {
return
}

conn := r.Meta().SESV2Client(ctx)

input := &sesv2.PutAccountSuppressionAttributesInput{}
response.Diagnostics.Append(fwflex.Expand(ctx, new, input)...)
if response.Diagnostics.HasError() {
return
}

_, err := conn.PutAccountSuppressionAttributes(ctx, input)

if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("updating SESv2 Account Suppression Attributes (%s)", new.ID.ValueString()), err.Error())

return
}

response.Diagnostics.Append(response.State.Set(ctx, &new)...)
}

func findAccountSuppressionAttributes(ctx context.Context, conn *sesv2.Client) (*awstypes.SuppressionAttributes, error) {
output, err := findAccount(ctx, conn)

if err != nil {
return nil, err
}

if output.SuppressionAttributes == nil {
return nil, tfresource.NewEmptyResultError(nil)
}

return output.SuppressionAttributes, nil
}

type accountSuppressionAttributesResourceModel struct {
ID types.String `tfsdk:"id"`
SuppressedReasons fwtypes.SetValueOf[fwtypes.StringEnum[awstypes.SuppressionListReason]] `tfsdk:"suppressed_reasons"`
}
129 changes: 129 additions & 0 deletions internal/service/sesv2/account_suppression_attributes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package sesv2_test

import (
"context"
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/service/sesv2/types"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfsesv2 "github.com/hashicorp/terraform-provider-aws/internal/service/sesv2"
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestAccSESV2AccountSuppressionAttributes_serial(t *testing.T) {
t.Parallel()

testCases := map[string]func(t *testing.T){
acctest.CtBasic: testAccAccountSuppressionAttributes_basic,
"update": testAccAccountSuppressionAttributes_update,
}

acctest.RunSerialTests1Level(t, testCases, 0)
}

func testAccAccountSuppressionAttributes_basic(t *testing.T) {
ctx := acctest.Context(t)
resourceName := "aws_sesv2_account_suppression_attributes.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.SESV2ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: acctest.CheckDestroyNoop,
Steps: []resource.TestStep{
{
Config: testAccAccountSuppressionAttributesConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckAccountSuppressionAttributesExists(ctx, resourceName),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("suppressed_reasons"), knownvalue.SetSizeExact(1)),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("suppressed_reasons"), knownvalue.SetExact(
[]knownvalue.Check{
knownvalue.StringExact(string(types.SuppressionListReasonComplaint)),
}),
),
},
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccAccountSuppressionAttributes_update(t *testing.T) {
ctx := acctest.Context(t)
resourceName := "aws_sesv2_account_suppression_attributes.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.SESV2ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: acctest.CheckDestroyNoop,
Steps: []resource.TestStep{
{
Config: testAccAccountSuppressionAttributesConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckAccountSuppressionAttributesExists(ctx, resourceName),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("suppressed_reasons"), knownvalue.SetSizeExact(1)),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("suppressed_reasons"), knownvalue.SetExact(
[]knownvalue.Check{
knownvalue.StringExact(string(types.SuppressionListReasonComplaint)),
}),
),
},
},
{
Config: testAccAccountSuppressionAttributesConfig_updated,
Check: resource.ComposeTestCheckFunc(
testAccCheckAccountSuppressionAttributesExists(ctx, resourceName),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("suppressed_reasons"), knownvalue.SetSizeExact(0)),
},
},
},
})
}

func testAccCheckAccountSuppressionAttributesExists(ctx context.Context, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

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

_, err := tfsesv2.FindAccountSuppressionAttributes(ctx, conn)

return err
}
}

const testAccAccountSuppressionAttributesConfig_basic = `
resource "aws_sesv2_account_suppression_attributes" "test" {
suppressed_reasons = ["COMPLAINT"]
}
`

const testAccAccountSuppressionAttributesConfig_updated = `
resource "aws_sesv2_account_suppression_attributes" "test" {
suppressed_reasons = []
}
`
1 change: 1 addition & 0 deletions internal/service/sesv2/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
ResourceEmailIdentityMailFromAttributes = resourceEmailIdentityMailFromAttributes
ResourceEmailIdentityPolicy = resourceEmailIdentityPolicy

FindAccountSuppressionAttributes = findAccountSuppressionAttributes
FindAccountVDMAttributes = findAccountVDMAttributes
FindConfigurationSetByID = findConfigurationSetByID
FindConfigurationSetEventDestinationByTwoPartKey = findConfigurationSetEventDestinationByTwoPartKey
Expand Down
7 changes: 6 additions & 1 deletion internal/service/sesv2/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions website/docs/r/sesv2_account_suppression_attributes.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
subcategory: "SESv2 (Simple Email V2)"
layout: "aws"
page_title: "AWS: aws_sesv2_account_suppression_attributes"
description: |-
Manages AWS SESv2 (Simple Email V2) account-level suppression attributes.
---

# Resource: aws_sesv2_account_suppression_attributes

Manages AWS SESv2 (Simple Email V2) account-level suppression attributes.

## Example Usage

```terraform
resource "aws_sesv2_account_suppression_attributes" "example" {
suppressed_reasons = ["COMPLAINT"]
}
```

## Argument Reference

The following arguments are required:

* `suppressed_reasons` - (Required) A list that contains the reasons that email addresses will be automatically added to the suppression list for your account. Valid values: `COMPLAINT`, `BOUNCE`.

## Attribute Reference

This resource exports no additional attributes.

## Import

In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import account-level suppression attributes using the account ID. For example:

```terraform
import {
to = aws_sesv2_account_suppression_attributes.example
id = "123456789012"
}
```

Using `terraform import`, import account-level suppression attributes using the account ID. For example:

```console
% terraform import aws_sesv2_account_suppression_attributes.example 123456789012
```

0 comments on commit 58d9fb8

Please sign in to comment.