github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_kinesis_stream.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/kinesis"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsKinesisStream() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsKinesisStreamCreate,
    18  		Read:   resourceAwsKinesisStreamRead,
    19  		Update: resourceAwsKinesisStreamUpdate,
    20  		Delete: resourceAwsKinesisStreamDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"shard_count": &schema.Schema{
    30  				Type:     schema.TypeInt,
    31  				Required: true,
    32  				ForceNew: true,
    33  			},
    34  
    35  			"arn": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Optional: true,
    38  				Computed: true,
    39  			},
    40  			"tags": tagsSchema(),
    41  		},
    42  	}
    43  }
    44  
    45  func resourceAwsKinesisStreamCreate(d *schema.ResourceData, meta interface{}) error {
    46  	conn := meta.(*AWSClient).kinesisconn
    47  	sn := d.Get("name").(string)
    48  	createOpts := &kinesis.CreateStreamInput{
    49  		ShardCount: aws.Int64(int64(d.Get("shard_count").(int))),
    50  		StreamName: aws.String(sn),
    51  	}
    52  
    53  	_, err := conn.CreateStream(createOpts)
    54  	if err != nil {
    55  		if awsErr, ok := err.(awserr.Error); ok {
    56  			return fmt.Errorf("[WARN] Error creating Kinesis Stream: \"%s\", code: \"%s\"", awsErr.Message(), awsErr.Code())
    57  		}
    58  		return err
    59  	}
    60  
    61  	stateConf := &resource.StateChangeConf{
    62  		Pending:    []string{"CREATING"},
    63  		Target:     "ACTIVE",
    64  		Refresh:    streamStateRefreshFunc(conn, sn),
    65  		Timeout:    5 * time.Minute,
    66  		Delay:      10 * time.Second,
    67  		MinTimeout: 3 * time.Second,
    68  	}
    69  
    70  	streamRaw, err := stateConf.WaitForState()
    71  	if err != nil {
    72  		return fmt.Errorf(
    73  			"Error waiting for Kinesis Stream (%s) to become active: %s",
    74  			sn, err)
    75  	}
    76  
    77  	s := streamRaw.(*kinesis.StreamDescription)
    78  	d.SetId(*s.StreamARN)
    79  	d.Set("arn", s.StreamARN)
    80  
    81  	return resourceAwsKinesisStreamUpdate(d, meta)
    82  }
    83  
    84  func resourceAwsKinesisStreamUpdate(d *schema.ResourceData, meta interface{}) error {
    85  	conn := meta.(*AWSClient).kinesisconn
    86  
    87  	d.Partial(true)
    88  	if err := setTagsKinesis(conn, d); err != nil {
    89  		return err
    90  	}
    91  
    92  	d.SetPartial("tags")
    93  	d.Partial(false)
    94  
    95  	return resourceAwsKinesisStreamRead(d, meta)
    96  }
    97  
    98  func resourceAwsKinesisStreamRead(d *schema.ResourceData, meta interface{}) error {
    99  	conn := meta.(*AWSClient).kinesisconn
   100  	sn := d.Get("name").(string)
   101  	describeOpts := &kinesis.DescribeStreamInput{
   102  		StreamName: aws.String(sn),
   103  	}
   104  	resp, err := conn.DescribeStream(describeOpts)
   105  	if err != nil {
   106  		if awsErr, ok := err.(awserr.Error); ok {
   107  			if awsErr.Code() == "ResourceNotFoundException" {
   108  				d.SetId("")
   109  				return nil
   110  			}
   111  			return fmt.Errorf("[WARN] Error reading Kinesis Stream: \"%s\", code: \"%s\"", awsErr.Message(), awsErr.Code())
   112  		}
   113  		return err
   114  	}
   115  
   116  	s := resp.StreamDescription
   117  	d.Set("arn", *s.StreamARN)
   118  	d.Set("shard_count", len(s.Shards))
   119  
   120  	// set tags
   121  	describeTagsOpts := &kinesis.ListTagsForStreamInput{
   122  		StreamName: aws.String(sn),
   123  	}
   124  	tagsResp, err := conn.ListTagsForStream(describeTagsOpts)
   125  	if err != nil {
   126  		log.Printf("[DEBUG] Error retrieving tags for Stream: %s. %s", sn, err)
   127  	} else {
   128  		d.Set("tags", tagsToMapKinesis(tagsResp.Tags))
   129  	}
   130  
   131  	return nil
   132  }
   133  
   134  func resourceAwsKinesisStreamDelete(d *schema.ResourceData, meta interface{}) error {
   135  	conn := meta.(*AWSClient).kinesisconn
   136  	sn := d.Get("name").(string)
   137  	_, err := conn.DeleteStream(&kinesis.DeleteStreamInput{
   138  		StreamName: aws.String(sn),
   139  	})
   140  
   141  	if err != nil {
   142  		return err
   143  	}
   144  
   145  	stateConf := &resource.StateChangeConf{
   146  		Pending:    []string{"DELETING"},
   147  		Target:     "DESTROYED",
   148  		Refresh:    streamStateRefreshFunc(conn, sn),
   149  		Timeout:    5 * time.Minute,
   150  		Delay:      10 * time.Second,
   151  		MinTimeout: 3 * time.Second,
   152  	}
   153  
   154  	_, err = stateConf.WaitForState()
   155  	if err != nil {
   156  		return fmt.Errorf(
   157  			"Error waiting for Stream (%s) to be destroyed: %s",
   158  			sn, err)
   159  	}
   160  
   161  	d.SetId("")
   162  	return nil
   163  }
   164  
   165  func streamStateRefreshFunc(conn *kinesis.Kinesis, sn string) resource.StateRefreshFunc {
   166  	return func() (interface{}, string, error) {
   167  		describeOpts := &kinesis.DescribeStreamInput{
   168  			StreamName: aws.String(sn),
   169  		}
   170  		resp, err := conn.DescribeStream(describeOpts)
   171  		if err != nil {
   172  			if awsErr, ok := err.(awserr.Error); ok {
   173  				if awsErr.Code() == "ResourceNotFoundException" {
   174  					return 42, "DESTROYED", nil
   175  				}
   176  				return nil, awsErr.Code(), err
   177  			}
   178  			return nil, "failed", err
   179  		}
   180  
   181  		return resp.StreamDescription, *resp.StreamDescription.StreamStatus, nil
   182  	}
   183  }