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

feat: Add CloudTrail Event Data Store suspend ingestion #40607

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
50 changes: 48 additions & 2 deletions internal/service/cloudtrail/event_data_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ func resourceEventDataStore() *schema.Resource {
Default: types.BillingModeExtendableRetentionPricing,
ValidateDiagFunc: enum.Validate[types.BillingMode](),
},
"suspend": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
names.AttrKMSKeyID: {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -294,6 +299,19 @@ func resourceEventDataStoreUpdate(ctx context.Context, d *schema.ResourceData, m
input.TerminationProtectionEnabled = aws.Bool(d.Get("termination_protection_enabled").(bool))
}

if d.HasChange("suspend") {
if d.Get("suspend").(bool) {
if _, err := stopEventDataStoreIngestion(ctx, conn, d.Id()); err != nil {
return sdkdiag.AppendErrorf(diags, "error stopping CloudTrail Event Data Store ingestion (%s): %s", d.Id(), err)
}

} else {
if _, err := startEventDataStoreIngestion(ctx, conn, d.Id()); err != nil {
return sdkdiag.AppendErrorf(diags, "error starting CloudTrail Event Data Store ingestion (%s): %s", d.Id(), err)
}
}
}

_, err := conn.UpdateEventDataStore(ctx, input)

if err != nil {
Expand Down Expand Up @@ -380,10 +398,38 @@ func statusEventDataStore(ctx context.Context, conn *cloudtrail.Client, arn stri
}
}

func stopEventDataStoreIngestion(ctx context.Context, conn *cloudtrail.Client, arn string) (*cloudtrail.StopEventDataStoreIngestionOutput, error) {
input := &cloudtrail.StopEventDataStoreIngestionInput{
EventDataStore: aws.String(arn),
}

output, err := conn.StopEventDataStoreIngestion(ctx, input)

if err != nil {
return nil, err
}

return output, nil
}

func startEventDataStoreIngestion(ctx context.Context, conn *cloudtrail.Client, arn string) (*cloudtrail.StartEventDataStoreIngestionOutput, error) {
input := &cloudtrail.StartEventDataStoreIngestionInput{
EventDataStore: aws.String(arn),
}

output, err := conn.StartEventDataStoreIngestion(ctx, input)

if err != nil {
return nil, err
}

return output, nil
}

func waitEventDataStoreAvailable(ctx context.Context, conn *cloudtrail.Client, arn string, timeout time.Duration) (*cloudtrail.GetEventDataStoreOutput, error) { //nolint:unparam
stateConf := &retry.StateChangeConf{
Pending: enum.Slice(types.EventDataStoreStatusCreated),
Target: enum.Slice(types.EventDataStoreStatusEnabled),
Pending: enum.Slice(types.EventDataStoreStatusCreated, types.EventDataStoreStatusStartingIngestion, types.EventDataStoreStatusStoppingIngestion),
Target: enum.Slice(types.EventDataStoreStatusEnabled, types.EventDataStoreStatusStoppedIngestion),
Refresh: statusEventDataStore(ctx, conn, arn),
Timeout: timeout,
}
Expand Down
42 changes: 42 additions & 0 deletions internal/service/cloudtrail/event_data_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,35 @@ func TestAccCloudTrailEventDataStore_billingMode(t *testing.T) {
})
}

func TestAccCloudTrailEventDataStore_suspend(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_cloudtrail_event_data_store.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.CloudTrailServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckEventDataStoreDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccEventDataStoreConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckEventDataStoreExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, "suspend", "false"),
),
},
{
Config: testAccEventDataStoreConfig_suspend(rName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckEventDataStoreExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, "suspend", "true"),
),
},
},
})
}

func TestAccCloudTrailEventDataStore_kmsKeyId(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down Expand Up @@ -394,6 +423,19 @@ resource "aws_cloudtrail_event_data_store" "test" {
`, rName)
}

func testAccEventDataStoreConfig_suspend(rName string, suspend bool) string {
return fmt.Sprintf(`
resource "aws_cloudtrail_event_data_store" "test" {
name = %[1]q

suspend = %[2]t
multi_region_enabled = false
organization_enabled = false
termination_protection_enabled = false # For ease of deletion.
}
`, rName, suspend)
}

func testAccEventDataStoreConfig_billingModeUpdated(rName string) string {
return fmt.Sprintf(`
resource "aws_cloudtrail_event_data_store" "test" {
Expand Down
Loading