This repository has been archived by the owner on Jul 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource_account_configuration.go
321 lines (264 loc) · 10.5 KB
/
resource_account_configuration.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
package form3
import (
"bytes"
"fmt"
"log"
form3 "github.com/form3tech-oss/terraform-provider-form3/api"
"github.com/form3tech-oss/terraform-provider-form3/client/accounts"
"github.com/form3tech-oss/terraform-provider-form3/models"
"github.com/go-openapi/strfmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceForm3AccountConfiguration() *schema.Resource {
return &schema.Resource{
Create: resourceAccountConfigurationCreate,
Read: resourceAccountConfigurationRead,
Delete: resourceAccountConfigurationDelete,
Update: resourceAccountConfigurationUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"account_configuration_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"organisation_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"account_generation_enabled": &schema.Schema{
Type: schema.TypeBool,
Required: true,
ForceNew: true,
},
"account_generation_configuration": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"country": {
Type: schema.TypeString,
Required: true,
},
"mod_check_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"bank_id": {
Type: schema.TypeString,
Optional: true,
},
"bic": {
Type: schema.TypeString,
Optional: true,
},
"base_currency": {
Type: schema.TypeString,
Optional: true,
},
"valid_account_ranges": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"minimum": {
Type: schema.TypeInt,
Required: true,
},
"maximum": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
},
},
},
},
}
}
func resourceAccountConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
id := d.Get("account_configuration_id").(string)
log.Printf("[INFO] Creating account configuration with id: %s", id)
configuration, err := createAccountConfigurationFromResourceData(d)
if err != nil {
return fmt.Errorf("failed to create account configuration: %s", form3.JsonErrorPrettyPrint(err))
}
createdConfiguration, err := client.AccountClient.Accounts.PostAccountconfigurations(accounts.NewPostAccountconfigurationsParams().
WithAccountConfigurationCreationRequest(&models.AccountConfigurationCreation{
Data: configuration,
}))
if err != nil {
return fmt.Errorf("failed to create account configuration: %s", form3.JsonErrorPrettyPrint(err))
}
d.SetId(createdConfiguration.Payload.Data.ID.String())
log.Printf("[INFO] configuration key: %s", d.Id())
return resourceAccountConfigurationRead(d, meta)
}
func resourceAccountConfigurationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
key := d.Id()
configurationId, _ := GetUUIDOK(d, "account_configuration_id")
log.Printf("[INFO] Reading account configuration for id: %s", key)
if configurationId == "" {
configurationId = strfmt.UUID(key)
log.Printf("[INFO] Importing account configuration id: %s ", key)
} else {
log.Printf("[INFO] Reading account configuration for id: %s", configurationId)
}
configuration, err := client.AccountClient.Accounts.GetAccountconfigurationsID(accounts.NewGetAccountconfigurationsIDParams().
WithID(configurationId))
if err != nil {
if !form3.IsJsonErrorStatusCode(err, 404) {
return fmt.Errorf("couldn't find account configuration: %s", form3.JsonErrorPrettyPrint(err))
}
d.SetId("")
return nil
}
_ = d.Set("organisation_id", configuration.Payload.Data.OrganisationID.String())
_ = d.Set("account_configuration_id", configuration.Payload.Data.ID.String())
_ = d.Set("account_generation_enabled", configuration.Payload.Data.Attributes.AccountGenerationEnabled)
accountGenerationConfigurations :=
make([]interface{}, 0, len(configuration.Payload.Data.Attributes.AccountGenerationConfiguration))
for _, element := range configuration.Payload.Data.Attributes.AccountGenerationConfiguration {
accountGenerationConfigurations = append(accountGenerationConfigurations, map[string]interface{}{
"country": element.Country,
"mod_check_enabled": *element.ModCheckEnabled,
"bank_id": element.BankID,
"bic": element.Bic,
"base_currency": element.BaseCurrency,
"valid_account_ranges": flattenValidAccountRanges(element.ValidAccountRanges),
})
}
if err := d.Set("account_generation_configuration", accountGenerationConfigurations); err != nil {
return err
}
return nil
}
func resourceAccountConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
configurationFromResource, err := createAccountConfigurationFromResourceDataWithVersion(d, client)
if err != nil {
return fmt.Errorf("error deleting account configuration: %s", form3.JsonErrorPrettyPrint(err))
}
log.Printf("[INFO] Deleting account configuration for id: %s", configurationFromResource.ID)
_, err = client.AccountClient.Accounts.DeleteAccountconfigurationsID(accounts.NewDeleteAccountconfigurationsIDParams().
WithID(configurationFromResource.ID).
WithVersion(*configurationFromResource.Version))
if err != nil {
return fmt.Errorf("error deleting account configuration: %s", form3.JsonErrorPrettyPrint(err))
}
return nil
}
func resourceAccountConfigurationUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
configuration, err := createAccountConfigurationFromResourceDataWithVersion(d, client)
if err != nil {
return fmt.Errorf("failed to update account configuration: %s", form3.JsonErrorPrettyPrint(err))
}
log.Printf("[INFO] Updating account configuration with id: %s", configuration.ID)
_, err = client.AccountClient.Accounts.PatchAccountconfigurationsID(accounts.NewPatchAccountconfigurationsIDParams().
WithID(configuration.ID).
WithConfigAmendRequest(&models.ConfigurationAmendment{
Data: configuration,
}))
if err != nil {
return fmt.Errorf("failed to update account configuration: %s", form3.JsonErrorPrettyPrint(err))
}
return nil
}
func createAccountConfigurationFromResourceDataWithVersion(d *schema.ResourceData, client *form3.AuthenticatedClient) (*models.AccountConfiguration, error) {
configuration, err := createAccountConfigurationFromResourceData(d)
if err != nil {
return nil, err
}
version, err := getAccountConfigurationVersion(client, configuration.ID)
if err != nil {
return nil, err
}
configuration.Version = &version
return configuration, nil
}
func createAccountConfigurationFromResourceData(d *schema.ResourceData) (*models.AccountConfiguration, error) {
configuration := models.AccountConfiguration{Attributes: &models.AccountConfigurationAttributes{}}
configuration.Type = "account_configurations"
if attr, ok := GetUUIDOK(d, "account_configuration_id"); ok {
configuration.ID = attr
}
if attr, ok := GetUUIDOK(d, "organisation_id"); ok {
configuration.OrganisationID = attr
}
if attr, ok := d.GetOk("account_generation_enabled"); ok {
configuration.Attributes.AccountGenerationEnabled = attr.(bool)
}
if attr, ok := d.GetOk("account_generation_configuration"); ok {
accountConfigurationArray := attr.([]interface{})
var accountConfigs []*models.AccountGenerationConfiguration
for _, accountConfigElement := range accountConfigurationArray {
country := accountConfigElement.(map[string]interface{})["country"].(string)
modCheckEnabled := accountConfigElement.(map[string]interface{})["mod_check_enabled"].(bool)
bankId := accountConfigElement.(map[string]interface{})["bank_id"].(string)
bic := accountConfigElement.(map[string]interface{})["bic"].(string)
baseCurrency := accountConfigElement.(map[string]interface{})["base_currency"].(string)
validAccountRangesSet := accountConfigElement.(map[string]interface{})["valid_account_ranges"].(*schema.Set).List()
var validAccountRanges []*models.AccountGenerationConfigurationValidAccountRangesItems
for _, accountRangeElement := range validAccountRangesSet {
validAccountRange := models.AccountGenerationConfigurationValidAccountRangesItems{
Minimum: int64(accountRangeElement.(map[string]interface{})["minimum"].(int)),
Maximum: int64(accountRangeElement.(map[string]interface{})["maximum"].(int)),
}
validAccountRanges = append(validAccountRanges, &validAccountRange)
}
accountConfig := models.AccountGenerationConfiguration{
Country: country,
ModCheckEnabled: &modCheckEnabled,
BankID: bankId,
Bic: bic,
BaseCurrency: baseCurrency,
ValidAccountRanges: validAccountRanges,
}
accountConfigs = append(accountConfigs, &accountConfig)
}
configuration.Attributes.AccountGenerationConfiguration = accountConfigs
}
return &configuration, nil
}
func getAccountConfigurationVersion(client *form3.AuthenticatedClient, configurationId strfmt.UUID) (int64, error) {
configuration, err := client.AccountClient.Accounts.GetAccountconfigurationsID(accounts.NewGetAccountconfigurationsIDParams().WithID(configurationId))
if err != nil {
if err != nil {
return -1, fmt.Errorf("error reading account configuration: %s", form3.JsonErrorPrettyPrint(err))
}
}
return *configuration.Payload.Data.Version, nil
}
func flattenValidAccountRanges(validAccountRanges []*models.AccountGenerationConfigurationValidAccountRangesItems) *schema.Set {
validAccountRangesSet := schema.NewSet(validAccountRangeHash, []interface{}{})
if validAccountRanges == nil {
return validAccountRangesSet
}
for _, value := range validAccountRanges {
validAccountRangesSet.Add(flattenValidAccountRange(value))
}
return validAccountRangesSet
}
func validAccountRangeHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]int64)
buf.WriteString(fmt.Sprintf("%d", m["minimum"]))
buf.WriteString(fmt.Sprintf("%d", m["maximum"]))
return hashcode.String(buf.String())
}
func flattenValidAccountRange(value *models.AccountGenerationConfigurationValidAccountRangesItems) map[string]int64 {
m := map[string]int64{}
m["minimum"] = value.Minimum
m["maximum"] = value.Maximum
return m
}