@@ -209,13 +209,19 @@ func ResourceLoadBalancer() *schema.Resource {
209
209
Importer : & schema.ResourceImporter {
210
210
StateContext : schema .ImportStatePassthroughContext ,
211
211
},
212
+ CustomizeDiff : customizeDiffLoadbalancer ,
212
213
}
213
214
}
214
215
215
216
// function to create a new load balancer
216
217
func resourceLoadBalancerCreate (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
217
218
client := m .(* civogo.Client )
218
219
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
+
219
225
name := d .Get ("name" ).(string )
220
226
221
227
// Check that either backend or instance_pool is provided
@@ -228,7 +234,8 @@ func resourceLoadBalancerCreate(ctx context.Context, d *schema.ResourceData, m i
228
234
229
235
// Prepare the load balancer create request
230
236
conf := & civogo.LoadBalancerConfig {
231
- Name : name ,
237
+ Name : name ,
238
+ Region : client .Region ,
232
239
}
233
240
234
241
if v , ok := d .GetOk ("service_name" ); ok {
@@ -256,7 +263,7 @@ func resourceLoadBalancerCreate(ctx context.Context, d *schema.ResourceData, m i
256
263
}
257
264
258
265
if v , ok := d .GetOk ("session_affinity_config_timeout" ); ok {
259
- conf .SessionAffinityConfigTimeout = v .(int32 )
266
+ conf .SessionAffinityConfigTimeout = int32 ( v .(int ) )
260
267
}
261
268
262
269
if v , ok := d .GetOk ("enable_proxy_protocol" ); ok {
@@ -321,6 +328,11 @@ func resourceLoadBalancerCreate(ctx context.Context, d *schema.ResourceData, m i
321
328
func resourceLoadBalancerRead (_ context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
322
329
client := meta .(* civogo.Client )
323
330
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
+
324
336
// Retrieve the load balancer information from the API
325
337
loadBalancer , err := client .GetLoadBalancer (d .Id ())
326
338
if err != nil {
@@ -340,8 +352,6 @@ func resourceLoadBalancerRead(_ context.Context, d *schema.ResourceData, meta in
340
352
d .Set ("cluster_id" , loadBalancer .ClusterID )
341
353
d .Set ("firewall_id" , loadBalancer .FirewallID )
342
354
343
- fmt .Println ("FIREEEE ID" , loadBalancer .FirewallID )
344
-
345
355
if err := d .Set ("backend" , flattenLoadBalancerBackend (loadBalancer .Backends )); err != nil {
346
356
return diag .Errorf ("error setting backend: %s" , err )
347
357
}
@@ -363,7 +373,8 @@ func resourceLoadBalancerUpdate(ctx context.Context, d *schema.ResourceData, m i
363
373
364
374
// Initialize an update request
365
375
updateRequest := & civogo.LoadBalancerUpdateConfig {
366
- Name : d .Get ("name" ).(string ),
376
+ Region : client .Region ,
377
+ Name : d .Get ("name" ).(string ),
367
378
}
368
379
369
380
// 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
395
406
updateRequest .EnableProxyProtocol = d .Get ("enable_proxy_protocol" ).(string )
396
407
}
397
408
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
-
438
409
// Send the update request to the Civo API
439
410
_ , err := client .UpdateLoadBalancer (loadBalancerID , updateRequest )
440
411
if err != nil {
@@ -528,3 +499,13 @@ func convertStringList(input []interface{}) []string {
528
499
}
529
500
return strList
530
501
}
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
+ }
0 commit comments