Skip to content

Commit

Permalink
resource/aws_ssm_parameter: Omit data_type default, add special aws:e…
Browse files Browse the repository at this point in the history
…c2:image error handling during read, and acceptance test aws:ec2:image data_type

Reference: #13326

Output from acceptance testing:

```
--- PASS: TestAccAWSSSMParameter_disappears (8.61s)
--- PASS: TestAccAWSSSMParameter_basic (12.28s)
--- PASS: TestAccAWSSSMParameter_fullPath (13.42s)
--- PASS: TestAccAWSSSMParameter_secure (16.02s)
--- PASS: TestAccAWSSSMParameter_DataType_AwsEc2Image (18.95s)
--- PASS: TestAccAWSSSMParameter_changeNameForcesNew (21.68s)
--- PASS: TestAccAWSSSMParameter_secure_with_key (23.16s)
--- PASS: TestAccAWSSSMParameter_updateDescription (24.50s)
--- PASS: TestAccAWSSSMParameter_updateType (26.40s)
--- PASS: TestAccAWSSSMParameter_Tier (28.93s)
--- PASS: TestAccAWSSSMParameter_tags (29.42s)
--- PASS: TestAccAWSSSMParameter_secure_keyUpdate (31.56s)
--- PASS: TestAccAWSSSMParameter_overwrite (32.82s)
```
  • Loading branch information
bflad committed Jul 31, 2020
1 parent e65a895 commit 8715812
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
10 changes: 8 additions & 2 deletions aws/resource_aws_ssm_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func resourceAwsSsmParameter() *schema.Resource {
"data_type": {
Type: schema.TypeString,
Optional: true,
Default: "text",
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"aws:ec2:image",
"text",
Expand Down Expand Up @@ -111,11 +111,17 @@ func resourceAwsSsmParameterRead(d *schema.ResourceData, meta interface{}) error
Name: aws.String(d.Id()),
WithDecryption: aws.Bool(true),
})
if isAWSErr(err, ssm.ErrCodeParameterNotFound, "") {

if isAWSErr(err, ssm.ErrCodeParameterNotFound, "") && d.IsNewResource() && d.Get("data_type").(string) == "aws:ec2:image" {
return fmt.Errorf("error reading SSM Parameter (%s) after creation: this can indicate that the provided parameter value could not be validated by SSM", d.Id())
}

if isAWSErr(err, ssm.ErrCodeParameterNotFound, "") && !d.IsNewResource() {
log.Printf("[WARN] SSM Parameter (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading SSM Parameter (%s): %w", d.Id(), err)
}
Expand Down
21 changes: 11 additions & 10 deletions aws/resource_aws_ssm_parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,9 @@ func TestAccAWSSSMParameter_secure(t *testing.T) {
})
}

func TestAccAWSSSMParameter_dataType(t *testing.T) {
func TestAccAWSSSMParameter_DataType_AwsEc2Image(t *testing.T) {
var param ssm.Parameter
name := fmt.Sprintf("%s_%s", t.Name(), acctest.RandString(10))
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_ssm_parameter.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -346,11 +346,10 @@ func TestAccAWSSSMParameter_dataType(t *testing.T) {
CheckDestroy: testAccCheckAWSSSMParameterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSSMParameterConfigDataType(name, "text", "teststring"),
Config: testAccAWSSSMParameterConfigDataTypeAwsEc2Image(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "value", "teststring"),
resource.TestCheckResourceAttr(resourceName, "data_type", "text"),
testAccCheckAWSSSMParameterExists(resourceName, &param),
resource.TestCheckResourceAttr(resourceName, "data_type", "aws:ec2:image"),
),
},
{
Expand Down Expand Up @@ -538,15 +537,17 @@ resource "aws_ssm_parameter" "test" {
`, rName, tier)
}

func testAccAWSSSMParameterConfigDataType(rName, data_type, value string) string {
return fmt.Sprintf(`
func testAccAWSSSMParameterConfigDataTypeAwsEc2Image(rName string) string {
return composeConfig(
testAccLatestAmazonLinuxHvmEbsAmiConfig(),
fmt.Sprintf(`
resource "aws_ssm_parameter" "test" {
name = %[1]q
data_type = %[2]q
data_type = "aws:ec2:image"
type = "String"
value = %[3]q
value = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
}
`, rName, data_type, value)
`, rName))
}

func testAccAWSSSMParameterBasicConfigTags1(rName, tagKey1, tagValue1 string) string {
Expand Down

0 comments on commit 8715812

Please sign in to comment.