github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_elastic_transcoder_preset.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/elastictranscoder" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/helper/schema" 12 ) 13 14 func resourceAwsElasticTranscoderPreset() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceAwsElasticTranscoderPresetCreate, 17 Read: resourceAwsElasticTranscoderPresetRead, 18 Delete: resourceAwsElasticTranscoderPresetDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "arn": &schema.Schema{ 22 Type: schema.TypeString, 23 Computed: true, 24 }, 25 26 "audio": &schema.Schema{ 27 Type: schema.TypeSet, 28 Optional: true, 29 ForceNew: true, 30 MaxItems: 1, 31 Elem: &schema.Resource{ 32 // elastictranscoder.AudioParameters 33 Schema: map[string]*schema.Schema{ 34 "audio_packing_mode": &schema.Schema{ 35 Type: schema.TypeString, 36 Optional: true, 37 ForceNew: true, 38 }, 39 "bit_rate": &schema.Schema{ 40 Type: schema.TypeString, 41 Optional: true, 42 ForceNew: true, 43 }, 44 "channels": &schema.Schema{ 45 Type: schema.TypeString, 46 Optional: true, 47 ForceNew: true, 48 }, 49 "codec": &schema.Schema{ 50 Type: schema.TypeString, 51 Optional: true, 52 ForceNew: true, 53 }, 54 "sample_rate": &schema.Schema{ 55 Type: schema.TypeString, 56 Optional: true, 57 ForceNew: true, 58 }, 59 }, 60 }, 61 }, 62 "audio_codec_options": &schema.Schema{ 63 Type: schema.TypeSet, 64 MaxItems: 1, 65 Optional: true, 66 ForceNew: true, 67 Elem: &schema.Resource{ 68 Schema: map[string]*schema.Schema{ 69 "bit_depth": &schema.Schema{ 70 Type: schema.TypeString, 71 Optional: true, 72 ForceNew: true, 73 }, 74 "bit_order": &schema.Schema{ 75 Type: schema.TypeString, 76 Optional: true, 77 ForceNew: true, 78 }, 79 "profile": &schema.Schema{ 80 Type: schema.TypeString, 81 Optional: true, 82 ForceNew: true, 83 }, 84 "signed": &schema.Schema{ 85 Type: schema.TypeString, 86 Optional: true, 87 ForceNew: true, 88 }, 89 }, 90 }, 91 }, 92 93 "container": &schema.Schema{ 94 Type: schema.TypeString, 95 Required: true, 96 ForceNew: true, 97 }, 98 99 "description": &schema.Schema{ 100 Type: schema.TypeString, 101 Optional: true, 102 ForceNew: true, 103 }, 104 105 "name": &schema.Schema{ 106 Type: schema.TypeString, 107 Optional: true, 108 Computed: true, 109 ForceNew: true, 110 }, 111 112 "thumbnails": &schema.Schema{ 113 Type: schema.TypeSet, 114 MaxItems: 1, 115 Optional: true, 116 ForceNew: true, 117 Elem: &schema.Resource{ 118 // elastictranscoder.Thumbnails 119 Schema: map[string]*schema.Schema{ 120 "aspect_ratio": &schema.Schema{ 121 Type: schema.TypeString, 122 Optional: true, 123 ForceNew: true, 124 }, 125 "format": &schema.Schema{ 126 Type: schema.TypeString, 127 Optional: true, 128 ForceNew: true, 129 }, 130 "interval": &schema.Schema{ 131 Type: schema.TypeString, 132 Optional: true, 133 ForceNew: true, 134 }, 135 "max_height": &schema.Schema{ 136 Type: schema.TypeString, 137 Optional: true, 138 ForceNew: true, 139 }, 140 "max_width": &schema.Schema{ 141 Type: schema.TypeString, 142 Optional: true, 143 ForceNew: true, 144 }, 145 "padding_policy": &schema.Schema{ 146 Type: schema.TypeString, 147 Optional: true, 148 ForceNew: true, 149 }, 150 "resolution:": &schema.Schema{ 151 Type: schema.TypeString, 152 Optional: true, 153 ForceNew: true, 154 }, 155 "sizing_policy": &schema.Schema{ 156 Type: schema.TypeString, 157 Optional: true, 158 ForceNew: true, 159 }, 160 }, 161 }, 162 }, 163 164 "type": &schema.Schema{ 165 Type: schema.TypeString, 166 Optional: true, 167 Computed: true, 168 }, 169 170 "video": &schema.Schema{ 171 Type: schema.TypeSet, 172 Optional: true, 173 ForceNew: true, 174 MaxItems: 1, 175 Elem: &schema.Resource{ 176 // elastictranscoder.VideoParameters 177 Schema: map[string]*schema.Schema{ 178 "aspect_ratio": &schema.Schema{ 179 Type: schema.TypeString, 180 Optional: true, 181 ForceNew: true, 182 }, 183 "bit_rate": &schema.Schema{ 184 Type: schema.TypeString, 185 Optional: true, 186 ForceNew: true, 187 }, 188 "codec": &schema.Schema{ 189 Type: schema.TypeString, 190 Optional: true, 191 ForceNew: true, 192 }, 193 "display_aspect_ratio": &schema.Schema{ 194 Type: schema.TypeString, 195 Optional: true, 196 ForceNew: true, 197 }, 198 "fixed_gop": &schema.Schema{ 199 Type: schema.TypeString, 200 Optional: true, 201 ForceNew: true, 202 }, 203 "frame_rate": &schema.Schema{ 204 Type: schema.TypeString, 205 Optional: true, 206 ForceNew: true, 207 }, 208 "keyframes_max_dist": &schema.Schema{ 209 Type: schema.TypeString, 210 Optional: true, 211 ForceNew: true, 212 }, 213 "max_frame_rate": &schema.Schema{ 214 Type: schema.TypeString, 215 Optional: true, 216 ForceNew: true, 217 }, 218 "max_height": &schema.Schema{ 219 Type: schema.TypeString, 220 Optional: true, 221 ForceNew: true, 222 }, 223 "max_width": &schema.Schema{ 224 Type: schema.TypeString, 225 Optional: true, 226 ForceNew: true, 227 }, 228 "padding_policy": &schema.Schema{ 229 Type: schema.TypeString, 230 Optional: true, 231 ForceNew: true, 232 }, 233 "resolution": &schema.Schema{ 234 Type: schema.TypeString, 235 Optional: true, 236 ForceNew: true, 237 }, 238 "sizing_policy": &schema.Schema{ 239 Type: schema.TypeString, 240 Default: "Fit", 241 Optional: true, 242 ForceNew: true, 243 }, 244 }, 245 }, 246 }, 247 248 "video_watermarks": &schema.Schema{ 249 Type: schema.TypeSet, 250 Optional: true, 251 ForceNew: true, 252 Elem: &schema.Resource{ 253 // elastictranscoder.PresetWatermark 254 Schema: map[string]*schema.Schema{ 255 "horizontal_align": &schema.Schema{ 256 Type: schema.TypeString, 257 Optional: true, 258 ForceNew: true, 259 }, 260 "horizontal_offset": &schema.Schema{ 261 Type: schema.TypeString, 262 Optional: true, 263 ForceNew: true, 264 }, 265 "id": &schema.Schema{ 266 Type: schema.TypeString, 267 Optional: true, 268 ForceNew: true, 269 }, 270 "max_height": &schema.Schema{ 271 Type: schema.TypeString, 272 Optional: true, 273 ForceNew: true, 274 }, 275 "max_width": &schema.Schema{ 276 Type: schema.TypeString, 277 Optional: true, 278 ForceNew: true, 279 }, 280 "opacity": &schema.Schema{ 281 Type: schema.TypeString, 282 Optional: true, 283 ForceNew: true, 284 }, 285 "sizing_policy": &schema.Schema{ 286 Type: schema.TypeString, 287 Optional: true, 288 ForceNew: true, 289 }, 290 "target": &schema.Schema{ 291 Type: schema.TypeString, 292 Optional: true, 293 ForceNew: true, 294 }, 295 "vertical_align": &schema.Schema{ 296 Type: schema.TypeString, 297 Optional: true, 298 ForceNew: true, 299 }, 300 "vertical_offset": &schema.Schema{ 301 Type: schema.TypeString, 302 Optional: true, 303 ForceNew: true, 304 }, 305 }, 306 }, 307 }, 308 309 "video_codec_options": &schema.Schema{ 310 Type: schema.TypeMap, 311 Optional: true, 312 ForceNew: true, 313 }, 314 }, 315 } 316 } 317 318 func resourceAwsElasticTranscoderPresetCreate(d *schema.ResourceData, meta interface{}) error { 319 elastictranscoderconn := meta.(*AWSClient).elastictranscoderconn 320 321 req := &elastictranscoder.CreatePresetInput{ 322 Audio: expandETAudioParams(d), 323 Container: aws.String(d.Get("container").(string)), 324 Description: getStringPtr(d, "description"), 325 Thumbnails: expandETThumbnails(d), 326 Video: exapandETVideoParams(d), 327 } 328 329 if name, ok := d.GetOk("name"); ok { 330 req.Name = aws.String(name.(string)) 331 } else { 332 name := resource.PrefixedUniqueId("tf-et-preset-") 333 d.Set("name", name) 334 req.Name = aws.String(name) 335 } 336 337 log.Printf("[DEBUG] Elastic Transcoder Preset create opts: %s", req) 338 resp, err := elastictranscoderconn.CreatePreset(req) 339 if err != nil { 340 return fmt.Errorf("Error creating Elastic Transcoder Preset: %s", err) 341 } 342 343 if resp.Warning != nil && *resp.Warning != "" { 344 log.Printf("[WARN] Elastic Transcoder Preset: %s", *resp.Warning) 345 } 346 347 d.SetId(*resp.Preset.Id) 348 d.Set("arn", *resp.Preset.Arn) 349 350 return nil 351 } 352 353 func expandETThumbnails(d *schema.ResourceData) *elastictranscoder.Thumbnails { 354 set, ok := d.GetOk("thumbnails") 355 if !ok { 356 return nil 357 } 358 359 s := set.(*schema.Set) 360 if s == nil || s.Len() == 0 { 361 return nil 362 } 363 t := s.List()[0].(map[string]interface{}) 364 365 return &elastictranscoder.Thumbnails{ 366 AspectRatio: getStringPtr(t, "aspect_ratio"), 367 Format: getStringPtr(t, "format"), 368 Interval: getStringPtr(t, "interval"), 369 MaxHeight: getStringPtr(t, "max_height"), 370 MaxWidth: getStringPtr(t, "max_width"), 371 PaddingPolicy: getStringPtr(t, "padding_policy"), 372 Resolution: getStringPtr(t, "resolution"), 373 SizingPolicy: getStringPtr(t, "sizing_policy"), 374 } 375 } 376 377 func expandETAudioParams(d *schema.ResourceData) *elastictranscoder.AudioParameters { 378 set, ok := d.GetOk("audio") 379 if !ok { 380 return nil 381 } 382 383 s := set.(*schema.Set) 384 if s == nil || s.Len() == 0 { 385 return nil 386 } 387 audio := s.List()[0].(map[string]interface{}) 388 389 return &elastictranscoder.AudioParameters{ 390 AudioPackingMode: getStringPtr(audio, "audio_packing_mode"), 391 BitRate: getStringPtr(audio, "bit_rate"), 392 Channels: getStringPtr(audio, "channels"), 393 Codec: getStringPtr(audio, "codec"), 394 CodecOptions: expandETAudioCodecOptions(d), 395 SampleRate: getStringPtr(audio, "sample_rate"), 396 } 397 } 398 399 func expandETAudioCodecOptions(d *schema.ResourceData) *elastictranscoder.AudioCodecOptions { 400 s := d.Get("audio_codec_options").(*schema.Set) 401 if s == nil || s.Len() == 0 { 402 return nil 403 } 404 405 codec := s.List()[0].(map[string]interface{}) 406 407 codecOpts := &elastictranscoder.AudioCodecOptions{ 408 BitDepth: getStringPtr(codec, "bit_depth"), 409 BitOrder: getStringPtr(codec, "bit_order"), 410 Profile: getStringPtr(codec, "profile"), 411 Signed: getStringPtr(codec, "signed"), 412 } 413 414 return codecOpts 415 } 416 417 func exapandETVideoParams(d *schema.ResourceData) *elastictranscoder.VideoParameters { 418 s := d.Get("video").(*schema.Set) 419 if s == nil || s.Len() == 0 { 420 return nil 421 } 422 p := s.List()[0].(map[string]interface{}) 423 424 return &elastictranscoder.VideoParameters{ 425 AspectRatio: getStringPtr(p, "aspect_ratio"), 426 BitRate: getStringPtr(p, "bit_rate"), 427 Codec: getStringPtr(p, "codec"), 428 CodecOptions: stringMapToPointers(d.Get("video_codec_options").(map[string]interface{})), 429 DisplayAspectRatio: getStringPtr(p, "display_aspect_ratio"), 430 FixedGOP: getStringPtr(p, "fixed_gop"), 431 FrameRate: getStringPtr(p, "frame_rate"), 432 KeyframesMaxDist: getStringPtr(p, "keyframes_max_dist"), 433 MaxFrameRate: getStringPtr(p, "max_frame_rate"), 434 MaxHeight: getStringPtr(p, "max_height"), 435 MaxWidth: getStringPtr(p, "max_width"), 436 PaddingPolicy: getStringPtr(p, "padding_policy"), 437 Resolution: getStringPtr(p, "resolution"), 438 SizingPolicy: getStringPtr(p, "sizing_policy"), 439 Watermarks: expandETVideoWatermarks(d), 440 } 441 } 442 443 func expandETVideoWatermarks(d *schema.ResourceData) []*elastictranscoder.PresetWatermark { 444 s := d.Get("video_watermarks").(*schema.Set) 445 if s == nil || s.Len() == 0 { 446 return nil 447 } 448 var watermarks []*elastictranscoder.PresetWatermark 449 450 for _, w := range s.List() { 451 watermark := &elastictranscoder.PresetWatermark{ 452 HorizontalAlign: getStringPtr(w, "horizontal_align"), 453 HorizontalOffset: getStringPtr(w, "horizontal_offset"), 454 Id: getStringPtr(w, "id"), 455 MaxHeight: getStringPtr(w, "max_height"), 456 MaxWidth: getStringPtr(w, "max_width"), 457 Opacity: getStringPtr(w, "opacity"), 458 SizingPolicy: getStringPtr(w, "sizing_policy"), 459 Target: getStringPtr(w, "target"), 460 VerticalAlign: getStringPtr(w, "vertical_align"), 461 VerticalOffset: getStringPtr(w, "vertical_offset"), 462 } 463 watermarks = append(watermarks, watermark) 464 } 465 466 return watermarks 467 } 468 469 func resourceAwsElasticTranscoderPresetRead(d *schema.ResourceData, meta interface{}) error { 470 elastictranscoderconn := meta.(*AWSClient).elastictranscoderconn 471 472 resp, err := elastictranscoderconn.ReadPreset(&elastictranscoder.ReadPresetInput{ 473 Id: aws.String(d.Id()), 474 }) 475 476 if err != nil { 477 if err, ok := err.(awserr.Error); ok && err.Code() == "ResourceNotFoundException" { 478 d.SetId("") 479 return nil 480 } 481 return err 482 } 483 484 log.Printf("[DEBUG] Elastic Transcoder Preset Read response: %#v", resp) 485 486 preset := resp.Preset 487 d.Set("arn", *preset.Arn) 488 489 if preset.Audio != nil { 490 err := d.Set("audio", flattenETAudioParameters(preset.Audio)) 491 if err != nil { 492 return err 493 } 494 495 if preset.Audio.CodecOptions != nil { 496 d.Set("audio_codec_options", flattenETAudioCodecOptions(preset.Audio.CodecOptions)) 497 } 498 } 499 500 d.Set("container", *preset.Container) 501 d.Set("name", *preset.Name) 502 503 if preset.Thumbnails != nil { 504 err := d.Set("thumbnails", flattenETThumbnails(preset.Thumbnails)) 505 if err != nil { 506 return err 507 } 508 } 509 510 d.Set("type", *preset.Type) 511 512 if preset.Video != nil { 513 err := d.Set("video", flattenETVideoParams(preset.Video)) 514 if err != nil { 515 return err 516 } 517 518 if preset.Video.CodecOptions != nil { 519 d.Set("video_codec_options", flattenETVideoCodecOptions(preset.Video.CodecOptions)) 520 } 521 522 if preset.Video.Watermarks != nil { 523 d.Set("video_watermarks", flattenETWatermarks(preset.Video.Watermarks)) 524 } 525 } 526 527 return nil 528 } 529 530 func flattenETAudioParameters(audio *elastictranscoder.AudioParameters) []map[string]interface{} { 531 m := setMap(make(map[string]interface{})) 532 533 m.SetString("audio_packing_mode", audio.AudioPackingMode) 534 m.SetString("bit_rate", audio.BitRate) 535 m.SetString("channels", audio.Channels) 536 m.SetString("codec", audio.Codec) 537 m.SetString("sample_rate", audio.SampleRate) 538 539 return m.MapList() 540 } 541 542 func flattenETAudioCodecOptions(opts *elastictranscoder.AudioCodecOptions) []map[string]interface{} { 543 if opts == nil { 544 return nil 545 } 546 547 m := setMap(make(map[string]interface{})) 548 549 m.SetString("bit_depth", opts.BitDepth) 550 m.SetString("bit_order", opts.BitOrder) 551 m.SetString("profile", opts.Profile) 552 m.SetString("signed", opts.Signed) 553 554 return m.MapList() 555 } 556 557 func flattenETThumbnails(thumbs *elastictranscoder.Thumbnails) []map[string]interface{} { 558 m := setMap(make(map[string]interface{})) 559 560 m.SetString("aspect_ratio", thumbs.AspectRatio) 561 m.SetString("format", thumbs.Format) 562 m.SetString("interval", thumbs.Interval) 563 m.SetString("max_height", thumbs.MaxHeight) 564 m.SetString("max_width", thumbs.MaxWidth) 565 m.SetString("padding_policy", thumbs.PaddingPolicy) 566 m.SetString("resolution", thumbs.Resolution) 567 m.SetString("sizing_policy", thumbs.SizingPolicy) 568 569 return m.MapList() 570 } 571 572 func flattenETVideoParams(video *elastictranscoder.VideoParameters) []map[string]interface{} { 573 m := setMap(make(map[string]interface{})) 574 575 m.SetString("aspect_ratio", video.AspectRatio) 576 m.SetString("bit_rate", video.BitRate) 577 m.SetString("codec", video.Codec) 578 m.SetString("display_aspect_ratio", video.DisplayAspectRatio) 579 m.SetString("fixed_gop", video.FixedGOP) 580 m.SetString("frame_rate", video.FrameRate) 581 m.SetString("keyframes_max_dist", video.KeyframesMaxDist) 582 m.SetString("max_frame_rate", video.MaxFrameRate) 583 m.SetString("max_height", video.MaxHeight) 584 m.SetString("max_width", video.MaxWidth) 585 m.SetString("padding_policy", video.PaddingPolicy) 586 m.SetString("resolution", video.Resolution) 587 m.SetString("sizing_policy", video.SizingPolicy) 588 589 return m.MapList() 590 } 591 592 func flattenETVideoCodecOptions(opts map[string]*string) []map[string]interface{} { 593 codecOpts := setMap(make(map[string]interface{})) 594 595 for k, v := range opts { 596 codecOpts.SetString(k, v) 597 } 598 599 return codecOpts.MapList() 600 } 601 602 func flattenETWatermarks(watermarks []*elastictranscoder.PresetWatermark) []map[string]interface{} { 603 var watermarkSet []map[string]interface{} 604 605 for _, w := range watermarks { 606 watermark := setMap(make(map[string]interface{})) 607 608 watermark.SetString("horizontal_align", w.HorizontalAlign) 609 watermark.SetString("horizontal_offset", w.HorizontalOffset) 610 watermark.SetString("id", w.Id) 611 watermark.SetString("max_height", w.MaxHeight) 612 watermark.SetString("max_width", w.MaxWidth) 613 watermark.SetString("opacity", w.Opacity) 614 watermark.SetString("sizing_policy", w.SizingPolicy) 615 watermark.SetString("target", w.Target) 616 watermark.SetString("vertical_align", w.VerticalAlign) 617 watermark.SetString("vertical_offset", w.VerticalOffset) 618 619 watermarkSet = append(watermarkSet, watermark.Map()) 620 } 621 622 return watermarkSet 623 } 624 625 func resourceAwsElasticTranscoderPresetDelete(d *schema.ResourceData, meta interface{}) error { 626 elastictranscoderconn := meta.(*AWSClient).elastictranscoderconn 627 628 log.Printf("[DEBUG] Elastic Transcoder Delete Preset: %s", d.Id()) 629 _, err := elastictranscoderconn.DeletePreset(&elastictranscoder.DeletePresetInput{ 630 Id: aws.String(d.Id()), 631 }) 632 633 if err != nil { 634 return fmt.Errorf("error deleting Elastic Transcoder Preset: %s", err) 635 } 636 637 return nil 638 }