forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_elastic_beanstalk_configuration_template.go
220 lines (176 loc) · 6.46 KB
/
resource_aws_elastic_beanstalk_configuration_template.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package aws
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
)
func resourceAwsElasticBeanstalkConfigurationTemplate() *schema.Resource {
return &schema.Resource{
Create: resourceAwsElasticBeanstalkConfigurationTemplateCreate,
Read: resourceAwsElasticBeanstalkConfigurationTemplateRead,
Update: resourceAwsElasticBeanstalkConfigurationTemplateUpdate,
Delete: resourceAwsElasticBeanstalkConfigurationTemplateDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"application": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"environment_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"setting": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: resourceAwsElasticBeanstalkOptionSetting(),
Set: optionSettingValueHash,
},
"solution_stack_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceAwsElasticBeanstalkConfigurationTemplateCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticbeanstalkconn
// Get the relevant properties
name := d.Get("name").(string)
appName := d.Get("application").(string)
optionSettings := gatherOptionSettings(d)
opts := elasticbeanstalk.CreateConfigurationTemplateInput{
ApplicationName: aws.String(appName),
TemplateName: aws.String(name),
OptionSettings: optionSettings,
}
if attr, ok := d.GetOk("description"); ok {
opts.Description = aws.String(attr.(string))
}
if attr, ok := d.GetOk("environment_id"); ok {
opts.EnvironmentId = aws.String(attr.(string))
}
if attr, ok := d.GetOk("solution_stack_name"); ok {
opts.SolutionStackName = aws.String(attr.(string))
}
log.Printf("[DEBUG] Elastic Beanstalk configuration template create opts: %s", opts)
if _, err := conn.CreateConfigurationTemplate(&opts); err != nil {
return fmt.Errorf("Error creating Elastic Beanstalk configuration template: %s", err)
}
d.SetId(name)
return resourceAwsElasticBeanstalkConfigurationTemplateRead(d, meta)
}
func resourceAwsElasticBeanstalkConfigurationTemplateRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticbeanstalkconn
log.Printf("[DEBUG] Elastic Beanstalk configuration template read: %s", d.Get("name").(string))
resp, err := conn.DescribeConfigurationSettings(&elasticbeanstalk.DescribeConfigurationSettingsInput{
TemplateName: aws.String(d.Id()),
ApplicationName: aws.String(d.Get("application").(string)),
})
if err != nil {
return err
}
// if len(resp.ConfigurationSettings) > 1 {
// settings := make(map[string]map[string]string)
// for _, setting := range resp.ConfigurationSettings {
// k := fmt.Sprintf("%s.%s", setting.)
// }
// }
if len(resp.ConfigurationSettings) != 1 {
log.Printf("[DEBUG] Elastic Beanstalk unexpected describe configuration template response: %+v", resp)
return fmt.Errorf("Error reading application properties: found %d applications, expected 1", len(resp.ConfigurationSettings))
}
d.Set("description", resp.ConfigurationSettings[0].Description)
return nil
}
func resourceAwsElasticBeanstalkConfigurationTemplateUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticbeanstalkconn
log.Printf("[DEBUG] Elastic Beanstalk configuration template update: %s", d.Get("name").(string))
if d.HasChange("description") {
if err := resourceAwsElasticBeanstalkConfigurationTemplateDescriptionUpdate(conn, d); err != nil {
return err
}
}
if d.HasChange("option_settings") {
if err := resourceAwsElasticBeanstalkConfigurationTemplateOptionSettingsUpdate(conn, d); err != nil {
return err
}
}
return resourceAwsElasticBeanstalkConfigurationTemplateRead(d, meta)
}
func resourceAwsElasticBeanstalkConfigurationTemplateDescriptionUpdate(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
_, err := conn.UpdateConfigurationTemplate(&elasticbeanstalk.UpdateConfigurationTemplateInput{
ApplicationName: aws.String(d.Get("application").(string)),
TemplateName: aws.String(d.Get("name").(string)),
Description: aws.String(d.Get("description").(string)),
})
return err
}
func resourceAwsElasticBeanstalkConfigurationTemplateOptionSettingsUpdate(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
if d.HasChange("option_settings") {
_, err := conn.ValidateConfigurationSettings(&elasticbeanstalk.ValidateConfigurationSettingsInput{
ApplicationName: aws.String(d.Get("application").(string)),
TemplateName: aws.String(d.Get("name").(string)),
OptionSettings: gatherOptionSettings(d),
})
if err != nil {
return err
}
o, n := d.GetChange("option_settings")
if o == nil {
o = new(schema.Set)
}
if n == nil {
n = new(schema.Set)
}
os := o.(*schema.Set)
ns := o.(*schema.Set)
remove := extractOptionSettings(os.Difference(ns))
add := extractOptionSettings(ns.Difference(os))
req := &elasticbeanstalk.UpdateConfigurationTemplateInput{
ApplicationName: aws.String(d.Get("application").(string)),
TemplateName: aws.String(d.Get("name").(string)),
OptionSettings: add,
}
for _, elem := range remove {
req.OptionsToRemove = append(req.OptionsToRemove, &elasticbeanstalk.OptionSpecification{
Namespace: elem.Namespace,
OptionName: elem.OptionName,
})
}
if _, err := conn.UpdateConfigurationTemplate(req); err != nil {
return err
}
}
return nil
}
func resourceAwsElasticBeanstalkConfigurationTemplateDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticbeanstalkconn
application := d.Get("application").(string)
_, err := conn.DeleteConfigurationTemplate(&elasticbeanstalk.DeleteConfigurationTemplateInput{
ApplicationName: aws.String(application),
TemplateName: aws.String(d.Id()),
})
return err
}
func gatherOptionSettings(d *schema.ResourceData) []*elasticbeanstalk.ConfigurationOptionSetting {
optionSettingsSet, ok := d.Get("option_settings").(*schema.Set)
if !ok || optionSettingsSet == nil {
optionSettingsSet = new(schema.Set)
}
return extractOptionSettings(optionSettingsSet)
}