github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  		Importer: &schema.ResourceImporter{
    20  			State: schema.ImportStatePassthrough,
    21  		},
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"name": &schema.Schema{
    25  				Type:         schema.TypeString,
    26  				Required:     true,
    27  				ForceNew:     true,
    28  				ValidateFunc: validateLogGroupName,
    29  			},
    30  
    31  			"retention_in_days": &schema.Schema{
    32  				Type:     schema.TypeInt,
    33  				Optional: true,
    34  				Default:  0,
    35  			},
    36  
    37  			"arn": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Computed: true,
    40  			},
    41  		},
    42  	}
    43  }
    44  
    45  func resourceAwsCloudWatchLogGroupCreate(d *schema.ResourceData, meta interface{}) error {
    46  	conn := meta.(*AWSClient).cloudwatchlogsconn
    47  
    48  	log.Printf("[DEBUG] Creating CloudWatch Log Group: %s", d.Get("name").(string))
    49  	_, err := conn.CreateLogGroup(&cloudwatchlogs.CreateLogGroupInput{
    50  		LogGroupName: aws.String(d.Get("name").(string)),
    51  	})
    52  	if err != nil {
    53  		return fmt.Errorf("Creating CloudWatch Log Group failed: %s", err)
    54  	}
    55  
    56  	d.SetId(d.Get("name").(string))
    57  
    58  	log.Println("[INFO] CloudWatch Log Group created")
    59  
    60  	return resourceAwsCloudWatchLogGroupUpdate(d, meta)
    61  }
    62  
    63  func resourceAwsCloudWatchLogGroupRead(d *schema.ResourceData, meta interface{}) error {
    64  	conn := meta.(*AWSClient).cloudwatchlogsconn
    65  	log.Printf("[DEBUG] Reading CloudWatch Log Group: %q", d.Get("name").(string))
    66  	lg, exists, err := lookupCloudWatchLogGroup(conn, d.Id(), nil)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	if !exists {
    72  		log.Printf("[DEBUG] CloudWatch Group %q Not Found", d.Id())
    73  		d.SetId("")
    74  		return nil
    75  	}
    76  
    77  	log.Printf("[DEBUG] Found Log Group: %#v", *lg)
    78  
    79  	d.Set("arn", lg.Arn)
    80  	d.Set("name", lg.LogGroupName)
    81  
    82  	if lg.RetentionInDays != nil {
    83  		d.Set("retention_in_days", lg.RetentionInDays)
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  func lookupCloudWatchLogGroup(conn *cloudwatchlogs.CloudWatchLogs,
    90  	name string, nextToken *string) (*cloudwatchlogs.LogGroup, bool, error) {
    91  	input := &cloudwatchlogs.DescribeLogGroupsInput{
    92  		LogGroupNamePrefix: aws.String(name),
    93  		NextToken:          nextToken,
    94  	}
    95  	resp, err := conn.DescribeLogGroups(input)
    96  	if err != nil {
    97  		return nil, true, err
    98  	}
    99  
   100  	for _, lg := range resp.LogGroups {
   101  		if *lg.LogGroupName == name {
   102  			return lg, true, nil
   103  		}
   104  	}
   105  
   106  	if resp.NextToken != nil {
   107  		return lookupCloudWatchLogGroup(conn, name, resp.NextToken)
   108  	}
   109  
   110  	return nil, false, nil
   111  }
   112  
   113  func resourceAwsCloudWatchLogGroupUpdate(d *schema.ResourceData, meta interface{}) error {
   114  	conn := meta.(*AWSClient).cloudwatchlogsconn
   115  
   116  	name := d.Get("name").(string)
   117  	log.Printf("[DEBUG] Updating CloudWatch Log Group: %q", name)
   118  
   119  	if d.HasChange("retention_in_days") {
   120  		var err error
   121  
   122  		if v, ok := d.GetOk("retention_in_days"); ok {
   123  			input := cloudwatchlogs.PutRetentionPolicyInput{
   124  				LogGroupName:    aws.String(name),
   125  				RetentionInDays: aws.Int64(int64(v.(int))),
   126  			}
   127  			log.Printf("[DEBUG] Setting retention for CloudWatch Log Group: %q: %s", name, input)
   128  			_, err = conn.PutRetentionPolicy(&input)
   129  		} else {
   130  			log.Printf("[DEBUG] Deleting retention for CloudWatch Log Group: %q", name)
   131  			_, err = conn.DeleteRetentionPolicy(&cloudwatchlogs.DeleteRetentionPolicyInput{
   132  				LogGroupName: aws.String(name),
   133  			})
   134  		}
   135  
   136  		if err != nil {
   137  			return err
   138  		}
   139  	}
   140  
   141  	return resourceAwsCloudWatchLogGroupRead(d, meta)
   142  }
   143  
   144  func resourceAwsCloudWatchLogGroupDelete(d *schema.ResourceData, meta interface{}) error {
   145  	conn := meta.(*AWSClient).cloudwatchlogsconn
   146  	log.Printf("[INFO] Deleting CloudWatch Log Group: %s", d.Id())
   147  	_, err := conn.DeleteLogGroup(&cloudwatchlogs.DeleteLogGroupInput{
   148  		LogGroupName: aws.String(d.Get("name").(string)),
   149  	})
   150  	if err != nil {
   151  		return fmt.Errorf("Error deleting CloudWatch Log Group: %s", err)
   152  	}
   153  	log.Println("[INFO] CloudWatch Log Group deleted")
   154  
   155  	d.SetId("")
   156  
   157  	return nil
   158  }