This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
/
resource_binding.go
243 lines (200 loc) · 6.6 KB
/
resource_binding.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package rabbitmq
import (
"encoding/json"
"fmt"
"log"
"net/url"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
rabbithole "github.com/michaelklishin/rabbit-hole/v2"
)
func resourceBinding() *schema.Resource {
return &schema.Resource{
Create: CreateBinding,
Read: ReadBinding,
Delete: DeleteBinding,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"source": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"vhost": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"destination": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"destination_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"properties_key": {
Type: schema.TypeString,
Computed: true,
},
"routing_key": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"arguments": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"arguments_json"},
},
"arguments_json": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.ValidateJsonString,
ConflictsWith: []string{"arguments"},
DiffSuppressFunc: structure.SuppressJsonDiff,
},
},
}
}
func CreateBinding(d *schema.ResourceData, meta interface{}) error {
rmqc := meta.(*rabbithole.Client)
vhost := d.Get("vhost").(string)
arguments := d.Get("arguments").(map[string]interface{})
// If arguments_json is used, unmarshal it into a generic interface
// and use it as the "arguments" key for the binding.
if v, ok := d.Get("arguments_json").(string); ok && v != "" {
var arguments_json map[string]interface{}
err := json.Unmarshal([]byte(v), &arguments_json)
if err != nil {
return err
}
arguments = arguments_json
}
bindingInfo := rabbithole.BindingInfo{
Source: d.Get("source").(string),
Destination: d.Get("destination").(string),
DestinationType: d.Get("destination_type").(string),
RoutingKey: d.Get("routing_key").(string),
Arguments: arguments,
}
propertiesKey, err := declareBinding(rmqc, vhost, bindingInfo)
if err != nil {
return err
}
log.Printf("[DEBUG] RabbitMQ: Binding properties key: %s", propertiesKey)
bindingInfo.PropertiesKey = propertiesKey
name := fmt.Sprintf("%s/%s/%s/%s/%s", percentEncodeSlashes(vhost), bindingInfo.Source, bindingInfo.Destination, bindingInfo.DestinationType, bindingInfo.PropertiesKey)
d.SetId(name)
return ReadBinding(d, meta)
}
func ReadBinding(d *schema.ResourceData, meta interface{}) error {
rmqc := meta.(*rabbithole.Client)
log.Printf("[TRACE] RabbitMQ: read binding resource ID (pre-split): %s", d.Id())
bindingId := strings.Split(d.Id(), "/")
log.Printf("[DEBUG] RabbitMQ: binding ID: %#v", bindingId)
if len(bindingId) < 5 {
return fmt.Errorf("Unable to determine binding ID")
}
vhost := percentDecodeSlashes(bindingId[0])
source := bindingId[1]
destination := bindingId[2]
destinationType := bindingId[3]
propertiesKey := bindingId[4]
log.Printf("[DEBUG] RabbitMQ: Attempting to find binding for: vhost=%s source=%s destination=%s destinationType=%s propertiesKey=%s",
vhost, source, destination, destinationType, propertiesKey)
bindings, err := rmqc.ListBindingsIn(vhost)
if err != nil {
return err
}
log.Printf("[DEBUG] RabbitMQ: Bindings retrieved: %#v", bindings)
bindingFound := false
for _, binding := range bindings {
log.Printf("[TRACE] RabbitMQ: Assessing binding: %#v", binding)
if binding.Source == source && binding.Destination == destination && binding.DestinationType == destinationType && binding.PropertiesKey == propertiesKey {
log.Printf("[DEBUG] RabbitMQ: Found Binding: %#v", binding)
bindingFound = true
d.Set("vhost", binding.Vhost)
d.Set("source", binding.Source)
d.Set("destination", binding.Destination)
d.Set("destination_type", binding.DestinationType)
d.Set("routing_key", binding.RoutingKey)
d.Set("properties_key", binding.PropertiesKey)
if v, ok := d.Get("arguments_json").(string); ok && v != "" {
bytes, err := json.Marshal(binding.Arguments)
if err != nil {
return fmt.Errorf("could not encode arguments as JSON: %w", err)
}
d.Set("arguments_json", string(bytes))
} else {
d.Set("arguments", binding.Arguments)
}
}
}
// The binding could not be found,
// so consider it deleted and remove from state
if !bindingFound {
d.SetId("")
}
return nil
}
func DeleteBinding(d *schema.ResourceData, meta interface{}) error {
rmqc := meta.(*rabbithole.Client)
bindingId := strings.Split(d.Id(), "/")
if len(bindingId) < 5 {
return fmt.Errorf("Unable to determine binding ID")
}
vhost := percentDecodeSlashes(bindingId[0])
source := bindingId[1]
destination := bindingId[2]
destinationType := bindingId[3]
propertiesKey := bindingId[4]
bindingInfo := rabbithole.BindingInfo{
Vhost: vhost,
Source: source,
Destination: destination,
DestinationType: destinationType,
PropertiesKey: propertiesKey,
}
log.Printf("[DEBUG] RabbitMQ: Attempting to delete binding for: vhost=%s source=%s destination=%s destinationType=%s propertiesKey=%s",
vhost, source, destination, destinationType, propertiesKey)
resp, err := rmqc.DeleteBinding(vhost, bindingInfo)
if err != nil {
return err
}
log.Printf("[DEBUG] RabbitMQ: Binding delete response: %#v", resp)
if resp.StatusCode == 404 {
// The binding was already deleted
return nil
}
if resp.StatusCode >= 400 {
return fmt.Errorf("Error deleting RabbitMQ binding: %s", resp.Status)
}
return nil
}
func declareBinding(rmqc *rabbithole.Client, vhost string, bindingInfo rabbithole.BindingInfo) (string, error) {
log.Printf("[DEBUG] RabbitMQ: Attempting to declare binding for: vhost=%s source=%s destination=%s destinationType=%s",
vhost, bindingInfo.Source, bindingInfo.Destination, bindingInfo.DestinationType)
resp, err := rmqc.DeclareBinding(vhost, bindingInfo)
log.Printf("[DEBUG] RabbitMQ: Binding declare response: %#v", resp)
if err != nil {
return "", err
}
if resp.StatusCode >= 400 {
return "", fmt.Errorf("Error declaring RabbitMQ binding: %s", resp.Status)
}
location := strings.Split(resp.Header.Get("Location"), "/")
propertiesKey, err := url.PathUnescape(location[len(location)-1])
if err != nil {
return "", err
}
return propertiesKey, nil
}