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