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_sepainstant_association.go
154 lines (124 loc) · 4.38 KB
/
resource_sepainstant_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
package form3
import (
"fmt"
"github.com/form3tech-oss/go-form3"
"github.com/form3tech-oss/go-form3/client/associations"
"github.com/form3tech-oss/go-form3/models"
"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"
"github.com/hashicorp/terraform/helper/schema"
"log"
)
func resourceForm3SepaInstantAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceSepaInstantAssociationCreate,
Read: resourceSepaInstantAssociationRead,
Delete: resourceSepaInstantAssociationDelete,
Schema: map[string]*schema.Schema{
"association_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"organisation_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"certificate_dn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"certificate_pin": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"certificate_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"bic": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceSepaInstantAssociationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
association, err := createSepaInstantNewAssociationFromResourceData(d)
if err != nil {
return fmt.Errorf("failed to create sepa instant association: %s", err)
}
createdAssociation, err := client.AssociationClient.Associations.PostSepainstant(associations.NewPostSepainstantParams().
WithCreationRequest(&models.SepaInstantAssociationCreation{
Data: association,
}))
if err != nil {
return fmt.Errorf("failed to create sepa instant association: %s", err)
}
d.SetId(createdAssociation.Payload.Data.ID.String())
log.Printf("[INFO] sepa instant association key: %s", d.Id())
return resourceSepaInstantAssociationRead(d, meta)
}
func resourceSepaInstantAssociationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
associationId, _ := GetUUIDOK(d, "association_id")
sepaInstantAssociation, err := client.AssociationClient.Associations.GetSepainstantID(associations.NewGetSepainstantIDParams().
WithID(associationId))
if err != nil {
apiError := err.(*runtime.APIError)
if apiError.Code == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("couldn't find sepa instant association: %s", err)
}
d.Set("association_id", sepaInstantAssociation.Payload.Data.ID.String())
d.Set("certificate_dn", sepaInstantAssociation.Payload.Data.Attributes.CertificateDn)
d.Set("certificate_pin", sepaInstantAssociation.Payload.Data.Attributes.CertificatePin)
d.Set("certificate_id", sepaInstantAssociation.Payload.Data.Attributes.CertificateID)
d.Set("bic", sepaInstantAssociation.Payload.Data.Attributes.Bic)
return nil
}
func resourceSepaInstantAssociationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
sepaInstantAssociation, err := client.AssociationClient.Associations.GetSepainstantID(associations.NewGetSepainstantIDParams().
WithID(strfmt.UUID(d.Id())))
if err != nil {
return fmt.Errorf("error deleting sepa instant association: %s", err)
}
_, err = client.AssociationClient.Associations.DeleteSepainstantID(associations.NewDeleteSepainstantIDParams().
WithID(sepaInstantAssociation.Payload.Data.ID).
WithVersion(*sepaInstantAssociation.Payload.Data.Version))
if err != nil {
return fmt.Errorf("error deleting sepa instant association: %s", err)
}
return nil
}
func createSepaInstantNewAssociationFromResourceData(d *schema.ResourceData) (*models.NewSepaInstantAssociation, error) {
association := models.NewSepaInstantAssociation{Attributes: &models.SepaInstantAssociationAttributes{}}
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("certificate_dn"); ok {
association.Attributes.CertificateDn = attr.(string)
}
if attr, ok := d.GetOk("certificate_pin"); ok {
association.Attributes.CertificatePin = attr.(string)
}
if attr, ok := d.GetOk("certificate_id"); ok {
association.Attributes.CertificateID = attr.(string)
}
if attr, ok := d.GetOk("bic"); ok {
association.Attributes.Bic = attr.(string)
}
return &association, nil
}