github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_kinesis_firehose_delivery_stream.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "strings" 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/firehose" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/helper/schema" 13 ) 14 15 func resourceAwsKinesisFirehoseDeliveryStream() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceAwsKinesisFirehoseDeliveryStreamCreate, 18 Read: resourceAwsKinesisFirehoseDeliveryStreamRead, 19 Update: resourceAwsKinesisFirehoseDeliveryStreamUpdate, 20 Delete: resourceAwsKinesisFirehoseDeliveryStreamDelete, 21 22 Schema: map[string]*schema.Schema{ 23 "name": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 }, 28 29 "destination": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 ForceNew: true, 33 StateFunc: func(v interface{}) string { 34 value := v.(string) 35 return strings.ToLower(value) 36 }, 37 }, 38 39 "role_arn": &schema.Schema{ 40 Type: schema.TypeString, 41 Required: true, 42 }, 43 44 "s3_bucket_arn": &schema.Schema{ 45 Type: schema.TypeString, 46 Required: true, 47 }, 48 49 "s3_prefix": &schema.Schema{ 50 Type: schema.TypeString, 51 Optional: true, 52 }, 53 54 "s3_buffer_size": &schema.Schema{ 55 Type: schema.TypeInt, 56 Optional: true, 57 Default: 5, 58 }, 59 60 "s3_buffer_interval": &schema.Schema{ 61 Type: schema.TypeInt, 62 Optional: true, 63 Default: 300, 64 }, 65 66 "s3_data_compression": &schema.Schema{ 67 Type: schema.TypeString, 68 Optional: true, 69 Default: "UNCOMPRESSED", 70 }, 71 72 "arn": &schema.Schema{ 73 Type: schema.TypeString, 74 Optional: true, 75 Computed: true, 76 }, 77 78 "version_id": &schema.Schema{ 79 Type: schema.TypeString, 80 Optional: true, 81 Computed: true, 82 }, 83 84 "destination_id": &schema.Schema{ 85 Type: schema.TypeString, 86 Optional: true, 87 Computed: true, 88 }, 89 }, 90 } 91 } 92 93 func resourceAwsKinesisFirehoseDeliveryStreamCreate(d *schema.ResourceData, meta interface{}) error { 94 conn := meta.(*AWSClient).firehoseconn 95 96 if d.Get("destination").(string) != "s3" { 97 return fmt.Errorf("[ERROR] AWS Kinesis Firehose only supports S3 destinations for the first implementation") 98 } 99 100 sn := d.Get("name").(string) 101 input := &firehose.CreateDeliveryStreamInput{ 102 DeliveryStreamName: aws.String(sn), 103 } 104 105 s3_config := &firehose.S3DestinationConfiguration{ 106 BucketARN: aws.String(d.Get("s3_bucket_arn").(string)), 107 RoleARN: aws.String(d.Get("role_arn").(string)), 108 BufferingHints: &firehose.BufferingHints{ 109 IntervalInSeconds: aws.Int64(int64(d.Get("s3_buffer_interval").(int))), 110 SizeInMBs: aws.Int64(int64(d.Get("s3_buffer_size").(int))), 111 }, 112 CompressionFormat: aws.String(d.Get("s3_data_compression").(string)), 113 } 114 if v, ok := d.GetOk("s3_prefix"); ok { 115 s3_config.Prefix = aws.String(v.(string)) 116 } 117 118 input.S3DestinationConfiguration = s3_config 119 120 _, err := conn.CreateDeliveryStream(input) 121 if err != nil { 122 if awsErr, ok := err.(awserr.Error); ok { 123 return fmt.Errorf("[WARN] Error creating Kinesis Firehose Delivery Stream: \"%s\", code: \"%s\"", awsErr.Message(), awsErr.Code()) 124 } 125 return err 126 } 127 128 stateConf := &resource.StateChangeConf{ 129 Pending: []string{"CREATING"}, 130 Target: "ACTIVE", 131 Refresh: firehoseStreamStateRefreshFunc(conn, sn), 132 Timeout: 5 * time.Minute, 133 Delay: 10 * time.Second, 134 MinTimeout: 3 * time.Second, 135 } 136 137 firehoseStream, err := stateConf.WaitForState() 138 if err != nil { 139 return fmt.Errorf( 140 "Error waiting for Kinesis Stream (%s) to become active: %s", 141 sn, err) 142 } 143 144 s := firehoseStream.(*firehose.DeliveryStreamDescription) 145 d.SetId(*s.DeliveryStreamARN) 146 d.Set("arn", s.DeliveryStreamARN) 147 148 return resourceAwsKinesisFirehoseDeliveryStreamRead(d, meta) 149 } 150 151 func resourceAwsKinesisFirehoseDeliveryStreamUpdate(d *schema.ResourceData, meta interface{}) error { 152 conn := meta.(*AWSClient).firehoseconn 153 154 if d.Get("destination").(string) != "s3" { 155 return fmt.Errorf("[ERROR] AWS Kinesis Firehose only supports S3 destinations for the first implementation") 156 } 157 158 sn := d.Get("name").(string) 159 s3_config := &firehose.S3DestinationUpdate{} 160 161 if d.HasChange("role_arn") { 162 s3_config.RoleARN = aws.String(d.Get("role_arn").(string)) 163 } 164 165 if d.HasChange("s3_bucket_arn") { 166 s3_config.BucketARN = aws.String(d.Get("s3_bucket_arn").(string)) 167 } 168 169 if d.HasChange("s3_prefix") { 170 s3_config.Prefix = aws.String(d.Get("s3_prefix").(string)) 171 } 172 173 if d.HasChange("s3_data_compression") { 174 s3_config.CompressionFormat = aws.String(d.Get("s3_data_compression").(string)) 175 } 176 177 if d.HasChange("s3_buffer_interval") || d.HasChange("s3_buffer_size") { 178 bufferingHints := &firehose.BufferingHints{ 179 IntervalInSeconds: aws.Int64(int64(d.Get("s3_buffer_interval").(int))), 180 SizeInMBs: aws.Int64(int64(d.Get("s3_buffer_size").(int))), 181 } 182 s3_config.BufferingHints = bufferingHints 183 } 184 185 destOpts := &firehose.UpdateDestinationInput{ 186 DeliveryStreamName: aws.String(sn), 187 CurrentDeliveryStreamVersionId: aws.String(d.Get("version_id").(string)), 188 DestinationId: aws.String(d.Get("destination_id").(string)), 189 S3DestinationUpdate: s3_config, 190 } 191 192 _, err := conn.UpdateDestination(destOpts) 193 if err != nil { 194 return fmt.Errorf( 195 "Error Updating Kinesis Firehose Delivery Stream: \"%s\"\n%s", 196 sn, err) 197 } 198 199 return resourceAwsKinesisFirehoseDeliveryStreamRead(d, meta) 200 } 201 202 func resourceAwsKinesisFirehoseDeliveryStreamRead(d *schema.ResourceData, meta interface{}) error { 203 conn := meta.(*AWSClient).firehoseconn 204 sn := d.Get("name").(string) 205 describeOpts := &firehose.DescribeDeliveryStreamInput{ 206 DeliveryStreamName: aws.String(sn), 207 } 208 resp, err := conn.DescribeDeliveryStream(describeOpts) 209 if err != nil { 210 if awsErr, ok := err.(awserr.Error); ok { 211 if awsErr.Code() == "ResourceNotFoundException" { 212 d.SetId("") 213 return nil 214 } 215 return fmt.Errorf("[WARN] Error reading Kinesis Firehose Delivery Stream: \"%s\", code: \"%s\"", awsErr.Message(), awsErr.Code()) 216 } 217 return err 218 } 219 220 s := resp.DeliveryStreamDescription 221 d.Set("version_id", s.VersionId) 222 d.Set("arn", *s.DeliveryStreamARN) 223 if len(s.Destinations) > 0 { 224 destination := s.Destinations[0] 225 d.Set("destination_id", *destination.DestinationId) 226 } 227 228 return nil 229 } 230 231 func resourceAwsKinesisFirehoseDeliveryStreamDelete(d *schema.ResourceData, meta interface{}) error { 232 conn := meta.(*AWSClient).firehoseconn 233 234 sn := d.Get("name").(string) 235 _, err := conn.DeleteDeliveryStream(&firehose.DeleteDeliveryStreamInput{ 236 DeliveryStreamName: aws.String(sn), 237 }) 238 239 if err != nil { 240 return err 241 } 242 243 stateConf := &resource.StateChangeConf{ 244 Pending: []string{"DELETING"}, 245 Target: "DESTROYED", 246 Refresh: firehoseStreamStateRefreshFunc(conn, sn), 247 Timeout: 5 * time.Minute, 248 Delay: 10 * time.Second, 249 MinTimeout: 3 * time.Second, 250 } 251 252 _, err = stateConf.WaitForState() 253 if err != nil { 254 return fmt.Errorf( 255 "Error waiting for Delivery Stream (%s) to be destroyed: %s", 256 sn, err) 257 } 258 259 d.SetId("") 260 return nil 261 } 262 263 func firehoseStreamStateRefreshFunc(conn *firehose.Firehose, sn string) resource.StateRefreshFunc { 264 return func() (interface{}, string, error) { 265 describeOpts := &firehose.DescribeDeliveryStreamInput{ 266 DeliveryStreamName: aws.String(sn), 267 } 268 resp, err := conn.DescribeDeliveryStream(describeOpts) 269 if err != nil { 270 if awsErr, ok := err.(awserr.Error); ok { 271 if awsErr.Code() == "ResourceNotFoundException" { 272 return 42, "DESTROYED", nil 273 } 274 return nil, awsErr.Code(), err 275 } 276 return nil, "failed", err 277 } 278 279 return resp.DeliveryStreamDescription, *resp.DeliveryStreamDescription.DeliveryStreamStatus, nil 280 } 281 }