diff --git a/civo/loadbalancer/resource_loadbalancer.go b/civo/loadbalancer/resource_loadbalancer.go index 8d2a1d4..3743056 100644 --- a/civo/loadbalancer/resource_loadbalancer.go +++ b/civo/loadbalancer/resource_loadbalancer.go @@ -209,6 +209,7 @@ func ResourceLoadBalancer() *schema.Resource { Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, }, + CustomizeDiff: customizeDiffLoadbalancer, } } @@ -216,6 +217,11 @@ func ResourceLoadBalancer() *schema.Resource { func resourceLoadBalancerCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { client := m.(*civogo.Client) + // overwrite the region if is defined in the datasource + if region, ok := d.GetOk("region"); ok { + client.Region = region.(string) + } + name := d.Get("name").(string) // Check that either backend or instance_pool is provided @@ -228,7 +234,8 @@ func resourceLoadBalancerCreate(ctx context.Context, d *schema.ResourceData, m i // Prepare the load balancer create request conf := &civogo.LoadBalancerConfig{ - Name: name, + Name: name, + Region: client.Region, } if v, ok := d.GetOk("service_name"); ok { @@ -256,7 +263,7 @@ func resourceLoadBalancerCreate(ctx context.Context, d *schema.ResourceData, m i } if v, ok := d.GetOk("session_affinity_config_timeout"); ok { - conf.SessionAffinityConfigTimeout = v.(int32) + conf.SessionAffinityConfigTimeout = int32(v.(int)) } if v, ok := d.GetOk("enable_proxy_protocol"); ok { @@ -321,6 +328,11 @@ func resourceLoadBalancerCreate(ctx context.Context, d *schema.ResourceData, m i func resourceLoadBalancerRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { client := meta.(*civogo.Client) + // overwrite the region if is defined in the datasource + if region, ok := d.GetOk("region"); ok { + client.Region = region.(string) + } + // Retrieve the load balancer information from the API loadBalancer, err := client.GetLoadBalancer(d.Id()) if err != nil { @@ -340,8 +352,6 @@ func resourceLoadBalancerRead(_ context.Context, d *schema.ResourceData, meta in d.Set("cluster_id", loadBalancer.ClusterID) d.Set("firewall_id", loadBalancer.FirewallID) - fmt.Println("FIREEEE ID", loadBalancer.FirewallID) - if err := d.Set("backend", flattenLoadBalancerBackend(loadBalancer.Backends)); err != nil { return diag.Errorf("error setting backend: %s", err) } @@ -363,7 +373,8 @@ func resourceLoadBalancerUpdate(ctx context.Context, d *schema.ResourceData, m i // Initialize an update request updateRequest := &civogo.LoadBalancerUpdateConfig{ - Name: d.Get("name").(string), + Region: client.Region, + Name: d.Get("name").(string), } // 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 updateRequest.EnableProxyProtocol = d.Get("enable_proxy_protocol").(string) } - // If backend configuration has changed, update backends in the request - if d.HasChange("backend") { - backends := d.Get("backend").([]interface{}) - updateRequest.Backends = make([]civogo.LoadBalancerBackendConfig, len(backends)) - - for i, backend := range backends { - b := backend.(map[string]interface{}) - updateRequest.Backends[i] = civogo.LoadBalancerBackendConfig{ - IP: b["ip"].(string), - Protocol: b["protocol"].(string), - SourcePort: int32(b["source_port"].(int)), - TargetPort: int32(b["target_port"].(int)), - HealthCheckPort: int32(b["health_check_port"].(int)), - } - } - } - - // If instance pool configuration has changed, update instance pools in the request - if d.HasChange("instance_pool") { - instancePools := d.Get("instance_pool").([]interface{}) - updateRequest.InstancePools = make([]civogo.LoadBalancerInstancePoolConfig, len(instancePools)) - - for i, instancePool := range instancePools { - p := instancePool.(map[string]interface{}) - healthCheck := p["health_check"].([]interface{})[0].(map[string]interface{}) - - updateRequest.InstancePools[i] = civogo.LoadBalancerInstancePoolConfig{ - Tags: convertStringList(p["tags"].([]interface{})), - Names: convertStringList(p["names"].([]interface{})), - Protocol: p["protocol"].(string), - SourcePort: int32(p["source_port"].(int)), - TargetPort: int32(p["target_port"].(int)), - HealthCheck: civogo.HealthCheck{ - Port: healthCheck["port"].(int32), - Path: healthCheck["path"].(string), - }, - } - } - } - // Send the update request to the Civo API _, err := client.UpdateLoadBalancer(loadBalancerID, updateRequest) if err != nil { @@ -528,3 +499,13 @@ func convertStringList(input []interface{}) []string { } return strList } + +func customizeDiffLoadbalancer(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error { + if d.Id() != "" && d.HasChange("instance_pool") { + return fmt.Errorf("the 'instance_pool' field is immutable") + } + if d.Id() != "" && d.HasChange("backend") { + return fmt.Errorf("the 'backend' field is immutable") + } + return nil +} diff --git a/go.mod b/go.mod index 6853aae..17c6ad3 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module github.com/civo/terraform-provider-civo require ( - github.com/civo/civogo v0.3.87 + github.com/civo/civogo v0.3.89 github.com/google/uuid v1.3.1 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/terraform-plugin-sdk/v2 v2.31.0 diff --git a/go.sum b/go.sum index bc58e8c..381eb20 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +12,10 @@ github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmms github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA= github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8= github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= -github.com/civo/civogo v0.3.87 h1:0uUOw04uNI98X4zcv5r4EHeD+tMJIFhqwJ/RntQ8k1s= -github.com/civo/civogo v0.3.87/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM= +github.com/civo/civogo v0.3.88 h1:katTTxgMg+6k9vdO++wzjz91KtjT/5dGyJy1i8l/z5w= +github.com/civo/civogo v0.3.88/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM= +github.com/civo/civogo v0.3.89 h1:g+I4NGVa5t0L2Z9+QbnEAqxE/3OCDUYvepje3oUkKVo= +github.com/civo/civogo v0.3.89/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM= github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=