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

Add support Cloudwatch Log Groups Data Source #7947

Closed
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
121 changes: 121 additions & 0 deletions aws/data_source_aws_cloudwatch_log_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceAwsCloudWatchLogGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsCloudWatchLogGroupsRead,

Schema: map[string]*schema.Schema{
"prefix": {
Type: schema.TypeString,
Required: true,
},
"names": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"arns": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"creation_times": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeInt},
Set: schema.HashInt,
},
"retention_in_days": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeInt},
},
"metric_filter_counts": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeInt},
},
"kms_key_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func getAllCloudWatchLogGroups(conn *cloudwatchlogs.CloudWatchLogs, input *cloudwatchlogs.DescribeLogGroupsInput) ([]*cloudwatchlogs.LogGroup, error) {
var logGroups []*cloudwatchlogs.LogGroup
var nextToken string

for {
if nextToken != "" {
input.NextToken = aws.String(nextToken)
}
resp, err := conn.DescribeLogGroups(input)
if err != nil {
return nil, fmt.Errorf("Error describing Cloud Watch Log Groups: %s", err)
}
logGroups = append(logGroups, resp.LogGroups...)
if resp.NextToken == nil {
break
}
nextToken = aws.StringValue(resp.NextToken)
}
return logGroups, nil
}

func dataSourceAwsCloudWatchLogGroupsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatchlogsconn
d.SetId(resource.UniqueId())

logGroups, err := getAllCloudWatchLogGroups(conn, &cloudwatchlogs.DescribeLogGroupsInput{
LogGroupNamePrefix: aws.String(d.Get("prefix").(string)),
})
if err != nil {
return err
}

var names, arns, creationTimes, retentionInDays, metricFilterCount, kmsKeyIDs []interface{}
for _, v := range logGroups {
names = append(names, aws.StringValue(v.LogGroupName))
arns = append(arns, aws.StringValue(v.Arn))
creationTimes = append(creationTimes, aws.Int64Value(v.CreationTime))
retentionInDays = append(retentionInDays, int(aws.Int64Value(v.RetentionInDays)))
metricFilterCount = append(metricFilterCount, int(aws.Int64Value(v.MetricFilterCount)))
kmsKeyIDs = append(kmsKeyIDs, aws.StringValue(v.KmsKeyId))
}

if err := d.Set("names", names); err != nil {
return fmt.Errorf("Error setting Cloud Watch Log Group names: %s", err)
}
if err := d.Set("arns", arns); err != nil {
return fmt.Errorf("Error setting Cloud Watch Log Group arns: %s", err)
}
if err := d.Set("creation_times", creationTimes); err != nil {
return fmt.Errorf("Error setting Cloud Watch Log Group creation_times: %s", err)
}
if err := d.Set("retention_in_days", retentionInDays); err != nil {
return fmt.Errorf("Error setting Cloud Watch Log Group retention_in_days: %s", err)
}
if err := d.Set("metric_filter_counts", metricFilterCount); err != nil {
return fmt.Errorf("Error setting Cloud Watch Log Group metric_filter_counts: %s", err)
}
if err := d.Set("kms_key_ids", kmsKeyIDs); err != nil {
return fmt.Errorf("Error setting Cloud Watch Log Group kms_key_ids: %s", err)
}

return nil
}
90 changes: 90 additions & 0 deletions aws/data_source_aws_cloudwatch_log_groups_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccAWSCloudwatchLogGroupsDataSource(t *testing.T) {
rInt := acctest.RandInt()

dataSourceName := "data.aws_cloudwatch_log_groups.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAWSCloudwatchLogGroupsDataSourceConfigCreateResource(rInt),
},
{
Config: testAccCheckAWSCloudwatchLogGroupsDataSourceConfig(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "names.#", "4"),
resource.TestCheckResourceAttr(dataSourceName, "arns.#", "4"),
resource.TestCheckResourceAttr(dataSourceName, "creation_times.#", "4"),
resource.TestCheckResourceAttr(dataSourceName, "retention_in_days.#", "4"),
resource.TestCheckResourceAttr(dataSourceName, "metric_filter_counts.#", "4"),
resource.TestCheckResourceAttr(dataSourceName, "kms_key_ids.#", "4"),
),
},
},
})
}

