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_starling_association.go
182 lines (144 loc) · 5.56 KB
/
resource_starling_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
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 resourceForm3StarlingAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceStarlingAssociationCreate,
Read: resourceStarlingAssociationRead,
Delete: resourceStarlingAssociationDelete,
Schema: map[string]*schema.Schema{
"association_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"organisation_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"starling_account_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"starling_account_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceStarlingAssociationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
name := d.Get("starling_account_name").(string)
log.Printf("[INFO] Creating association with account name: %s", name)
association, err := createNewAssociationFromResourceData(d)
if err != nil {
return fmt.Errorf("failed to create association: %s", form3.JsonErrorPrettyPrint(err))
}
createdAssociation, err := client.AssociationClient.Associations.PostStarling(associations.NewPostStarlingParams().
WithCreationRequest(&models.AssociationCreation{
Data: association,
}))
if err != nil {
return fmt.Errorf("failed to create association: %s", form3.JsonErrorPrettyPrint(err))
}
d.SetId(createdAssociation.Payload.Data.ID.String())
log.Printf("[INFO] association key: %s", d.Id())
return resourceStarlingAssociationRead(d, meta)
}
func resourceStarlingAssociationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
key := d.Id()
associationId, _ := GetUUIDOK(d, "association_id")
associationName := d.Get("starling_account_name").(string)
log.Printf("[INFO] Reading association for id: %s account name: %s", key, associationName)
association, err := client.AssociationClient.Associations.GetStarlingID(associations.NewGetStarlingIDParams().
WithID(associationId))
if err != nil {
if !form3.IsJsonErrorStatusCode(err, 404) {
return fmt.Errorf("couldn't find association: %s", form3.JsonErrorPrettyPrint(err))
}
d.SetId("")
return nil
}
d.Set("association_id", association.Payload.Data.ID.String())
d.Set("starling_account_name", association.Payload.Data.Attributes.StarlingAccountName)
d.Set("starling_account_id", association.Payload.Data.Attributes.StarlingAccountUID)
return nil
}
func resourceStarlingAssociationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
associationFromResource, err := createAssociationFromResourceDataWithVersion(d, client)
if err != nil {
return fmt.Errorf("error deleting association: %s", form3.JsonErrorPrettyPrint(err))
}
log.Printf("[INFO] Deleting association for id: %s account name: %s", associationFromResource.ID, associationFromResource.Attributes.StarlingAccountName)
_, err = client.AssociationClient.Associations.DeleteStarlingID(associations.NewDeleteStarlingIDParams().
WithID(associationFromResource.ID).
WithVersion(*associationFromResource.Version))
if err != nil {
return fmt.Errorf("error deleting association: %s", form3.JsonErrorPrettyPrint(err))
}
return nil
}
func createAssociationFromResourceDataWithVersion(d *schema.ResourceData, client *form3.AuthenticatedClient) (*models.Association, error) {
association, err := createAssociationFromResourceData(d)
if err != nil {
return nil, err
}
version, err := getAssociationVersion(client, association.ID)
if err != nil {
return nil, err
}
association.Version = &version
return association, nil
}
func createAssociationFromResourceData(d *schema.ResourceData) (*models.Association, error) {
association := models.Association{Attributes: &models.AssociationAttributes{}}
association.Type = "associations"
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("starling_account_name"); ok {
association.Attributes.StarlingAccountName = attr.(string)
}
if attr, ok := GetUUIDOK(d, "starling_account_id"); ok {
association.Attributes.StarlingAccountUID = attr
}
return &association, nil
}
func createNewAssociationFromResourceData(d *schema.ResourceData) (*models.NewAssociation, error) {
association := models.NewAssociation{Attributes: &models.NewAssociationAttributes{}}
association.Type = "associations"
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("starling_account_name"); ok {
association.Attributes.StarlingAccountName = attr.(string)
}
return &association, nil
}
func getAssociationVersion(client *form3.AuthenticatedClient, associationId strfmt.UUID) (int64, error) {
association, err := client.AssociationClient.Associations.GetStarlingID(associations.NewGetStarlingIDParams().WithID(associationId))
if err != nil {
if err != nil {
return -1, fmt.Errorf("error reading association: %s", form3.JsonErrorPrettyPrint(err))
}
}
return *association.Payload.Data.Version, nil
}