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_bic.go
163 lines (129 loc) · 4.07 KB
/
resource_bic.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
package form3
import (
"fmt"
form3 "github.com/form3tech-oss/terraform-provider-form3/api"
"github.com/form3tech-oss/terraform-provider-form3/client/accounts"
"github.com/form3tech-oss/terraform-provider-form3/models"
"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)
func resourceForm3Bic() *schema.Resource {
return &schema.Resource{
Create: resourceBicCreate,
Read: resourceBicRead,
Delete: resourceBicDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"bic_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"organisation_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"bic": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceBicCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
bicID := d.Get("bic_id").(string)
log.Printf("[INFO] Creating bic with id: %s", bicID)
bicResource, err := createBicFromResourceData(d)
if err != nil {
return err
}
log.Printf("[DEBUG] bic create: %#v", bicResource)
createdBic, err := client.AccountClient.Accounts.PostBics(accounts.NewPostBicsParams().
WithBicCreationRequest(&models.BicCreation{
Data: bicResource,
}))
if err != nil {
return fmt.Errorf("failed to create bic: %s", err)
}
d.SetId(createdBic.Payload.Data.ID.String())
log.Printf("[INFO] bic key: %s", d.Id())
return resourceBicRead(d, meta)
}
func resourceBicRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
bicID, _ := GetUUIDOK(d, "bic_id")
if bicID == "" {
bicID = strfmt.UUID(d.Id())
log.Printf("[INFO] Importing bic with resource id: %s.", bicID)
} else {
log.Printf("[INFO] Reading bic with resource id: %s.", bicID)
}
bicResponse, err := client.AccountClient.Accounts.GetBicsID(accounts.NewGetBicsIDParams().
WithID(bicID))
if err != nil {
apiError, ok := err.(*runtime.APIError)
if ok && apiError.Code == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("couldn't find bic: %s", err)
}
d.Set("bic_id", bicResponse.Payload.Data.ID.String())
d.Set("organisation_id", bicResponse.Payload.Data.OrganisationID.String())
d.Set("bic", bicResponse.Payload.Data.Attributes.Bic)
return nil
}
func resourceBicDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
bicFromResource, err := createBicFromResourceDataWithVersion(d, client)
if err != nil {
return fmt.Errorf("error deleting bic: %s", err)
}
log.Printf("[INFO] Deleting bic with id: %s bic: %s", bicFromResource.ID, bicFromResource.Attributes.Bic)
_, err = client.AccountClient.Accounts.DeleteBicsID(accounts.NewDeleteBicsIDParams().
WithID(bicFromResource.ID).
WithVersion(*bicFromResource.Version))
if err != nil {
return fmt.Errorf("error deleting bic: %s", err)
}
return nil
}
func createBicFromResourceData(d *schema.ResourceData) (*models.Bic, error) {
bic := models.Bic{Attributes: &models.BicAttributes{}}
bic.Type = "bics"
if attr, ok := GetUUIDOK(d, "bic_id"); ok {
bic.ID = attr
}
if attr, ok := GetUUIDOK(d, "organisation_id"); ok {
bic.OrganisationID = attr
}
if attr, ok := d.GetOk("bic"); ok {
bic.Attributes.Bic = attr.(string)
}
return &bic, nil
}
func createBicFromResourceDataWithVersion(d *schema.ResourceData, client *form3.AuthenticatedClient) (*models.Bic, error) {
bic, err := createBicFromResourceData(d)
version, err := getBicVersion(client, bic.ID)
if err != nil {
return nil, err
}
bic.Version = &version
return bic, nil
}
func getBicVersion(client *form3.AuthenticatedClient, id strfmt.UUID) (int64, error) {
bic, err := client.AccountClient.Accounts.GetBicsID(accounts.NewGetBicsIDParams().WithID(id))
if err != nil {
if err != nil {
return -1, fmt.Errorf("error reading bic id: %s", err)
}
}
return *bic.Payload.Data.Version, nil
}