Skip to content

Commit 2f0ef61

Browse files
committed
make backend and instance_pool immutable
1 parent 23c9cda commit 2f0ef61

File tree

3 files changed

+31
-48
lines changed

3 files changed

+31
-48
lines changed

Diff for: civo/loadbalancer/resource_loadbalancer.go

+26-45
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,19 @@ func ResourceLoadBalancer() *schema.Resource {
209209
Importer: &schema.ResourceImporter{
210210
StateContext: schema.ImportStatePassthroughContext,
211211
},
212+
CustomizeDiff: customizeDiffLoadbalancer,
212213
}
213214
}
214215

215216
// function to create a new load balancer
216217
func resourceLoadBalancerCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
217218
client := m.(*civogo.Client)
218219

220+
// overwrite the region if is defined in the datasource
221+
if region, ok := d.GetOk("region"); ok {
222+
client.Region = region.(string)
223+
}
224+
219225
name := d.Get("name").(string)
220226

221227
// Check that either backend or instance_pool is provided
@@ -228,7 +234,8 @@ func resourceLoadBalancerCreate(ctx context.Context, d *schema.ResourceData, m i
228234

229235
// Prepare the load balancer create request
230236
conf := &civogo.LoadBalancerConfig{
231-
Name: name,
237+
Name: name,
238+
Region: client.Region,
232239
}
233240

234241
if v, ok := d.GetOk("service_name"); ok {
@@ -256,7 +263,7 @@ func resourceLoadBalancerCreate(ctx context.Context, d *schema.ResourceData, m i
256263
}
257264

258265
if v, ok := d.GetOk("session_affinity_config_timeout"); ok {
259-
conf.SessionAffinityConfigTimeout = v.(int32)
266+
conf.SessionAffinityConfigTimeout = int32(v.(int))
260267
}
261268

262269
if v, ok := d.GetOk("enable_proxy_protocol"); ok {
@@ -321,6 +328,11 @@ func resourceLoadBalancerCreate(ctx context.Context, d *schema.ResourceData, m i
321328
func resourceLoadBalancerRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
322329
client := meta.(*civogo.Client)
323330

331+
// overwrite the region if is defined in the datasource
332+
if region, ok := d.GetOk("region"); ok {
333+
client.Region = region.(string)
334+
}
335+
324336
// Retrieve the load balancer information from the API
325337
loadBalancer, err := client.GetLoadBalancer(d.Id())
326338
if err != nil {
@@ -340,8 +352,6 @@ func resourceLoadBalancerRead(_ context.Context, d *schema.ResourceData, meta in
340352
d.Set("cluster_id", loadBalancer.ClusterID)
341353
d.Set("firewall_id", loadBalancer.FirewallID)
342354

343-
fmt.Println("FIREEEE ID", loadBalancer.FirewallID)
344-
345355
if err := d.Set("backend", flattenLoadBalancerBackend(loadBalancer.Backends)); err != nil {
346356
return diag.Errorf("error setting backend: %s", err)
347357
}
@@ -363,7 +373,8 @@ func resourceLoadBalancerUpdate(ctx context.Context, d *schema.ResourceData, m i
363373

364374
// Initialize an update request
365375
updateRequest := &civogo.LoadBalancerUpdateConfig{
366-
Name: d.Get("name").(string),
376+
Region: client.Region,
377+
Name: d.Get("name").(string),
367378
}
368379

369380
// Check if any relevant fields have changed and update the request accordingly
@@ -395,46 +406,6 @@ func resourceLoadBalancerUpdate(ctx context.Context, d *schema.ResourceData, m i
395406
updateRequest.EnableProxyProtocol = d.Get("enable_proxy_protocol").(string)
396407
}
397408

398-
// If backend configuration has changed, update backends in the request
399-
if d.HasChange("backend") {
400-
backends := d.Get("backend").([]interface{})
401-
updateRequest.Backends = make([]civogo.LoadBalancerBackendConfig, len(backends))
402-
403-
for i, backend := range backends {
404-
b := backend.(map[string]interface{})
405-
updateRequest.Backends[i] = civogo.LoadBalancerBackendConfig{
406-
IP: b["ip"].(string),
407-
Protocol: b["protocol"].(string),
408-
SourcePort: int32(b["source_port"].(int)),
409-
TargetPort: int32(b["target_port"].(int)),
410-
HealthCheckPort: int32(b["health_check_port"].(int)),
411-
}
412-
}
413-
}
414-
415-
// If instance pool configuration has changed, update instance pools in the request
416-
if d.HasChange("instance_pool") {
417-
instancePools := d.Get("instance_pool").([]interface{})
418-
updateRequest.InstancePools = make([]civogo.LoadBalancerInstancePoolConfig, len(instancePools))
419-
420-
for i, instancePool := range instancePools {
421-
p := instancePool.(map[string]interface{})
422-
healthCheck := p["health_check"].([]interface{})[0].(map[string]interface{})
423-
424-
updateRequest.InstancePools[i] = civogo.LoadBalancerInstancePoolConfig{
425-
Tags: convertStringList(p["tags"].([]interface{})),
426-
Names: convertStringList(p["names"].([]interface{})),
427-
Protocol: p["protocol"].(string),
428-
SourcePort: int32(p["source_port"].(int)),
429-
TargetPort: int32(p["target_port"].(int)),
430-
HealthCheck: civogo.HealthCheck{
431-
Port: healthCheck["port"].(int32),
432-
Path: healthCheck["path"].(string),
433-
},
434-
}
435-
}
436-
}
437-
438409
// Send the update request to the Civo API
439410
_, err := client.UpdateLoadBalancer(loadBalancerID, updateRequest)
440411
if err != nil {
@@ -528,3 +499,13 @@ func convertStringList(input []interface{}) []string {
528499
}
529500
return strList
530501
}
502+
503+
func customizeDiffLoadbalancer(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
504+
if d.Id() != "" && d.HasChange("instance_pool") {
505+
return fmt.Errorf("the 'instance_pool' field is immutable")
506+
}
507+
if d.Id() != "" && d.HasChange("backend") {
508+
return fmt.Errorf("the 'backend' field is immutable")
509+
}
510+
return nil
511+
}

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module github.com/civo/terraform-provider-civo
22

33
require (
4-
github.com/civo/civogo v0.3.87
4+
github.com/civo/civogo v0.3.89
55
github.com/google/uuid v1.3.1
66
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
77
github.com/hashicorp/terraform-plugin-sdk/v2 v2.31.0

Diff for: go.sum

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmms
1212
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
1313
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
1414
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
15-
github.com/civo/civogo v0.3.87 h1:0uUOw04uNI98X4zcv5r4EHeD+tMJIFhqwJ/RntQ8k1s=
16-
github.com/civo/civogo v0.3.87/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM=
15+
github.com/civo/civogo v0.3.88 h1:katTTxgMg+6k9vdO++wzjz91KtjT/5dGyJy1i8l/z5w=
16+
github.com/civo/civogo v0.3.88/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM=
17+
github.com/civo/civogo v0.3.89 h1:g+I4NGVa5t0L2Z9+QbnEAqxE/3OCDUYvepje3oUkKVo=
18+
github.com/civo/civogo v0.3.89/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM=
1719
github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs=
1820
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
1921
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=

0 commit comments

Comments
 (0)