diff --git a/civo/datasource_loadbalancer.go b/civo/datasource_loadbalancer.go new file mode 100644 index 00000000..9fab063d --- /dev/null +++ b/civo/datasource_loadbalancer.go @@ -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 +} diff --git a/civo/datasource_loadbalancer.go.disabled b/civo/datasource_loadbalancer.go.disabled deleted file mode 100644 index 115bed55..00000000 --- a/civo/datasource_loadbalancer.go.disabled +++ /dev/null @@ -1,162 +0,0 @@ -package civo - -import ( - "fmt" - "log" - "strings" - - "github.com/civo/civogo" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" -) - -// 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, - ValidateFunc: validation.NoZeroValues, - ExactlyOneOf: []string{"id", "hostname"}, - }, - "hostname": { - Type: schema.TypeString, - Optional: true, - ValidateFunc: validation.NoZeroValues, - ExactlyOneOf: []string{"id", "hostname"}, - Description: "The hostname of the load balancer", - }, - "region": { - Type: schema.TypeString, - Optional: true, - ValidateFunc: validation.NoZeroValues, - Description: "The region where load balancer is running", - }, - // Computed resource - "protocol": { - Type: schema.TypeString, - Computed: true, - Description: "The protocol used", - }, - "tls_certificate": { - Type: schema.TypeString, - Computed: true, - Description: "If is set will be returned", - }, - "tls_key": { - Type: schema.TypeString, - Computed: true, - Description: "If is set will be returned", - }, - "port": { - Type: schema.TypeInt, - Computed: true, - Description: "The port set in the configuration", - }, - "max_request_size": { - Type: schema.TypeInt, - Computed: true, - Description: "The max request size set in the configuration", - }, - "policy": { - Type: schema.TypeString, - Computed: true, - Description: "The policy set in the load balancer", - }, - "health_check_path": { - Type: schema.TypeString, - Computed: true, - Description: "The path to check the health of the backend", - }, - "fail_timeout": { - Type: schema.TypeInt, - Computed: true, - Description: "The wait time until the backend is marked as a failure", - }, - "max_conns": { - Type: schema.TypeInt, - Computed: true, - Description: "How many concurrent connections can each backend handle", - }, - "ignore_invalid_backend_tls": { - Type: schema.TypeBool, - Computed: true, - Description: "Should self-signed/invalid certificates be ignored from the backend servers", - }, - "backend": { - Type: schema.TypeList, - Computed: true, - Description: "", - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "instance_id": { - Type: schema.TypeString, - Computed: true, - Description: "The instance ID", - }, - "protocol": { - Type: schema.TypeString, - Computed: true, - Description: "The protocol used in the configuration", - }, - "port": { - Type: schema.TypeInt, - Computed: true, - Description: "The port set in the configuration", - }, - }, - }, - }, - }, - } -} - -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 id, ok := d.GetOk("id"); ok { - log.Printf("[INFO] Getting the LoadBalancer by id") - searchBy = id.(string) - } else if hostname, ok := d.GetOk("hostname"); ok { - log.Printf("[INFO] Getting the LoadBalancer by hostname") - searchBy = hostname.(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("hostname", lb.Hostname) - d.Set("protocol", lb.Protocol) - d.Set("tls_certificate", lb.TLSCertificate) - d.Set("tls_key", lb.TLSKey) - d.Set("port", lb.Port) - d.Set("max_request_size", lb.MaxRequestSize) - d.Set("policy", lb.Policy) - d.Set("health_check_path", lb.HealthCheckPath) - d.Set("fail_timeout", lb.FailTimeout) - d.Set("max_conns", lb.MaxConns) - d.Set("ignore_invalid_backend_tls", lb.IgnoreInvalidBackendTLS) - - if err := d.Set("backend", flattenLoadBalancerBackend(lb.Backends)); err != nil { - return fmt.Errorf("[ERR] error retrieving the backend for load balancer error: %#v", err) - } - - return nil -} diff --git a/civo/provider.go b/civo/provider.go index 78b6e143..3451c486 100644 --- a/civo/provider.go +++ b/civo/provider.go @@ -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(), },