github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/resource_aws_emr_security_configuration.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/service/emr"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceAwsEMRSecurityConfiguration() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceAwsEmrSecurityConfigurationCreate,
    16  		Read:   resourceAwsEmrSecurityConfigurationRead,
    17  		Delete: resourceAwsEmrSecurityConfigurationDelete,
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": &schema.Schema{
    24  				Type:          schema.TypeString,
    25  				Optional:      true,
    26  				Computed:      true,
    27  				ForceNew:      true,
    28  				ConflictsWith: []string{"name_prefix"},
    29  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    30  					value := v.(string)
    31  					if len(value) > 10280 {
    32  						errors = append(errors, fmt.Errorf(
    33  							"%q cannot be longer than 10280 characters", k))
    34  					}
    35  					return
    36  				},
    37  			},
    38  			"name_prefix": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Optional: true,
    41  				ForceNew: true,
    42  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    43  					value := v.(string)
    44  					if len(value) > 10000 {
    45  						errors = append(errors, fmt.Errorf(
    46  							"%q cannot be longer than 10000 characters, name is limited to 10280", k))
    47  					}
    48  					return
    49  				},
    50  			},
    51  
    52  			"configuration": {
    53  				Type:         schema.TypeString,
    54  				Required:     true,
    55  				ForceNew:     true,
    56  				ValidateFunc: validateJsonString,
    57  			},
    58  
    59  			"creation_date": {
    60  				Type:     schema.TypeString,
    61  				Computed: true,
    62  			},
    63  		},
    64  	}
    65  }
    66  
    67  func resourceAwsEmrSecurityConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
    68  	conn := meta.(*AWSClient).emrconn
    69  
    70  	var emrSCName string
    71  	if v, ok := d.GetOk("name"); ok {
    72  		emrSCName = v.(string)
    73  	} else {
    74  		if v, ok := d.GetOk("name_prefix"); ok {
    75  			emrSCName = resource.PrefixedUniqueId(v.(string))
    76  		} else {
    77  			emrSCName = resource.PrefixedUniqueId("tf-emr-sc-")
    78  		}
    79  	}
    80  
    81  	resp, err := conn.CreateSecurityConfiguration(&emr.CreateSecurityConfigurationInput{
    82  		Name: aws.String(emrSCName),
    83  		SecurityConfiguration: aws.String(d.Get("configuration").(string)),
    84  	})
    85  
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	d.SetId(*resp.Name)
    91  	return resourceAwsEmrSecurityConfigurationRead(d, meta)
    92  }
    93  
    94  func resourceAwsEmrSecurityConfigurationRead(d *schema.ResourceData, meta interface{}) error {
    95  	conn := meta.(*AWSClient).emrconn
    96  
    97  	resp, err := conn.DescribeSecurityConfiguration(&emr.DescribeSecurityConfigurationInput{
    98  		Name: aws.String(d.Id()),
    99  	})
   100  	if err != nil {
   101  		if isAWSErr(err, "InvalidRequestException", "does not exist") {
   102  			log.Printf("[WARN] EMR Security Configuraiton (%s) not found, removing from state", d.Id())
   103  			d.SetId("")
   104  			return nil
   105  		}
   106  		return err
   107  	}
   108  
   109  	d.Set("creation_date", resp.CreationDateTime)
   110  	d.Set("name", resp.Name)
   111  	d.Set("configuration", resp.SecurityConfiguration)
   112  
   113  	return nil
   114  }
   115  
   116  func resourceAwsEmrSecurityConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
   117  	conn := meta.(*AWSClient).emrconn
   118  
   119  	_, err := conn.DeleteSecurityConfiguration(&emr.DeleteSecurityConfigurationInput{
   120  		Name: aws.String(d.Id()),
   121  	})
   122  	if err != nil {
   123  		if isAWSErr(err, "InvalidRequestException", "does not exist") {
   124  			d.SetId("")
   125  			return nil
   126  		}
   127  		return err
   128  	}
   129  	d.SetId("")
   130  
   131  	return nil
   132  }