func testAccCheckAWSCloudwatchLogGroupsDataSourceConfigCreateResource(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_log_group" "default" {
name = "tf-acc-test-%[1]d-default"
}

resource "aws_cloudwatch_log_group" "retention_in_days" {
name = "tf-acc-test-%[1]d-retention-in-days"
retention_in_days = 365
}

resource "aws_cloudwatch_log_group" "name_prefix" {
name_prefix = "tf-acc-test-%[1]d-"
}

resource "aws_kms_key" "foo" {
description = "Terraform acc test %[1]d"
deletion_window_in_days = 7
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "kms-tf-%[1]d",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "kms:*",
"Resource": "*"
}
]
}
POLICY
}

resource "aws_cloudwatch_log_group" "kms_key_id" {
name = "tf-acc-test-%[1]d-kms-key-id"
kms_key_id = "${aws_kms_key.foo.arn}"
}
`, rInt)
}

func testAccCheckAWSCloudwatchLogGroupsDataSourceConfig(rInt int) string {
return fmt.Sprintf(`
%s

data "aws_cloudwatch_log_groups" "test" {
prefix = "tf-acc-test-%[2]d-"
}
`, testAccCheckAWSCloudwatchLogGroupsDataSourceConfigCreateResource(rInt), rInt)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func Provider() terraform.ResourceProvider {
"aws_cloudhsm_v2_cluster": dataSourceCloudHsm2Cluster(),
"aws_cloudtrail_service_account": dataSourceAwsCloudTrailServiceAccount(),
"aws_cloudwatch_log_group": dataSourceAwsCloudwatchLogGroup(),
"aws_cloudwatch_log_groups": dataSourceAwsCloudWatchLogGroups(),
"aws_cognito_user_pools": dataSourceAwsCognitoUserPools(),
"aws_codecommit_repository": dataSourceAwsCodeCommitRepository(),
"aws_cur_report_definition": dataSourceAwsCurReportDefinition(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@
<li>
<a href="/docs/providers/aws/d/cloudwatch_log_group.html">aws_cloudwatch_log_group</a>
</li>
<li>
<a href="/docs/providers/aws/d/cloudwatch_log_groups.html">aws_cloudwatch_log_groups</a>
</li>
</ul>
</li>
<li>
Expand Down
36 changes: 36 additions & 0 deletions website/docs/d/cloudwatch_log_groups.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
subcategory: "CloudWatch"
layout: "aws"
page_title: "AWS: aws_cloudwatch_log_groups"
description: |-
Get information on a Cloudwatch Log Groups.
---

# Data Source: aws_cloudwatch_log_groups

Use this data source to get information about an AWS Cloudwatch Log Group

## Example Usage

```hcl
data "aws_cloudwatch_log_groups" "example" {
prefix = "tf-logs-"
}
```

## Argument Reference

The following arguments are supported:

* `prefix` - (Required) The prefix of the Cloudwatch log group name

## Attributes Reference

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

* `names` - A list of the Cloud Watch Log Groups Names in the current region.
* `arns` - A list of the Cloud Watch Log Groups Arns in the current region.
* `creation_times` - A list of the Cloud Watch Log Groups creation time, expressed as the number of milliseconds after Jan 1, 1970 00:00:00 UTC.
* `retention_in_days` - A list of the Cloud Watch Log Groups retention in day.
* `metric_filter_counts` - A list of the Cloud Watch Log Groups metric filter count.
* `kms_key_ids` - A list of the Cloud Watch Log Groups KMS Key ID.