github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 "github.com/hashicorp/errwrap" 12 ) 13 14 func resourceAwsCloudWatchLogGroup() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceAwsCloudWatchLogGroupCreate, 17 Read: resourceAwsCloudWatchLogGroupRead, 18 Update: resourceAwsCloudWatchLogGroupUpdate, 19 Delete: resourceAwsCloudWatchLogGroupDelete, 20 Importer: &schema.ResourceImporter{ 21 State: schema.ImportStatePassthrough, 22 }, 23 24 Schema: map[string]*schema.Schema{ 25 "name": { 26 Type: schema.TypeString, 27 Required: true, 28 ForceNew: true, 29 ValidateFunc: validateLogGroupName, 30 }, 31 32 "retention_in_days": { 33 Type: schema.TypeInt, 34 Optional: true, 35 Default: 0, 36 }, 37 38 "arn": { 39 Type: schema.TypeString, 40 Computed: true, 41 }, 42 43 "tags": tagsSchema(), 44 }, 45 } 46 } 47 48 func resourceAwsCloudWatchLogGroupCreate(d *schema.ResourceData, meta interface{}) error { 49 conn := meta.(*AWSClient).cloudwatchlogsconn 50 51 log.Printf("[DEBUG] Creating CloudWatch Log Group: %s", d.Get("name").(string)) 52 53 _, err := conn.CreateLogGroup(&cloudwatchlogs.CreateLogGroupInput{ 54 LogGroupName: aws.String(d.Get("name").(string)), 55 }) 56 if err != nil { 57 return fmt.Errorf("Creating CloudWatch Log Group failed: %s", err) 58 } 59 60 d.SetId(d.Get("name").(string)) 61 62 log.Println("[INFO] CloudWatch Log Group created") 63 64 return resourceAwsCloudWatchLogGroupUpdate(d, meta) 65 } 66 67 func resourceAwsCloudWatchLogGroupRead(d *schema.ResourceData, meta interface{}) error { 68 conn := meta.(*AWSClient).cloudwatchlogsconn 69 log.Printf("[DEBUG] Reading CloudWatch Log Group: %q", d.Get("name").(string)) 70 lg, exists, err := lookupCloudWatchLogGroup(conn, d.Id(), nil) 71 if err != nil { 72 return err 73 } 74 75 if !exists { 76 log.Printf("[DEBUG] CloudWatch Group %q Not Found", d.Id()) 77 d.SetId("") 78 return nil 79 } 80 81 log.Printf("[DEBUG] Found Log Group: %#v", *lg) 82 83 d.Set("arn", lg.Arn) 84 d.Set("name", lg.LogGroupName) 85 86 if lg.RetentionInDays != nil { 87 d.Set("retention_in_days", lg.RetentionInDays) 88 } 89 90 tags, err := flattenCloudWatchTags(d, conn) 91 if err != nil { 92 return err 93 } 94 d.Set("tags", tags) 95 96 return nil 97 } 98 99 func lookupCloudWatchLogGroup(conn *cloudwatchlogs.CloudWatchLogs, 100 name string, nextToken *string) (*cloudwatchlogs.LogGroup, bool, error) { 101 input := &cloudwatchlogs.DescribeLogGroupsInput{ 102 LogGroupNamePrefix: aws.String(name), 103 NextToken: nextToken, 104 } 105 resp, err := conn.DescribeLogGroups(input) 106 if err != nil { 107 return nil, true, err 108 } 109 110 for _, lg := range resp.LogGroups { 111 if *lg.LogGroupName == name { 112 return lg, true, nil 113 } 114 } 115 116 if resp.NextToken != nil { 117 return lookupCloudWatchLogGroup(conn, name, resp.NextToken) 118 } 119 120 return nil, false, nil 121 } 122 123 func resourceAwsCloudWatchLogGroupUpdate(d *schema.ResourceData, meta interface{}) error { 124 conn := meta.(*AWSClient).cloudwatchlogsconn 125 126 name := d.Get("name").(string) 127 log.Printf("[DEBUG] Updating CloudWatch Log Group: %q", name) 128 129 if d.HasChange("retention_in_days") { 130 var err error 131 132 if v, ok := d.GetOk("retention_in_days"); ok { 133 input := cloudwatchlogs.PutRetentionPolicyInput{ 134 LogGroupName: aws.String(name), 135 RetentionInDays: aws.Int64(int64(v.(int))), 136 } 137 log.Printf("[DEBUG] Setting retention for CloudWatch Log Group: %q: %s", name, input) 138 _, err = conn.PutRetentionPolicy(&input) 139 } else { 140 log.Printf("[DEBUG] Deleting retention for CloudWatch Log Group: %q", name) 141 _, err = conn.DeleteRetentionPolicy(&cloudwatchlogs.DeleteRetentionPolicyInput{ 142 LogGroupName: aws.String(name), 143 }) 144 } 145 146 if err != nil { 147 return err 148 } 149 } 150 151 if d.HasChange("tags") { 152 oraw, nraw := d.GetChange("tags") 153 o := oraw.(map[string]interface{}) 154 n := nraw.(map[string]interface{}) 155 create, remove := diffCloudWatchTags(o, n) 156 157 if len(remove) > 0 { 158 log.Printf("[DEBUG] Removing tags from %s", name) 159 _, err := conn.UntagLogGroup(&cloudwatchlogs.UntagLogGroupInput{ 160 LogGroupName: aws.String(name), 161 Tags: remove, 162 }) 163 if err != nil { 164 return err 165 } 166 } 167 168 if len(create) > 0 { 169 log.Printf("[DEBUG] Creating tags on %s", name) 170 _, err := conn.TagLogGroup(&cloudwatchlogs.TagLogGroupInput{ 171 LogGroupName: aws.String(name), 172 Tags: create, 173 }) 174 if err != nil { 175 return err 176 } 177 } 178 } 179 180 return resourceAwsCloudWatchLogGroupRead(d, meta) 181 } 182 183 func diffCloudWatchTags(oldTags map[string]interface{}, newTags map[string]interface{}) (map[string]*string, []*string) { 184 create := make(map[string]*string) 185 for k, v := range newTags { 186 create[k] = aws.String(v.(string)) 187 } 188 189 var remove []*string 190 for _, t := range oldTags { 191 old, ok := create[t.(string)] 192 if !ok || *old != t.(string) { 193 remove = append(remove, aws.String(t.(string))) 194 } 195 } 196 197 return create, remove 198 } 199 200 func resourceAwsCloudWatchLogGroupDelete(d *schema.ResourceData, meta interface{}) error { 201 conn := meta.(*AWSClient).cloudwatchlogsconn 202 log.Printf("[INFO] Deleting CloudWatch Log Group: %s", d.Id()) 203 _, err := conn.DeleteLogGroup(&cloudwatchlogs.DeleteLogGroupInput{ 204 LogGroupName: aws.String(d.Get("name").(string)), 205 }) 206 if err != nil { 207 return fmt.Errorf("Error deleting CloudWatch Log Group: %s", err) 208 } 209 log.Println("[INFO] CloudWatch Log Group deleted") 210 211 d.SetId("") 212 213 return nil 214 } 215 216 func flattenCloudWatchTags(d *schema.ResourceData, conn *cloudwatchlogs.CloudWatchLogs) (map[string]interface{}, error) { 217 tagsOutput, err := conn.ListTagsLogGroup(&cloudwatchlogs.ListTagsLogGroupInput{ 218 LogGroupName: aws.String(d.Get("name").(string)), 219 }) 220 if err != nil { 221 return nil, errwrap.Wrapf("Error Getting CloudWatch Logs Tag List: %s", err) 222 } 223 if tagsOutput != nil { 224 output := make(map[string]interface{}, len(tagsOutput.Tags)) 225 226 for i, v := range tagsOutput.Tags { 227 output[i] = *v 228 } 229 230 return output, nil 231 } 232 233 return make(map[string]interface{}), nil 234 }