github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/resource_aws_elastic_beanstalk_application_version.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/aws/awserr"
    11  	"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
    12  	"time"
    13  )
    14  
    15  func resourceAwsElasticBeanstalkApplicationVersion() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsElasticBeanstalkApplicationVersionCreate,
    18  		Read:   resourceAwsElasticBeanstalkApplicationVersionRead,
    19  		Update: resourceAwsElasticBeanstalkApplicationVersionUpdate,
    20  		Delete: resourceAwsElasticBeanstalkApplicationVersionDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"application": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  			"description": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Optional: true,
    31  			},
    32  			"bucket": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  			},
    37  			"key": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Required: true,
    40  				ForceNew: true,
    41  			},
    42  			"name": &schema.Schema{
    43  				Type:     schema.TypeString,
    44  				Required: true,
    45  				ForceNew: true,
    46  			},
    47  			"force_delete": &schema.Schema{
    48  				Type:     schema.TypeBool,
    49  				Optional: true,
    50  				Default:  false,
    51  			},
    52  		},
    53  	}
    54  }
    55  
    56  func resourceAwsElasticBeanstalkApplicationVersionCreate(d *schema.ResourceData, meta interface{}) error {
    57  	conn := meta.(*AWSClient).elasticbeanstalkconn
    58  
    59  	application := d.Get("application").(string)
    60  	description := d.Get("description").(string)
    61  	bucket := d.Get("bucket").(string)
    62  	key := d.Get("key").(string)
    63  	name := d.Get("name").(string)
    64  
    65  	s3Location := elasticbeanstalk.S3Location{
    66  		S3Bucket: aws.String(bucket),
    67  		S3Key:    aws.String(key),
    68  	}
    69  
    70  	createOpts := elasticbeanstalk.CreateApplicationVersionInput{
    71  		ApplicationName: aws.String(application),
    72  		Description:     aws.String(description),
    73  		SourceBundle:    &s3Location,
    74  		VersionLabel:    aws.String(name),
    75  	}
    76  
    77  	log.Printf("[DEBUG] Elastic Beanstalk Application Version create opts: %s", createOpts)
    78  	_, err := conn.CreateApplicationVersion(&createOpts)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	d.SetId(name)
    84  	log.Printf("[INFO] Elastic Beanstalk Application Version Label: %s", name)
    85  
    86  	return resourceAwsElasticBeanstalkApplicationVersionRead(d, meta)
    87  }
    88  
    89  func resourceAwsElasticBeanstalkApplicationVersionRead(d *schema.ResourceData, meta interface{}) error {
    90  	conn := meta.(*AWSClient).elasticbeanstalkconn
    91  
    92  	resp, err := conn.DescribeApplicationVersions(&elasticbeanstalk.DescribeApplicationVersionsInput{
    93  		VersionLabels: []*string{aws.String(d.Id())},
    94  	})
    95  
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	if len(resp.ApplicationVersions) == 0 {
   101  		log.Printf("[DEBUG] Elastic Beanstalk application version read: application version not found")
   102  
   103  		d.SetId("")
   104  
   105  		return nil
   106  	} else if len(resp.ApplicationVersions) != 1 {
   107  		return fmt.Errorf("Error reading application version properties: found %d application versions, expected 1", len(resp.ApplicationVersions))
   108  	}
   109  
   110  	if err := d.Set("description", resp.ApplicationVersions[0].Description); err != nil {
   111  		return err
   112  	}
   113  
   114  	return nil
   115  }
   116  
   117  func resourceAwsElasticBeanstalkApplicationVersionUpdate(d *schema.ResourceData, meta interface{}) error {
   118  	conn := meta.(*AWSClient).elasticbeanstalkconn
   119  
   120  	if d.HasChange("description") {
   121  		if err := resourceAwsElasticBeanstalkApplicationVersionDescriptionUpdate(conn, d); err != nil {
   122  			return err
   123  		}
   124  	}
   125  
   126  	return resourceAwsElasticBeanstalkApplicationVersionRead(d, meta)
   127  
   128  }
   129  
   130  func resourceAwsElasticBeanstalkApplicationVersionDelete(d *schema.ResourceData, meta interface{}) error {
   131  	conn := meta.(*AWSClient).elasticbeanstalkconn
   132  
   133  	application := d.Get("application").(string)
   134  	name := d.Id()
   135  
   136  	if d.Get("force_delete").(bool) == false {
   137  		environments, err := versionUsedBy(application, name, conn)
   138  		if err != nil {
   139  			return err
   140  		}
   141  
   142  		if len(environments) > 1 {
   143  			return fmt.Errorf("Unable to delete Application Version, it is currently in use by the following environments: %s.", environments)
   144  		}
   145  	}
   146  	_, err := conn.DeleteApplicationVersion(&elasticbeanstalk.DeleteApplicationVersionInput{
   147  		ApplicationName:    aws.String(application),
   148  		VersionLabel:       aws.String(name),
   149  		DeleteSourceBundle: aws.Bool(false),
   150  	})
   151  
   152  	if err != nil {
   153  		if awserr, ok := err.(awserr.Error); ok {
   154  			// application version is pending delete, or no longer exists.
   155  			if awserr.Code() == "InvalidParameterValue" {
   156  				d.SetId("")
   157  				return nil
   158  			}
   159  		}
   160  		return err
   161  	}
   162  
   163  	d.SetId("")
   164  	return nil
   165  }
   166  
   167  func resourceAwsElasticBeanstalkApplicationVersionDescriptionUpdate(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
   168  	application := d.Get("application").(string)
   169  	description := d.Get("description").(string)
   170  	name := d.Get("name").(string)
   171  
   172  	log.Printf("[DEBUG] Elastic Beanstalk application version: %s, update description: %s", name, description)
   173  
   174  	_, err := conn.UpdateApplicationVersion(&elasticbeanstalk.UpdateApplicationVersionInput{
   175  		ApplicationName: aws.String(application),
   176  		Description:     aws.String(description),
   177  		VersionLabel:    aws.String(name),
   178  	})
   179  
   180  	return err
   181  }
   182  
   183  func versionUsedBy(applicationName, versionLabel string, conn *elasticbeanstalk.ElasticBeanstalk) ([]string, error) {
   184  	now := time.Now()
   185  	resp, err := conn.DescribeEnvironments(&elasticbeanstalk.DescribeEnvironmentsInput{
   186  		ApplicationName:       aws.String(applicationName),
   187  		VersionLabel:          aws.String(versionLabel),
   188  		IncludeDeleted:        aws.Bool(true),
   189  		IncludedDeletedBackTo: aws.Time(now.Add(-1 * time.Minute)),
   190  	})
   191  
   192  	if err != nil {
   193  		return nil, err
   194  	}
   195  
   196  	var environmentIDs []string
   197  	for _, environment := range resp.Environments {
   198  		environmentIDs = append(environmentIDs, *environment.EnvironmentId)
   199  	}
   200  
   201  	return environmentIDs, nil
   202  }