github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_cloudwatch_log_group.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/service/cloudwatchlogs" 11 ) 12 13 func resourceAwsCloudWatchLogGroup() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAwsCloudWatchLogGroupCreate, 16 Read: resourceAwsCloudWatchLogGroupRead, 17 Update: resourceAwsCloudWatchLogGroupUpdate, 18 Delete: resourceAwsCloudWatchLogGroupDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "name": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 }, 26 27 "retention_in_days": &schema.Schema{ 28 Type: schema.TypeInt, 29 Optional: true, 30 Default: 0, 31 }, 32 33 "arn": &schema.Schema{ 34 Type: schema.TypeString, 35 Computed: true, 36 }, 37 }, 38 } 39 } 40 41 func resourceAwsCloudWatchLogGroupCreate(d *schema.ResourceData, meta interface{}) error { 42 conn := meta.(*AWSClient).cloudwatchlogsconn 43 44 log.Printf("[DEBUG] Creating CloudWatch Log Group: %s", d.Get("name").(string)) 45 _, err := conn.CreateLogGroup(&cloudwatchlogs.CreateLogGroupInput{ 46 LogGroupName: aws.String(d.Get("name").(string)), 47 }) 48 if err != nil { 49 return fmt.Errorf("Creating CloudWatch Log Group failed: %s", err) 50 } 51 52 d.SetId(d.Get("name").(string)) 53 54 log.Println("[INFO] CloudWatch Log Group created") 55 56 return resourceAwsCloudWatchLogGroupUpdate(d, meta) 57 } 58 59 func resourceAwsCloudWatchLogGroupRead(d *schema.ResourceData, meta interface{}) error { 60 conn := meta.(*AWSClient).cloudwatchlogsconn 61 log.Printf("[DEBUG] Reading CloudWatch Log Group: %q", d.Get("name").(string)) 62 lg, err := lookupCloudWatchLogGroup(conn, d.Get("name").(string), nil) 63 if err != nil { 64 return err 65 } 66 67 log.Printf("[DEBUG] Found Log Group: %#v", *lg) 68 69 d.Set("arn", *lg.Arn) 70 d.Set("name", *lg.LogGroupName) 71 72 if lg.RetentionInDays != nil { 73 d.Set("retention_in_days", *lg.RetentionInDays) 74 } 75 76 return nil 77 } 78 79 func lookupCloudWatchLogGroup(conn *cloudwatchlogs.CloudWatchLogs, 80 name string, nextToken *string) (*cloudwatchlogs.LogGroup, error) { 81 input := &cloudwatchlogs.DescribeLogGroupsInput{ 82 LogGroupNamePrefix: aws.String(name), 83 NextToken: nextToken, 84 } 85 resp, err := conn.DescribeLogGroups(input) 86 if err != nil { 87 return nil, err 88 } 89 90 for _, lg := range resp.LogGroups { 91 if *lg.LogGroupName == name { 92 return lg, nil 93 } 94 } 95 96 if resp.NextToken != nil { 97 return lookupCloudWatchLogGroup(conn, name, resp.NextToken) 98 } 99 100 return nil, fmt.Errorf("CloudWatch Log Group %q not found", name) 101 } 102 103 func resourceAwsCloudWatchLogGroupUpdate(d *schema.ResourceData, meta interface{}) error { 104 conn := meta.(*AWSClient).cloudwatchlogsconn 105 106 name := d.Get("name").(string) 107 log.Printf("[DEBUG] Updating CloudWatch Log Group: %q", name) 108 109 if d.HasChange("retention_in_days") { 110 var err error 111 112 if v, ok := d.GetOk("retention_in_days"); ok { 113 input := cloudwatchlogs.PutRetentionPolicyInput{ 114 LogGroupName: aws.String(name), 115 RetentionInDays: aws.Int64(int64(v.(int))), 116 } 117 log.Printf("[DEBUG] Setting retention for CloudWatch Log Group: %q: %s", name, input) 118 _, err = conn.PutRetentionPolicy(&input) 119 } else { 120 log.Printf("[DEBUG] Deleting retention for CloudWatch Log Group: %q", name) 121 _, err = conn.DeleteRetentionPolicy(&cloudwatchlogs.DeleteRetentionPolicyInput{ 122 LogGroupName: aws.String(name), 123 }) 124 } 125 126 return err 127 } 128 129 return resourceAwsCloudWatchLogGroupRead(d, meta) 130 } 131 132 func resourceAwsCloudWatchLogGroupDelete(d *schema.ResourceData, meta interface{}) error { 133 conn := meta.(*AWSClient).cloudwatchlogsconn 134 log.Printf("[INFO] Deleting CloudWatch Log Group: %s", d.Id()) 135 _, err := conn.DeleteLogGroup(&cloudwatchlogs.DeleteLogGroupInput{ 136 LogGroupName: aws.String(d.Get("name").(string)), 137 }) 138 if err != nil { 139 return fmt.Errorf("Error deleting CloudWatch Log Group: %s", err) 140 } 141 log.Println("[INFO] CloudWatch Log Group deleted") 142 143 d.SetId("") 144 145 return nil 146 }