Skip to content

Commit

Permalink
d/aws_cloudwatch_log_groups: Remove trailing ':*' from ARNs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Sep 21, 2021
1 parent 7f28665 commit 0278225
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 48 deletions.
63 changes: 35 additions & 28 deletions aws/data_source_aws_cloudwatch_log_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
tfcloudwatchlogs "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/cloudwatchlogs"
)

func dataSourceAwsCloudwatchLogGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsCloudwatchLogGroupsRead,

Schema: map[string]*schema.Schema{
"arns": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"log_group_name_prefix": {
Type: schema.TypeString,
Required: true,
},
"arns": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"log_group_names": {
Type: schema.TypeList,
Optional: true,
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
Expand All @@ -33,32 +35,37 @@ func dataSourceAwsCloudwatchLogGroups() *schema.Resource {
func dataSourceAwsCloudwatchLogGroupsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn

logGroupNamePrefix := d.Get("log_group_name_prefix").(string)
input := &cloudwatchlogs.DescribeLogGroupsInput{LogGroupNamePrefix: &logGroupNamePrefix}
var logGroupNames = []string{}
var arns = []string{}

err := conn.DescribeLogGroupsPages(input,
func(page *cloudwatchlogs.DescribeLogGroupsOutput, lastPage bool) bool {
for _, group := range page.LogGroups {
logGroupNames = append(logGroupNames, aws.StringValue(group.LogGroupName))
arns = append(arns, aws.StringValue(group.Arn))
}
return !lastPage
})
if err != nil {
return err
input := &cloudwatchlogs.DescribeLogGroupsInput{
LogGroupNamePrefix: aws.String(d.Get("log_group_name_prefix").(string)),
}

err = d.Set("log_group_names", logGroupNames)
if err != nil {
return fmt.Errorf("Error setting Log Group Names: %s", err)
}
var results []*cloudwatchlogs.LogGroup

err := conn.DescribeLogGroupsPages(input, func(page *cloudwatchlogs.DescribeLogGroupsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

results = append(results, page.LogGroups...)

return !lastPage
})

err = d.Set("arns", arns)
if err != nil {
return fmt.Errorf("Error setting Log Group Arns: %s", err)
return fmt.Errorf("error reading CloudWatch Log Groups: %w", err)
}

d.SetId(meta.(*AWSClient).region)

var arns, logGroupNames []string

for _, r := range results {
arns = append(arns, tfcloudwatchlogs.TrimLogGroupARNWildcardSuffix(aws.StringValue(r.Arn)))
logGroupNames = append(logGroupNames, aws.StringValue(r.LogGroupName))
}

d.Set("arns", arns)
d.Set("log_group_names", logGroupNames)

return nil
}
33 changes: 18 additions & 15 deletions aws/data_source_aws_cloudwatch_log_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,49 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccAWSCloudwatchLogGroupsDataSource_basic(t *testing.T) {
rName1 := acctest.RandomWithPrefix("/abc/tf-acc-test1")
rName2 := acctest.RandomWithPrefix("/abc/tf-acc-test2")

resourceName := "data.aws_cloudwatch_log_groups.blah"
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "data.aws_cloudwatch_log_groups.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, cloudwatchlogs.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAWSCloudwatchLogGroupsDataSourceConfig(rName1, rName2),
Config: testAccCheckAWSCloudwatchLogGroupsDataSourceConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "arns.#", "2"),
resource.TestCheckTypeSetElemAttrPair(resourceName, "arns.*", "aws_cloudwatch_log_group.test1", "arn"),
resource.TestCheckTypeSetElemAttrPair(resourceName, "arns.*", "aws_cloudwatch_log_group.test2", "arn"),
resource.TestCheckResourceAttr(resourceName, "log_group_names.#", "2"),
resource.TestCheckResourceAttrSet(resourceName, "arns.0"),
resource.TestCheckResourceAttr(resourceName, "log_group_names.0", rName1),
resource.TestCheckTypeSetElemAttrPair(resourceName, "log_group_names.*", "aws_cloudwatch_log_group.test1", "name"),
resource.TestCheckTypeSetElemAttrPair(resourceName, "log_group_names.*", "aws_cloudwatch_log_group.test2", "name"),
),
},
},
})
}

