2015-09-16 23:02:28 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2017-02-10 09:38:14 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-09-16 23:02:28 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
2017-01-10 13:28:36 +01:00
|
|
|
"github.com/hashicorp/errwrap"
|
2015-09-16 23:02:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsCloudWatchLogGroup() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsCloudWatchLogGroupCreate,
|
|
|
|
Read: resourceAwsCloudWatchLogGroupRead,
|
|
|
|
Update: resourceAwsCloudWatchLogGroupUpdate,
|
|
|
|
Delete: resourceAwsCloudWatchLogGroupDelete,
|
2016-06-23 02:55:29 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2015-09-16 23:02:28 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-01-10 13:28:36 +01:00
|
|
|
"name": {
|
2016-03-05 17:37:45 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ValidateFunc: validateLogGroupName,
|
2015-09-16 23:02:28 +02:00
|
|
|
},
|
|
|
|
|
2017-01-10 13:28:36 +01:00
|
|
|
"retention_in_days": {
|
2015-09-16 23:02:28 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 0,
|
|
|
|
},
|
|
|
|
|
2017-01-10 13:28:36 +01:00
|
|
|
"arn": {
|
2015-09-16 23:02:28 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-01-10 13:28:36 +01:00
|
|
|
|
|
|
|
"tags": tagsSchema(),
|
2015-09-16 23:02:28 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsCloudWatchLogGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).cloudwatchlogsconn
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Creating CloudWatch Log Group: %s", d.Get("name").(string))
|
2017-01-10 13:28:36 +01:00
|
|
|
|
2015-09-16 23:02:28 +02:00
|
|
|
_, err := conn.CreateLogGroup(&cloudwatchlogs.CreateLogGroupInput{
|
|
|
|
LogGroupName: aws.String(d.Get("name").(string)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-02-10 09:38:14 +01:00
|
|
|
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "ResourceAlreadyExistsException" {
|
|
|
|
return fmt.Errorf("Creating CloudWatch Log Group failed: %s: The CloudWatch Log Group '%s' already exists.", err, d.Get("name").(string))
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Creating CloudWatch Log Group failed: %s '%s'", err, d.Get("name"))
|
2015-09-16 23:02:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(d.Get("name").(string))
|
|
|
|
|
|
|
|
log.Println("[INFO] CloudWatch Log Group created")
|
|
|
|
|
|
|
|
return resourceAwsCloudWatchLogGroupUpdate(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsCloudWatchLogGroupRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).cloudwatchlogsconn
|
|
|
|
log.Printf("[DEBUG] Reading CloudWatch Log Group: %q", d.Get("name").(string))
|
2016-07-11 20:06:44 +02:00
|
|
|
lg, exists, err := lookupCloudWatchLogGroup(conn, d.Id(), nil)
|
2015-09-16 23:02:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-11 20:06:44 +02:00
|
|
|
if !exists {
|
|
|
|
log.Printf("[DEBUG] CloudWatch Group %q Not Found", d.Id())
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-16 23:02:28 +02:00
|
|
|
log.Printf("[DEBUG] Found Log Group: %#v", *lg)
|
|
|
|
|
2016-08-28 20:25:30 +02:00
|
|
|
d.Set("arn", lg.Arn)
|
|
|
|
d.Set("name", lg.LogGroupName)
|
2015-09-16 23:02:28 +02:00
|
|
|
|
|
|
|
if lg.RetentionInDays != nil {
|
2016-08-28 20:25:30 +02:00
|
|
|
d.Set("retention_in_days", lg.RetentionInDays)
|
2015-09-16 23:02:28 +02:00
|
|
|
}
|
|
|
|
|
2017-03-03 16:32:04 +01:00
|
|
|
if meta.(*AWSClient).partition != "aws-us-gov" {
|
|
|
|
tags, err := flattenCloudWatchTags(d, conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Set("tags", tags)
|
2017-01-10 13:28:36 +01:00
|
|
|
}
|
|
|
|
|
2015-09-16 23:02:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupCloudWatchLogGroup(conn *cloudwatchlogs.CloudWatchLogs,
|
2016-07-11 20:06:44 +02:00
|
|
|
name string, nextToken *string) (*cloudwatchlogs.LogGroup, bool, error) {
|
2015-09-16 23:02:28 +02:00
|
|
|
input := &cloudwatchlogs.DescribeLogGroupsInput{
|
|
|
|
LogGroupNamePrefix: aws.String(name),
|
|
|
|
NextToken: nextToken,
|
|
|
|
}
|
|
|
|
resp, err := conn.DescribeLogGroups(input)
|
|
|
|
if err != nil {
|
2016-07-11 20:06:44 +02:00
|
|
|
return nil, true, err
|
2015-09-16 23:02:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, lg := range resp.LogGroups {
|
|
|
|
if *lg.LogGroupName == name {
|
2016-07-11 20:06:44 +02:00
|
|
|
return lg, true, nil
|
2015-09-16 23:02:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.NextToken != nil {
|
|
|
|
return lookupCloudWatchLogGroup(conn, name, resp.NextToken)
|
|
|
|
}
|
|
|
|
|
2016-07-11 20:06:44 +02:00
|
|
|
return nil, false, nil
|
2015-09-16 23:02:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsCloudWatchLogGroupUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).cloudwatchlogsconn
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
log.Printf("[DEBUG] Updating CloudWatch Log Group: %q", name)
|
|
|
|
|
|
|
|
if d.HasChange("retention_in_days") {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("retention_in_days"); ok {
|
|
|
|
input := cloudwatchlogs.PutRetentionPolicyInput{
|
|
|
|
LogGroupName: aws.String(name),
|
|
|
|
RetentionInDays: aws.Int64(int64(v.(int))),
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] Setting retention for CloudWatch Log Group: %q: %s", name, input)
|
|
|
|
_, err = conn.PutRetentionPolicy(&input)
|
|
|
|
} else {
|
|
|
|
log.Printf("[DEBUG] Deleting retention for CloudWatch Log Group: %q", name)
|
|
|
|
_, err = conn.DeleteRetentionPolicy(&cloudwatchlogs.DeleteRetentionPolicyInput{
|
|
|
|
LogGroupName: aws.String(name),
|
|
|
|
})
|
2016-04-28 14:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-09-16 23:02:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 16:32:04 +01:00
|
|
|
if meta.(*AWSClient).partition != "aws-us-gov" && d.HasChange("tags") {
|
2017-01-10 13:28:36 +01:00
|
|
|
oraw, nraw := d.GetChange("tags")
|
|
|
|
o := oraw.(map[string]interface{})
|
|
|
|
n := nraw.(map[string]interface{})
|
|
|
|
create, remove := diffCloudWatchTags(o, n)
|
|
|
|
|
|
|
|
if len(remove) > 0 {
|
|
|
|
log.Printf("[DEBUG] Removing tags from %s", name)
|
|
|
|
_, err := conn.UntagLogGroup(&cloudwatchlogs.UntagLogGroupInput{
|
|
|
|
LogGroupName: aws.String(name),
|
|
|
|
Tags: remove,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(create) > 0 {
|
|
|
|
log.Printf("[DEBUG] Creating tags on %s", name)
|
|
|
|
_, err := conn.TagLogGroup(&cloudwatchlogs.TagLogGroupInput{
|
|
|
|
LogGroupName: aws.String(name),
|
|
|
|
Tags: create,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 23:02:28 +02:00
|
|
|
return resourceAwsCloudWatchLogGroupRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2017-01-10 13:28:36 +01:00
|
|
|
func diffCloudWatchTags(oldTags map[string]interface{}, newTags map[string]interface{}) (map[string]*string, []*string) {
|
|
|
|
create := make(map[string]*string)
|
|
|
|
for k, v := range newTags {
|
|
|
|
create[k] = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
var remove []*string
|
|
|
|
for _, t := range oldTags {
|
|
|
|
old, ok := create[t.(string)]
|
|
|
|
if !ok || *old != t.(string) {
|
|
|
|
remove = append(remove, aws.String(t.(string)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return create, remove
|
|
|
|
}
|
|
|
|
|
2015-09-16 23:02:28 +02:00
|
|
|
func resourceAwsCloudWatchLogGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).cloudwatchlogsconn
|
|
|
|
log.Printf("[INFO] Deleting CloudWatch Log Group: %s", d.Id())
|
|
|
|
_, err := conn.DeleteLogGroup(&cloudwatchlogs.DeleteLogGroupInput{
|
|
|
|
LogGroupName: aws.String(d.Get("name").(string)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting CloudWatch Log Group: %s", err)
|
|
|
|
}
|
|
|
|
log.Println("[INFO] CloudWatch Log Group deleted")
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-10 13:28:36 +01:00
|
|
|
|
|
|
|
func flattenCloudWatchTags(d *schema.ResourceData, conn *cloudwatchlogs.CloudWatchLogs) (map[string]interface{}, error) {
|
|
|
|
tagsOutput, err := conn.ListTagsLogGroup(&cloudwatchlogs.ListTagsLogGroupInput{
|
|
|
|
LogGroupName: aws.String(d.Get("name").(string)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-01-29 20:19:48 +01:00
|
|
|
return nil, errwrap.Wrapf("Error Getting CloudWatch Logs Tag List: {{err}}", err)
|
2017-01-10 13:28:36 +01:00
|
|
|
}
|
|
|
|
if tagsOutput != nil {
|
|
|
|
output := make(map[string]interface{}, len(tagsOutput.Tags))
|
|
|
|
|
|
|
|
for i, v := range tagsOutput.Tags {
|
|
|
|
output[i] = *v
|
|
|
|
}
|
|
|
|
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return make(map[string]interface{}), nil
|
|
|
|
}
|