forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_opsworks_application.go
631 lines (565 loc) · 18.3 KB
/
resource_aws_opsworks_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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
package aws
import (
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/opsworks"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsOpsworksApplication() *schema.Resource {
return &schema.Resource{
Create: resourceAwsOpsworksApplicationCreate,
Read: resourceAwsOpsworksApplicationRead,
Update: resourceAwsOpsworksApplicationUpdate,
Delete: resourceAwsOpsworksApplicationDelete,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"short_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Optional: true,
},
// aws-flow-ruby | java | rails | php | nodejs | static | other
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
expected := [7]string{"aws-flow-ruby", "java", "rails", "php", "nodejs", "static", "other"}
found := false
for _, b := range expected {
if b == value {
found = true
}
}
if !found {
errors = append(errors, fmt.Errorf(
"%q has to be one of [aws-flow-ruby, java, rails, php, nodejs, static, other]", k))
}
return
},
},
"stack_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
// TODO: the following 4 vals are really part of the Attributes array. We should validate that only ones relevant to the chosen type are set, perhaps. (what is the default type? how do they map?)
"document_root": &schema.Schema{
Type: schema.TypeString,
Optional: true,
//Default: "public",
},
"rails_env": &schema.Schema{
Type: schema.TypeString,
Optional: true,
//Default: "production",
},
"auto_bundle_on_deploy": &schema.Schema{
Type: schema.TypeString,
Optional: true,
//Default: true,
},
"aws_flow_ruby_settings": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"app_source": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"username": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"password": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"revision": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"ssh_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
},
// AutoSelectOpsworksMysqlInstance, OpsworksMysqlInstance, or RdsDbInstance.
// anything beside auto select will lead into failure in case the instance doesn't exist
// XXX: validation?
"data_source_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"data_source_database_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"data_source_arn": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"domains": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"environment": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"value": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"secure": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
},
},
},
},
"enable_ssl": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"ssl_configuration": &schema.Schema{
Type: schema.TypeList,
Optional: true,
//Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"certificate": &schema.Schema{
Type: schema.TypeString,
Required: true,
StateFunc: func(v interface{}) string {
switch v.(type) {
case string:
return strings.TrimSpace(v.(string))
default:
return ""
}
},
},
"private_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
StateFunc: func(v interface{}) string {
switch v.(type) {
case string:
return strings.TrimSpace(v.(string))
default:
return ""
}
},
},
"chain": &schema.Schema{
Type: schema.TypeString,
Optional: true,
StateFunc: func(v interface{}) string {
switch v.(type) {
case string:
return strings.TrimSpace(v.(string))
default:
return ""
}
},
},
},
},
},
},
}
}
func resourceAwsOpsworksApplicationValidate(d *schema.ResourceData) error {
appSourceCount := d.Get("app_source.#").(int)
if appSourceCount > 1 {
return fmt.Errorf("Only one app_source is permitted.")
}
sslCount := d.Get("ssl_configuration.#").(int)
if sslCount > 1 {
return fmt.Errorf("Only one ssl_configuration is permitted.")
}
if d.Get("type") == opsworks.AppTypeNodejs || d.Get("type") == opsworks.AppTypeJava {
// allowed attributes: none
if d.Get("document_root").(string) != "" || d.Get("rails_env").(string) != "" || d.Get("auto_bundle_on_deploy").(string) != "" || d.Get("aws_flow_ruby_settings").(string) != "" {
return fmt.Errorf("No additional attributes are allowed for app type '%s'.", d.Get("type").(string))
}
} else if d.Get("type") == opsworks.AppTypeRails {
// allowed attributes: document_root, rails_env, auto_bundle_on_deploy
if d.Get("aws_flow_ruby_settings").(string) != "" {
return fmt.Errorf("Only 'document_root, rails_env, auto_bundle_on_deploy' are allowed for app type '%s'.", opsworks.AppTypeRails)
}
// rails_env is required
if _, ok := d.GetOk("rails_env"); !ok {
return fmt.Errorf("Set rails_env must be set if type is set to rails.")
}
} else if d.Get("type") == opsworks.AppTypePhp || d.Get("type") == opsworks.AppTypeStatic || d.Get("type") == opsworks.AppTypeOther {
log.Printf("[DEBUG] the app type is : %s", d.Get("type").(string))
log.Printf("[DEBUG] the attributes are: document_root '%s', rails_env '%s', auto_bundle_on_deploy '%s', aws_flow_ruby_settings '%s'", d.Get("document_root").(string), d.Get("rails_env").(string), d.Get("auto_bundle_on_deploy").(string), d.Get("aws_flow_ruby_settings").(string))
// allowed attributes: document_root
if d.Get("rails_env").(string) != "" || d.Get("auto_bundle_on_deploy").(string) != "" || d.Get("aws_flow_ruby_settings").(string) != "" {
return fmt.Errorf("Only 'document_root' is allowed for app type '%s'.", d.Get("type").(string))
}
} else if d.Get("type") == opsworks.AppTypeAwsFlowRuby {
// allowed attributes: aws_flow_ruby_settings
if d.Get("document_root").(string) != "" || d.Get("rails_env").(string) != "" || d.Get("auto_bundle_on_deploy").(string) != "" {
return fmt.Errorf("Only 'aws_flow_ruby_settings' is allowed for app type '%s'.", d.Get("type").(string))
}
}
return nil
}
func resourceAwsOpsworksApplicationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AWSClient).opsworksconn
req := &opsworks.DescribeAppsInput{
AppIds: []*string{
aws.String(d.Id()),
},
}
log.Printf("[DEBUG] Reading OpsWorks app: %s", d.Id())
resp, err := client.DescribeApps(req)
if err != nil {
if awserr, ok := err.(awserr.Error); ok {
if awserr.Code() == "ResourceNotFoundException" {
log.Printf("[INFO] App not found: %s", d.Id())
d.SetId("")
return nil
}
}
return err
}
app := resp.Apps[0]
d.Set("name", app.Name)
d.Set("stack_id", app.StackId)
d.Set("type", app.Type)
d.Set("description", app.Description)
d.Set("domains", flattenStringList(app.Domains))
d.Set("enable_ssl", app.EnableSsl)
resourceAwsOpsworksSetApplicationSsl(d, app.SslConfiguration)
resourceAwsOpsworksSetApplicationSource(d, app.AppSource)
resourceAwsOpsworksSetApplicationDataSources(d, app.DataSources)
resourceAwsOpsworksSetApplicationEnvironmentVariable(d, app.Environment)
resourceAwsOpsworksSetApplicationAttributes(d, app.Attributes)
return nil
}
func resourceAwsOpsworksApplicationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AWSClient).opsworksconn
err := resourceAwsOpsworksApplicationValidate(d)
if err != nil {
return err
}
req := &opsworks.CreateAppInput{
Name: aws.String(d.Get("name").(string)),
Shortname: aws.String(d.Get("short_name").(string)),
StackId: aws.String(d.Get("stack_id").(string)),
Type: aws.String(d.Get("type").(string)),
Description: aws.String(d.Get("description").(string)),
Domains: expandStringList(d.Get("domains").([]interface{})),
EnableSsl: aws.Bool(d.Get("enable_ssl").(bool)),
SslConfiguration: resourceAwsOpsworksApplicationSsl(d),
AppSource: resourceAwsOpsworksApplicationSource(d),
DataSources: resourceAwsOpsworksApplicationDataSources(d),
Environment: resourceAwsOpsworksApplicationEnvironmentVariable(d),
Attributes: resourceAwsOpsworksApplicationAttributes(d),
}
var resp *opsworks.CreateAppOutput
err = resource.Retry(2*time.Minute, func() *resource.RetryError {
var cerr error
resp, cerr = client.CreateApp(req)
if cerr != nil {
log.Printf("[INFO] client error")
if opserr, ok := cerr.(awserr.Error); ok {
// XXX: handle errors
log.Printf("[ERROR] OpsWorks error: %s message: %s", opserr.Code(), opserr.Message())
return resource.RetryableError(cerr)
}
return resource.NonRetryableError(cerr)
}
return nil
})
if err != nil {
return err
}
appID := *resp.AppId
d.SetId(appID)
d.Set("id", appID)
return resourceAwsOpsworksApplicationRead(d, meta)
}
func resourceAwsOpsworksApplicationUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AWSClient).opsworksconn
err := resourceAwsOpsworksApplicationValidate(d)
if err != nil {
return err
}
req := &opsworks.UpdateAppInput{
AppId: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
Type: aws.String(d.Get("type").(string)),
Description: aws.String(d.Get("description").(string)),
Domains: expandStringList(d.Get("domains").([]interface{})),
EnableSsl: aws.Bool(d.Get("enable_ssl").(bool)),
SslConfiguration: resourceAwsOpsworksApplicationSsl(d),
AppSource: resourceAwsOpsworksApplicationSource(d),
DataSources: resourceAwsOpsworksApplicationDataSources(d),
Environment: resourceAwsOpsworksApplicationEnvironmentVariable(d),
Attributes: resourceAwsOpsworksApplicationAttributes(d),
}
log.Printf("[DEBUG] Updating OpsWorks layer: %s", d.Id())
err = resource.Retry(2*time.Minute, func() *resource.RetryError {
_, cerr := client.UpdateApp(req)
if cerr != nil {
log.Printf("[INFO] client error")
if opserr, ok := cerr.(awserr.Error); ok {
// XXX: handle errors
log.Printf("[ERROR] OpsWorks error: %s message: %s", opserr.Code(), opserr.Message())
return resource.NonRetryableError(cerr)
}
return resource.RetryableError(cerr)
}
return nil
})
if err != nil {
return err
}
return resourceAwsOpsworksApplicationRead(d, meta)
}
func resourceAwsOpsworksApplicationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AWSClient).opsworksconn
req := &opsworks.DeleteAppInput{
AppId: aws.String(d.Id()),
}
log.Printf("[DEBUG] Deleting OpsWorks application: %s", d.Id())
_, err := client.DeleteApp(req)
return err
}
func resourceAwsOpsworksSetApplicationEnvironmentVariable(d *schema.ResourceData, v []*opsworks.EnvironmentVariable) {
log.Printf("[DEBUG] envs: %s %d", v, len(v))
if len(v) == 0 {
d.Set("environment", nil)
return
}
newValue := make([]*map[string]interface{}, len(v))
for i := 0; i < len(v); i++ {
config := v[i]
data := make(map[string]interface{})
newValue[i] = &data
if config.Key != nil {
data["key"] = *config.Key
}
if config.Value != nil {
data["value"] = *config.Value
}
if config.Secure != nil {
if bool(*config.Secure) {
data["secure"] = &opsworksTrueString
} else {
data["secure"] = &opsworksFalseString
}
}
log.Printf("[DEBUG] v: %s", data)
}
d.Set("environment", newValue)
}
func resourceAwsOpsworksApplicationEnvironmentVariable(d *schema.ResourceData) []*opsworks.EnvironmentVariable {
environmentVariables := d.Get("environment").(*schema.Set).List()
result := make([]*opsworks.EnvironmentVariable, len(environmentVariables))
for i := 0; i < len(environmentVariables); i++ {
env := environmentVariables[i].(map[string]interface{})
result[i] = &opsworks.EnvironmentVariable{
Key: aws.String(env["key"].(string)),
Value: aws.String(env["value"].(string)),
Secure: aws.Bool(env["secure"].(bool)),
}
}
return result
}
func resourceAwsOpsworksApplicationSource(d *schema.ResourceData) *opsworks.Source {
count := d.Get("app_source.#").(int)
if count == 0 {
return nil
}
return &opsworks.Source{
Type: aws.String(d.Get("app_source.0.type").(string)),
Url: aws.String(d.Get("app_source.0.url").(string)),
Username: aws.String(d.Get("app_source.0.username").(string)),
Password: aws.String(d.Get("app_source.0.password").(string)),
Revision: aws.String(d.Get("app_source.0.revision").(string)),
SshKey: aws.String(d.Get("app_source.0.ssh_key").(string)),
}
}
func resourceAwsOpsworksSetApplicationSource(d *schema.ResourceData, v *opsworks.Source) {
nv := make([]interface{}, 0, 1)
if v != nil {
m := make(map[string]interface{})
if v.Type != nil {
m["type"] = *v.Type
}
if v.Url != nil {
m["url"] = *v.Url
}
if v.Username != nil {
m["username"] = *v.Username
}
if v.Password != nil {
m["password"] = *v.Password
}
if v.Revision != nil {
m["revision"] = *v.Revision
}
nv = append(nv, m)
}
err := d.Set("app_source", nv)
if err != nil {
// should never happen
panic(err)
}
}
func resourceAwsOpsworksApplicationDataSources(d *schema.ResourceData) []*opsworks.DataSource {
arn := d.Get("data_source_arn").(string)
databaseName := d.Get("data_source_database_name").(string)
databaseType := d.Get("data_source_type").(string)
result := make([]*opsworks.DataSource, 1)
if len(arn) > 0 || len(databaseName) > 0 || len(databaseType) > 0 {
result[0] = &opsworks.DataSource{
Arn: aws.String(arn),
DatabaseName: aws.String(databaseName),
Type: aws.String(databaseType),
}
}
return result
}
func resourceAwsOpsworksSetApplicationDataSources(d *schema.ResourceData, v []*opsworks.DataSource) {
d.Set("data_source_arn", nil)
d.Set("data_source_database_name", nil)
d.Set("data_source_type", nil)
if len(v) == 0 {
return
}
d.Set("data_source_arn", v[0].Arn)
d.Set("data_source_database_name", v[0].DatabaseName)
d.Set("data_source_type", v[0].Type)
}
func resourceAwsOpsworksApplicationSsl(d *schema.ResourceData) *opsworks.SslConfiguration {
count := d.Get("ssl_configuration.#").(int)
if count == 0 {
return nil
}
return &opsworks.SslConfiguration{
PrivateKey: aws.String(d.Get("ssl_configuration.0.private_key").(string)),
Certificate: aws.String(d.Get("ssl_configuration.0.certificate").(string)),
Chain: aws.String(d.Get("ssl_configuration.0.chain").(string)),
}
}
func resourceAwsOpsworksSetApplicationSsl(d *schema.ResourceData, v *opsworks.SslConfiguration) {
nv := make([]interface{}, 0, 1)
set := false
if v != nil {
m := make(map[string]interface{})
if v.PrivateKey != nil {
m["private_key"] = *v.PrivateKey
set = true
}
if v.Certificate != nil {
m["certificate"] = *v.Certificate
set = true
}
if v.Chain != nil {
m["chain"] = *v.Chain
set = true
}
if set {
nv = append(nv, m)
}
}
err := d.Set("ssl_configuration", nv)
if err != nil {
// should never happen
panic(err)
}
}
func resourceAwsOpsworksApplicationAttributes(d *schema.ResourceData) map[string]*string {
attributes := make(map[string]*string)
if val := d.Get("document_root").(string); len(val) > 0 {
attributes[opsworks.AppAttributesKeysDocumentRoot] = aws.String(val)
}
if val := d.Get("aws_flow_ruby_settings").(string); len(val) > 0 {
attributes[opsworks.AppAttributesKeysAwsFlowRubySettings] = aws.String(val)
}
if val := d.Get("rails_env").(string); len(val) > 0 {
attributes[opsworks.AppAttributesKeysRailsEnv] = aws.String(val)
}
if val := d.Get("auto_bundle_on_deploy").(string); len(val) > 0 {
if val == "1" {
val = "true"
} else if val == "0" {
val = "false"
}
attributes[opsworks.AppAttributesKeysAutoBundleOnDeploy] = aws.String(val)
}
return attributes
}
func resourceAwsOpsworksSetApplicationAttributes(d *schema.ResourceData, v map[string]*string) {
d.Set("document_root", nil)
d.Set("rails_env", nil)
d.Set("aws_flow_ruby_settings", nil)
d.Set("auto_bundle_on_deploy", nil)
if d.Get("type") == opsworks.AppTypeNodejs || d.Get("type") == opsworks.AppTypeJava {
return
} else if d.Get("type") == opsworks.AppTypeRails {
if val, ok := v[opsworks.AppAttributesKeysDocumentRoot]; ok {
d.Set("document_root", val)
}
if val, ok := v[opsworks.AppAttributesKeysRailsEnv]; ok {
d.Set("rails_env", val)
}
if val, ok := v[opsworks.AppAttributesKeysAutoBundleOnDeploy]; ok {
d.Set("auto_bundle_on_deploy", val)
}
return
} else if d.Get("type") == opsworks.AppTypePhp || d.Get("type") == opsworks.AppTypeStatic || d.Get("type") == opsworks.AppTypeOther {
if val, ok := v[opsworks.AppAttributesKeysDocumentRoot]; ok {
d.Set("document_root", val)
}
return
} else if d.Get("type") == opsworks.AppTypeAwsFlowRuby {
if val, ok := v[opsworks.AppAttributesKeysAwsFlowRubySettings]; ok {
d.Set("aws_flow_ruby_settings", val)
}
return
}
return
}