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_ebury_gateway_association.go
114 lines (89 loc) · 3.18 KB
/
resource_ebury_gateway_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
package form3
import (
"fmt"
"log"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/runtime"
"github.com/form3tech-oss/terraform-provider-form3/client/associations"
"github.com/form3tech-oss/terraform-provider-form3/models"
form3 "github.com/form3tech-oss/terraform-provider-form3/api"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceForm3EburyAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceEburyAssociationCreate,
Read: resourceEburyAssociationRead,
Delete: resourceEburyAssociationDelete,
Schema: map[string]*schema.Schema{
"association_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"organisation_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
SchemaVersion: 0,
}
}
func resourceEburyAssociationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
association, err := createEburyAssociationFromResourceData(d)
if err != nil {
return fmt.Errorf("failed to create ebury association: %s", err)
}
createdAssociation, err := client.AssociationClient.Associations.PostEbury(
associations.NewPostEburyParams().WithCreationRequest(&models.EburyAssociationCreation{Data: association}))
if err != nil {
return fmt.Errorf("failed to create ebury association: %s", err)
}
d.SetId(createdAssociation.Payload.Data.ID.String())
log.Printf("[INFO] ebury association key: %s", d.Id())
return resourceEburyAssociationRead(d, meta)
}
func resourceEburyAssociationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
associationId, _ := GetUUIDOK(d, "association_id")
eburyAssociation, err := client.AssociationClient.Associations.GetEburyID(
associations.NewGetEburyIDParams().WithID(associationId))
if err != nil {
apiError, ok := err.(*runtime.APIError)
if ok && apiError.Code == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("couldn't find ebury gateway association: %s", err)
}
_ = d.Set("association_id", eburyAssociation.Payload.Data.ID.String())
return nil
}
func resourceEburyAssociationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
eburyAssociation, err := client.AssociationClient.Associations.GetEburyID(associations.NewGetEburyIDParams().
WithID(strfmt.UUID(d.Id())))
if err != nil {
return fmt.Errorf("error deleting ebury gateway association: %s", err)
}
_, err = client.AssociationClient.Associations.DeleteEburyID(associations.NewDeleteEburyIDParams().
WithID(eburyAssociation.Payload.Data.ID).
WithVersion(*eburyAssociation.Payload.Data.Version))
if err != nil {
return fmt.Errorf("error deleting ebury gateway association: %s", err)
}
return nil
}
func createEburyAssociationFromResourceData(d *schema.ResourceData) (*models.NewEburyAssociation, error) {
association := models.NewEburyAssociation{
Type: string(models.ResourceTypeEburyAssociations),
}
if attr, ok := GetUUIDOK(d, "association_id"); ok {
association.ID = attr
}
if attr, ok := GetUUIDOK(d, "organisation_id"); ok {
association.OrganisationID = attr
}
return &association, nil
}