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