forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_heroku_app.go
452 lines (380 loc) · 10.2 KB
/
resource_heroku_app.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
package heroku
import (
"context"
"fmt"
"log"
"github.com/cyberdelia/heroku-go/v3"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform/helper/schema"
)
// herokuApplication is a value type used to hold the details of an
// application. We use this for common storage of values needed for the
// heroku.App and heroku.OrganizationApp types
type herokuApplication struct {
Name string
Region string
Stack string
GitURL string
WebURL string
OrganizationName string
Locked bool
}
// type application is used to store all the details of a heroku app
type application struct {
Id string // Id of the resource
App *herokuApplication // The heroku application
Client *heroku.Service // Client to interact with the heroku API
Vars map[string]string // The vars on the application
Organization bool // is the application organization app
}
// Updates the application to have the latest from remote
func (a *application) Update() error {
var errs []error
var err error
if !a.Organization {
app, err := a.Client.AppInfo(context.TODO(), a.Id)
if err != nil {
errs = append(errs, err)
} else {
a.App = &herokuApplication{}
a.App.Name = app.Name
a.App.Region = app.Region.Name
a.App.Stack = app.Stack.Name
a.App.GitURL = app.GitURL
a.App.WebURL = app.WebURL
}
} else {
app, err := a.Client.OrganizationAppInfo(context.TODO(), a.Id)
if err != nil {
errs = append(errs, err)
} else {
// No inheritance between OrganizationApp and App is killing it :/
a.App = &herokuApplication{}
a.App.Name = app.Name
a.App.Region = app.Region.Name
a.App.Stack = app.Stack.Name
a.App.GitURL = app.GitURL
a.App.WebURL = app.WebURL
if app.Organization != nil {
a.App.OrganizationName = app.Organization.Name
} else {
log.Println("[DEBUG] Something is wrong - didn't get information about organization name, while the app is marked as being so")
}
a.App.Locked = app.Locked
}
}
a.Vars, err = retrieveConfigVars(a.Id, a.Client)
if err != nil {
errs = append(errs, err)
}
if len(errs) > 0 {
return &multierror.Error{Errors: errs}
}
return nil
}
func resourceHerokuApp() *schema.Resource {
return &schema.Resource{
Create: switchHerokuAppCreate,
Read: resourceHerokuAppRead,
Update: resourceHerokuAppUpdate,
Delete: resourceHerokuAppDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"region": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"stack": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"config_vars": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeMap,
},
},
"all_config_vars": {
Type: schema.TypeMap,
Computed: true,
},
"git_url": {
Type: schema.TypeString,
Computed: true,
},
"web_url": {
Type: schema.TypeString,
Computed: true,
},
"heroku_hostname": {
Type: schema.TypeString,
Computed: true,
},
"organization": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"locked": {
Type: schema.TypeBool,
Optional: true,
},
"personal": {
Type: schema.TypeBool,
Optional: true,
},
},
},
},
},
}
}
func isOrganizationApp(d *schema.ResourceData) bool {
v := d.Get("organization").([]interface{})
return len(v) > 0 && v[0] != nil
}
func switchHerokuAppCreate(d *schema.ResourceData, meta interface{}) error {
if isOrganizationApp(d) {
return resourceHerokuOrgAppCreate(d, meta)
}
return resourceHerokuAppCreate(d, meta)
}
func resourceHerokuAppCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
// Build up our creation options
opts := heroku.AppCreateOpts{}
if v, ok := d.GetOk("name"); ok {
vs := v.(string)
log.Printf("[DEBUG] App name: %s", vs)
opts.Name = &vs
}
if v, ok := d.GetOk("region"); ok {
vs := v.(string)
log.Printf("[DEBUG] App region: %s", vs)
opts.Region = &vs
}
if v, ok := d.GetOk("stack"); ok {
vs := v.(string)
log.Printf("[DEBUG] App stack: %s", vs)
opts.Stack = &vs
}
log.Printf("[DEBUG] Creating Heroku app...")
a, err := client.AppCreate(context.TODO(), opts)
if err != nil {
return err
}
d.SetId(a.Name)
log.Printf("[INFO] App ID: %s", d.Id())
if v, ok := d.GetOk("config_vars"); ok {
err = updateConfigVars(d.Id(), client, nil, v.([]interface{}))
if err != nil {
return err
}
}
return resourceHerokuAppRead(d, meta)
}
func resourceHerokuOrgAppCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
// Build up our creation options
opts := heroku.OrganizationAppCreateOpts{}
v := d.Get("organization").([]interface{})
if len(v) > 1 {
return fmt.Errorf("Error Creating Heroku App: Only 1 Heroku Organization is permitted")
}
orgDetails := v[0].(map[string]interface{})
if v := orgDetails["name"]; v != nil {
vs := v.(string)
log.Printf("[DEBUG] Organization name: %s", vs)
opts.Organization = &vs
}
if v := orgDetails["personal"]; v != nil {
vs := v.(bool)
log.Printf("[DEBUG] Organization Personal: %t", vs)
opts.Personal = &vs
}
if v := orgDetails["locked"]; v != nil {
vs := v.(bool)
log.Printf("[DEBUG] Organization locked: %t", vs)
opts.Locked = &vs
}
if v := d.Get("name"); v != nil {
vs := v.(string)
log.Printf("[DEBUG] App name: %s", vs)
opts.Name = &vs
}
if v, ok := d.GetOk("region"); ok {
vs := v.(string)
log.Printf("[DEBUG] App region: %s", vs)
opts.Region = &vs
}
if v, ok := d.GetOk("stack"); ok {
vs := v.(string)
log.Printf("[DEBUG] App stack: %s", vs)
opts.Stack = &vs
}
log.Printf("[DEBUG] Creating Heroku app...")
a, err := client.OrganizationAppCreate(context.TODO(), opts)
if err != nil {
return err
}
d.SetId(a.Name)
log.Printf("[INFO] App ID: %s", d.Id())
if v, ok := d.GetOk("config_vars"); ok {
err = updateConfigVars(d.Id(), client, nil, v.([]interface{}))
if err != nil {
return err
}
}
return resourceHerokuAppRead(d, meta)
}
func resourceHerokuAppRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
configVars := make(map[string]string)
care := make(map[string]struct{})
for _, v := range d.Get("config_vars").([]interface{}) {
for k := range v.(map[string]interface{}) {
care[k] = struct{}{}
}
}
organizationApp := isOrganizationApp(d)
// Only set the config_vars that we have set in the configuration.
// The "all_config_vars" field has all of them.
app, err := resourceHerokuAppRetrieve(d.Id(), organizationApp, client)
if err != nil {
return err
}
for k, v := range app.Vars {
if _, ok := care[k]; ok {
configVars[k] = v
}
}
var configVarsValue []map[string]string
if len(configVars) > 0 {
configVarsValue = []map[string]string{configVars}
}
d.Set("name", app.App.Name)
d.Set("stack", app.App.Stack)
d.Set("region", app.App.Region)
d.Set("git_url", app.App.GitURL)
d.Set("web_url", app.App.WebURL)
d.Set("config_vars", configVarsValue)
d.Set("all_config_vars", app.Vars)
if organizationApp {
orgDetails := map[string]interface{}{
"name": app.App.OrganizationName,
"locked": app.App.Locked,
"personal": false,
}
err := d.Set("organization", []interface{}{orgDetails})
if err != nil {
return err
}
}
// We know that the hostname on heroku will be the name+herokuapp.com
// You need this to do things like create DNS CNAME records
d.Set("heroku_hostname", fmt.Sprintf("%s.herokuapp.com", app.App.Name))
return nil
}
func resourceHerokuAppUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
// If name changed, update it
if d.HasChange("name") {
v := d.Get("name").(string)
opts := heroku.AppUpdateOpts{
Name: &v,
}
renamedApp, err := client.AppUpdate(context.TODO(), d.Id(), opts)
if err != nil {
return err
}
// Store the new ID
d.SetId(renamedApp.Name)
}
// If the config vars changed, then recalculate those
if d.HasChange("config_vars") {
o, n := d.GetChange("config_vars")
if o == nil {
o = []interface{}{}
}
if n == nil {
n = []interface{}{}
}
err := updateConfigVars(
d.Id(), client, o.([]interface{}), n.([]interface{}))
if err != nil {
return err
}
}
return resourceHerokuAppRead(d, meta)
}
func resourceHerokuAppDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
log.Printf("[INFO] Deleting App: %s", d.Id())
_, err := client.AppDelete(context.TODO(), d.Id())
if err != nil {
return fmt.Errorf("Error deleting App: %s", err)
}
d.SetId("")
return nil
}
func resourceHerokuAppRetrieve(id string, organization bool, client *heroku.Service) (*application, error) {
app := application{Id: id, Client: client, Organization: organization}
err := app.Update()
if err != nil {
return nil, fmt.Errorf("Error retrieving app: %s", err)
}
return &app, nil
}
func retrieveConfigVars(id string, client *heroku.Service) (map[string]string, error) {
vars, err := client.ConfigVarInfoForApp(context.TODO(), id)
if err != nil {
return nil, err
}
nonNullVars := map[string]string{}
for k, v := range vars {
if v != nil {
nonNullVars[k] = *v
}
}
return nonNullVars, nil
}
// Updates the config vars for from an expanded configuration.
func updateConfigVars(
id string,
client *heroku.Service,
o []interface{},
n []interface{}) error {
vars := make(map[string]*string)
for _, v := range o {
if v != nil {
for k := range v.(map[string]interface{}) {
vars[k] = nil
}
}
}
for _, v := range n {
if v != nil {
for k, v := range v.(map[string]interface{}) {
val := v.(string)
vars[k] = &val
}
}
}
log.Printf("[INFO] Updating config vars: *%#v", vars)
if _, err := client.ConfigVarUpdate(context.TODO(), id, vars); err != nil {
return fmt.Errorf("Error updating config vars: %s", err)
}
return nil
}