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_subscription.go
209 lines (167 loc) · 6.23 KB
/
resource_subscription.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package form3
import (
"fmt"
form3 "github.com/form3tech-oss/terraform-provider-form3/api"
"github.com/form3tech-oss/terraform-provider-form3/client/subscriptions"
"github.com/form3tech-oss/terraform-provider-form3/models"
"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"
"github.com/hashicorp/terraform/helper/schema"
"log"
)
func resourceForm3Subscription() *schema.Resource {
return &schema.Resource{
Create: resourceSubscriptionCreate,
Read: resourceSubscriptionRead,
Update: resourceSubscriptionUpdate,
Delete: resourceSubscriptionDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"organisation_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"subscription_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"callback_transport": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"callback_uri": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"event_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"record_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceSubscriptionCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
subscriptionId := d.Get("subscription_id").(string)
log.Printf("[INFO] Creating subscription with id: %s", subscriptionId)
subscription, err := createSubscriptionFromResourceData(d)
if err != nil {
return err
}
log.Printf("[DEBUG] subscription create: %#v", subscription)
createdSubscription, err := client.NotificationClient.Subscriptions.PostSubscriptions(subscriptions.NewPostSubscriptionsParams().
WithSubscriptionCreationRequest(&models.SubscriptionCreation{Data: subscription}))
if err != nil {
return fmt.Errorf("failed to create subscription: %s", err)
}
d.SetId(createdSubscription.Payload.Data.ID.String())
log.Printf("[INFO] subscription key: %s", d.Id())
return resourceSubscriptionRead(d, meta)
}
func resourceSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
key := d.Id()
subscriptionId, _ := GetUUIDOK(d, "subscription_id")
if subscriptionId == "" {
subscriptionId = strfmt.UUID(key)
log.Printf("[INFO] Importing subscription for id: %s ", key)
} else {
log.Printf("[INFO] Reading subscription for id: %s ", subscriptionId)
}
subscription, err := client.NotificationClient.Subscriptions.GetSubscriptionsID(
subscriptions.NewGetSubscriptionsIDParams().WithID(subscriptionId))
if err != nil {
apiError, ok := err.(*runtime.APIError)
if ok && apiError.Code == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("couldn't find subscription: %s", err)
}
d.Set("subscription_id", subscription.Payload.Data.ID.String())
d.Set("organisation_id", subscription.Payload.Data.OrganisationID.String())
d.Set("callback_transport", subscription.Payload.Data.Attributes.CallbackTransport)
d.Set("callback_uri", subscription.Payload.Data.Attributes.CallbackURI)
d.Set("event_type", subscription.Payload.Data.Attributes.EventType)
d.Set("record_type", subscription.Payload.Data.Attributes.RecordType)
return nil
}
func resourceSubscriptionUpdate(d *schema.ResourceData, meta interface{}) error {
d.Partial(false)
client := meta.(*form3.AuthenticatedClient)
subscriptionFromResource, err := createSubscriptionFromResourceDataWithVersion(d, client)
if err != nil {
return fmt.Errorf("error updating subscription: %s", err)
}
_, err = client.NotificationClient.Subscriptions.PatchSubscriptionsID(subscriptions.NewPatchSubscriptionsIDParams().
WithID(subscriptionFromResource.ID).
WithSubscriptionUpdateRequest(&models.SubscriptionCreation{Data: subscriptionFromResource}))
if err != nil {
return fmt.Errorf("error updating subscription: %s", err)
}
return nil
}
func resourceSubscriptionDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
subscriptionFromResource, err := createSubscriptionFromResourceDataWithVersion(d, client)
if err != nil {
return fmt.Errorf("error deleting subscription: %s", err)
}
log.Printf("[INFO] Deleting subscription for id: %s", subscriptionFromResource.ID)
_, err = client.NotificationClient.Subscriptions.DeleteSubscriptionsID(subscriptions.NewDeleteSubscriptionsIDParams().
WithID(subscriptionFromResource.ID).
WithVersion(*subscriptionFromResource.Version))
if err != nil {
return fmt.Errorf("error deleting subscription: %s", err)
}
return nil
}
func createSubscriptionFromResourceDataWithVersion(d *schema.ResourceData, client *form3.AuthenticatedClient) (*models.Subscription, error) {
subscription, err := createSubscriptionFromResourceData(d)
version, err := getSubscriptionVersion(client, subscription.ID)
if err != nil {
return nil, err
}
subscription.Version = &version
return subscription, nil
}
func createSubscriptionFromResourceData(d *schema.ResourceData) (*models.Subscription, error) {
subscription := models.Subscription{Attributes: &models.SubscriptionAttributes{}}
subscription.Type = "subscriptions"
if attr, ok := GetUUIDOK(d, "subscription_id"); ok {
subscription.ID = attr
}
if attr, ok := GetUUIDOK(d, "organisation_id"); ok {
subscription.OrganisationID = attr
}
if attr, ok := d.GetOk("callback_transport"); ok {
subscription.Attributes.CallbackTransport = attr.(string)
}
if attr, ok := d.GetOk("callback_uri"); ok {
subscription.Attributes.CallbackURI = attr.(string)
}
if attr, ok := d.GetOk("event_type"); ok {
subscription.Attributes.EventType = attr.(string)
}
if attr, ok := d.GetOk("record_type"); ok {
subscription.Attributes.RecordType = attr.(string)
}
return &subscription, nil
}
func getSubscriptionVersion(client *form3.AuthenticatedClient, subscriptionId strfmt.UUID) (int64, error) {
subscription, err := client.NotificationClient.Subscriptions.GetSubscriptionsID(subscriptions.NewGetSubscriptionsIDParams().
WithID(subscriptionId))
if err != nil {
return -1, fmt.Errorf("error reading subscription: %s", err)
}
return *subscription.Payload.Data.Version, nil
}