github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  							Default:  "30",
   216  							Optional: true,
   217  							ForceNew: true,
   218  						},
   219  						"max_height": &schema.Schema{
   220  							Type:     schema.TypeString,
   221  							Optional: true,
   222  							ForceNew: true,
   223  						},
   224  						"max_width": &schema.Schema{
   225  							Type:     schema.TypeString,
   226  							Optional: true,
   227  							ForceNew: true,
   228  						},
   229  						"padding_policy": &schema.Schema{
   230  							Type:     schema.TypeString,
   231  							Optional: true,
   232  							ForceNew: true,
   233  						},
   234  						"resolution": &schema.Schema{
   235  							Type:     schema.TypeString,
   236  							Optional: true,
   237  							ForceNew: true,
   238  						},
   239  						"sizing_policy": &schema.Schema{
   240  							Type:     schema.TypeString,
   241  							Default:  "Fit",
   242  							Optional: true,
   243  							ForceNew: true,
   244  						},
   245  					},
   246  				},
   247  			},
   248  
   249  			"video_watermarks": &schema.Schema{
   250  				Type:     schema.TypeSet,
   251  				Optional: true,
   252  				ForceNew: true,
   253  				Elem: &schema.Resource{
   254  					// elastictranscoder.PresetWatermark
   255  					Schema: map[string]*schema.Schema{
   256  						"horizontal_align": &schema.Schema{
   257  							Type:     schema.TypeString,
   258  							Optional: true,
   259  							ForceNew: true,
   260  						},
   261  						"horizontal_offset": &schema.Schema{
   262  							Type:     schema.TypeString,
   263  							Optional: true,
   264  							ForceNew: true,
   265  						},
   266  						"id": &schema.Schema{
   267  							Type:     schema.TypeString,
   268  							Optional: true,
   269  							ForceNew: true,
   270  						},
   271  						"max_height": &schema.Schema{
   272  							Type:     schema.TypeString,
   273  							Optional: true,
   274  							ForceNew: true,
   275  						},
   276  						"max_width": &schema.Schema{
   277  							Type:     schema.TypeString,
   278  							Optional: true,
   279  							ForceNew: true,
   280  						},
   281  						"opacity": &schema.Schema{
   282  							Type:     schema.TypeString,
   283  							Optional: true,
   284  							ForceNew: true,
   285  						},
   286  						"sizing_policy": &schema.Schema{
   287  							Type:     schema.TypeString,
   288  							Optional: true,
   289  							ForceNew: true,
   290  						},
   291  						"target": &schema.Schema{
   292  							Type:     schema.TypeString,
   293  							Optional: true,
   294  							ForceNew: true,
   295  						},
   296  						"vertical_align": &schema.Schema{
   297  							Type:     schema.TypeString,
   298  							Optional: true,
   299  							ForceNew: true,
   300  						},
   301  						"vertical_offset": &schema.Schema{
   302  							Type:     schema.TypeString,
   303  							Optional: true,
   304  							ForceNew: true,
   305  						},
   306  					},
   307  				},
   308  			},
   309  
   310  			"video_codec_options": &schema.Schema{
   311  				Type:     schema.TypeMap,
   312  				Optional: true,
   313  				ForceNew: true,
   314  			},
   315  		},
   316  	}
   317  }
   318  
   319  func resourceAwsElasticTranscoderPresetCreate(d *schema.ResourceData, meta interface{}) error {
   320  	elastictranscoderconn := meta.(*AWSClient).elastictranscoderconn
   321  
   322  	req := &elastictranscoder.CreatePresetInput{
   323  		Audio:       expandETAudioParams(d),
   324  		Container:   aws.String(d.Get("container").(string)),
   325  		Description: getStringPtr(d, "description"),
   326  		Thumbnails:  expandETThumbnails(d),
   327  		Video:       exapandETVideoParams(d),
   328  	}
   329  
   330  	if name, ok := d.GetOk("name"); ok {
   331  		req.Name = aws.String(name.(string))
   332  	} else {
   333  		name := resource.PrefixedUniqueId("tf-et-preset-")
   334  		d.Set("name", name)
   335  		req.Name = aws.String(name)
   336  	}
   337  
   338  	log.Printf("[DEBUG] Elastic Transcoder Preset create opts: %s", req)
   339  	resp, err := elastictranscoderconn.CreatePreset(req)
   340  	if err != nil {
   341  		return fmt.Errorf("Error creating Elastic Transcoder Preset: %s", err)
   342  	}
   343  
   344  	if resp.Warning != nil && *resp.Warning != "" {
   345  		log.Printf("[WARN] Elastic Transcoder Preset: %s", *resp.Warning)
   346  	}
   347  
   348  	d.SetId(*resp.Preset.Id)
   349  	d.Set("arn", *resp.Preset.Arn)
   350  
   351  	return nil
   352  }
   353  
   354  func expandETThumbnails(d *schema.ResourceData) *elastictranscoder.Thumbnails {
   355  	set, ok := d.GetOk("thumbnails")
   356  	if !ok {
   357  		return nil
   358  	}
   359  
   360  	s := set.(*schema.Set)
   361  	if s == nil || s.Len() == 0 {
   362  		return nil
   363  	}
   364  	t := s.List()[0].(map[string]interface{})
   365  
   366  	return &elastictranscoder.Thumbnails{
   367  		AspectRatio:   getStringPtr(t, "aspect_ratio"),
   368  		Format:        getStringPtr(t, "format"),
   369  		Interval:      getStringPtr(t, "interval"),
   370  		MaxHeight:     getStringPtr(t, "max_height"),
   371  		MaxWidth:      getStringPtr(t, "max_width"),
   372  		PaddingPolicy: getStringPtr(t, "padding_policy"),
   373  		Resolution:    getStringPtr(t, "resolution"),
   374  		SizingPolicy:  getStringPtr(t, "sizing_policy"),
   375  	}
   376  }
   377  
   378  func expandETAudioParams(d *schema.ResourceData) *elastictranscoder.AudioParameters {
   379  	set, ok := d.GetOk("audio")
   380  	if !ok {
   381  		return nil
   382  	}
   383  
   384  	s := set.(*schema.Set)
   385  	if s == nil || s.Len() == 0 {
   386  		return nil
   387  	}
   388  	audio := s.List()[0].(map[string]interface{})
   389  
   390  	return &elastictranscoder.AudioParameters{
   391  		AudioPackingMode: getStringPtr(audio, "audio_packing_mode"),
   392  		BitRate:          getStringPtr(audio, "bit_rate"),
   393  		Channels:         getStringPtr(audio, "channels"),
   394  		Codec:            getStringPtr(audio, "codec"),
   395  		CodecOptions:     expandETAudioCodecOptions(d),
   396  		SampleRate:       getStringPtr(audio, "sample_rate"),
   397  	}
   398  }
   399  
   400  func expandETAudioCodecOptions(d *schema.ResourceData) *elastictranscoder.AudioCodecOptions {
   401  	s := d.Get("audio_codec_options").(*schema.Set)
   402  	if s == nil || s.Len() == 0 {
   403  		return nil
   404  	}
   405  
   406  	codec := s.List()[0].(map[string]interface{})
   407  
   408  	codecOpts := &elastictranscoder.AudioCodecOptions{
   409  		BitDepth: getStringPtr(codec, "bit_depth"),
   410  		BitOrder: getStringPtr(codec, "bit_order"),
   411  		Profile:  getStringPtr(codec, "profile"),
   412  		Signed:   getStringPtr(codec, "signed"),
   413  	}
   414  
   415  	return codecOpts
   416  }
   417  
   418  func exapandETVideoParams(d *schema.ResourceData) *elastictranscoder.VideoParameters {
   419  	s := d.Get("video").(*schema.Set)
   420  	if s == nil || s.Len() == 0 {
   421  		return nil
   422  	}
   423  	p := s.List()[0].(map[string]interface{})
   424  
   425  	return &elastictranscoder.VideoParameters{
   426  		AspectRatio:        getStringPtr(p, "aspect_ratio"),
   427  		BitRate:            getStringPtr(p, "bit_rate"),
   428  		Codec:              getStringPtr(p, "codec"),
   429  		CodecOptions:       stringMapToPointers(d.Get("video_codec_options").(map[string]interface{})),
   430  		DisplayAspectRatio: getStringPtr(p, "display_aspect_ratio"),
   431  		FixedGOP:           getStringPtr(p, "fixed_gop"),
   432  		FrameRate:          getStringPtr(p, "frame_rate"),
   433  		KeyframesMaxDist:   getStringPtr(p, "keyframes_max_dist"),
   434  		MaxFrameRate:       getStringPtr(p, "max_frame_rate"),
   435  		MaxHeight:          getStringPtr(p, "max_height"),
   436  		MaxWidth:           getStringPtr(p, "max_width"),
   437  		PaddingPolicy:      getStringPtr(p, "padding_policy"),
   438  		Resolution:         getStringPtr(p, "resolution"),
   439  		SizingPolicy:       getStringPtr(p, "sizing_policy"),
   440  		Watermarks:         expandETVideoWatermarks(d),
   441  	}
   442  }
   443  
   444  func expandETVideoWatermarks(d *schema.ResourceData) []*elastictranscoder.PresetWatermark {
   445  	s := d.Get("video_watermarks").(*schema.Set)
   446  	if s == nil || s.Len() == 0 {
   447  		return nil
   448  	}
   449  	var watermarks []*elastictranscoder.PresetWatermark
   450  
   451  	for _, w := range s.List() {
   452  		watermark := &elastictranscoder.PresetWatermark{
   453  			HorizontalAlign:  getStringPtr(w, "horizontal_align"),
   454  			HorizontalOffset: getStringPtr(w, "horizontal_offset"),
   455  			Id:               getStringPtr(w, "id"),
   456  			MaxHeight:        getStringPtr(w, "max_height"),
   457  			MaxWidth:         getStringPtr(w, "max_width"),
   458  			Opacity:          getStringPtr(w, "opacity"),
   459  			SizingPolicy:     getStringPtr(w, "sizing_policy"),
   460  			Target:           getStringPtr(w, "target"),
   461  			VerticalAlign:    getStringPtr(w, "vertical_align"),
   462  			VerticalOffset:   getStringPtr(w, "vertical_offset"),
   463  		}
   464  		watermarks = append(watermarks, watermark)
   465  	}
   466  
   467  	return watermarks
   468  }
   469  
   470  func resourceAwsElasticTranscoderPresetRead(d *schema.ResourceData, meta interface{}) error {
   471  	elastictranscoderconn := meta.(*AWSClient).elastictranscoderconn
   472  
   473  	resp, err := elastictranscoderconn.ReadPreset(&elastictranscoder.ReadPresetInput{
   474  		Id: aws.String(d.Id()),
   475  	})
   476  
   477  	if err != nil {
   478  		if err, ok := err.(awserr.Error); ok && err.Code() == "ResourceNotFoundException" {
   479  			d.SetId("")
   480  			return nil
   481  		}
   482  		return err
   483  	}
   484  
   485  	log.Printf("[DEBUG] Elastic Transcoder Preset Read response: %#v", resp)
   486  
   487  	preset := resp.Preset
   488  	d.Set("arn", *preset.Arn)
   489  
   490  	if preset.Audio != nil {
   491  		err := d.Set("audio", flattenETAudioParameters(preset.Audio))
   492  		if err != nil {
   493  			return err
   494  		}
   495  
   496  		if preset.Audio.CodecOptions != nil {
   497  			d.Set("audio_codec_options", flattenETAudioCodecOptions(preset.Audio.CodecOptions))
   498  		}
   499  	}
   500  
   501  	d.Set("container", *preset.Container)
   502  	d.Set("name", *preset.Name)
   503  
   504  	if preset.Thumbnails != nil {
   505  		err := d.Set("thumbnails", flattenETThumbnails(preset.Thumbnails))
   506  		if err != nil {
   507  			return err
   508  		}
   509  	}
   510  
   511  	d.Set("type", *preset.Type)
   512  
   513  	if preset.Video != nil {
   514  		err := d.Set("video", flattenETVideoParams(preset.Video))
   515  		if err != nil {
   516  			return err
   517  		}
   518  
   519  		if preset.Video.CodecOptions != nil {
   520  			d.Set("video_codec_options", flattenETVideoCodecOptions(preset.Video.CodecOptions))
   521  		}
   522  
   523  		if preset.Video.Watermarks != nil {
   524  			d.Set("video_watermarks", flattenETWatermarks(preset.Video.Watermarks))
   525  		}
   526  	}
   527  
   528  	return nil
   529  }
   530  
   531  func flattenETAudioParameters(audio *elastictranscoder.AudioParameters) []map[string]interface{} {
   532  	m := setMap(make(map[string]interface{}))
   533  
   534  	m.SetString("audio_packing_mode", audio.AudioPackingMode)
   535  	m.SetString("bit_rate", audio.BitRate)
   536  	m.SetString("channels", audio.Channels)
   537  	m.SetString("codec", audio.Codec)
   538  	m.SetString("sample_rate", audio.SampleRate)
   539  
   540  	return m.MapList()
   541  }
   542  
   543  func flattenETAudioCodecOptions(opts *elastictranscoder.AudioCodecOptions) []map[string]interface{} {
   544  	if opts == nil {
   545  		return nil
   546  	}
   547  
   548  	m := setMap(make(map[string]interface{}))
   549  
   550  	m.SetString("bit_depth", opts.BitDepth)
   551  	m.SetString("bit_order", opts.BitOrder)
   552  	m.SetString("profile", opts.Profile)
   553  	m.SetString("signed", opts.Signed)
   554  
   555  	return m.MapList()
   556  }
   557  
   558  func flattenETThumbnails(thumbs *elastictranscoder.Thumbnails) []map[string]interface{} {
   559  	m := setMap(make(map[string]interface{}))
   560  
   561  	m.SetString("aspect_ratio", thumbs.AspectRatio)
   562  	m.SetString("format", thumbs.Format)
   563  	m.SetString("interval", thumbs.Interval)
   564  	m.SetString("max_height", thumbs.MaxHeight)
   565  	m.SetString("max_width", thumbs.MaxWidth)
   566  	m.SetString("padding_policy", thumbs.PaddingPolicy)
   567  	m.SetString("resolution", thumbs.Resolution)
   568  	m.SetString("sizing_policy", thumbs.SizingPolicy)
   569  
   570  	return m.MapList()
   571  }
   572  
   573  func flattenETVideoParams(video *elastictranscoder.VideoParameters) []map[string]interface{} {
   574  	m := setMap(make(map[string]interface{}))
   575  
   576  	m.SetString("aspect_ratio", video.AspectRatio)
   577  	m.SetString("bit_rate", video.BitRate)
   578  	m.SetString("codec", video.Codec)
   579  	m.SetString("display_aspect_ratio", video.DisplayAspectRatio)
   580  	m.SetString("fixed_gop", video.FixedGOP)
   581  	m.SetString("frame_rate", video.FrameRate)
   582  	m.SetString("keyframes_max_dist", video.KeyframesMaxDist)
   583  	m.SetString("max_frame_rate", video.MaxFrameRate)
   584  	m.SetString("max_height", video.MaxHeight)
   585  	m.SetString("max_width", video.MaxWidth)
   586  	m.SetString("padding_policy", video.PaddingPolicy)
   587  	m.SetString("resolution", video.Resolution)
   588  	m.SetString("sizing_policy", video.SizingPolicy)
   589  
   590  	return m.MapList()
   591  }
   592  
   593  func flattenETVideoCodecOptions(opts map[string]*string) []map[string]interface{} {
   594  	codecOpts := setMap(make(map[string]interface{}))
   595  
   596  	for k, v := range opts {
   597  		codecOpts.SetString(k, v)
   598  	}
   599  
   600  	return codecOpts.MapList()
   601  }
   602  
   603  func flattenETWatermarks(watermarks []*elastictranscoder.PresetWatermark) []map[string]interface{} {
   604  	var watermarkSet []map[string]interface{}
   605  
   606  	for _, w := range watermarks {
   607  		watermark := setMap(make(map[string]interface{}))
   608  
   609  		watermark.SetString("horizontal_align", w.HorizontalAlign)
   610  		watermark.SetString("horizontal_offset", w.HorizontalOffset)
   611  		watermark.SetString("id", w.Id)
   612  		watermark.SetString("max_height", w.MaxHeight)
   613  		watermark.SetString("max_width", w.MaxWidth)
   614  		watermark.SetString("opacity", w.Opacity)
   615  		watermark.SetString("sizing_policy", w.SizingPolicy)
   616  		watermark.SetString("target", w.Target)
   617  		watermark.SetString("vertical_align", w.VerticalAlign)
   618  		watermark.SetString("vertical_offset", w.VerticalOffset)
   619  
   620  		watermarkSet = append(watermarkSet, watermark.Map())
   621  	}
   622  
   623  	return watermarkSet
   624  }
   625  
   626  func resourceAwsElasticTranscoderPresetDelete(d *schema.ResourceData, meta interface{}) error {
   627  	elastictranscoderconn := meta.(*AWSClient).elastictranscoderconn
   628  
   629  	log.Printf("[DEBUG] Elastic Transcoder Delete Preset: %s", d.Id())
   630  	_, err := elastictranscoderconn.DeletePreset(&elastictranscoder.DeletePresetInput{
   631  		Id: aws.String(d.Id()),
   632  	})
   633  
   634  	if err != nil {
   635  		return fmt.Errorf("error deleting Elastic Transcoder Preset: %s", err)
   636  	}
   637  
   638  	return nil
   639  }