Skip to content

Commit

Permalink
resource/aws_dlm_lifecycle_policy: Address #5558 PR feedback
Browse files Browse the repository at this point in the history
```
--- PASS: TestAccAWSDlmLifecyclePolicy_Basic (15.16s)
--- PASS: TestAccAWSDlmLifecyclePolicy_Full (20.28s)
```
  • Loading branch information
bflad committed Nov 6, 2018
1 parent b18f21d commit 5185e50
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 37 deletions.
35 changes: 24 additions & 11 deletions aws/resource_aws_dlm_lifecycle_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"regexp"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dlm"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
Expand Down Expand Up @@ -145,7 +144,7 @@ func resourceAwsDlmLifecyclePolicyCreate(d *schema.ResourceData, meta interface{
log.Printf("[INFO] Creating DLM lifecycle policy: %s", input)
out, err := conn.CreateLifecyclePolicy(&input)
if err != nil {
return err
return fmt.Errorf("error creating DLM Lifecycle Policy: %s", err)
}

d.SetId(*out.PolicyId)
Expand All @@ -160,12 +159,15 @@ func resourceAwsDlmLifecyclePolicyRead(d *schema.ResourceData, meta interface{})
out, err := conn.GetLifecyclePolicy(&dlm.GetLifecyclePolicyInput{
PolicyId: aws.String(d.Id()),
})

if isAWSErr(err, dlm.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] DLM Lifecycle Policy (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "ResourceNotFoundException" && !d.IsNewResource() {
d.SetId("")
return nil
}
return err
return fmt.Errorf("error reading DLM Lifecycle Policy (%s): %s", d.Id(), err)
}

d.Set("description", out.Policy.Description)
Expand Down Expand Up @@ -201,7 +203,7 @@ func resourceAwsDlmLifecyclePolicyUpdate(d *schema.ResourceData, meta interface{
log.Printf("[INFO] Updating lifecycle policy %s", d.Id())
_, err := conn.UpdateLifecyclePolicy(&input)
if err != nil {
return err
return fmt.Errorf("error updating DLM Lifecycle Policy (%s): %s", d.Id(), err)
}

return resourceAwsDlmLifecyclePolicyRead(d, meta)
Expand All @@ -215,13 +217,17 @@ func resourceAwsDlmLifecyclePolicyDelete(d *schema.ResourceData, meta interface{
PolicyId: aws.String(d.Id()),
})
if err != nil {
return err
return fmt.Errorf("error deleting DLM Lifecycle Policy (%s): %s", d.Id(), err)
}

return nil
}

func expandDlmPolicyDetails(cfg []interface{}) *dlm.PolicyDetails {
if len(cfg) == 0 || cfg[0] == nil {
return nil
}

policyDetails := &dlm.PolicyDetails{}
m := cfg[0].(map[string]interface{})
if v, ok := m["resource_types"]; ok {
Expand Down Expand Up @@ -284,6 +290,9 @@ func flattenDlmSchedules(schedules []*dlm.Schedule) []map[string]interface{} {
}

func expandDlmCreateRule(cfg []interface{}) *dlm.CreateRule {
if len(cfg) == 0 || cfg[0] == nil {
return nil
}
c := cfg[0].(map[string]interface{})
createRule := &dlm.CreateRule{
Interval: aws.Int64(int64(c["interval"].(int))),
Expand All @@ -310,8 +319,12 @@ func flattenDlmCreateRule(createRule *dlm.CreateRule) []map[string]interface{} {
}

func expandDlmRetainRule(cfg []interface{}) *dlm.RetainRule {
if len(cfg) == 0 || cfg[0] == nil {
return nil
}
m := cfg[0].(map[string]interface{})
return &dlm.RetainRule{
Count: aws.Int64(int64(cfg[0].(map[string]interface{})["count"].(int))),
Count: aws.Int64(int64(m["count"].(int))),
}
}

Expand All @@ -337,7 +350,7 @@ func expandDlmTags(m map[string]interface{}) []*dlm.Tag {
func flattenDlmTags(tags []*dlm.Tag) map[string]string {
result := make(map[string]string)
for _, t := range tags {
result[*t.Key] = *t.Value
result[aws.StringValue(t.Key)] = aws.StringValue(t.Value)
}

return result
Expand Down
67 changes: 41 additions & 26 deletions aws/resource_aws_dlm_lifecycle_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dlm"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSDlmLifecyclePolicyBasic(t *testing.T) {
func TestAccAWSDlmLifecyclePolicy_Basic(t *testing.T) {
resourceName := "aws_dlm_lifecycle_policy.basic"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.Test(t, resource.TestCase{
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: dlmLifecyclePolicyDestroy,
Steps: []resource.TestStep{
{
Config: dlmLifecyclePolicyBasicConfig(),
Config: dlmLifecyclePolicyBasicConfig(rName),
Check: resource.ComposeTestCheckFunc(
checkDlmLifecyclePolicyExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "description", "tf-acc-basic"),
Expand All @@ -44,16 +45,17 @@ func TestAccAWSDlmLifecyclePolicyBasic(t *testing.T) {
})
}

func TestAccAWSDlmLifecyclePolicyFull(t *testing.T) {
func TestAccAWSDlmLifecyclePolicy_Full(t *testing.T) {
resourceName := "aws_dlm_lifecycle_policy.full"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.Test(t, resource.TestCase{
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: dlmLifecyclePolicyDestroy,
Steps: []resource.TestStep{
{
Config: dlmLifecyclePolicyFullConfig(),
Config: dlmLifecyclePolicyFullConfig(rName),
Check: resource.ComposeTestCheckFunc(
checkDlmLifecyclePolicyExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "description", "tf-acc-full"),
Expand All @@ -70,7 +72,7 @@ func TestAccAWSDlmLifecyclePolicyFull(t *testing.T) {
),
},
{
Config: dlmLifecyclePolicyFullUpdateConfig(),
Config: dlmLifecyclePolicyFullUpdateConfig(rName),
Check: resource.ComposeTestCheckFunc(
checkDlmLifecyclePolicyExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "description", "tf-acc-full-updated"),
Expand Down Expand Up @@ -99,16 +101,17 @@ func dlmLifecyclePolicyDestroy(s *terraform.State) error {
}

input := dlm.GetLifecyclePolicyInput{
PolicyId: aws.String(rs.Primary.Attributes["name"]),
PolicyId: aws.String(rs.Primary.ID),
}

out, err := conn.GetLifecyclePolicy(&input)

if isAWSErr(err, dlm.ErrCodeResourceNotFoundException, "") {
return nil
}

if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "ResourceNotFoundException" {
return nil
}
return err
return fmt.Errorf("error getting DLM Lifecycle Policy (%s): %s", rs.Primary.ID, err)
}

if out.Policy != nil {
Expand All @@ -121,19 +124,31 @@ func dlmLifecyclePolicyDestroy(s *terraform.State) error {

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

conn := testAccProvider.Meta().(*AWSClient).dlmconn

input := dlm.GetLifecyclePolicyInput{
PolicyId: aws.String(rs.Primary.ID),
}

_, err := conn.GetLifecyclePolicy(&input)

if err != nil {
return fmt.Errorf("error getting DLM Lifecycle Policy (%s): %s", rs.Primary.ID, err)
}

return nil
}
}

func dlmLifecyclePolicyBasicConfig() string {
return fmt.Sprint(`
func dlmLifecyclePolicyBasicConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "dlm_lifecycle_role" {
name = "tf-acc-basic-dlm-lifecycle-role"
name = %q
assume_role_policy = <<EOF
{
Expand Down Expand Up @@ -176,13 +191,13 @@ resource "aws_dlm_lifecycle_policy" "basic" {
}
}
}
`)
`, rName)
}

func dlmLifecyclePolicyFullConfig() string {
return fmt.Sprint(`
func dlmLifecyclePolicyFullConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "dlm_lifecycle_role" {
name = "tf-acc-full-dlm-lifecycle-role"
name = %q
assume_role_policy = <<EOF
{
Expand Down Expand Up @@ -232,13 +247,13 @@ resource "aws_dlm_lifecycle_policy" "full" {
}
}
}
`)
`, rName)
}

func dlmLifecyclePolicyFullUpdateConfig() string {
return fmt.Sprint(`
func dlmLifecyclePolicyFullUpdateConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "dlm_lifecycle_role" {
name = "tf-acc-full-dlm-lifecycle-role"
name = %q
assume_role_policy = <<EOF
{
Expand Down Expand Up @@ -288,5 +303,5 @@ resource "aws_dlm_lifecycle_policy" "full" {
}
}
}
`)
`, rName)
}
9 changes: 9 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,15 @@
</ul>
</li>

<li<%= sidebar_current("docs-aws-resource-dlm") %>>
<a href="#">Data Lifecycle Manager Resources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-aws-resource-dlm-lifecycle-policy") %>>
<a href="/docs/providers/aws/r/dlm_lifecycle_policy.html">aws_dlm_lifecycle_policy</a>
</li>
</ul>
</li>

<li<%= sidebar_current("docs-aws-resource-dms") %>>
<a href="#">Database Migration Service</a>
<ul class="nav nav-visible">
Expand Down

0 comments on commit 5185e50

Please sign in to comment.