From 13bbde0d4308d075cec616132f00a2f7a42e4639 Mon Sep 17 00:00:00 2001 From: John Robison <39737211+jrobison-sb@users.noreply.github.com> Date: Tue, 8 Sep 2020 19:56:46 -0400 Subject: [PATCH 01/22] Use dot internal instead of dot local --- website/docs/r/service_discovery_service.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/service_discovery_service.html.markdown b/website/docs/r/service_discovery_service.html.markdown index 6b691cde263..d1774671cff 100644 --- a/website/docs/r/service_discovery_service.html.markdown +++ b/website/docs/r/service_discovery_service.html.markdown @@ -20,7 +20,7 @@ resource "aws_vpc" "example" { } resource "aws_service_discovery_private_dns_namespace" "example" { - name = "example.terraform.local" + name = "example.terraform.internal" description = "example" vpc = aws_vpc.example.id } From 873750e0d73920fc4e44d4d1ab059bdcad174620 Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Sun, 24 Jan 2021 18:03:54 +0100 Subject: [PATCH 02/22] feat: initial impl. eventbridge event replay --- aws/resource_aws_cloudwatch_event_replay.go | 197 ++++++++++++++++++++ aws/validators.go | 5 + 2 files changed, 202 insertions(+) create mode 100644 aws/resource_aws_cloudwatch_event_replay.go diff --git a/aws/resource_aws_cloudwatch_event_replay.go b/aws/resource_aws_cloudwatch_event_replay.go new file mode 100644 index 00000000000..bca61971f64 --- /dev/null +++ b/aws/resource_aws_cloudwatch_event_replay.go @@ -0,0 +1,197 @@ +package aws + +import ( + "fmt" + "github.com/aws/aws-sdk-go/aws" + events "github.com/aws/aws-sdk-go/service/cloudwatchevents" + "github.com/hashicorp/aws-sdk-go-base/tfawserr" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "log" +) + +func resourceAwsCloudWatchEventReplay() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsCloudWatchEventArchiveCreate, + Read: resourceAwsCloudWatchEventArchiveRead, + Update: resourceAwsCloudWatchEventArchiveUpdate, + Delete: resourceAwsCloudWatchEventArchiveDelete, + + Schema: map[string]*schema.Schema{ + "archive_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateCloudWatchEventArchiveName, + }, + + "description": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringLenBetween(0, 512), + }, + + "event_pattern": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validateEventPatternValue(), + StateFunc: func(v interface{}) string { + json, _ := structure.NormalizeJsonString(v.(string)) + return json + }, + }, + + "event_source_arn": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validateArn, + }, + + "retention_days": { + Type: schema.TypeInt, + Optional: true, + }, + }, + } +} + +func resourceAwsCloudWatchEventArchiveCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudwatcheventsconn + + input, err := buildCreateArchiveInputStruct(d) + + if err != nil { + return fmt.Errorf("Creating CloudWatch Events Archive parameters failed: %w", err) + } + + log.Printf("[DEBUG] Creating CloudWatch Events Archive: %s", input) + + _, err = conn.CreateArchive(input) + if err != nil { + return fmt.Errorf("Creating CloudWatch Events Archive failed: %w", err) + } + + d.SetId(d.Get("archive_name").(string)) + + log.Printf("[INFO] CloudWatch Events Archive (%s) created", d.Id()) + + return resourceAwsCloudWatchEventArchiveRead(d, meta) +} + +func resourceAwsCloudWatchEventArchiveRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudwatcheventsconn + archiveName := d.Get("archive_name").(string) + input := &events.DescribeArchiveInput{ + ArchiveName: aws.String(archiveName), + } + + out, err := conn.DescribeArchive(input) + + if err != nil { + return fmt.Errorf("Error reading CloudWatch Events archive: %w", err) + } + + log.Printf("[DEBUG] Found Archive: #{*out}") + + // Review Question - Is there a problem in setting more than should name and Arn? + d.Set("archive_name", out.ArchiveName) + d.Set("description", out.Description) + d.Set("event_pattern", out.EventPattern) + d.Set("event_source_arn", out.EventSourceArn) + d.Set("arn", out.ArchiveArn) + d.Set("retention_days", out.RetentionDays) + d.Set("event_count", out.EventCount) + d.Set("state", out.State) + d.Set("creation_time", out.CreationTime) + + return nil +} + +func resourceAwsCloudWatchEventArchiveUpdate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudwatcheventsconn + + input, err := buildUpdateArchiveInputStruct(d) + + if err != nil { + return fmt.Errorf("Creating CloudWatch Events Archive parameters failed: %w", err) + } + + log.Printf("[DEBUG] Updating CloudWatch Events Archive: %s", input) + _, err = conn.UpdateArchive(input) + if err != nil { + return fmt.Errorf("error updating CloudWatch Events Archive (%s): %w", d.Id(), err) + } + + return resourceAwsCloudWatchEventArchiveRead(d, meta) +} + +func resourceAwsCloudWatchEventArchiveDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudwatcheventsconn + + input := &events.DeleteArchiveInput{ + ArchiveName: aws.String(d.Get("archive_name").(string)), + } + + _, err := conn.DeleteArchive(input) + if err != nil { + if tfawserr.ErrCodeEquals(err, events.ErrCodeResourceNotFoundException) { + return nil + } + return fmt.Errorf("error deleting CloudWatch Events Archive (%s): %w", d.Id(), err) + } + + return nil +} + +func buildCreateArchiveInputStruct(d *schema.ResourceData) (*events.CreateArchiveInput, error) { + input := events.CreateArchiveInput{ + ArchiveName: aws.String(d.Get("archive_name").(string)), + } + + if v, ok := d.GetOk("event_pattern"); ok { + pattern, err := structure.NormalizeJsonString(v) + if err != nil { + return nil, fmt.Errorf("event pattern contains an invalid JSON: %w", err) + } + input.EventPattern = aws.String(pattern) + } + + if v, ok := d.GetOk("description"); ok { + input.Description = aws.String(v.(string)) + } + + if v, ok := d.GetOk("event_source_arn"); ok { + input.EventSourceArn = aws.String(v.(string)) + } + + if v, ok := d.GetOk("retention_days"); ok { + input.RetentionDays = aws.Int64(v.(int64)) + } + + return &input, nil +} + +func buildUpdateArchiveInputStruct(d *schema.ResourceData) (*events.UpdateArchiveInput, error) { + input := events.UpdateArchiveInput{ + ArchiveName: aws.String(d.Get("archive_name").(string)), + } + + if v, ok := d.GetOk("event_pattern"); ok { + pattern, err := structure.NormalizeJsonString(v) + if err != nil { + return nil, fmt.Errorf("event pattern contains an invalid JSON: %w", err) + } + input.EventPattern = aws.String(pattern) + } + + if v, ok := d.GetOk("description"); ok { + input.Description = aws.String(v.(string)) + } + + if v, ok := d.GetOk("retention_days"); ok { + input.RetentionDays = aws.Int64(v.(int64)) + } + + return &input, nil +} diff --git a/aws/validators.go b/aws/validators.go index 14f80bb226b..5371520a839 100644 --- a/aws/validators.go +++ b/aws/validators.go @@ -2501,6 +2501,11 @@ var validateCloudWatchEventBusName = validation.All( validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9._\-]+$`), ""), ) +var validateCloudWatchEventArchiveName = validation.All( + validation.StringLenBetween(1, 48), + validation.StringMatch(regexp.MustCompile(`^[\.\-_A-Za-z0-9]+`), ""), +) + var validateServiceDiscoveryNamespaceName = validation.All( validation.StringLenBetween(1, 1024), validation.StringMatch(regexp.MustCompile(`^[0-9A-Za-z._-]+$`), ""), From 5a1d71748aa3f6ce314d660a7a5c147ca1f7a3c3 Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Sun, 24 Jan 2021 18:21:53 +0100 Subject: [PATCH 03/22] fix: remove creation time attr --- aws/resource_aws_cloudwatch_event_replay.go | 1 - 1 file changed, 1 deletion(-) diff --git a/aws/resource_aws_cloudwatch_event_replay.go b/aws/resource_aws_cloudwatch_event_replay.go index bca61971f64..d76881162fa 100644 --- a/aws/resource_aws_cloudwatch_event_replay.go +++ b/aws/resource_aws_cloudwatch_event_replay.go @@ -103,7 +103,6 @@ func resourceAwsCloudWatchEventArchiveRead(d *schema.ResourceData, meta interfac d.Set("retention_days", out.RetentionDays) d.Set("event_count", out.EventCount) d.Set("state", out.State) - d.Set("creation_time", out.CreationTime) return nil } From dc7cd6295f82c415ad105fcfcb27227cd016a5fb Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Mon, 25 Jan 2021 16:34:24 +0100 Subject: [PATCH 04/22] fix: rename resource to Archive not Replay --- ...event_replay.go => resource_aws_cloudwatch_event_archive.go} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename aws/{resource_aws_cloudwatch_event_replay.go => resource_aws_cloudwatch_event_archive.go} (98%) diff --git a/aws/resource_aws_cloudwatch_event_replay.go b/aws/resource_aws_cloudwatch_event_archive.go similarity index 98% rename from aws/resource_aws_cloudwatch_event_replay.go rename to aws/resource_aws_cloudwatch_event_archive.go index d76881162fa..4347e3b39f6 100644 --- a/aws/resource_aws_cloudwatch_event_replay.go +++ b/aws/resource_aws_cloudwatch_event_archive.go @@ -11,7 +11,7 @@ import ( "log" ) -func resourceAwsCloudWatchEventReplay() *schema.Resource { +func resourceAwsCloudWatchEventArchive() *schema.Resource { return &schema.Resource{ Create: resourceAwsCloudWatchEventArchiveCreate, Read: resourceAwsCloudWatchEventArchiveRead, From 9fbcf2f92f6e3fe076198c39caa0e8f6d0e4f22d Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Mon, 25 Jan 2021 16:34:59 +0100 Subject: [PATCH 05/22] feat: add cloudwatch_event_archive resource --- aws/provider.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/provider.go b/aws/provider.go index 367c9c4bf61..3bd069175a4 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -491,6 +491,7 @@ func Provider() *schema.Provider { "aws_cloudwatch_event_permission": resourceAwsCloudWatchEventPermission(), "aws_cloudwatch_event_rule": resourceAwsCloudWatchEventRule(), "aws_cloudwatch_event_target": resourceAwsCloudWatchEventTarget(), + "aws_cloudwatch_event_archive": resourceAwsCloudWatchEventArchive(), "aws_cloudwatch_log_destination": resourceAwsCloudWatchLogDestination(), "aws_cloudwatch_log_destination_policy": resourceAwsCloudWatchLogDestinationPolicy(), "aws_cloudwatch_log_group": resourceAwsCloudWatchLogGroup(), From 6426fd5555bc673327ecc5322a4e252df8f64b7a Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Mon, 25 Jan 2021 17:06:38 +0100 Subject: [PATCH 06/22] improv: group required fields; add computed arn --- aws/resource_aws_cloudwatch_event_archive.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_archive.go b/aws/resource_aws_cloudwatch_event_archive.go index 4347e3b39f6..816132f6f4e 100644 --- a/aws/resource_aws_cloudwatch_event_archive.go +++ b/aws/resource_aws_cloudwatch_event_archive.go @@ -25,13 +25,16 @@ func resourceAwsCloudWatchEventArchive() *schema.Resource { ForceNew: true, ValidateFunc: validateCloudWatchEventArchiveName, }, - + "event_source_arn": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validateArn, + }, "description": { Type: schema.TypeString, Optional: true, ValidateFunc: validation.StringLenBetween(0, 512), }, - "event_pattern": { Type: schema.TypeString, Optional: true, @@ -41,17 +44,14 @@ func resourceAwsCloudWatchEventArchive() *schema.Resource { return json }, }, - - "event_source_arn": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validateArn, - }, - "retention_days": { Type: schema.TypeInt, Optional: true, }, + "arn": { + Type: schema.TypeString, + Computed: true, + }, }, } } @@ -101,8 +101,6 @@ func resourceAwsCloudWatchEventArchiveRead(d *schema.ResourceData, meta interfac d.Set("event_source_arn", out.EventSourceArn) d.Set("arn", out.ArchiveArn) d.Set("retention_days", out.RetentionDays) - d.Set("event_count", out.EventCount) - d.Set("state", out.State) return nil } From dfd45160381807d9f2f3d9d8c645fc244527f8b2 Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Mon, 25 Jan 2021 18:30:33 +0100 Subject: [PATCH 07/22] fix: account for value being saved as int, not int64 --- aws/resource_aws_cloudwatch_event_archive.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_cloudwatch_event_archive.go b/aws/resource_aws_cloudwatch_event_archive.go index 816132f6f4e..e52bfe58f99 100644 --- a/aws/resource_aws_cloudwatch_event_archive.go +++ b/aws/resource_aws_cloudwatch_event_archive.go @@ -187,7 +187,8 @@ func buildUpdateArchiveInputStruct(d *schema.ResourceData) (*events.UpdateArchiv } if v, ok := d.GetOk("retention_days"); ok { - input.RetentionDays = aws.Int64(v.(int64)) + retentionInDays := int64(v.(int)) + input.RetentionDays = aws.Int64(retentionInDays) } return &input, nil From 5fc50794af50898a825190433ed225735c6650c8 Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Mon, 25 Jan 2021 18:30:52 +0100 Subject: [PATCH 08/22] improv: add acceptance tests --- ...ource_aws_cloudwatch_event_archive_test.go | 219 ++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 aws/resource_aws_cloudwatch_event_archive_test.go diff --git a/aws/resource_aws_cloudwatch_event_archive_test.go b/aws/resource_aws_cloudwatch_event_archive_test.go new file mode 100644 index 00000000000..bc911bee96a --- /dev/null +++ b/aws/resource_aws_cloudwatch_event_archive_test.go @@ -0,0 +1,219 @@ +package aws + +import ( + "fmt" + "github.com/aws/aws-sdk-go/aws" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "log" + "testing" + + events "github.com/aws/aws-sdk-go/service/cloudwatchevents" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func init() { + resource.AddTestSweepers("aws_cloudwatch_event_archive", &resource.Sweeper{ + Name: "aws_cloudwatch_event_archive", + F: testSweepCloudWatchEventArchives, + Dependencies: []string{ + "aws_cloudwatch_event_bus", + }, + }) +} + +func testSweepCloudWatchEventArchives(region string) error { + client, err := sharedClientForRegion(region) + if err != nil { + return fmt.Errorf("Error getting client: %w", err) + } + conn := client.(*AWSClient).cloudwatcheventsconn + + input := &events.ListArchivesInput{} + + for { + output, err := conn.ListArchives(input) + if err != nil { + if testSweepSkipSweepError(err) { + log.Printf("[WARN] Skipping CloudWatch Events archive sweep for %s: %s", region, err) + return nil + } + return fmt.Errorf("Error retrieving CloudWatch Events archive: %w", err) + } + + if len(output.Archives) == 0 { + log.Print("[DEBUG] No CloudWatch Events archives to sweep") + return nil + } + + for _, archive := range output.Archives { + name := aws.StringValue(archive.ArchiveName) + if name == "default" { + continue + } + + log.Printf("[INFO] Deleting CloudWatch Events archive (%s)", name) + _, err := conn.DeleteArchive(&events.DeleteArchiveInput{ + ArchiveName: aws.String(name), + }) + if err != nil { + return fmt.Errorf("Error deleting CloudWatch Events archive (%s): %w", name, err) + } + } + + if output.NextToken == nil { + break + } + input.NextToken = output.NextToken + } + + return nil +} + +func TestAccAWSCloudWatchArchive_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, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudWatchArchiveConfig(archiveName), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudWatchArchiveExists(resourceName, &v1), + resource.TestCheckResourceAttr(resourceName, "archive_name", archiveName), + resource.TestCheckResourceAttr(resourceName, "retention_days", "0"), + testAccCheckResourceAttrRegionalARN(resourceName, "arn", "events", fmt.Sprintf("archive/%s", archiveName)), + ), + }, + }, + }) +} + +func TestAccAWSCloudWatchArchive_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, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudWatchArchiveConfig(archiveName), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudWatchArchiveExists(resourceName, &v1), + ), + }, + { + Config: testAccAWSCloudWatchArchiveConfig_updateRetention(archiveName), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudWatchArchiveExists(resourceName, &v1), + resource.TestCheckResourceAttr(resourceName, "retention_days", "7"), + ), + }, + }, + }) +} + +func TestAccAWSCloudWatchEventArchive_disappears(t *testing.T) { + var v 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: testAccCheckAWSCloudWatchEventBusDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudWatchEventBusConfig(archiveName), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudWatchArchiveExists(resourceName, &v), + testAccCheckResourceDisappears(testAccProvider, resourceAwsCloudWatchEventArchive(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccCheckAWSCloudWatchArchiveDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_cloudwatch_event_archive" { + continue + } + + params := events.DescribeArchiveInput{ + ArchiveName: aws.String(rs.Primary.ID), + } + + resp, err := conn.DescribeArchive(¶ms) + + if err == nil { + return fmt.Errorf("CloudWatch Events event bus (%s) still exists: %s", rs.Primary.ID, resp) + } + } + + return nil +} + +func testAccCheckCloudWatchArchiveExists(n string, v *events.DescribeArchiveOutput) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn + params := events.DescribeArchiveInput{ + ArchiveName: aws.String(rs.Primary.ID), + } + + resp, err := conn.DescribeArchive(¶ms) + if err != nil { + return err + } + + if resp == nil { + return fmt.Errorf("CloudWatch Events archive (%s) not found", n) + } + + *v = *resp + + return nil + } +} + +func testAccAWSCloudWatchArchiveConfig(name string) string { + return fmt.Sprintf(` +resource "aws_cloudwatch_event_bus" "test" { + name = %[1]q +} + +resource "aws_cloudwatch_event_archive" "test" { + archive_name = %[1]q + event_source_arn = aws_cloudwatch_event_bus.test.arn +} +`, name) +} + +func testAccAWSCloudWatchArchiveConfig_updateRetention(name string) string { + return fmt.Sprintf(` +resource "aws_cloudwatch_event_bus" "test" { + name = %[1]q +} + +resource "aws_cloudwatch_event_archive" "test" { + archive_name = %[1]q + event_source_arn = aws_cloudwatch_event_bus.test.arn + retention_days = 7 +} +`, name) +} From 5648d21421aabf30a014860acdaba07784d75738 Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Mon, 25 Jan 2021 18:49:51 +0100 Subject: [PATCH 09/22] docs: add new event archive section --- .../r/cloudwatch_event_archive.html.markdown | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 website/docs/r/cloudwatch_event_archive.html.markdown diff --git a/website/docs/r/cloudwatch_event_archive.html.markdown b/website/docs/r/cloudwatch_event_archive.html.markdown new file mode 100644 index 00000000000..77bdb425295 --- /dev/null +++ b/website/docs/r/cloudwatch_event_archive.html.markdown @@ -0,0 +1,38 @@ +--- +subcategory: "EventBridge (CloudWatch Events)" +layout: "aws" +page_title: "AWS: aws_cloudwatch_event_archive" +description: |- + Provides an EventBridge event archive resource. +--- + +# Resource: aws_cloudwatch_event_archive + +Provides an EventBridge event archive resource. + +~> **Note:** EventBridge was formerly known as CloudWatch Events. The functionality is identical. + + +## Example Usage + +```hcl +resource "aws_cloudwatch_event_archive" "order" { + archive_name = "order-archive" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `archive_name` - (Required) The name of the new event archive. The archive name cannot exceed 48 chars. +* `event_source_arn` - (Required) Event bus source ARN from where these events should be archived. +* `description` - (Optional) The description of the new event archive. +* `event_pattern` - (Optional) Instructs the new event archive to only capture events matched by this pattern. By default, it attempts to archive every event received in the `event_source_arn`. +* `retention_days` - (Optional) The maximum number of days to retain events in the new event archive. By default, it archives indefinitely. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `arn` - The Amazon Resource Name (ARN) of the event archive. From 55701ca6c1a57f529d02c6f157fe243e3caf99ce Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Mon, 25 Jan 2021 19:11:28 +0100 Subject: [PATCH 10/22] chore: terrafmt linting fix --- aws/resource_aws_cloudwatch_event_archive_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_archive_test.go b/aws/resource_aws_cloudwatch_event_archive_test.go index bc911bee96a..45b83f6e70f 100644 --- a/aws/resource_aws_cloudwatch_event_archive_test.go +++ b/aws/resource_aws_cloudwatch_event_archive_test.go @@ -198,7 +198,7 @@ resource "aws_cloudwatch_event_bus" "test" { } resource "aws_cloudwatch_event_archive" "test" { - archive_name = %[1]q + archive_name = %[1]q event_source_arn = aws_cloudwatch_event_bus.test.arn } `, name) @@ -211,9 +211,9 @@ resource "aws_cloudwatch_event_bus" "test" { } resource "aws_cloudwatch_event_archive" "test" { - archive_name = %[1]q + archive_name = %[1]q event_source_arn = aws_cloudwatch_event_bus.test.arn - retention_days = 7 + retention_days = 7 } `, name) } From cde2067dd10acbedf8a8fd092218274212f60c78 Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Tue, 26 Jan 2021 08:23:04 +0100 Subject: [PATCH 11/22] chore: import order and group linting fix --- aws/resource_aws_cloudwatch_event_archive.go | 3 ++- aws/resource_aws_cloudwatch_event_archive_test.go | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_archive.go b/aws/resource_aws_cloudwatch_event_archive.go index e52bfe58f99..e2b48718056 100644 --- a/aws/resource_aws_cloudwatch_event_archive.go +++ b/aws/resource_aws_cloudwatch_event_archive.go @@ -2,13 +2,14 @@ package aws import ( "fmt" + "log" + "github.com/aws/aws-sdk-go/aws" events "github.com/aws/aws-sdk-go/service/cloudwatchevents" "github.com/hashicorp/aws-sdk-go-base/tfawserr" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" - "log" ) func resourceAwsCloudWatchEventArchive() *schema.Resource { diff --git a/aws/resource_aws_cloudwatch_event_archive_test.go b/aws/resource_aws_cloudwatch_event_archive_test.go index 45b83f6e70f..800c26402ba 100644 --- a/aws/resource_aws_cloudwatch_event_archive_test.go +++ b/aws/resource_aws_cloudwatch_event_archive_test.go @@ -2,14 +2,14 @@ package aws import ( "fmt" - "github.com/aws/aws-sdk-go/aws" - "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "log" "testing" + "github.com/aws/aws-sdk-go/aws" events "github.com/aws/aws-sdk-go/service/cloudwatchevents" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) func init() { From b81753a5c40ffb28bd60001b76097bad74329ed1 Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Tue, 26 Jan 2021 08:58:52 +0100 Subject: [PATCH 12/22] improv: test event pattern update --- aws/resource_aws_cloudwatch_event_archive_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_archive_test.go b/aws/resource_aws_cloudwatch_event_archive_test.go index 800c26402ba..93e28abbbd1 100644 --- a/aws/resource_aws_cloudwatch_event_archive_test.go +++ b/aws/resource_aws_cloudwatch_event_archive_test.go @@ -110,10 +110,11 @@ func TestAccAWSCloudWatchArchive_update(t *testing.T) { ), }, { - Config: testAccAWSCloudWatchArchiveConfig_updateRetention(archiveName), + Config: testAccAWSCloudWatchArchiveConfig_updateRetentionAndPattern(archiveName), Check: resource.ComposeTestCheckFunc( testAccCheckCloudWatchArchiveExists(resourceName, &v1), resource.TestCheckResourceAttr(resourceName, "retention_days", "7"), + testAccCheckResourceAttrEquivalentJSON(resourceName, "event_pattern", "{\"source\":[\"company.team.service\"]}"), ), }, }, @@ -204,7 +205,7 @@ resource "aws_cloudwatch_event_archive" "test" { `, name) } -func testAccAWSCloudWatchArchiveConfig_updateRetention(name string) string { +func testAccAWSCloudWatchArchiveConfig_updateRetentionAndPattern(name string) string { return fmt.Sprintf(` resource "aws_cloudwatch_event_bus" "test" { name = %[1]q @@ -214,6 +215,11 @@ resource "aws_cloudwatch_event_archive" "test" { archive_name = %[1]q event_source_arn = aws_cloudwatch_event_bus.test.arn retention_days = 7 + event_pattern = < Date: Tue, 26 Jan 2021 08:59:35 +0100 Subject: [PATCH 13/22] improv: add another example with all arguments --- .../r/cloudwatch_event_archive.html.markdown | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/website/docs/r/cloudwatch_event_archive.html.markdown b/website/docs/r/cloudwatch_event_archive.html.markdown index 77bdb425295..0271d75384f 100644 --- a/website/docs/r/cloudwatch_event_archive.html.markdown +++ b/website/docs/r/cloudwatch_event_archive.html.markdown @@ -16,8 +16,33 @@ Provides an EventBridge event archive resource. ## Example Usage ```hcl +resource "aws_cloudwatch_event_bus" "order" { + name = "orders" +} + resource "aws_cloudwatch_event_archive" "order" { - archive_name = "order-archive" + archive_name = "order-archive" + event_source_arn = aws_cloudwatch_event_bus.order.arn +} +``` + +## Example all optional arguments + +```hcl +resource "aws_cloudwatch_event_bus" "order" { + name = "orders" +} + +resource "aws_cloudwatch_event_archive" "order" { + archive_name = "order-archive" + description = "Archived events from order service" + event_source_arn = aws_cloudwatch_event_bus.order.arn + retention_days = 7 + event_pattern = < Date: Tue, 26 Jan 2021 09:05:51 +0100 Subject: [PATCH 14/22] chore: lint website docs markdown --- .../docs/r/cloudwatch_event_archive.html.markdown | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/website/docs/r/cloudwatch_event_archive.html.markdown b/website/docs/r/cloudwatch_event_archive.html.markdown index 0271d75384f..1ea080ecde1 100644 --- a/website/docs/r/cloudwatch_event_archive.html.markdown +++ b/website/docs/r/cloudwatch_event_archive.html.markdown @@ -21,8 +21,8 @@ resource "aws_cloudwatch_event_bus" "order" { } resource "aws_cloudwatch_event_archive" "order" { - archive_name = "order-archive" - event_source_arn = aws_cloudwatch_event_bus.order.arn + archive_name = "order-archive" + event_source_arn = aws_cloudwatch_event_bus.order.arn } ``` @@ -34,11 +34,11 @@ resource "aws_cloudwatch_event_bus" "order" { } resource "aws_cloudwatch_event_archive" "order" { - archive_name = "order-archive" - description = "Archived events from order service" - event_source_arn = aws_cloudwatch_event_bus.order.arn - retention_days = 7 - event_pattern = < Date: Tue, 26 Jan 2021 18:24:28 +0100 Subject: [PATCH 15/22] feat: add import support --- aws/resource_aws_cloudwatch_event_archive.go | 6 ++++-- aws/resource_aws_cloudwatch_event_archive_test.go | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_archive.go b/aws/resource_aws_cloudwatch_event_archive.go index e2b48718056..747d629d452 100644 --- a/aws/resource_aws_cloudwatch_event_archive.go +++ b/aws/resource_aws_cloudwatch_event_archive.go @@ -18,6 +18,9 @@ func resourceAwsCloudWatchEventArchive() *schema.Resource { Read: resourceAwsCloudWatchEventArchiveRead, Update: resourceAwsCloudWatchEventArchiveUpdate, Delete: resourceAwsCloudWatchEventArchiveDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "archive_name": { @@ -82,9 +85,8 @@ func resourceAwsCloudWatchEventArchiveCreate(d *schema.ResourceData, meta interf func resourceAwsCloudWatchEventArchiveRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).cloudwatcheventsconn - archiveName := d.Get("archive_name").(string) input := &events.DescribeArchiveInput{ - ArchiveName: aws.String(archiveName), + ArchiveName: aws.String(d.Id()), } out, err := conn.DescribeArchive(input) diff --git a/aws/resource_aws_cloudwatch_event_archive_test.go b/aws/resource_aws_cloudwatch_event_archive_test.go index 93e28abbbd1..fff44dbc5c2 100644 --- a/aws/resource_aws_cloudwatch_event_archive_test.go +++ b/aws/resource_aws_cloudwatch_event_archive_test.go @@ -89,6 +89,11 @@ func TestAccAWSCloudWatchArchive_basic(t *testing.T) { testAccCheckResourceAttrRegionalARN(resourceName, "arn", "events", fmt.Sprintf("archive/%s", archiveName)), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, }, }) } From f7293259aacc6df90e637dd2e34e2b099c0a0834 Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Tue, 26 Jan 2021 18:44:17 +0100 Subject: [PATCH 16/22] docs: add import support --- website/docs/r/cloudwatch_event_archive.html.markdown | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/website/docs/r/cloudwatch_event_archive.html.markdown b/website/docs/r/cloudwatch_event_archive.html.markdown index 1ea080ecde1..a68aa879b41 100644 --- a/website/docs/r/cloudwatch_event_archive.html.markdown +++ b/website/docs/r/cloudwatch_event_archive.html.markdown @@ -61,3 +61,11 @@ The following arguments are supported: In addition to all arguments above, the following attributes are exported: * `arn` - The Amazon Resource Name (ARN) of the event archive. + +## Import + +Event Archive can be imported using their name, for example + +```bash +terraform import aws_cloudwatch_event_archive.imported_event_archive order_archive +``` From 74232eefffaab26a558fda8942f162e6808f87e1 Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Tue, 26 Jan 2021 18:49:24 +0100 Subject: [PATCH 17/22] improv: use name instead of archive_name argument --- aws/resource_aws_cloudwatch_event_archive.go | 14 ++++++++------ aws/resource_aws_cloudwatch_event_archive_test.go | 6 +++--- .../docs/r/cloudwatch_event_archive.html.markdown | 8 ++++---- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_archive.go b/aws/resource_aws_cloudwatch_event_archive.go index 747d629d452..44258184740 100644 --- a/aws/resource_aws_cloudwatch_event_archive.go +++ b/aws/resource_aws_cloudwatch_event_archive.go @@ -23,7 +23,7 @@ func resourceAwsCloudWatchEventArchive() *schema.Resource { }, Schema: map[string]*schema.Schema{ - "archive_name": { + "name": { Type: schema.TypeString, Required: true, ForceNew: true, @@ -76,7 +76,7 @@ func resourceAwsCloudWatchEventArchiveCreate(d *schema.ResourceData, meta interf return fmt.Errorf("Creating CloudWatch Events Archive failed: %w", err) } - d.SetId(d.Get("archive_name").(string)) + d.SetId(d.Get("name").(string)) log.Printf("[INFO] CloudWatch Events Archive (%s) created", d.Id()) @@ -98,7 +98,7 @@ func resourceAwsCloudWatchEventArchiveRead(d *schema.ResourceData, meta interfac log.Printf("[DEBUG] Found Archive: #{*out}") // Review Question - Is there a problem in setting more than should name and Arn? - d.Set("archive_name", out.ArchiveName) + d.Set("name", out.ArchiveName) d.Set("description", out.Description) d.Set("event_pattern", out.EventPattern) d.Set("event_source_arn", out.EventSourceArn) @@ -130,7 +130,7 @@ func resourceAwsCloudWatchEventArchiveDelete(d *schema.ResourceData, meta interf conn := meta.(*AWSClient).cloudwatcheventsconn input := &events.DeleteArchiveInput{ - ArchiveName: aws.String(d.Get("archive_name").(string)), + ArchiveName: aws.String(d.Get("name").(string)), } _, err := conn.DeleteArchive(input) @@ -146,7 +146,7 @@ func resourceAwsCloudWatchEventArchiveDelete(d *schema.ResourceData, meta interf func buildCreateArchiveInputStruct(d *schema.ResourceData) (*events.CreateArchiveInput, error) { input := events.CreateArchiveInput{ - ArchiveName: aws.String(d.Get("archive_name").(string)), + ArchiveName: aws.String(d.Get("name").(string)), } if v, ok := d.GetOk("event_pattern"); ok { @@ -174,7 +174,7 @@ func buildCreateArchiveInputStruct(d *schema.ResourceData) (*events.CreateArchiv func buildUpdateArchiveInputStruct(d *schema.ResourceData) (*events.UpdateArchiveInput, error) { input := events.UpdateArchiveInput{ - ArchiveName: aws.String(d.Get("archive_name").(string)), + ArchiveName: aws.String(d.Get("name").(string)), } if v, ok := d.GetOk("event_pattern"); ok { @@ -196,3 +196,5 @@ func buildUpdateArchiveInputStruct(d *schema.ResourceData) (*events.UpdateArchiv return &input, nil } + +// create a datasource diff --git a/aws/resource_aws_cloudwatch_event_archive_test.go b/aws/resource_aws_cloudwatch_event_archive_test.go index fff44dbc5c2..467a0171499 100644 --- a/aws/resource_aws_cloudwatch_event_archive_test.go +++ b/aws/resource_aws_cloudwatch_event_archive_test.go @@ -84,7 +84,7 @@ func TestAccAWSCloudWatchArchive_basic(t *testing.T) { Config: testAccAWSCloudWatchArchiveConfig(archiveName), Check: resource.ComposeTestCheckFunc( testAccCheckCloudWatchArchiveExists(resourceName, &v1), - resource.TestCheckResourceAttr(resourceName, "archive_name", archiveName), + resource.TestCheckResourceAttr(resourceName, "name", archiveName), resource.TestCheckResourceAttr(resourceName, "retention_days", "0"), testAccCheckResourceAttrRegionalARN(resourceName, "arn", "events", fmt.Sprintf("archive/%s", archiveName)), ), @@ -204,7 +204,7 @@ resource "aws_cloudwatch_event_bus" "test" { } resource "aws_cloudwatch_event_archive" "test" { - archive_name = %[1]q + name = %[1]q event_source_arn = aws_cloudwatch_event_bus.test.arn } `, name) @@ -217,7 +217,7 @@ resource "aws_cloudwatch_event_bus" "test" { } resource "aws_cloudwatch_event_archive" "test" { - archive_name = %[1]q + name = %[1]q event_source_arn = aws_cloudwatch_event_bus.test.arn retention_days = 7 event_pattern = < Date: Thu, 18 Feb 2021 08:52:53 +0100 Subject: [PATCH 18/22] fix: address Bill's review. --- aws/resource_aws_cloudwatch_event_archive.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_cloudwatch_event_archive.go b/aws/resource_aws_cloudwatch_event_archive.go index 44258184740..63c6cdc180c 100644 --- a/aws/resource_aws_cloudwatch_event_archive.go +++ b/aws/resource_aws_cloudwatch_event_archive.go @@ -32,6 +32,7 @@ func resourceAwsCloudWatchEventArchive() *schema.Resource { "event_source_arn": { Type: schema.TypeString, Required: true, + ForceNew: true, ValidateFunc: validateArn, }, "description": { @@ -97,7 +98,6 @@ func resourceAwsCloudWatchEventArchiveRead(d *schema.ResourceData, meta interfac log.Printf("[DEBUG] Found Archive: #{*out}") - // Review Question - Is there a problem in setting more than should name and Arn? d.Set("name", out.ArchiveName) d.Set("description", out.Description) d.Set("event_pattern", out.EventPattern) From 2d569eab6f59120b31d837607aa4e8b5e900793b Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Thu, 18 Feb 2021 09:18:58 +0100 Subject: [PATCH 19/22] fix: address Bill's review on tests. --- aws/resource_aws_cloudwatch_event_archive_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_archive_test.go b/aws/resource_aws_cloudwatch_event_archive_test.go index 467a0171499..7d1d7b048e3 100644 --- a/aws/resource_aws_cloudwatch_event_archive_test.go +++ b/aws/resource_aws_cloudwatch_event_archive_test.go @@ -87,6 +87,8 @@ func TestAccAWSCloudWatchArchive_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "name", archiveName), resource.TestCheckResourceAttr(resourceName, "retention_days", "0"), testAccCheckResourceAttrRegionalARN(resourceName, "arn", "events", fmt.Sprintf("archive/%s", archiveName)), + resource.TestCheckResourceAttr(resourceName, "description", ""), + resource.TestCheckResourceAttr(resourceName, "event_pattern", ""), ), }, { @@ -115,11 +117,12 @@ func TestAccAWSCloudWatchArchive_update(t *testing.T) { ), }, { - Config: testAccAWSCloudWatchArchiveConfig_updateRetentionAndPattern(archiveName), + Config: testAccAWSCloudWatchArchiveConfig_updateAttributes(archiveName), Check: resource.ComposeTestCheckFunc( testAccCheckCloudWatchArchiveExists(resourceName, &v1), resource.TestCheckResourceAttr(resourceName, "retention_days", "7"), testAccCheckResourceAttrEquivalentJSON(resourceName, "event_pattern", "{\"source\":[\"company.team.service\"]}"), + resource.TestCheckResourceAttr(resourceName, "description", "test"), ), }, }, @@ -210,7 +213,7 @@ resource "aws_cloudwatch_event_archive" "test" { `, name) } -func testAccAWSCloudWatchArchiveConfig_updateRetentionAndPattern(name string) string { +func testAccAWSCloudWatchArchiveConfig_updateAttributes(name string) string { return fmt.Sprintf(` resource "aws_cloudwatch_event_bus" "test" { name = %[1]q @@ -220,6 +223,7 @@ resource "aws_cloudwatch_event_archive" "test" { name = %[1]q event_source_arn = aws_cloudwatch_event_bus.test.arn retention_days = 7 + description = "test" event_pattern = < Date: Thu, 18 Feb 2021 09:19:36 +0100 Subject: [PATCH 20/22] fix: address Bill's review on docs. --- website/docs/r/cloudwatch_event_archive.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/cloudwatch_event_archive.html.markdown b/website/docs/r/cloudwatch_event_archive.html.markdown index 63319f1de96..f883809d322 100644 --- a/website/docs/r/cloudwatch_event_archive.html.markdown +++ b/website/docs/r/cloudwatch_event_archive.html.markdown @@ -50,7 +50,7 @@ PATTERN The following arguments are supported: -* `name` - (Required) The name of the new event archive. The archive name cannot exceed 48 chars. +* `name` - (Required) The name of the new event archive. The archive name cannot exceed 48 characters. * `event_source_arn` - (Required) Event bus source ARN from where these events should be archived. * `description` - (Optional) The description of the new event archive. * `event_pattern` - (Optional) Instructs the new event archive to only capture events matched by this pattern. By default, it attempts to archive every event received in the `event_source_arn`. From a826846e68c0cbc0cef49d367dc86500a7f85efd Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Thu, 18 Feb 2021 09:24:50 +0100 Subject: [PATCH 21/22] chore: add changelog entry. --- .changelog/17270.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/17270.txt diff --git a/.changelog/17270.txt b/.changelog/17270.txt new file mode 100644 index 00000000000..295207a0934 --- /dev/null +++ b/.changelog/17270.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +aws_cloudwatch_event_archive +``` From d341f8f868370d85f49903c2c5b0140e55c7e3cc Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Thu, 18 Feb 2021 10:00:33 +0100 Subject: [PATCH 22/22] chore: lint security hub test --- aws/resource_aws_securityhub_invite_accepter_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_securityhub_invite_accepter_test.go b/aws/resource_aws_securityhub_invite_accepter_test.go index e6e124d155f..a0015867bc8 100644 --- a/aws/resource_aws_securityhub_invite_accepter_test.go +++ b/aws/resource_aws_securityhub_invite_accepter_test.go @@ -96,7 +96,7 @@ func testAccAWSSecurityHubInviteAccepterConfig_basic() string { return composeConfig( testAccAlternateAccountProviderConfig(), ` resource "aws_securityhub_invite_accepter" "test" { - master_id = aws_securityhub_member.source.master_id + master_id = aws_securityhub_member.source.master_id depends_on = [aws_securityhub_account.test] }