Skip to content

Commit

Permalink
Merge pull request #17754 from heitorlessa/fix/cloudwatch_event_archi…
Browse files Browse the repository at this point in the history
…ve_int64

fix: cloudwatch_event_archive retention type on creation bug #16067
  • Loading branch information
gdavison authored Feb 24, 2021
2 parents a0a19aa + 5942510 commit 7546a7f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .changelog/17754.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``release-note:bug
resource/aws_cloudwatch_event_archive: Fix retention_days type conversion on creation
```
13 changes: 8 additions & 5 deletions aws/resource_aws_cloudwatch_event_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ func resourceAwsCloudWatchEventArchiveRead(d *schema.ResourceData, meta interfac

out, err := conn.DescribeArchive(input)

if isAWSErr(err, events.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] CloudWatch Events archive (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("Error reading CloudWatch Events archive: %w", err)
}
Expand Down Expand Up @@ -166,7 +172,7 @@ func buildCreateArchiveInputStruct(d *schema.ResourceData) (*events.CreateArchiv
}

if v, ok := d.GetOk("retention_days"); ok {
input.RetentionDays = aws.Int64(v.(int64))
input.RetentionDays = aws.Int64(int64(v.(int)))
}

return &input, nil
Expand All @@ -190,11 +196,8 @@ func buildUpdateArchiveInputStruct(d *schema.ResourceData) (*events.UpdateArchiv
}

if v, ok := d.GetOk("retention_days"); ok {
retentionInDays := int64(v.(int))
input.RetentionDays = aws.Int64(retentionInDays)
input.RetentionDays = aws.Int64(int64(v.(int)))
}

return &input, nil
}

// create a datasource
78 changes: 61 additions & 17 deletions aws/resource_aws_cloudwatch_event_archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,20 @@ func testSweepCloudWatchEventArchives(region string) error {
return nil
}

func TestAccAWSCloudWatchArchive_basic(t *testing.T) {
func TestAccAWSCloudWatchEventArchive_basic(t *testing.T) {
var v1 events.DescribeArchiveOutput
archiveName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_cloudwatch_event_archive.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchArchiveDestroy,
CheckDestroy: testAccCheckAWSCloudWatchEventArchiveDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchArchiveConfig(archiveName),
Config: testAccAWSCloudWatchEventArchiveConfig(archiveName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchArchiveExists(resourceName, &v1),
testAccCheckCloudWatchEventArchiveExists(resourceName, &v1),
resource.TestCheckResourceAttr(resourceName, "name", archiveName),
resource.TestCheckResourceAttr(resourceName, "retention_days", "0"),
testAccCheckResourceAttrRegionalARN(resourceName, "arn", "events", fmt.Sprintf("archive/%s", archiveName)),
Expand All @@ -100,26 +100,26 @@ func TestAccAWSCloudWatchArchive_basic(t *testing.T) {
})
}

func TestAccAWSCloudWatchArchive_update(t *testing.T) {
func TestAccAWSCloudWatchEventArchive_update(t *testing.T) {
var v1 events.DescribeArchiveOutput
resourceName := "aws_cloudwatch_event_archive.test"
archiveName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchArchiveDestroy,
CheckDestroy: testAccCheckAWSCloudWatchEventArchiveDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchArchiveConfig(archiveName),
Config: testAccAWSCloudWatchEventArchiveConfig(archiveName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchArchiveExists(resourceName, &v1),
testAccCheckCloudWatchEventArchiveExists(resourceName, &v1),
),
},
{
Config: testAccAWSCloudWatchArchiveConfig_updateAttributes(archiveName),
Config: testAccAWSCloudWatchEventArchiveConfig_updateAttributes(archiveName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchArchiveExists(resourceName, &v1),
testAccCheckCloudWatchEventArchiveExists(resourceName, &v1),
resource.TestCheckResourceAttr(resourceName, "retention_days", "7"),
testAccCheckResourceAttrEquivalentJSON(resourceName, "event_pattern", "{\"source\":[\"company.team.service\"]}"),
resource.TestCheckResourceAttr(resourceName, "description", "test"),
Expand All @@ -137,12 +137,12 @@ func TestAccAWSCloudWatchEventArchive_disappears(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchEventBusDestroy,
CheckDestroy: testAccCheckAWSCloudWatchEventArchiveDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchEventBusConfig(archiveName),
Config: testAccAWSCloudWatchEventArchiveConfig(archiveName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchArchiveExists(resourceName, &v),
testAccCheckCloudWatchEventArchiveExists(resourceName, &v),
testAccCheckResourceDisappears(testAccProvider, resourceAwsCloudWatchEventArchive(), resourceName),
),
ExpectNonEmptyPlan: true,
Expand All @@ -151,7 +151,7 @@ func TestAccAWSCloudWatchEventArchive_disappears(t *testing.T) {
})
}

func testAccCheckAWSCloudWatchArchiveDestroy(s *terraform.State) error {
func testAccCheckAWSCloudWatchEventArchiveDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn

for _, rs := range s.RootModule().Resources {
Expand All @@ -173,7 +173,7 @@ func testAccCheckAWSCloudWatchArchiveDestroy(s *terraform.State) error {
return nil
}

func testAccCheckCloudWatchArchiveExists(n string, v *events.DescribeArchiveOutput) resource.TestCheckFunc {
func testAccCheckCloudWatchEventArchiveExists(n string, v *events.DescribeArchiveOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -200,7 +200,37 @@ func testAccCheckCloudWatchArchiveExists(n string, v *events.DescribeArchiveOutp
}
}

func testAccAWSCloudWatchArchiveConfig(name string) string {
func TestAccAWSCloudWatchEventArchive_retentionSetOnCreation(t *testing.T) {
var v1 events.DescribeArchiveOutput
archiveName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_cloudwatch_event_archive.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchEventArchiveDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchEventArchiveConfig_retentionOnCreation(archiveName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventArchiveExists(resourceName, &v1),
resource.TestCheckResourceAttr(resourceName, "name", archiveName),
resource.TestCheckResourceAttr(resourceName, "retention_days", "1"),
testAccCheckResourceAttrRegionalARN(resourceName, "arn", "events", fmt.Sprintf("archive/%s", archiveName)),
resource.TestCheckResourceAttr(resourceName, "description", ""),
resource.TestCheckResourceAttr(resourceName, "event_pattern", ""),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccAWSCloudWatchEventArchiveConfig(name string) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_event_bus" "test" {
name = %[1]q
Expand All @@ -213,7 +243,7 @@ resource "aws_cloudwatch_event_archive" "test" {
`, name)
}

func testAccAWSCloudWatchArchiveConfig_updateAttributes(name string) string {
func testAccAWSCloudWatchEventArchiveConfig_updateAttributes(name string) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_event_bus" "test" {
name = %[1]q
Expand All @@ -232,3 +262,17 @@ PATTERN
}
`, name)
}

func testAccAWSCloudWatchEventArchiveConfig_retentionOnCreation(name string) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_event_bus" "test" {
name = %[1]q
}
resource "aws_cloudwatch_event_archive" "test" {
name = %[1]q
event_source_arn = aws_cloudwatch_event_bus.test.arn
retention_days = 1
}
`, name)
}

0 comments on commit 7546a7f

Please sign in to comment.