Skip to content
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

resource/aws_ssm_parameter: Refresh from state on 404 #1436

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions aws/resource_aws_ssm_parameter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -66,8 +65,10 @@ func resourceAwsSsmParameterRead(d *schema.ResourceData, meta interface{}) error
return errwrap.Wrapf("[ERROR] Error describing SSM parameter: {{err}}", err)
}

if len(resp.InvalidParameters) > 0 {
return fmt.Errorf("[ERROR] SSM Parameter %s is invalid", d.Id())
if len(resp.Parameters) == 0 {
log.Printf("[WARN] SSM Param %q not found, removing from state", d.Id())
d.SetId("")
return nil
}

param := resp.Parameters[0]
Expand Down
135 changes: 73 additions & 62 deletions aws/resource_aws_ssm_parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func TestAccAWSSSMParameter_basic(t *testing.T) {
var param ssm.Parameter
name := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -21,15 +22,37 @@ func TestAccAWSSSMParameter_basic(t *testing.T) {
{
Config: testAccAWSSSMParameterBasicConfig(name, "bar"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMParameterHasValue("aws_ssm_parameter.foo", "bar"),
testAccCheckAWSSSMParameterType("aws_ssm_parameter.foo", "String"),
testAccCheckAWSSSMParameterExists("aws_ssm_parameter.foo", &param),
resource.TestCheckResourceAttr("aws_ssm_parameter.foo", "value", "bar"),
resource.TestCheckResourceAttr("aws_ssm_parameter.foo", "type", "String"),
),
},
},
})
}

func TestAccAWSSSMParameter_disappears(t *testing.T) {
var param ssm.Parameter
name := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSSMParameterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSSMParameterBasicConfig(name, "bar"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMParameterExists("aws_ssm_parameter.foo", &param),
testAccCheckAWSSSMParameterDisappears(&param),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSSSMParameter_update(t *testing.T) {
var param ssm.Parameter
name := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -42,15 +65,17 @@ func TestAccAWSSSMParameter_update(t *testing.T) {
{
Config: testAccAWSSSMParameterBasicConfigOverwrite(name, "baz1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMParameterHasValue("aws_ssm_parameter.foo", "baz1"),
testAccCheckAWSSSMParameterType("aws_ssm_parameter.foo", "String"),
testAccCheckAWSSSMParameterExists("aws_ssm_parameter.foo", &param),
resource.TestCheckResourceAttr("aws_ssm_parameter.foo", "value", "baz1"),
resource.TestCheckResourceAttr("aws_ssm_parameter.foo", "type", "String"),
),
},
},
})
}

func TestAccAWSSSMParameter_changeNameForcesNew(t *testing.T) {
var beforeParam, afterParam ssm.Parameter
before := acctest.RandString(10)
after := acctest.RandString(10)
resource.Test(t, resource.TestCase{
Expand All @@ -60,18 +85,23 @@ func TestAccAWSSSMParameter_changeNameForcesNew(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccAWSSSMParameterBasicConfig(before, "bar"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMParameterExists("aws_ssm_parameter.foo", &beforeParam),
),
},
{
Config: testAccAWSSSMParameterBasicConfig(after, "bar"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMParameterDestroyed(before),
testAccCheckAWSSSMParameterExists("aws_ssm_parameter.foo", &afterParam),
testAccCheckAWSSSMParameterRecreated(t, &beforeParam, &afterParam),
),
},
},
})
}

func TestAccAWSSSMParameter_secure(t *testing.T) {
var param ssm.Parameter
name := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -81,15 +111,17 @@ func TestAccAWSSSMParameter_secure(t *testing.T) {
{
Config: testAccAWSSSMParameterSecureConfig(name, "secret"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMParameterHasValue("aws_ssm_parameter.secret_foo", "secret"),
testAccCheckAWSSSMParameterType("aws_ssm_parameter.secret_foo", "SecureString"),
testAccCheckAWSSSMParameterExists("aws_ssm_parameter.secret_foo", &param),
resource.TestCheckResourceAttr("aws_ssm_parameter.secret_foo", "value", "secret"),
resource.TestCheckResourceAttr("aws_ssm_parameter.secret_foo", "type", "SecureString"),
),
},
},
})
}

