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_swift_association.go
175 lines (142 loc) · 5.39 KB
/
resource_swift_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
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 resourceForm3SwiftAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceSwiftAssociationCreate,
Read: resourceSwiftAssociationRead,
Delete: resourceSwiftAssociationDelete,
Update: resourceSwiftAssociationUpdate,
Schema: map[string]*schema.Schema{
"association_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"organisation_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"bic": {
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
},
}
}
func resourceSwiftAssociationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
association, err := createSwiftNewAssociationFromResourceData(d)
if err != nil {
return fmt.Errorf("failed to create swift association: %s", form3.JsonErrorPrettyPrint(err))
}
createdAssociation, err := client.AssociationClient.Associations.PostSwift(associations.NewPostSwiftParams().
WithCreationRequest(&models.SwiftAssociationCreation{
Data: association,
}))
if err != nil {
return fmt.Errorf("failed to create swift association: %s", form3.JsonErrorPrettyPrint(err))
}
d.SetId(createdAssociation.Payload.Data.ID.String())
log.Printf("[INFO] swift association key: %s", d.Id())
return resourceSwiftAssociationRead(d, meta)
}
func createSwiftNewAssociationFromResourceData(d *schema.ResourceData) (*models.SwiftNewAssociation, error) {
association := models.SwiftNewAssociation{
Type: string(models.ResourceTypeSwiftAssociations),
Attributes: &models.SwiftAssociationAttributes{},
}
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("bic"); ok {
association.Attributes.Bic = attr.(string)
}
return &association, nil
}
func resourceSwiftAssociationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
associationId, _ := GetUUIDOK(d, "association_id")
swiftAssociation, err := client.AssociationClient.Associations.GetSwiftID(associations.NewGetSwiftIDParams().
WithID(associationId))
if err != nil {
if !form3.IsJsonErrorStatusCode(err, 404) {
return fmt.Errorf("couldn't find swift association: %s", form3.JsonErrorPrettyPrint(err))
}
d.SetId("")
return nil
}
_ = d.Set("association_id", swiftAssociation.Payload.Data.ID.String())
_ = d.Set("organisation_id", swiftAssociation.Payload.Data.OrganisationID.String())
_ = d.Set("bic", swiftAssociation.Payload.Data.Attributes.Bic)
return nil
}
func resourceSwiftAssociationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
swiftAssociation, err := client.AssociationClient.Associations.GetSwiftID(associations.NewGetSwiftIDParams().
WithID(strfmt.UUID(d.Id())))
if err != nil {
return fmt.Errorf("error deleting swift association: %s", form3.JsonErrorPrettyPrint(err))
}
_, err = client.AssociationClient.Associations.DeleteSwiftID(associations.NewDeleteSwiftIDParams().
WithID(swiftAssociation.Payload.Data.ID).
WithVersion(*swiftAssociation.Payload.Data.Version))
if err != nil {
return fmt.Errorf("error deleting swift association: %s", form3.JsonErrorPrettyPrint(err))
}
return nil
}
func resourceSwiftAssociationUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
association, err := createSwiftUpdateAssociationFromResourceData(d)
if err != nil {
return fmt.Errorf("failed to update swift association: %s", form3.JsonErrorPrettyPrint(err))
}
existingAssociation, err := client.AssociationClient.Associations.GetSwiftID(associations.NewGetSwiftIDParams().
WithID(association.ID))
if err != nil {
return fmt.Errorf("error fetching swift association: %s", form3.JsonErrorPrettyPrint(err))
}
if existingAssociation == nil || existingAssociation.Payload == nil {
return fmt.Errorf("swift association with id %s is nil or has a nil payload", association.ID)
}
association.Version = *existingAssociation.Payload.Data.Version
log.Printf("[INFO] Updating swift association with id: %s", association.ID)
_, err = client.AssociationClient.Associations.PatchSwiftID(associations.NewPatchSwiftIDParams().
WithID(association.ID).
WithPayload(&models.SwiftAssociationPatch{
Data: association,
}))
if err != nil {
return fmt.Errorf("failed to update swift association: %s", form3.JsonErrorPrettyPrint(err))
}
return nil
}
func createSwiftUpdateAssociationFromResourceData(d *schema.ResourceData) (*models.SwiftUpdateAssociation, error) {
association := &models.SwiftUpdateAssociation{
Type: string(models.ResourceTypeSwiftAssociations),
Attributes: &models.SwiftUpdateAssociationAttributes{},
}
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("bic"); ok {
association.Attributes.Bic = attr.(string)
}
return association, nil
}