github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/aws/resource_aws_elastic_beanstalk_application.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/aws/awserr"
    12  	"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
    13  	"github.com/hashicorp/terraform/helper/resource"
    14  )
    15  
    16  func resourceAwsElasticBeanstalkApplication() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceAwsElasticBeanstalkApplicationCreate,
    19  		Read:   resourceAwsElasticBeanstalkApplicationRead,
    20  		Update: resourceAwsElasticBeanstalkApplicationUpdate,
    21  		Delete: resourceAwsElasticBeanstalkApplicationDelete,
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"name": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  			"description": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Optional: true,
    32  				ForceNew: false,
    33  			},
    34  		},
    35  	}
    36  }
    37  
    38  func resourceAwsElasticBeanstalkApplicationCreate(d *schema.ResourceData, meta interface{}) error {
    39  	beanstalkConn := meta.(*AWSClient).elasticbeanstalkconn
    40  
    41  	// Get the name and description
    42  	name := d.Get("name").(string)
    43  	description := d.Get("description").(string)
    44  
    45  	log.Printf("[DEBUG] Elastic Beanstalk application create: %s, description: %s", name, description)
    46  
    47  	req := &elasticbeanstalk.CreateApplicationInput{
    48  		ApplicationName: aws.String(name),
    49  		Description:     aws.String(description),
    50  	}
    51  
    52  	_, err := beanstalkConn.CreateApplication(req)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	d.SetId(name)
    58  
    59  	return resourceAwsElasticBeanstalkApplicationRead(d, meta)
    60  }
    61  
    62  func resourceAwsElasticBeanstalkApplicationUpdate(d *schema.ResourceData, meta interface{}) error {
    63  	beanstalkConn := meta.(*AWSClient).elasticbeanstalkconn
    64  
    65  	if d.HasChange("description") {
    66  		if err := resourceAwsElasticBeanstalkApplicationDescriptionUpdate(beanstalkConn, d); err != nil {
    67  			return err
    68  		}
    69  	}
    70  
    71  	return resourceAwsElasticBeanstalkApplicationRead(d, meta)
    72  }
    73  
    74  func resourceAwsElasticBeanstalkApplicationDescriptionUpdate(beanstalkConn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
    75  	name := d.Get("name").(string)
    76  	description := d.Get("description").(string)
    77  
    78  	log.Printf("[DEBUG] Elastic Beanstalk application: %s, update description: %s", name, description)
    79  
    80  	_, err := beanstalkConn.UpdateApplication(&elasticbeanstalk.UpdateApplicationInput{
    81  		ApplicationName: aws.String(name),
    82  		Description:     aws.String(description),
    83  	})
    84  
    85  	return err
    86  }
    87  
    88  func resourceAwsElasticBeanstalkApplicationRead(d *schema.ResourceData, meta interface{}) error {
    89  	a, err := getBeanstalkApplication(d, meta)
    90  	if err != nil {
    91  		return err
    92  	}
    93  	if a == nil {
    94  		return err
    95  	}
    96  
    97  	d.Set("description", a.Description)
    98  	return nil
    99  }
   100  
   101  func resourceAwsElasticBeanstalkApplicationDelete(d *schema.ResourceData, meta interface{}) error {
   102  	beanstalkConn := meta.(*AWSClient).elasticbeanstalkconn
   103  
   104  	a, err := getBeanstalkApplication(d, meta)
   105  	if err != nil {
   106  		return err
   107  	}
   108  	_, err = beanstalkConn.DeleteApplication(&elasticbeanstalk.DeleteApplicationInput{
   109  		ApplicationName: aws.String(d.Id()),
   110  	})
   111  
   112  	return resource.Retry(10*time.Second, func() *resource.RetryError {
   113  		if a, _ = getBeanstalkApplication(d, meta); a != nil {
   114  			return resource.RetryableError(
   115  				fmt.Errorf("Beanstalk Application still exists"))
   116  		}
   117  		return nil
   118  	})
   119  }
   120  
   121  func getBeanstalkApplication(
   122  	d *schema.ResourceData,
   123  	meta interface{}) (*elasticbeanstalk.ApplicationDescription, error) {
   124  	conn := meta.(*AWSClient).elasticbeanstalkconn
   125  
   126  	resp, err := conn.DescribeApplications(&elasticbeanstalk.DescribeApplicationsInput{
   127  		ApplicationNames: []*string{aws.String(d.Id())},
   128  	})
   129  
   130  	if err != nil {
   131  		if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() != "InvalidBeanstalkAppID.NotFound" {
   132  			log.Printf("[Err] Error reading Elastic Beanstalk Application (%s): Application not found", d.Id())
   133  			d.SetId("")
   134  			return nil, nil
   135  		}
   136  		return nil, err
   137  	}
   138  
   139  	switch {
   140  	case len(resp.Applications) > 1:
   141  		return nil, fmt.Errorf("Error %d Applications matched, expected 1", len(resp.Applications))
   142  	case len(resp.Applications) == 0:
   143  		d.SetId("")
   144  		return nil, nil
   145  	default:
   146  		return resp.Applications[0], nil
   147  	}
   148  }