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 required suffix when specifying log group ARN #35941

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 6 additions & 1 deletion internal/service/fis/experiment_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"log"
"strings"
"time"

"github.com/YakDriver/regexache"
Expand Down Expand Up @@ -655,9 +656,13 @@ func expandExperimentTemplateCloudWatchLogsConfiguration(l []any) *awstypes.Expe
}

raw := l[0].(map[string]any)
logGroupArn := raw["log_group_arn"].(string)
if !strings.HasSuffix(logGroupArn, ":*") {
logGroupArn += ":*"
}

config := awstypes.ExperimentTemplateCloudWatchLogsLogConfigurationInput{
LogGroupArn: aws.String(raw["log_group_arn"].(string)),
LogGroupArn: aws.String(logGroupArn),
}
return &config
}
Expand Down
86 changes: 83 additions & 3 deletions internal/service/fis/experiment_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,15 +379,25 @@ func TestAccFISExperimentTemplate_loggingConfiguration(t *testing.T) {
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckExperimentTemplateDestroy(ctx),
Steps: []resource.TestStep{
// Cloudwatch Logging
// Cloudwatch Logging with ":*" suffix
{
Config: testAccExperimentTemplateConfig_logConfigCloudWatch(rName, "An experiment template for testing", "test-action-1", "", "aws:ec2:terminate-instances", "Instances", "to-terminate-1", "aws:ec2:instance", "COUNT(1)", "env", "test"),
Config: testAccExperimentTemplateConfig_logConfigCloudWatchWithSuffix(rName, "An experiment template for testing", "test-action-1", "", "aws:ec2:terminate-instances", "Instances", "to-terminate-1", "aws:ec2:instance", "COUNT(1)", "env", "test"),
Check: resource.ComposeTestCheckFunc(
testAccExperimentTemplateExists(ctx, resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "log_configuration.0.log_schema_version", "2"),
acctest.CheckResourceAttrRegionalARN(ctx, resourceName, "log_configuration.0.cloudwatch_logs_configuration.0.log_group_arn", "logs", fmt.Sprintf("log-group:%s:*", rName)),
),
},
// Cloudwatch Logging without ":*" suffix
{
Config: testAccExperimentTemplateConfig_logConfigCloudWatchWithoutSuffix(rName, "An experiment template for testing", "test-action-1", "", "aws:ec2:terminate-instances", "Instances", "to-terminate-1", "aws:ec2:instance", "COUNT(1)", "env", "test"),
Check: resource.ComposeTestCheckFunc(
testAccExperimentTemplateExists(ctx, resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "log_configuration.0.log_schema_version", "2"),
acctest.CheckResourceAttrRegionalARN(ctx, resourceName, "log_configuration.0.cloudwatch_logs_configuration.0.log_group_arn", "logs", fmt.Sprintf("log-group:%s:*", rName)),
),
ExpectNonEmptyPlan: true, // Expect 1 change: arn:aws:logs:us-west-2:012345678910:log-group:my-log-group:* -> arn:aws:logs:us-west-2:012345678910:log-group:my-log-group
},
// Delete Logging
{
Config: testAccExperimentTemplateConfig_basic(rName, "An experiment template for testing", "test-action-1", "", "aws:ec2:terminate-instances", "Instances", "to-terminate-1", "aws:ec2:instance", "COUNT(1)", "env", "test"),
Expand Down Expand Up @@ -896,7 +906,7 @@ resource "aws_fis_experiment_template" "test" {
`, rName+"-fis", desc, actionName, actionDesc, actionID, actionTargetK, actionTargetV, paramK1, paramV1, targetResType, targetSelectMode, targetResTagK, targetResTagV))
}

func testAccExperimentTemplateConfig_logConfigCloudWatch(rName, desc, actionName, actionDesc, actionID, actionTargetK, actionTargetV, targetResType, targetSelectMode, targetResTagK, targetResTagV string) string {
func testAccExperimentTemplateConfig_logConfigCloudWatchWithSuffix(rName, desc, actionName, actionDesc, actionID, actionTargetK, actionTargetV, targetResType, targetSelectMode, targetResTagK, targetResTagV string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}

Expand Down Expand Up @@ -966,6 +976,76 @@ resource "aws_fis_experiment_template" "test" {
`, rName, desc, actionName, actionDesc, actionID, actionTargetK, actionTargetV, targetResType, targetSelectMode, targetResTagK, targetResTagV)
}

func testAccExperimentTemplateConfig_logConfigCloudWatchWithoutSuffix(rName, desc, actionName, actionDesc, actionID, actionTargetK, actionTargetV, targetResType, targetSelectMode, targetResTagK, targetResTagV string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}

resource "aws_iam_role" "test" {
name = %[1]q

assume_role_policy = jsonencode({
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = [
"fis.${data.aws_partition.current.dns_suffix}",
]
}
}]
Version = "2012-10-17"
})
}

resource "aws_cloudwatch_log_group" "test" {
name = %[1]q
}

resource "aws_fis_experiment_template" "test" {
description = %[2]q
role_arn = aws_iam_role.test.arn

stop_condition {
source = "none"
}

action {
name = %[3]q
description = %[4]q
action_id = %[5]q

target {
key = %[6]q
value = %[7]q
}
}

target {
name = %[7]q
resource_type = %[8]q
selection_mode = %[9]q

resource_tag {
key = %[10]q
value = %[11]q
}
}

log_configuration {
log_schema_version = 2

cloudwatch_logs_configuration {
log_group_arn = aws_cloudwatch_log_group.test.arn
}
}

tags = {
Name = %[1]q
}
}
`, rName, desc, actionName, actionDesc, actionID, actionTargetK, actionTargetV, targetResType, targetSelectMode, targetResTagK, targetResTagV)
}

func testAccExperimentTemplateConfig_logConfigS3(rName, desc, actionName, actionDesc, actionID, actionTargetK, actionTargetV, targetResType, targetSelectMode, targetResTagK, targetResTagV string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/fis_experiment_template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ resource "aws_fis_experiment_template" "example" {
value = "example"
}
}

log_configuration {
log_schema_version = 2

cloudwatch_logs_configuration {
log_group_arn = aws_cloudwatch_log_group.example.arn
}
}
}
```

Expand Down
Loading