Skip to content

Commit 3d69307

Browse files
authored
Merge branch 'main' into single-firewall-rule
2 parents 70c7a26 + d85d9c6 commit 3d69307

File tree

2 files changed

+42
-50
lines changed

2 files changed

+42
-50
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 1.* (Unreleased)
2+
3+
IMPROVEMENTS:
4+
5+
* Cleanup RabbitMQ configuration resource ([#215](https://github.com/cloudamqp/terraform-provider-cloudamqp/pull/215))
6+
17
## 1.27.0 (Jun 12, 2023)
28

39
NOTES:

cloudamqp/resource_cloudamqp_rabbitmq_configuration.go

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
func resourceRabbitMqConfiguration() *schema.Resource {
1515
return &schema.Resource{
16-
Create: resourceRabbitMqConfigurationCreate,
16+
Create: resourceRabbitMqConfigurationUpdate,
1717
Read: resourceRabbitMqConfigurationRead,
1818
Update: resourceRabbitMqConfigurationUpdate,
1919
Delete: resourceRabbitMqConfigurationDelete,
@@ -154,40 +154,15 @@ func resourceRabbitMqConfiguration() *schema.Resource {
154154
}
155155
}
156156

157-
func resourceRabbitMqConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
158-
api := meta.(*api.API)
159-
keys := rabbitMqConfigurationWriteAttributeKeys()
160-
params := make(map[string]interface{})
161-
for _, k := range keys {
162-
v := d.Get(k)
163-
if v == nil || v == 0 || v == 0.0 || v == "" {
164-
continue
165-
} else if k == "connection_max" {
166-
if v == -1 {
167-
v = "infinity"
168-
}
169-
} else if k == "consumer_timeout" {
170-
if v == -1 {
171-
v = "false"
172-
}
173-
} else if k == "log_exchange_level" {
174-
k = "log.exchange.level"
175-
}
176-
params["rabbit."+k] = v
177-
}
178-
err := api.UpdateRabbitMqConfiguration(d.Get("instance_id").(int), params, d.Get("sleep").(int), d.Get("timeout").(int))
179-
if err != nil {
180-
return err
181-
}
182-
id := strconv.Itoa(d.Get("instance_id").(int))
183-
d.SetId(id)
184-
return resourceRabbitMqConfigurationRead(d, meta)
185-
}
186-
187157
func resourceRabbitMqConfigurationRead(d *schema.ResourceData, meta interface{}) error {
188-
api := meta.(*api.API)
189-
instanceID, _ := strconv.Atoi(d.Id())
190-
data, err := api.ReadRabbitMqConfiguration(instanceID, d.Get("sleep").(int), d.Get("timeout").(int))
158+
var (
159+
api = meta.(*api.API)
160+
instanceID, _ = strconv.Atoi(d.Id())
161+
sleep = d.Get("sleep").(int)
162+
timeout = d.Get("timeout").(int)
163+
)
164+
165+
data, err := api.ReadRabbitMqConfiguration(instanceID, sleep, timeout)
191166
log.Printf("[DEBUG] cloudamqp::resource::rabbitmq_configuration::read data: %v", data)
192167
if err != nil {
193168
return err
@@ -196,23 +171,18 @@ func resourceRabbitMqConfigurationRead(d *schema.ResourceData, meta interface{})
196171
d.Set("instance_id", instanceID)
197172
for k, v := range data {
198173
if validateRabbitMqConfigurationJSONField(k) {
174+
if v == nil || v == "" {
175+
continue
176+
}
199177
key := strings.ReplaceAll(k, "rabbit.", "")
200178
if key == "connection_max" {
201-
if v == "infinity" || v == nil {
179+
if v == "infinity" {
202180
v = -1
203181
}
204182
} else if key == "consumer_timeout" {
205183
if v == "false" {
206184
v = -1
207185
}
208-
} else if key == "queue_index_embed_msgs_below" {
209-
if v == nil {
210-
v = 4096
211-
}
212-
} else if key == "max_message_size" {
213-
if v == nil {
214-
v = 134217728
215-
}
216186
} else if key == "log.exchange.level" {
217187
key = "log_exchange_level"
218188
}
@@ -223,12 +193,22 @@ func resourceRabbitMqConfigurationRead(d *schema.ResourceData, meta interface{})
223193
}
224194

225195
func resourceRabbitMqConfigurationUpdate(d *schema.ResourceData, meta interface{}) error {
226-
api := meta.(*api.API)
227-
keys := rabbitMqConfigurationWriteAttributeKeys()
228-
params := make(map[string]interface{})
196+
var (
197+
api = meta.(*api.API)
198+
instanceID = d.Get("instance_id").(int)
199+
keys = rabbitMqConfigurationWriteAttributeKeys()
200+
params = make(map[string]interface{})
201+
sleep = d.Get("sleep").(int)
202+
timeout = d.Get("timeout").(int)
203+
)
204+
229205
for _, k := range keys {
206+
if !d.HasChange(k) {
207+
continue
208+
}
209+
230210
v := d.Get(k)
231-
if v == nil {
211+
if v == nil || v == "" {
232212
continue
233213
} else if k == "connection_max" {
234214
if v == -1 {
@@ -243,10 +223,16 @@ func resourceRabbitMqConfigurationUpdate(d *schema.ResourceData, meta interface{
243223
}
244224
params["rabbit."+k] = v
245225
}
246-
err := api.UpdateRabbitMqConfiguration(d.Get("instance_id").(int), params, d.Get("sleep").(int), d.Get("timeout").(int))
247-
if err != nil {
248-
return err
226+
227+
log.Printf("[DEBUG] RabbitMQ configuration params: %v", params)
228+
if len(params) > 0 {
229+
err := api.UpdateRabbitMqConfiguration(instanceID, params, sleep, timeout)
230+
if err != nil {
231+
return err
232+
}
249233
}
234+
235+
d.SetId(strconv.Itoa(instanceID))
250236
return resourceRabbitMqConfigurationRead(d, meta)
251237
}
252238

0 commit comments

Comments
 (0)