-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support Cloudwatch Log Groups Data Source
- Loading branch information
1 parent
5daf292
commit 105560c
Showing
5 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/helper/resource" | ||
"github.com/hashicorp/terraform/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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_cloudwatch_log_groups" | ||
sidebar_current: "docs-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. |