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_payport_association.go
204 lines (167 loc) · 6.55 KB
/
resource_payport_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
package form3
import (
"fmt"
"log"
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/strfmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceForm3PayportAssociation() *schema.Resource {
return &schema.Resource{
Create: resourcePayportAssociationCreate,
Read: resourcePayportAssociationRead,
Delete: resourcePayportAssociationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"payport_association_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"organisation_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"participant_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"participant_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"customer_sending_fps_institution": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"sponsor_bank_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"sponsor_account_number": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
func resourcePayportAssociationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
participantId := d.Get("participant_id").(string)
log.Printf("[INFO] Creating payport association with participant id: %s", participantId)
association, err := createPayportAssociationFromResourceData(d)
if err != nil {
return fmt.Errorf("failed to create payport association: %s", form3.JsonErrorPrettyPrint(err))
}
createdAssociation, err := client.AssociationClient.Associations.PostPayport(associations.NewPostPayportParams().
WithCreationRequest(&models.PayportAssociationCreation{
Data: association,
}))
if err != nil {
return fmt.Errorf("failed to create payport association: %s", form3.JsonErrorPrettyPrint(err))
}
d.SetId(createdAssociation.Payload.Data.ID.String())
log.Printf("[INFO] association key: %s", d.Id())
return resourcePayportAssociationRead(d, meta)
}
func resourcePayportAssociationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
key := d.Id()
associationId, _ := GetUUIDOK(d, "payport_association_id")
participantId := d.Get("participant_id").(string)
if associationId == "" {
associationId = strfmt.UUID(key)
log.Printf("[INFO] Importing payport association for id: %s participant id: %s", associationId, participantId)
} else {
log.Printf("[INFO] Reading payport association for id: %s participant id: %s", associationId, participantId)
}
association, err := client.AssociationClient.Associations.GetPayportID(associations.NewGetPayportIDParams().
WithID(associationId))
if err != nil {
if !form3.IsJsonErrorStatusCode(err, 404) {
return fmt.Errorf("couldn't find payport association: %s", form3.JsonErrorPrettyPrint(err))
}
d.SetId("")
return nil
}
d.Set("payport_association_id", association.Payload.Data.ID.String())
d.Set("participant_id", association.Payload.Data.Attributes.ParticipantID)
d.Set("participant_type", association.Payload.Data.Attributes.ParticipantType)
d.Set("customer_sending_fps_institution", association.Payload.Data.Attributes.CustomerSendingFpsInstitution)
d.Set("sponsor_bank_id", association.Payload.Data.Attributes.SponsorBankID)
d.Set("sponsor_account_number", association.Payload.Data.Attributes.SponsorAccountNumber)
d.Set("organisation_id", association.Payload.Data.OrganisationID.String())
return nil
}
func resourcePayportAssociationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
associationFromResource, err := createPayportAssociationFromResourceDataWithVersion(d, client)
if err != nil {
return fmt.Errorf("error deleting payport association: %s", form3.JsonErrorPrettyPrint(err))
}
log.Printf("[INFO] Deleting payport association for id: %s participant id: %s", associationFromResource.ID, associationFromResource.Attributes.ParticipantID)
_, err = client.AssociationClient.Associations.DeletePayportID(associations.NewDeletePayportIDParams().
WithID(associationFromResource.ID).
WithVersion(*associationFromResource.Version))
if err != nil {
return fmt.Errorf("error deleting payport association: %s", form3.JsonErrorPrettyPrint(err))
}
return nil
}
func createPayportAssociationFromResourceDataWithVersion(d *schema.ResourceData, client *form3.AuthenticatedClient) (*models.PayportAssociation, error) {
association, err := createPayportAssociationFromResourceData(d)
if err != nil {
return nil, err
}
version, err := getPayportAssociationVersion(client, association.ID)
if err != nil {
return nil, err
}
association.Version = &version
return association, nil
}
func createPayportAssociationFromResourceData(d *schema.ResourceData) (*models.PayportAssociation, error) {
association := models.PayportAssociation{Attributes: &models.PayportAssociationAttributes{}}
association.Type = "associations"
if attr, ok := GetUUIDOK(d, "payport_association_id"); ok {
association.ID = attr
}
if attr, ok := GetUUIDOK(d, "organisation_id"); ok {
association.OrganisationID = attr
}
if attr, ok := d.GetOk("participant_id"); ok {
association.Attributes.ParticipantID = attr.(string)
}
if attr, ok := d.GetOk("participant_type"); ok {
association.Attributes.ParticipantType = models.PayportParticipantType(attr.(string))
}
if attr, ok := d.GetOk("customer_sending_fps_institution"); ok {
association.Attributes.CustomerSendingFpsInstitution = attr.(string)
}
if attr, ok := d.GetOk("sponsor_bank_id"); ok {
association.Attributes.SponsorBankID = attr.(string)
}
if attr, ok := d.GetOk("sponsor_account_number"); ok {
association.Attributes.SponsorAccountNumber = attr.(string)
}
return &association, nil
}
func getPayportAssociationVersion(client *form3.AuthenticatedClient, associationId strfmt.UUID) (int64, error) {
association, err := client.AssociationClient.Associations.GetPayportID(associations.NewGetPayportIDParams().WithID(associationId))
if err != nil {
if err != nil {
return -1, fmt.Errorf("error reading payport association: %s", form3.JsonErrorPrettyPrint(err))
}
}
return *association.Payload.Data.Version, nil
}