func testAccCheckAWSCloudwatchLogGroupsDataSourceConfig(rName1 string, rName2 string) string {
func testAccCheckAWSCloudwatchLogGroupsDataSourceConfig(rName string) string {
return fmt.Sprintf(`
resource aws_cloudwatch_log_group "test1" {
name = "%s"
name = "%[1]s/1"
}
resource aws_cloudwatch_log_group "test2" {
name = "%s"
name = "%[1]s/2"
}
data aws_cloudwatch_log_groups "blah" {
log_group_name_prefix = "/abc"
data aws_cloudwatch_log_groups "test" {
log_group_name_prefix = %[1]q
depends_on = [aws_cloudwatch_log_group.test1,aws_cloudwatch_log_group.test2]
}
`, rName1, rName2)
`, rName)
}
14 changes: 14 additions & 0 deletions aws/internal/service/cloudwatchlogs/arn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cloudwatchlogs

import (
"strings"
)

const (
logGroupARNWildcardSuffix = ":*"
)

// TrimLogGroupARNWildcardSuffix trims any wilcard suffix from a Log Group ARN.
func TrimLogGroupARNWildcardSuffix(arn string) string {
return strings.TrimSuffix(arn, logGroupARNWildcardSuffix)
}
39 changes: 39 additions & 0 deletions aws/internal/service/cloudwatchlogs/arn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cloudwatchlogs_test

import (
"testing"

tfcloudwatchlogs "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/cloudwatchlogs"
)

func TestTrimLogGroupARNWildcardSuffix(t *testing.T) {
testCases := []struct {
TestName string
InputARN string
ExpectedARN string
}{
{
TestName: "Empty string",
},
{
TestName: "No suffix",
InputARN: "arn:aws-us-gov:logs:us-gov-west-1:123456789012:log-group:tf-acc-test-6899758375212691725/1",
ExpectedARN: "arn:aws-us-gov:logs:us-gov-west-1:123456789012:log-group:tf-acc-test-6899758375212691725/1",
},
{
TestName: "With suffix",
InputARN: "arn:aws-us-gov:logs:us-gov-west-1:123456789012:log-group:tf-acc-test-6899758375212691725/1:*",
ExpectedARN: "arn:aws-us-gov:logs:us-gov-west-1:123456789012:log-group:tf-acc-test-6899758375212691725/1",
},
}

for _, testCase := range testCases {
t.Run(testCase.TestName, func(t *testing.T) {
got := tfcloudwatchlogs.TrimLogGroupARNWildcardSuffix(testCase.InputARN)

if got != testCase.ExpectedARN {
t.Errorf("got %s, expected %s", got, testCase.ExpectedARN)
}
})
}
}
4 changes: 2 additions & 2 deletions aws/resource_aws_cloudwatch_log_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package aws
import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
tfcloudwatchlogs "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/cloudwatchlogs"
)

func resourceAwsCloudWatchLogGroup() *schema.Resource {
Expand Down Expand Up @@ -138,7 +138,7 @@ func resourceAwsCloudWatchLogGroupRead(d *schema.ResourceData, meta interface{})
return nil
}

d.Set("arn", strings.TrimSuffix(aws.StringValue(lg.Arn), ":*"))
d.Set("arn", tfcloudwatchlogs.TrimLogGroupARNWildcardSuffix(aws.StringValue(lg.Arn)))
d.Set("name", lg.LogGroupName)
d.Set("kms_key_id", lg.KmsKeyId)
d.Set("retention_in_days", lg.RetentionInDays)
Expand Down
6 changes: 3 additions & 3 deletions website/docs/d/cloudwatch_log_groups.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Use this data source to get a list of AWS Cloudwatch Log Groups

## Example Usage

```hcl
```terraform
data "aws_cloudwatch_log_groups" "example" {
log_group_prefix = "/MyImportantLogs"
}
Expand All @@ -28,5 +28,5 @@ The following arguments are supported:

In addition to all arguments above, the following attributes are exported:

* `arns` - The ARNs of the Cloudwatch log groups
* `log_group_names` - The names of the Cloudwatch log groups
* `arns` - Set of ARNs of the Cloudwatch log groups
* `log_group_names` - Set of names of the Cloudwatch log groups

0 comments on commit 0278225

Please sign in to comment.