Skip to content

Commit

Permalink
Enable loab balancer support for datasource (#121)
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Anarse <vishal@civo.com>
  • Loading branch information
Vishal Anarase committed Feb 26, 2022
1 parent ed203fb commit c397e6f
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 164 deletions.
183 changes: 183 additions & 0 deletions civo/datasource_loadbalancer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package civo

import (
"fmt"
"log"
"strings"

"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// Data source to get from the api a specific Load Balancer
// using the id or the hostname of the Load Balancer
func dataSourceLoadBalancer() *schema.Resource {
return &schema.Resource{
Description: strings.Join([]string{
"Get information on a load balancer for use in other resources. This data source provides all of the load balancers properties as configured on your Civo account.",
"An error will be raised if the provided load balancer name does not exist in your Civo account.",
}, "\n\n"),
Read: dataSourceLoadBalancerRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Description: "The id of the load balancer to retrieve (You can find this id from service annotations 'kubernetes.civo.com/loadbalancer-id')",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the load balancer (You can find this name from service annotations 'kubernetes.civo.com/loadbalancer-name')",
},
"public_ip": {
Type: schema.TypeString,
Computed: true,
Description: "The public ip of the load balancer",
},
"algorithm": {
Type: schema.TypeString,
Computed: true,
Description: "The algorithm used by the load balancer",
},
"external_traffic_policy": {
Type: schema.TypeString,
Computed: true,
Description: "The external traffic policy of the load balancer",
},
"session_affinity": {
Type: schema.TypeString,
Computed: true,
Description: "The session affinity of the load balancer",
},
"session_affinity_config_timeout": {
Type: schema.TypeInt,
Computed: true,
Description: "The session affinity config timeout of the load balancer",
},
"enable_proxy_protocol": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether the load balancer is configured to proxy protocol",
},
"private_ip": {
Type: schema.TypeString,
Computed: true,
Description: "The private ip of the load balancer",
},
"firewall_id": {
Type: schema.TypeString,
Computed: true,
Description: "The firewall id of the load balancer",
},
"cluster_id": {
Type: schema.TypeString,
Computed: true,
Description: "The cluster id of the load balancer",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "The state of the load balancer",
},
"backends": {
Type: schema.TypeList,
Computed: true,
Description: "",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Computed: true,
Description: "The ip of the backend",
},
"protocol": {
Type: schema.TypeString,
Computed: true,
Description: "The protocol of the backend",
},
"source_port": {
Type: schema.TypeInt,
Computed: true,
Description: "The source port of the backend",
},
"target_port": {
Type: schema.TypeInt,
Computed: true,
Description: "The target port of the backend",
},
"health_check_port": {
Type: schema.TypeInt,
Computed: true,
Description: "The health check port of the backend",
},
},
},
},
},
}
}

func dataSourceLoadBalancerRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

// overwrite the region if is define in the datasource
if region, ok := d.GetOk("region"); ok {
apiClient.Region = region.(string)
}

var searchBy string

if name, ok := d.GetOk("name"); ok {
log.Printf("[INFO] Getting the LoadBalancer by name")
searchBy = name.(string)
} else if id, ok := d.GetOk("id"); ok {
log.Printf("[INFO] Getting the LoadBalancer by id")
searchBy = id.(string)
}

lb, err := apiClient.FindLoadBalancer(searchBy)
if err != nil {
return fmt.Errorf("[ERR] failed to retrive LoadBalancer: %s", err)
}

d.SetId(lb.ID)
d.Set("name", lb.Name)
d.Set("public_ip", lb.PublicIP)
d.Set("algorithm", lb.Algorithm)
d.Set("external_traffic_policy", lb.ExternalTrafficPolicy)
d.Set("session_affinity", lb.SessionAffinity)
d.Set("session_affinity_config_timeout", lb.SessionAffinityConfigTimeout)
d.Set("enable_proxy_protocol", lb.EnableProxyProtocol)
d.Set("private_ip", lb.PrivateIP)
d.Set("firewall_id", lb.FirewallID)
d.Set("cluster_id", lb.ClusterID)
d.Set("state", lb.State)

if err := d.Set("backends", flattenLoadBalancerBackend(lb.Backends)); err != nil {
return fmt.Errorf("[ERR] error retrieving the backends for load balancer error: %#v", err)
}

return nil
}

// function to flatten the load balancer backend when is coming from the api
func flattenLoadBalancerBackend(backend []civogo.LoadBalancerBackend) []interface{} {
if backend == nil {
return nil
}

flattenedBackend := make([]interface{}, len(backend))
for i, back := range backend {
rawRule := map[string]interface{}{
"ip": back.IP,
"protocol": back.Protocol,
"source_port": back.SourcePort,
"target_port": back.TargetPort,
"health_check_port": back.HealthCheckPort,
}

flattenedBackend[i] = rawRule
}

return flattenedBackend
}
162 changes: 0 additions & 162 deletions civo/datasource_loadbalancer.go.disabled

This file was deleted.

4 changes: 2 additions & 2 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func Provider() *schema.Provider {
"civo_network": dataSourceNetwork(),
"civo_volume": dataSourceVolume(),
"civo_firewall": dataSourceFirewall(),
// "civo_loadbalancer": dataSourceLoadBalancer(),
"civo_ssh_key": dataSourceSSHKey(),
"civo_loadbalancer": dataSourceLoadBalancer(),
"civo_ssh_key": dataSourceSSHKey(),
// "civo_snapshot": dataSourceSnapshot(),
"civo_region": dataSourceRegion(),
},
Expand Down

0 comments on commit c397e6f

Please sign in to comment.