github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/builtin/providers/aws/resource_aws_elastic_transcoder_pipeline.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     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/elastictranscoder"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsElasticTranscoderPipeline() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsElasticTranscoderPipelineCreate,
    18  		Read:   resourceAwsElasticTranscoderPipelineRead,
    19  		Update: resourceAwsElasticTranscoderPipelineUpdate,
    20  		Delete: resourceAwsElasticTranscoderPipelineDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"arn": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Computed: true,
    26  			},
    27  
    28  			"aws_kms_key_arn": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Optional: true,
    31  			},
    32  
    33  			// ContentConfig also requires ThumbnailConfig
    34  			"content_config": &schema.Schema{
    35  				Type:     schema.TypeSet,
    36  				Optional: true,
    37  				Computed: true,
    38  				MaxItems: 1,
    39  				Elem: &schema.Resource{
    40  					// elastictranscoder.PipelineOutputConfig
    41  					Schema: map[string]*schema.Schema{
    42  						"bucket": &schema.Schema{
    43  							Type:     schema.TypeString,
    44  							Optional: true,
    45  							// AWS may insert the bucket name here taken from output_bucket
    46  							Computed: true,
    47  						},
    48  						"storage_class": &schema.Schema{
    49  							Type:     schema.TypeString,
    50  							Optional: true,
    51  						},
    52  					},
    53  				},
    54  			},
    55  
    56  			"content_config_permissions": &schema.Schema{
    57  				Type:     schema.TypeSet,
    58  				Optional: true,
    59  				Elem: &schema.Resource{
    60  					Schema: map[string]*schema.Schema{
    61  						"access": &schema.Schema{
    62  							Type:     schema.TypeList,
    63  							Optional: true,
    64  							Elem:     &schema.Schema{Type: schema.TypeString},
    65  						},
    66  						"grantee": &schema.Schema{
    67  							Type:     schema.TypeString,
    68  							Optional: true,
    69  						},
    70  						"grantee_type": &schema.Schema{
    71  							Type:     schema.TypeString,
    72  							Optional: true,
    73  						},
    74  					},
    75  				},
    76  			},
    77  
    78  			"input_bucket": &schema.Schema{
    79  				Type:     schema.TypeString,
    80  				Required: true,
    81  			},
    82  
    83  			"name": &schema.Schema{
    84  				Type:     schema.TypeString,
    85  				Optional: true,
    86  				Computed: true,
    87  				ForceNew: true,
    88  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    89  					value := v.(string)
    90  					if !regexp.MustCompile(`^[.0-9A-Za-z-_]+$`).MatchString(value) {
    91  						errors = append(errors, fmt.Errorf(
    92  							"only alphanumeric characters, hyphens, underscores, and periods allowed in %q", k))
    93  					}
    94  					if len(value) > 40 {
    95  						errors = append(errors, fmt.Errorf("%q cannot be longer than 40 characters", k))
    96  					}
    97  					return
    98  				},
    99  			},
   100  
   101  			"notifications": &schema.Schema{
   102  				Type:     schema.TypeSet,
   103  				Optional: true,
   104  				MaxItems: 1,
   105  				Elem: &schema.Resource{
   106  					Schema: map[string]*schema.Schema{
   107  						"completed": &schema.Schema{
   108  							Type:     schema.TypeString,
   109  							Optional: true,
   110  						},
   111  						"error": &schema.Schema{
   112  							Type:     schema.TypeString,
   113  							Optional: true,
   114  						},
   115  						"progressing": &schema.Schema{
   116  							Type:     schema.TypeString,
   117  							Optional: true,
   118  						},
   119  						"warning": &schema.Schema{
   120  							Type:     schema.TypeString,
   121  							Optional: true,
   122  						},
   123  					},
   124  				},
   125  			},
   126  
   127  			// The output_bucket must be set, or both of content_config.bucket
   128  			// and thumbnail_config.bucket.
   129  			// This is set as Computed, because the API may or may not return
   130  			// this as set based on the other 2 configurations.
   131  			"output_bucket": &schema.Schema{
   132  				Type:     schema.TypeString,
   133  				Optional: true,
   134  				Computed: true,
   135  			},
   136  
   137  			"role": &schema.Schema{
   138  				Type:     schema.TypeString,
   139  				Required: true,
   140  			},
   141  
   142  			"thumbnail_config": &schema.Schema{
   143  				Type:     schema.TypeSet,
   144  				Optional: true,
   145  				Computed: true,
   146  				MaxItems: 1,
   147  				Elem: &schema.Resource{
   148  					// elastictranscoder.PipelineOutputConfig
   149  					Schema: map[string]*schema.Schema{
   150  						"bucket": &schema.Schema{
   151  							Type:     schema.TypeString,
   152  							Optional: true,
   153  							// AWS may insert the bucket name here taken from output_bucket
   154  							Computed: true,
   155  						},
   156  						"storage_class": &schema.Schema{
   157  							Type:     schema.TypeString,
   158  							Optional: true,
   159  						},
   160  					},
   161  				},
   162  			},
   163  
   164  			"thumbnail_config_permissions": &schema.Schema{
   165  				Type:     schema.TypeSet,
   166  				Optional: true,
   167  				Elem: &schema.Resource{
   168  					Schema: map[string]*schema.Schema{
   169  						"access": &schema.Schema{
   170  							Type:     schema.TypeList,
   171  							Optional: true,
   172  							Elem:     &schema.Schema{Type: schema.TypeString},
   173  						},
   174  						"grantee": &schema.Schema{
   175  							Type:     schema.TypeString,
   176  							Optional: true,
   177  						},
   178  						"grantee_type": &schema.Schema{
   179  							Type:     schema.TypeString,
   180  							Optional: true,
   181  						},
   182  					},
   183  				},
   184  			},
   185  		},
   186  	}
   187  }
   188  
   189  func resourceAwsElasticTranscoderPipelineCreate(d *schema.ResourceData, meta interface{}) error {
   190  	elastictranscoderconn := meta.(*AWSClient).elastictranscoderconn
   191  
   192  	req := &elastictranscoder.CreatePipelineInput{
   193  		AwsKmsKeyArn:    getStringPtr(d, "aws_kms_key_arn"),
   194  		ContentConfig:   expandETPiplineOutputConfig(d, "content_config"),
   195  		InputBucket:     aws.String(d.Get("input_bucket").(string)),
   196  		Notifications:   expandETNotifications(d),
   197  		OutputBucket:    getStringPtr(d, "output_bucket"),
   198  		Role:            getStringPtr(d, "role"),
   199  		ThumbnailConfig: expandETPiplineOutputConfig(d, "thumbnail_config"),
   200  	}
   201  
   202  	if name, ok := d.GetOk("name"); ok {
   203  		req.Name = aws.String(name.(string))
   204  	} else {
   205  		name := resource.PrefixedUniqueId("tf-et-")
   206  		d.Set("name", name)
   207  		req.Name = aws.String(name)
   208  	}
   209  
   210  	if (req.OutputBucket == nil && (req.ContentConfig == nil || req.ContentConfig.Bucket == nil)) ||
   211  		(req.OutputBucket != nil && req.ContentConfig != nil && req.ContentConfig.Bucket != nil) {
   212  		return fmt.Errorf("[ERROR] you must specify only one of output_bucket or content_config.bucket")
   213  	}
   214  
   215  	log.Printf("[DEBUG] Elastic Transcoder Pipeline create opts: %s", req)
   216  	resp, err := elastictranscoderconn.CreatePipeline(req)
   217  	if err != nil {
   218  		return fmt.Errorf("Error creating Elastic Transcoder Pipeline: %s", err)
   219  	}
   220  
   221  	d.SetId(*resp.Pipeline.Id)
   222  
   223  	for _, w := range resp.Warnings {
   224  		log.Printf("[WARN] Elastic Transcoder Pipeline %v: %v", *w.Code, *w.Message)
   225  	}
   226  
   227  	return resourceAwsElasticTranscoderPipelineRead(d, meta)
   228  }
   229  
   230  func expandETNotifications(d *schema.ResourceData) *elastictranscoder.Notifications {
   231  	set, ok := d.GetOk("notifications")
   232  	if !ok {
   233  		return nil
   234  	}
   235  
   236  	s := set.(*schema.Set)
   237  	if s == nil || s.Len() == 0 {
   238  		return nil
   239  	}
   240  
   241  	m := s.List()[0].(map[string]interface{})
   242  
   243  	return &elastictranscoder.Notifications{
   244  		Completed:   getStringPtr(m, "completed"),
   245  		Error:       getStringPtr(m, "error"),
   246  		Progressing: getStringPtr(m, "progressing"),
   247  		Warning:     getStringPtr(m, "warning"),
   248  	}
   249  }
   250  
   251  func flattenETNotifications(n *elastictranscoder.Notifications) []map[string]interface{} {
   252  	if n == nil {
   253  		return nil
   254  	}
   255  
   256  	allEmpty := func(s ...*string) bool {
   257  		for _, s := range s {
   258  			if s != nil && *s != "" {
   259  				return false
   260  			}
   261  		}
   262  		return true
   263  	}
   264  
   265  	// the API always returns a Notifications value, even when all fields are nil
   266  	if allEmpty(n.Completed, n.Error, n.Progressing, n.Warning) {
   267  		return nil
   268  	}
   269  
   270  	m := setMap(make(map[string]interface{}))
   271  
   272  	m.SetString("completed", n.Completed)
   273  	m.SetString("error", n.Error)
   274  	m.SetString("progressing", n.Progressing)
   275  	m.SetString("warning", n.Warning)
   276  	return m.MapList()
   277  }
   278  
   279  func expandETPiplineOutputConfig(d *schema.ResourceData, key string) *elastictranscoder.PipelineOutputConfig {
   280  	set, ok := d.GetOk(key)
   281  	if !ok {
   282  		return nil
   283  	}
   284  
   285  	s := set.(*schema.Set)
   286  	if s == nil || s.Len() == 0 {
   287  		return nil
   288  	}
   289  
   290  	cc := s.List()[0].(map[string]interface{})
   291  
   292  	cfg := &elastictranscoder.PipelineOutputConfig{
   293  		Bucket:       getStringPtr(cc, "bucket"),
   294  		StorageClass: getStringPtr(cc, "storage_class"),
   295  	}
   296  
   297  	switch key {
   298  	case "content_config":
   299  		cfg.Permissions = expandETPermList(d.Get("content_config_permissions").(*schema.Set))
   300  	case "thumbnail_config":
   301  		cfg.Permissions = expandETPermList(d.Get("thumbnail_config_permissions").(*schema.Set))
   302  	}
   303  
   304  	return cfg
   305  }
   306  
   307  func flattenETPipelineOutputConfig(cfg *elastictranscoder.PipelineOutputConfig) []map[string]interface{} {
   308  	m := setMap(make(map[string]interface{}))
   309  
   310  	m.SetString("bucket", cfg.Bucket)
   311  	m.SetString("storage_class", cfg.StorageClass)
   312  
   313  	return m.MapList()
   314  }
   315  
   316  func expandETPermList(permissions *schema.Set) []*elastictranscoder.Permission {
   317  	var perms []*elastictranscoder.Permission
   318  
   319  	for _, p := range permissions.List() {
   320  		perm := &elastictranscoder.Permission{
   321  			Access:      getStringPtrList(p.(map[string]interface{}), "access"),
   322  			Grantee:     getStringPtr(p, "grantee"),
   323  			GranteeType: getStringPtr(p, "grantee_type"),
   324  		}
   325  		perms = append(perms, perm)
   326  	}
   327  	return perms
   328  }
   329  
   330  func flattenETPermList(perms []*elastictranscoder.Permission) []map[string]interface{} {
   331  	var set []map[string]interface{}
   332  
   333  	for _, p := range perms {
   334  		m := setMap(make(map[string]interface{}))
   335  		m.Set("access", flattenStringList(p.Access))
   336  		m.SetString("grantee", p.Grantee)
   337  		m.SetString("grantee_type", p.GranteeType)
   338  
   339  		set = append(set, m)
   340  	}
   341  	return set
   342  }
   343  
   344  func resourceAwsElasticTranscoderPipelineUpdate(d *schema.ResourceData, meta interface{}) error {
   345  	elastictranscoderconn := meta.(*AWSClient).elastictranscoderconn
   346  
   347  	req := &elastictranscoder.UpdatePipelineInput{
   348  		Id: aws.String(d.Id()),
   349  	}
   350  
   351  	if d.HasChange("aws_kms_key_arn") {
   352  		req.AwsKmsKeyArn = getStringPtr(d, "aws_kms_key_arn")
   353  	}
   354  
   355  	if d.HasChange("content_config") {
   356  		req.ContentConfig = expandETPiplineOutputConfig(d, "content_config")
   357  	}
   358  
   359  	if d.HasChange("input_bucket") {
   360  		req.InputBucket = getStringPtr(d, "input_bucket")
   361  	}
   362  
   363  	if d.HasChange("name") {
   364  		req.Name = getStringPtr(d, "name")
   365  	}
   366  
   367  	if d.HasChange("notifications") {
   368  		req.Notifications = expandETNotifications(d)
   369  	}
   370  
   371  	if d.HasChange("role") {
   372  		req.Role = getStringPtr(d, "role")
   373  	}
   374  
   375  	if d.HasChange("thumbnail_config") {
   376  		req.ThumbnailConfig = expandETPiplineOutputConfig(d, "thumbnail_config")
   377  	}
   378  
   379  	log.Printf("[DEBUG] Updating Elastic Transcoder Pipeline: %#v", req)
   380  	output, err := elastictranscoderconn.UpdatePipeline(req)
   381  	if err != nil {
   382  		return fmt.Errorf("Error updating Elastic Transcoder pipeline: %s", err)
   383  	}
   384  
   385  	for _, w := range output.Warnings {
   386  		log.Printf("[WARN] Elastic Transcoder Pipeline %v: %v", *w.Code, *w.Message)
   387  	}
   388  
   389  	return resourceAwsElasticTranscoderPipelineRead(d, meta)
   390  }
   391  
   392  func resourceAwsElasticTranscoderPipelineRead(d *schema.ResourceData, meta interface{}) error {
   393  	elastictranscoderconn := meta.(*AWSClient).elastictranscoderconn
   394  
   395  	resp, err := elastictranscoderconn.ReadPipeline(&elastictranscoder.ReadPipelineInput{
   396  		Id: aws.String(d.Id()),
   397  	})
   398  
   399  	if err != nil {
   400  		if err, ok := err.(awserr.Error); ok && err.Code() == "ResourceNotFoundException" {
   401  			d.SetId("")
   402  			return nil
   403  		}
   404  		return err
   405  	}
   406  
   407  	log.Printf("[DEBUG] Elastic Transcoder Pipeline Read response: %#v", resp)
   408  
   409  	pipeline := resp.Pipeline
   410  
   411  	d.Set("arn", *pipeline.Arn)
   412  
   413  	if arn := pipeline.AwsKmsKeyArn; arn != nil {
   414  		d.Set("aws_kms_key_arn", *arn)
   415  	}
   416  
   417  	if pipeline.ContentConfig != nil {
   418  		err := d.Set("content_config", flattenETPipelineOutputConfig(pipeline.ContentConfig))
   419  		if err != nil {
   420  			return fmt.Errorf("error setting content_config: %s", err)
   421  		}
   422  
   423  		if pipeline.ContentConfig.Permissions != nil {
   424  			err := d.Set("content_config_permissions", flattenETPermList(pipeline.ContentConfig.Permissions))
   425  			if err != nil {
   426  				return fmt.Errorf("error setting content_config_permissions: %s", err)
   427  			}
   428  		}
   429  	}
   430  
   431  	d.Set("input_bucket", *pipeline.InputBucket)
   432  	d.Set("name", *pipeline.Name)
   433  
   434  	notifications := flattenETNotifications(pipeline.Notifications)
   435  	if notifications != nil {
   436  		if err := d.Set("notifications", notifications); err != nil {
   437  			return fmt.Errorf("error setting notifications: %s", err)
   438  		}
   439  	}
   440  
   441  	d.Set("role", *pipeline.Role)
   442  
   443  	if pipeline.ThumbnailConfig != nil {
   444  		err := d.Set("thumbnail_config", flattenETPipelineOutputConfig(pipeline.ThumbnailConfig))
   445  		if err != nil {
   446  			return fmt.Errorf("error setting thumbnail_config: %s", err)
   447  		}
   448  
   449  		if pipeline.ThumbnailConfig.Permissions != nil {
   450  			err := d.Set("thumbnail_config_permissions", flattenETPermList(pipeline.ThumbnailConfig.Permissions))
   451  			if err != nil {
   452  				return fmt.Errorf("error setting thumbnail_config_permissions: %s", err)
   453  			}
   454  		}
   455  	}
   456  
   457  	if pipeline.OutputBucket != nil {
   458  		d.Set("output_bucket", *pipeline.OutputBucket)
   459  	}
   460  
   461  	return nil
   462  }
   463  
   464  func resourceAwsElasticTranscoderPipelineDelete(d *schema.ResourceData, meta interface{}) error {
   465  	elastictranscoderconn := meta.(*AWSClient).elastictranscoderconn
   466  
   467  	log.Printf("[DEBUG] Elastic Transcoder Delete Pipeline: %s", d.Id())
   468  	_, err := elastictranscoderconn.DeletePipeline(&elastictranscoder.DeletePipelineInput{
   469  		Id: aws.String(d.Id()),
   470  	})
   471  	if err != nil {
   472  		return fmt.Errorf("error deleting Elastic Transcoder Pipeline: %s", err)
   473  	}
   474  	return nil
   475  }