2015-05-27 21:17:26 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-10-03 01:11:49 +02:00
|
|
|
"log"
|
2015-05-27 21:17:26 +02:00
|
|
|
"time"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/kinesis"
|
2015-05-27 21:17:26 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsKinesisStream() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsKinesisStreamCreate,
|
|
|
|
Read: resourceAwsKinesisStreamRead,
|
2015-10-03 01:11:49 +02:00
|
|
|
Update: resourceAwsKinesisStreamUpdate,
|
2015-05-27 21:17:26 +02:00
|
|
|
Delete: resourceAwsKinesisStreamDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"shard_count": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"arn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-10-03 01:11:49 +02:00
|
|
|
"tags": tagsSchema(),
|
2015-05-27 21:17:26 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsKinesisStreamCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).kinesisconn
|
|
|
|
sn := d.Get("name").(string)
|
|
|
|
createOpts := &kinesis.CreateStreamInput{
|
2015-07-28 22:29:46 +02:00
|
|
|
ShardCount: aws.Int64(int64(d.Get("shard_count").(int))),
|
2015-05-27 21:17:26 +02:00
|
|
|
StreamName: aws.String(sn),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.CreateStream(createOpts)
|
|
|
|
if err != nil {
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
|
|
|
return fmt.Errorf("[WARN] Error creating Kinesis Stream: \"%s\", code: \"%s\"", awsErr.Message(), awsErr.Code())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"CREATING"},
|
|
|
|
Target: "ACTIVE",
|
|
|
|
Refresh: streamStateRefreshFunc(conn, sn),
|
|
|
|
Timeout: 5 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
streamRaw, err := stateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for Kinesis Stream (%s) to become active: %s",
|
|
|
|
sn, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := streamRaw.(*kinesis.StreamDescription)
|
2015-05-29 16:40:34 +02:00
|
|
|
d.SetId(*s.StreamARN)
|
|
|
|
d.Set("arn", s.StreamARN)
|
2015-05-27 21:17:26 +02:00
|
|
|
|
2015-10-03 01:11:49 +02:00
|
|
|
return resourceAwsKinesisStreamUpdate(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsKinesisStreamUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).kinesisconn
|
|
|
|
|
|
|
|
d.Partial(true)
|
|
|
|
if err := setTagsKinesis(conn, d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("tags")
|
|
|
|
d.Partial(false)
|
|
|
|
|
|
|
|
return resourceAwsKinesisStreamRead(d, meta)
|
2015-05-27 21:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsKinesisStreamRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).kinesisconn
|
2015-10-03 01:11:49 +02:00
|
|
|
sn := d.Get("name").(string)
|
2015-05-27 21:17:26 +02:00
|
|
|
describeOpts := &kinesis.DescribeStreamInput{
|
2015-10-03 01:11:49 +02:00
|
|
|
StreamName: aws.String(sn),
|
2015-05-27 21:17:26 +02:00
|
|
|
}
|
|
|
|
resp, err := conn.DescribeStream(describeOpts)
|
|
|
|
if err != nil {
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
|
|
|
if awsErr.Code() == "ResourceNotFoundException" {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("[WARN] Error reading Kinesis Stream: \"%s\", code: \"%s\"", awsErr.Message(), awsErr.Code())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := resp.StreamDescription
|
|
|
|
d.Set("arn", *s.StreamARN)
|
|
|
|
d.Set("shard_count", len(s.Shards))
|
|
|
|
|
2015-10-03 01:11:49 +02:00
|
|
|
// set tags
|
|
|
|
describeTagsOpts := &kinesis.ListTagsForStreamInput{
|
|
|
|
StreamName: aws.String(sn),
|
|
|
|
}
|
|
|
|
tagsResp, err := conn.ListTagsForStream(describeTagsOpts)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[DEBUG] Error retrieving tags for Stream: %s. %s", sn, err)
|
|
|
|
} else {
|
|
|
|
d.Set("tags", tagsToMapKinesis(tagsResp.Tags))
|
|
|
|
}
|
|
|
|
|
2015-05-27 21:17:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsKinesisStreamDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).kinesisconn
|
2015-05-29 16:40:34 +02:00
|
|
|
sn := d.Get("name").(string)
|
2015-05-27 21:17:26 +02:00
|
|
|
_, err := conn.DeleteStream(&kinesis.DeleteStreamInput{
|
2015-05-29 16:40:34 +02:00
|
|
|
StreamName: aws.String(sn),
|
2015-05-27 21:17:26 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"DELETING"},
|
|
|
|
Target: "DESTROYED",
|
2015-05-29 16:40:34 +02:00
|
|
|
Refresh: streamStateRefreshFunc(conn, sn),
|
2015-05-27 21:17:26 +02:00
|
|
|
Timeout: 5 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for Stream (%s) to be destroyed: %s",
|
2015-05-29 16:40:34 +02:00
|
|
|
sn, err)
|
2015-05-27 21:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-29 16:40:34 +02:00
|
|
|
func streamStateRefreshFunc(conn *kinesis.Kinesis, sn string) resource.StateRefreshFunc {
|
2015-05-27 21:17:26 +02:00
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
describeOpts := &kinesis.DescribeStreamInput{
|
2015-05-29 16:40:34 +02:00
|
|
|
StreamName: aws.String(sn),
|
2015-05-27 21:17:26 +02:00
|
|
|
}
|
|
|
|
resp, err := conn.DescribeStream(describeOpts)
|
|
|
|
if err != nil {
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
|
|
|
if awsErr.Code() == "ResourceNotFoundException" {
|
|
|
|
return 42, "DESTROYED", nil
|
|
|
|
}
|
|
|
|
return nil, awsErr.Code(), err
|
|
|
|
}
|
|
|
|
return nil, "failed", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.StreamDescription, *resp.StreamDescription.StreamStatus, nil
|
|
|
|
}
|
|
|
|
}
|