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_reconciliation_association.go
158 lines (131 loc) · 4.75 KB
/
resource_reconciliation_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
package form3
import (
"fmt"
"log"
"net/http"
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 resourceForm3ReconciliationAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceReconciliationAssociationCreate,
Read: resourceReconciliationAssociationRead,
Delete: resourceReconciliationAssociationDelete,
Schema: map[string]*schema.Schema{
"association_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"organisation_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"bank_ids": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Required: true,
ForceNew: true,
},
"scheme_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceReconciliationAssociationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
association, err := createReconciliationNewAssociationFromResourceData(d)
if err != nil {
return fmt.Errorf("failed to create Reconciliation association: %s", form3.JsonErrorPrettyPrint(err))
}
createdAssociation, err := client.AssociationClient.Associations.PostReconciliation(associations.NewPostReconciliationParams().
WithCreationRequest(&models.ReconciliationAssociationCreation{
Data: association,
}))
if err != nil {
return fmt.Errorf("failed to create reconciliation_service association: %s", form3.JsonErrorPrettyPrint(err))
}
d.SetId(createdAssociation.Payload.Data.ID.String())
log.Printf("[INFO] Reconciliation reconciliation_service association key: %s", d.Id())
return resourceReconciliationAssociationRead(d, meta)
}
func resourceReconciliationAssociationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
associationId, _ := GetUUIDOK(d, "association_id")
association, err := client.AssociationClient.Associations.GetReconciliationAssociationID(associations.NewGetReconciliationAssociationIDParams().
WithAssociationID(associationId))
if err != nil {
if !form3.IsJsonErrorStatusCode(err, http.StatusNotFound) {
return fmt.Errorf("couldn't find reconciliation_service association: %s", form3.JsonErrorPrettyPrint(err))
}
d.SetId("")
return nil
}
if err := d.Set("association_id", association.Payload.Data.ID.String()); err != nil {
return err
}
if err := d.Set("name", association.Payload.Data.Attributes.Name); err != nil {
return err
}
if err := d.Set("bank_ids", association.Payload.Data.Attributes.BankIds); err != nil {
return err
}
if err := d.Set("scheme_type", association.Payload.Data.Attributes.SchemeType); err != nil {
return err
}
return nil
}
func resourceReconciliationAssociationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
association, err := client.AssociationClient.Associations.GetReconciliationAssociationID(associations.NewGetReconciliationAssociationIDParams().
WithAssociationID(strfmt.UUID(d.Id())))
if err != nil {
return fmt.Errorf("error deleting reconciliation_service association: %s", form3.JsonErrorPrettyPrint(err))
}
_, err = client.AssociationClient.Associations.DeleteReconciliationAssociationID(associations.NewDeleteReconciliationAssociationIDParams().
WithAssociationID(association.Payload.Data.ID).
WithVersion(*association.Payload.Data.Version))
if err != nil {
return fmt.Errorf("error deleting reconciliation_service association: %s", form3.JsonErrorPrettyPrint(err))
}
return nil
}
func createReconciliationNewAssociationFromResourceData(d *schema.ResourceData) (*models.ReconciliationNewAssociation, error) {
association := models.ReconciliationNewAssociation{
Type: models.ReconciliationAssociationTypeReconciliationAssociations,
Attributes: &models.ReconciliationAssociationAttributes{},
}
if attr, ok := GetUUIDOK(d, "association_id"); ok {
association.ID = attr
}
if attr, ok := GetUUIDOK(d, "organisation_id"); ok {
association.OrganisationID = attr
}
if attr, ok := d.GetOk("name"); ok {
association.Attributes.Name = attr.(string)
}
if attr, ok := d.GetOk("bank_ids"); ok {
arr := attr.(*schema.Set).List()
for _, v := range arr {
association.Attributes.BankIds = append(association.Attributes.BankIds, v.(string))
}
}
if attr, ok := d.GetOk("scheme_type"); ok {
association.Attributes.SchemeType = attr.(string)
}
return &association, nil
}