Skip to content

Commit

Permalink
resource/aws_ses_sending_ip_pool: initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed May 23, 2020
1 parent 2cc9c47 commit 2ab5161
Show file tree
Hide file tree
Showing 9 changed files with 12,820 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,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/simpledb"
Expand Down Expand Up @@ -313,6 +314,7 @@ type AWSClient struct {
serverlessapplicationrepositoryconn *serverlessapplicationrepository.ServerlessApplicationRepository
servicequotasconn *servicequotas.ServiceQuotas
sesconn *ses.SES
sesv2Conn *sesv2.SESV2
sfnconn *sfn.SFN
shieldconn *shield.Shield
simpledbconn *simpledb.SimpleDB
Expand Down Expand Up @@ -529,6 +531,7 @@ func (c *Config) Client() (interface{}, error) {
serverlessapplicationrepositoryconn: 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"])})),
simpledbconn: simpledb.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["sdb"])})),
snsconn: sns.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints["sns"])})),
Expand Down
2 changes: 2 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ func Provider() terraform.ResourceProvider {
"aws_ses_receipt_filter": resourceAwsSesReceiptFilter(),
"aws_ses_receipt_rule": resourceAwsSesReceiptRule(),
"aws_ses_receipt_rule_set": resourceAwsSesReceiptRuleSet(),
"aws_ses_sending_ip_pool": resourceAwsSesSendingIpPool(),
"aws_ses_configuration_set": resourceAwsSesConfigurationSet(),
"aws_ses_event_destination": resourceAwsSesEventDestination(),
"aws_ses_identity_notification_topic": resourceAwsSesNotificationTopic(),
Expand Down Expand Up @@ -1129,6 +1130,7 @@ func init() {
"servicediscovery",
"servicequotas",
"ses",
"sesv2",
"shield",
"sns",
"sqs",
Expand Down
137 changes: 137 additions & 0 deletions aws/resource_aws_ses_sending_ip_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package aws

import (
"fmt"
"log"
"sort"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sesv2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func resourceAwsSesSendingIpPool() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSesSendingIpPoolCreate,
Read: resourceAwsSesSendingIpPoolRead,
Update: resourceAwsSesSendingIpPoolUpdate,
Delete: resourceAwsSesSendingIpPoolDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ips": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func resourceAwsSesSendingIpPoolCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sesv2Conn

poolName := d.Get("name").(string)
_, err := conn.CreateDedicatedIpPool(&sesv2.CreateDedicatedIpPoolInput{
PoolName: aws.String(poolName),
})
if err != nil {
return fmt.Errorf("Error creating SESv2 ip pool: %s", err)
}

d.SetId(poolName)

// Set other properties of the sending pool
if err := resourceAwsSesSendingIpPoolUpdate(d, meta); err != nil {
return err
}

return resourceAwsSesSendingIpPoolRead(d, meta)
}

func resourceAwsSesSendingIpPoolRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sesv2Conn

response, err := conn.ListDedicatedIpPools(&sesv2.ListDedicatedIpPoolsInput{
PageSize: aws.Int64(100),
})
if err != nil {
d.SetId("")
return fmt.Errorf("Error reading SESv2 ip pool: %v", err)
}
for i := range response.DedicatedIpPools {
if n := *response.DedicatedIpPools[i]; strings.EqualFold(n, d.Id()) {
d.Set("name", d.Id())

ips, err := readDedicatedIPs(conn, d.Id())
if err != nil {
return err
}
return d.Set("ips", ips)
}
}
return fmt.Errorf("unable to find %s sending pool", d.Id())
}

func readDedicatedIPs(conn *sesv2.SESV2, poolName string) ([]string, error) {
resp, err := conn.GetDedicatedIps(&sesv2.GetDedicatedIpsInput{
PageSize: aws.Int64(100),
PoolName: aws.String(poolName),
})
if err != nil {
return nil, err
}

var out []string
for i := range resp.DedicatedIps {
if ip := resp.DedicatedIps[i]; ip != nil && ip.Ip != nil {
out = append(out, *ip.Ip)
}
}
sort.Strings(out)
return out, nil
}

func resourceAwsSesSendingIpPoolUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sesv2Conn

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

if v, ok := d.GetOk("ips"); ok {
ips := v.([]interface{})
for i := range ips {
_, err := conn.PutDedicatedIpInPool(&sesv2.PutDedicatedIpInPoolInput{
DestinationPoolName: aws.String(name),
Ip: aws.String(ips[i].(string)),
})
if err != nil {
return fmt.Errorf("Error adding IP to pool: %v", err)
}
}
}

// Read all IP's on the pool
ips, err := readDedicatedIPs(conn, d.Id())
if err != nil {
return err
}
return d.Set("ips", ips)
}

func resourceAwsSesSendingIpPoolDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sesv2Conn
log.Printf("[DEBUG] SES Delete Sending IP Pool: id=%s name=%s", d.Id(), d.Get("name").(string))
_, err := conn.DeleteDedicatedIpPool(&sesv2.DeleteDedicatedIpPoolInput{
PoolName: aws.String(d.Id()),
})
return err
}
86 changes: 86 additions & 0 deletions aws/resource_aws_ses_sending_ip_pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package aws

import (
"errors"
"testing"

"github.com/aws/aws-sdk-go/service/sesv2"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccAwsSESIpSendingPool_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAWSSES(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsSESIpSendingPoolDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsSESIpSendingPoolConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsSESIpSendingPoolExists("aws_ses_ip_sending_pool.test"),
),
},
},
})
}

func testAccCheckAwsSESIpSendingPoolDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).sesv2Conn

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

response, err := conn.ListDedicatedIpPools(&sesv2.ListDedicatedIpPoolsInput{})
if err != nil {
return err
}

found := false
for i := range response.DedicatedIpPools {
if n := *response.DedicatedIpPools[i]; n == "sender" {
found = true
}
}
if found {
return errors.New("The sending ip pool still exists")
}
}

return nil
}

func testAccCheckAwsSESIpSendingPoolExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).sesv2Conn

response, err := conn.ListDedicatedIpPools(&sesv2.ListDedicatedIpPoolsInput{})
if err != nil {
return err
}

found := false
for i := range response.DedicatedIpPools {
if *response.DedicatedIpPools[i] == "sender" {
found = true
}
}

if !found {
return errors.New("The sending ip pool was not created")
}

return nil
}
}

const testAccAwsSESIpSendingPoolConfig = `
resource "aws_ses_sending_ip_pool" "test" {
name = "sender"
}
`
Loading

0 comments on commit 2ab5161

Please sign in to comment.