/
resource_heroku_app_config_association.go
227 lines (181 loc) · 6.23 KB
/
resource_heroku_app_config_association.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
package heroku
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
heroku "github.com/heroku/heroku-go/v5"
"log"
"time"
)
func resourceHerokuAppConfigAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceHerokuAppConfigAssociationCreate,
Read: resourceHerokuAppConfigAssociationRead,
Update: resourceHerokuAppConfigAssociationUpdate,
Delete: resourceHerokuAppConfigAssociationDelete,
Importer: &schema.ResourceImporter{
State: resourceHerokuAppConfigAssociationImport,
},
Schema: map[string]*schema.Schema{
"app_id": {
Type: schema.TypeString,
Required: true,
},
"vars": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"sensitive_vars": {
Type: schema.TypeMap,
Sensitive: true,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Sensitive: true,
},
},
},
}
}
func resourceHerokuAppConfigAssociationImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
noImportErr := fmt.Errorf("not possible to import this resource")
return nil, noImportErr
}
func resourceHerokuAppConfigAssociationCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*Config).Api
appId := getAppId(d)
vars := getVars(d)
sensitiveVars := getSensitiveVars(d)
// Check for duplicates between vars & sensitive_vars
dupeErr := duplicateVarsChecker(vars, sensitiveVars)
if dupeErr != nil {
return dupeErr
}
// Combine Both Variables
combinedVars := mergeVars(vars, sensitiveVars)
// Update vars on the app
if err := updateVars(appId, client, nil, combinedVars); err != nil {
return err
}
d.SetId(fmt.Sprintf("config:%s", appId))
return resourceHerokuAppConfigAssociationRead(d, m)
}
func resourceHerokuAppConfigAssociationRead(d *schema.ResourceData, m interface{}) error {
client := m.(*Config).Api
appId := getAppId(d)
setErr := d.Set("app_id", appId)
if setErr != nil {
return setErr
}
remoteAppVars, remoteAppGetErr := retrieveConfigVars(appId, client)
if remoteAppGetErr != nil {
return remoteAppGetErr
}
vettedConfigVars, vettedSensitiveConfigVars := vetVarsForState(getVars(d), getSensitiveVars(d), remoteAppVars)
if err := d.Set("vars", vettedConfigVars); err != nil {
log.Printf("[WARN] Error setting app config vars: %s", err)
}
if err := d.Set("sensitive_vars", vettedSensitiveConfigVars); err != nil {
log.Printf("[WARN] Error setting app config sensitive vars: %s", err)
}
return nil
}
func resourceHerokuAppConfigAssociationUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*Config).Api
appId := getAppId(d)
var oldVars, newVars, oldSensitiveVars, newSensitiveVars, allOldVars, allNewVars map[string]interface{}
oldVars, newVars = getVarDiff(d, "vars")
oldSensitiveVars, newSensitiveVars = getVarDiff(d, "sensitive_vars")
// Merge the vars
allOldVars = mergeVars(oldVars, oldSensitiveVars)
allNewVars = mergeVars(newVars, newSensitiveVars)
// Update vars on the app
if err := updateVars(appId, client, allOldVars, allNewVars); err != nil {
return err
}
return resourceHerokuAppConfigAssociationRead(d, m)
}
func resourceHerokuAppConfigAssociationDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*Config).Api
appId := getAppId(d)
vars := getVars(d)
sensitiveVars := getSensitiveVars(d)
allVars := mergeVars(vars, sensitiveVars)
// Essentially execute an update to delete all the vars listed in the schema only
if err := updateVars(appId, client, allVars, nil); err != nil {
return err
}
// Remove resource from state
d.SetId("")
return nil
}
func updateVars(id string, client *heroku.Service, o map[string]interface{}, n map[string]interface{}) error {
vars := constructVars(o, n)
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)
}
releases, err := client.ReleaseList(
context.TODO(),
id,
&heroku.ListRange{Descending: true, Field: "version", Max: 1},
)
if err != nil {
return err
}
if len(releases) == 0 {
return errors.New("no release found")
}
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"succeeded"},
Refresh: releaseStateRefreshFunc(client, id, releases[0].ID),
Timeout: 20 * time.Minute,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("error waiting for new release (%s) to succeed: %s", releases[0].ID, err)
}
return nil
}
// getVars extracts the vars attribute generically from a Heroku resource.
func getVars(d *schema.ResourceData) map[string]interface{} {
var vars map[string]interface{}
if v, ok := d.GetOk("vars"); ok {
vs := v.(map[string]interface{})
log.Printf("[DEBUG] vars: %s", vs)
vars = vs
}
return vars
}
// getVars extracts the vars attribute generically from a Heroku resource.
func getSensitiveVars(d *schema.ResourceData) map[string]interface{} {
var sensitiveVars map[string]interface{}
if v, ok := d.GetOk("sensitive_vars"); ok {
vs := v.(map[string]interface{})
log.Printf("[DEBUG] sensitive vars: %s", vs)
sensitiveVars = vs
}
return sensitiveVars
}
// Check to see if vars defined for this resource are already on the app. This is to avoid a infinite dirty plan
// if vars were defined on the BOTH the heroku_app & heroku_app_config_association resources
// as well as avoiding config drift with manually managed config vars.
func checkForExistingVars(appConfigVars map[string]*string, newVars map[string]interface{}) error {
var existingVars []string
for k := range newVars {
if _, ok := appConfigVars[k]; ok {
// Add vars that already exist on the app to existingVars
existingVars = append(existingVars, k)
}
}
if len(existingVars) > 0 {
return fmt.Errorf("[ERROR] The following config vars already exist (either added manually or via heroku_app) on the app prior to this resource creating them: %v\n"+
"To prevent an infinite dirty plan/config drift, please define these vars in terraform in either heroku_app.config_vars OR heroku_app_config_association", existingVars)
}
return nil
}