func TestAccAWSSSMParameter_secure_with_key(t *testing.T) {
var param ssm.Parameter
name := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -99,94 +131,73 @@ func TestAccAWSSSMParameter_secure_with_key(t *testing.T) {
{
Config: testAccAWSSSMParameterSecureConfigWithKey(name, "secret"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMParameterHasValue("aws_ssm_parameter.secret_foo", "secret"),
testAccCheckAWSSSMParameterType("aws_ssm_parameter.secret_foo", "SecureString"),
testAccCheckAWSSSMParameterExists("aws_ssm_parameter.secret_foo", &param),
resource.TestCheckResourceAttr("aws_ssm_parameter.secret_foo", "value", "secret"),
resource.TestCheckResourceAttr("aws_ssm_parameter.secret_foo", "type", "SecureString"),
),
},
},
})
}

func testAccCheckAWSSSMGetParameter(s *terraform.State, n string) ([]*ssm.Parameter, error) {
rs, ok := s.RootModule().Resources[n]
if !ok {
return []*ssm.Parameter{}, fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return []*ssm.Parameter{}, fmt.Errorf("No SSM Parameter ID is set")
}

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

paramInput := &ssm.GetParametersInput{
Names: []*string{
aws.String(rs.Primary.Attributes["name"]),
},
WithDecryption: aws.Bool(true),
}

resp, _ := conn.GetParameters(paramInput)

if len(resp.Parameters) == 0 {
return resp.Parameters, fmt.Errorf("Expected AWS SSM Parameter to be created, but wasn't found")
func testAccCheckAWSSSMParameterRecreated(t *testing.T,
before, after *ssm.Parameter) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *before.Name == *after.Name {
t.Fatalf("Expected change of SSM Param Names, but both were %v", *before.Name)
}
return nil
}
return resp.Parameters, nil
}

func testAccCheckAWSSSMParameterHasValue(n string, v string) resource.TestCheckFunc {
func testAccCheckAWSSSMParameterExists(n string, param *ssm.Parameter) resource.TestCheckFunc {
return func(s *terraform.State) error {
parameters, err := testAccCheckAWSSSMGetParameter(s, n)
if err != nil {
return err
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

parameterValue := parameters[0].Value

if *parameterValue != v {
return fmt.Errorf("Expected AWS SSM Parameter to have value %s but had %s", v, *parameterValue)
if rs.Primary.ID == "" {
return fmt.Errorf("No SSM Parameter ID is set")
}

return nil
}
}
conn := testAccProvider.Meta().(*AWSClient).ssmconn

func testAccCheckAWSSSMParameterType(n string, v string) resource.TestCheckFunc {
return func(s *terraform.State) error {
parameters, err := testAccCheckAWSSSMGetParameter(s, n)
paramInput := &ssm.GetParametersInput{
Names: []*string{
aws.String(rs.Primary.Attributes["name"]),
},
WithDecryption: aws.Bool(true),
}

resp, err := conn.GetParameters(paramInput)
if err != nil {
return err
}

parameterValue := parameters[0].Type

if *parameterValue != v {
return fmt.Errorf("Expected AWS SSM Parameter to have type %s but had %s", v, *parameterValue)
if len(resp.Parameters) == 0 {
return fmt.Errorf("Expected AWS SSM Parameter to be created, but wasn't found")
}

*param = *resp.Parameters[0]

return nil
}
}

func testAccCheckAWSSSMParameterDestroyed(name string) resource.TestCheckFunc {
func testAccCheckAWSSSMParameterDisappears(param *ssm.Parameter) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ssmconn

paramInput := &ssm.GetParametersInput{
Names: []*string{
aws.String(name),
},
paramInput := &ssm.DeleteParameterInput{
Name: param.Name,
}

resp, err := conn.GetParameters(paramInput)
_, err := conn.DeleteParameter(paramInput)
if err != nil {
return err
}

if len(resp.Parameters) > 0 {
return fmt.Errorf("Expected AWS SSM Parameter to be gone, but was still found")
}

return nil
}
}
Expand Down Expand Up @@ -214,7 +225,7 @@ func testAccCheckAWSSSMParameterDestroy(s *terraform.State) error {
return nil
}

return fmt.Errorf("Default error in SSM Parameter Test")
return nil
}

func testAccAWSSSMParameterBasicConfig(rName string, value string) string {
Expand Down