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,
|
|
|
|
},
|
|
|
|
|
2016-02-19 23:10:55 +01:00
|
|
|
"retention_period": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 24,
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(int)
|
|
|
|
if value < 24 || value > 168 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q must be between 24 and 168 hours", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-07-22 00:37:58 +02:00
|
|
|
"shard_level_metrics": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
|
2015-05-27 21:17:26 +02:00
|
|
|
"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"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"ACTIVE"},
|
2015-05-27 21:17:26 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-12-14 17:26:44 +01:00
|
|
|
s := streamRaw.(kinesisStreamState)
|
|
|
|
d.SetId(s.arn)
|
|
|
|
d.Set("arn", s.arn)
|
|
|
|
d.Set("shard_count", s.shardCount)
|
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)
|
|
|
|
|
2016-02-19 23:10:55 +01:00
|
|
|
if err := setKinesisRetentionPeriod(conn, d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-22 00:37:58 +02:00
|
|
|
if err := updateKinesisShardLevelMetrics(conn, d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-19 23:10:55 +01:00
|
|
|
|
2015-10-03 01:11:49 +02:00
|
|
|
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-12-14 17:26:44 +01:00
|
|
|
|
|
|
|
state, err := readKinesisStreamState(conn, sn)
|
2015-05-27 21:17:26 +02:00
|
|
|
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
|
|
|
|
|
2015-12-14 17:26:44 +01:00
|
|
|
}
|
|
|
|
d.Set("arn", state.arn)
|
|
|
|
d.Set("shard_count", state.shardCount)
|
2016-02-19 23:10:55 +01:00
|
|
|
d.Set("retention_period", state.retentionPeriod)
|
2015-05-27 21:17:26 +02:00
|
|
|
|
2016-07-22 00:37:58 +02:00
|
|
|
if len(state.shardLevelMetrics) > 0 {
|
|
|
|
d.Set("shard_level_metrics", state.shardLevelMetrics)
|
|
|
|
}
|
|
|
|
|
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"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"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
|
|
|
|
}
|
|
|
|
|
2016-02-19 23:10:55 +01:00
|
|
|
func setKinesisRetentionPeriod(conn *kinesis.Kinesis, d *schema.ResourceData) error {
|
|
|
|
sn := d.Get("name").(string)
|
|
|
|
|
|
|
|
oraw, nraw := d.GetChange("retention_period")
|
|
|
|
o := oraw.(int)
|
|
|
|
n := nraw.(int)
|
|
|
|
|
|
|
|
if n == 0 {
|
|
|
|
log.Printf("[DEBUG] Kinesis Stream (%q) Retention Period Not Changed", sn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if n > o {
|
|
|
|
log.Printf("[DEBUG] Increasing %s Stream Retention Period to %d", sn, n)
|
|
|
|
_, err := conn.IncreaseStreamRetentionPeriod(&kinesis.IncreaseStreamRetentionPeriodInput{
|
|
|
|
StreamName: aws.String(sn),
|
|
|
|
RetentionPeriodHours: aws.Int64(int64(n)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
log.Printf("[DEBUG] Decreasing %s Stream Retention Period to %d", sn, n)
|
|
|
|
_, err := conn.DecreaseStreamRetentionPeriod(&kinesis.DecreaseStreamRetentionPeriodInput{
|
|
|
|
StreamName: aws.String(sn),
|
|
|
|
RetentionPeriodHours: aws.Int64(int64(n)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 00:37:58 +02:00
|
|
|
if err := waitForKinesisToBeActive(conn, sn); err != nil {
|
|
|
|
return err
|
2016-02-19 23:10:55 +01:00
|
|
|
}
|
|
|
|
|
2016-07-22 00:37:58 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateKinesisShardLevelMetrics(conn *kinesis.Kinesis, d *schema.ResourceData) error {
|
|
|
|
sn := d.Get("name").(string)
|
|
|
|
|
|
|
|
o, n := d.GetChange("shard_level_metrics")
|
|
|
|
if o == nil {
|
|
|
|
o = new(schema.Set)
|
|
|
|
}
|
|
|
|
if n == nil {
|
|
|
|
n = new(schema.Set)
|
|
|
|
}
|
|
|
|
|
|
|
|
os := o.(*schema.Set)
|
|
|
|
ns := n.(*schema.Set)
|
|
|
|
|
|
|
|
disableMetrics := os.Difference(ns)
|
|
|
|
if disableMetrics.Len() != 0 {
|
|
|
|
metrics := disableMetrics.List()
|
|
|
|
log.Printf("[DEBUG] Disabling shard level metrics %v for stream %s", metrics, sn)
|
|
|
|
|
|
|
|
props := &kinesis.DisableEnhancedMonitoringInput{
|
|
|
|
StreamName: aws.String(sn),
|
|
|
|
ShardLevelMetrics: expandStringList(metrics),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.DisableEnhancedMonitoring(props)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failure to disable shard level metrics for stream %s: %s", sn, err)
|
|
|
|
}
|
|
|
|
if err := waitForKinesisToBeActive(conn, sn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enabledMetrics := ns.Difference(os)
|
|
|
|
if enabledMetrics.Len() != 0 {
|
|
|
|
metrics := enabledMetrics.List()
|
|
|
|
log.Printf("[DEBUG] Enabling shard level metrics %v for stream %s", metrics, sn)
|
|
|
|
|
|
|
|
props := &kinesis.EnableEnhancedMonitoringInput{
|
|
|
|
StreamName: aws.String(sn),
|
|
|
|
ShardLevelMetrics: expandStringList(metrics),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.EnableEnhancedMonitoring(props)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failure to enable shard level metrics for stream %s: %s", sn, err)
|
|
|
|
}
|
|
|
|
if err := waitForKinesisToBeActive(conn, sn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-19 23:10:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-14 17:26:44 +01:00
|
|
|
type kinesisStreamState struct {
|
2016-07-22 00:37:58 +02:00
|
|
|
arn string
|
|
|
|
status string
|
|
|
|
shardCount int
|
|
|
|
retentionPeriod int64
|
|
|
|
shardLevelMetrics []string
|
2015-12-14 17:26:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func readKinesisStreamState(conn *kinesis.Kinesis, sn string) (kinesisStreamState, error) {
|
|
|
|
describeOpts := &kinesis.DescribeStreamInput{
|
|
|
|
StreamName: aws.String(sn),
|
|
|
|
}
|
|
|
|
|
|
|
|
var state kinesisStreamState
|
|
|
|
err := conn.DescribeStreamPages(describeOpts, func(page *kinesis.DescribeStreamOutput, last bool) (shouldContinue bool) {
|
|
|
|
state.arn = aws.StringValue(page.StreamDescription.StreamARN)
|
|
|
|
state.status = aws.StringValue(page.StreamDescription.StreamStatus)
|
2016-03-01 19:46:39 +01:00
|
|
|
state.shardCount += len(openShards(page.StreamDescription.Shards))
|
2016-02-19 23:10:55 +01:00
|
|
|
state.retentionPeriod = aws.Int64Value(page.StreamDescription.RetentionPeriodHours)
|
2016-07-22 00:37:58 +02:00
|
|
|
state.shardLevelMetrics = flattenKinesisShardLevelMetrics(page.StreamDescription.EnhancedMonitoring)
|
2015-12-14 17:26:44 +01:00
|
|
|
return !last
|
|
|
|
})
|
|
|
|
return state, err
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-12-14 17:26:44 +01:00
|
|
|
state, err := readKinesisStreamState(conn, sn)
|
2015-05-27 21:17:26 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-12-14 17:26:44 +01:00
|
|
|
return state, state.status, nil
|
2015-05-27 21:17:26 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-01 19:46:39 +01:00
|
|
|
|
2016-07-22 00:37:58 +02:00
|
|
|
func waitForKinesisToBeActive(conn *kinesis.Kinesis, sn string) error {
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"UPDATING"},
|
|
|
|
Target: []string{"ACTIVE"},
|
|
|
|
Refresh: streamStateRefreshFunc(conn, sn),
|
|
|
|
Timeout: 5 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := stateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for Kinesis Stream (%s) to become active: %s",
|
|
|
|
sn, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-01 19:46:39 +01:00
|
|
|
// See http://docs.aws.amazon.com/kinesis/latest/dev/kinesis-using-sdk-java-resharding-merge.html
|
|
|
|
func openShards(shards []*kinesis.Shard) []*kinesis.Shard {
|
|
|
|
var open []*kinesis.Shard
|
|
|
|
for _, s := range shards {
|
|
|
|
if s.SequenceNumberRange.EndingSequenceNumber == nil {
|
|
|
|
open = append(open, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return open
|
|
|
|
}
|