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_confirmation_of_payee_association.go
222 lines (183 loc) · 6.84 KB
/
resource_confirmation_of_payee_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
package form3
import (
"fmt"
form3 "github.com/form3tech-oss/terraform-provider-form3/api"
"github.com/form3tech-oss/terraform-provider-form3/client/associations"
"github.com/form3tech-oss/terraform-provider-form3/models"
"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)
func resourceForm3ConfirmationOfPayeeAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceConfirmationOfPayeeAssociationCreate,
Read: resourceConfirmationOfPayeeAssociationRead,
Delete: resourceConfirmationOfPayeeAssociationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"association_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"organisation_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"open_banking_organisation_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"open_banking_public_key_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"signing_key_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"signing_dn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"signing_certificate_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"exact_match_threshold": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"close_match_threshold": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceConfirmationOfPayeeAssociationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
log.Print("[INFO] Creating ConfirmationOfPayee association ")
association, err := createConfirmationOfPayeeNewAssociationFromResourceData(d)
if err != nil {
return fmt.Errorf("failed to create ConfirmationOfPayee association: %s", err)
}
createdAssociation, err := client.AssociationClient.Associations.PostConfirmationOfPayee(associations.NewPostConfirmationOfPayeeParams().
WithCreationRequest(&models.CoPAssociationCreation{
Data: association,
}))
if err != nil {
return fmt.Errorf("failed to create ConfirmationOfPayee association: %s", err)
}
d.SetId(createdAssociation.Payload.Data.ID.String())
log.Printf("[INFO] ConfirmationOfPayee association key: %s", d.Id())
return resourceConfirmationOfPayeeAssociationRead(d, meta)
}
func resourceConfirmationOfPayeeAssociationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
associationId, _ := GetUUIDOK(d, "association_id")
if associationId == "" {
associationId = strfmt.UUID(d.Id())
log.Printf("[INFO] Importing association with resource id: %s.", associationId)
} else {
log.Printf("[INFO] Reading association with resource id: %s.", associationId)
}
association, err := client.AssociationClient.Associations.GetConfirmationOfPayeeID(associations.NewGetConfirmationOfPayeeIDParams().
WithID(associationId))
if err != nil {
apiError, ok := err.(*runtime.APIError)
if ok && apiError.Code == 404 {
d.SetId("")
return nil
} else {
return err
}
}
d.Set("association_id", association.Payload.Data.ID.String())
d.Set("organisation_id", association.Payload.Data.OrganisationID.String())
d.Set("open_banking_organisation_id", association.Payload.Data.Attributes.OpenBankingOrganisationID)
d.Set("open_banking_public_key_id", association.Payload.Data.Attributes.PublicKeyID)
d.Set("signing_key_id", association.Payload.Data.Relationships.SigningCertificate.Data.KeyID)
d.Set("signing_dn", association.Payload.Data.Relationships.SigningCertificate.Data.Dn)
d.Set("signing_certificate_id", association.Payload.Data.Relationships.SigningCertificate.Data.ID)
d.Set("exact_match_threshold", association.Payload.Data.Attributes.MatchingCriteria.ExactMatchThreshold)
d.Set("close_match_threshold", association.Payload.Data.Attributes.MatchingCriteria.CloseMatchThreshold)
return nil
}
func resourceConfirmationOfPayeeAssociationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
association, err := client.AssociationClient.Associations.GetConfirmationOfPayeeID(associations.NewGetConfirmationOfPayeeIDParams().
WithID(strfmt.UUID(d.Id())))
if err != nil {
return fmt.Errorf("error deleting ConfirmationOfPayee association: %s", err)
}
log.Printf("[INFO] Deleting ConfirmationOfPayee association for id: %s ", association.Payload.Data.ID)
params := associations.NewDeleteConfirmationOfPayeeIDParams().
WithID(*association.Payload.Data.ID)
if association.Payload.Data.Version != nil {
params.WithVersion(*association.Payload.Data.Version)
}
_, err = client.AssociationClient.Associations.DeleteConfirmationOfPayeeID(params)
if err != nil {
return fmt.Errorf("error deleting ConfirmationOfPayee association: %s", err)
}
return nil
}
func createConfirmationOfPayeeNewAssociationFromResourceData(d *schema.ResourceData) (*models.CoPAssociation, error) {
association := &models.CoPAssociation{
Type: "confirmation_of_payee_associations",
Relationships: &models.CoPAssociationRelationships{
SigningCertificate: &models.SigningCertificate{
Data: &models.SigningCertificateData{
Type: "certificates",
},
},
},
Attributes: &models.CoPAssociationAttributes{},
}
if attr, ok := GetUUIDOK(d, "association_id"); ok {
uuid := strfmt.UUID(attr.String())
association.ID = &uuid
}
if attr, ok := GetUUIDOK(d, "organisation_id"); ok {
uuid := strfmt.UUID(attr.String())
association.OrganisationID = &uuid
}
if attr, ok := d.GetOk("open_banking_organisation_id"); ok {
association.Attributes.OpenBankingOrganisationID = swag.String(attr.(string))
}
if attr, ok := d.GetOk("open_banking_public_key_id"); ok {
association.Attributes.PublicKeyID = swag.String(attr.(string))
}
if attr, ok := GetUUIDOK(d, "signing_key_id"); ok {
uuid := strfmt.UUID(attr.String())
association.Relationships.SigningCertificate.Data.KeyID = &uuid
}
if attr, ok := GetUUIDOK(d, "signing_certificate_id"); ok {
uuid := strfmt.UUID(attr.String())
association.Relationships.SigningCertificate.Data.ID = &uuid
}
if attr, ok := d.GetOk("signing_dn"); ok {
association.Relationships.SigningCertificate.Data.Dn = swag.String(attr.(string))
}
association.Attributes.MatchingCriteria = &models.MatchingCriteria{}
if attr, ok := d.GetOk("exact_match_threshold"); ok {
association.Attributes.MatchingCriteria.ExactMatchThreshold = swag.String(attr.(string))
}
if attr, ok := d.GetOk("close_match_threshold"); ok {
association.Attributes.MatchingCriteria.CloseMatchThreshold = swag.String(attr.(string))
}
return association, nil
}