github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_cloudwatch_log_stream.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
    10  	"github.com/hashicorp/errwrap"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  )
    13  
    14  func resourceAwsCloudWatchLogStream() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceAwsCloudWatchLogStreamCreate,
    17  		Read:   resourceAwsCloudWatchLogStreamRead,
    18  		Delete: resourceAwsCloudWatchLogStreamDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"arn": {
    22  				Type:     schema.TypeString,
    23  				Computed: true,
    24  			},
    25  
    26  			"name": {
    27  				Type:         schema.TypeString,
    28  				Required:     true,
    29  				ForceNew:     true,
    30  				ValidateFunc: validateCloudWatchLogStreamName,
    31  			},
    32  
    33  			"log_group_name": {
    34  				Type:     schema.TypeString,
    35  				Required: true,
    36  				ForceNew: true,
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  func resourceAwsCloudWatchLogStreamCreate(d *schema.ResourceData, meta interface{}) error {
    43  	conn := meta.(*AWSClient).cloudwatchlogsconn
    44  
    45  	log.Printf("[DEBUG] Creating CloudWatch Log Stream: %s", d.Get("name").(string))
    46  	_, err := conn.CreateLogStream(&cloudwatchlogs.CreateLogStreamInput{
    47  		LogGroupName:  aws.String(d.Get("log_group_name").(string)),
    48  		LogStreamName: aws.String(d.Get("name").(string)),
    49  	})
    50  	if err != nil {
    51  		return errwrap.Wrapf("Creating CloudWatch Log Stream failed: {{err}}", err)
    52  	}
    53  
    54  	d.SetId(d.Get("name").(string))
    55  
    56  	return resourceAwsCloudWatchLogStreamRead(d, meta)
    57  }
    58  
    59  func resourceAwsCloudWatchLogStreamRead(d *schema.ResourceData, meta interface{}) error {
    60  	conn := meta.(*AWSClient).cloudwatchlogsconn
    61  
    62  	ls, exists, err := lookupCloudWatchLogStream(conn, d.Id(), d.Get("log_group_name").(string), nil)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	if !exists {
    68  		log.Printf("[DEBUG] CloudWatch Stream %q Not Found. Removing from state", d.Id())
    69  		d.SetId("")
    70  		return nil
    71  	}
    72  
    73  	d.Set("arn", ls.Arn)
    74  	d.Set("name", ls.LogStreamName)
    75  
    76  	return nil
    77  }
    78  
    79  func resourceAwsCloudWatchLogStreamDelete(d *schema.ResourceData, meta interface{}) error {
    80  	conn := meta.(*AWSClient).cloudwatchlogsconn
    81  
    82  	log.Printf("[INFO] Deleting CloudWatch Log Stream: %s", d.Id())
    83  	params := &cloudwatchlogs.DeleteLogStreamInput{
    84  		LogGroupName:  aws.String(d.Get("log_group_name").(string)),
    85  		LogStreamName: aws.String(d.Id()),
    86  	}
    87  	_, err := conn.DeleteLogStream(params)
    88  	if err != nil {
    89  		return errwrap.Wrapf("Error deleting CloudWatch Log Stream: {{err}}", err)
    90  	}
    91  
    92  	return nil
    93  }
    94  
    95  func lookupCloudWatchLogStream(conn *cloudwatchlogs.CloudWatchLogs,
    96  	name string, logStreamName string, nextToken *string) (*cloudwatchlogs.LogStream, bool, error) {
    97  	input := &cloudwatchlogs.DescribeLogStreamsInput{
    98  		LogStreamNamePrefix: aws.String(name),
    99  		LogGroupName:        aws.String(logStreamName),
   100  		NextToken:           nextToken,
   101  	}
   102  	resp, err := conn.DescribeLogStreams(input)
   103  	if err != nil {
   104  		return nil, true, err
   105  	}
   106  
   107  	for _, ls := range resp.LogStreams {
   108  		if *ls.LogStreamName == name {
   109  			return ls, true, nil
   110  		}
   111  	}
   112  
   113  	if resp.NextToken != nil {
   114  		return lookupCloudWatchLogStream(conn, name, logStreamName, resp.NextToken)
   115  	}
   116  
   117  	return nil, false, nil
   118  }
   119  
   120  func validateCloudWatchLogStreamName(v interface{}, k string) (ws []string, errors []error) {
   121  	value := v.(string)
   122  	if regexp.MustCompile(`:`).MatchString(value) {
   123  		errors = append(errors, fmt.Errorf(
   124  			"colons not allowed in %q:", k))
   125  	}
   126  	if len(value) < 1 || len(value) > 512 {
   127  		errors = append(errors, fmt.Errorf(
   128  			"%q must be between 1 and 512 characters: %q", k, value))
   129  	}
   130  
   131  	return
   132  
   133  }