Skip to content

Commit

Permalink
Added the new data source loadbalancer to get one loadbalancer from t…
Browse files Browse the repository at this point in the history
…he civo cloud

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed May 21, 2020
1 parent c97d499 commit 255aaf4
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
130 changes: 130 additions & 0 deletions civo/datasource_loadbalancer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package civo

import (
"fmt"
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"log"
)

// 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{
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"},
},
// Computed resource
"protocol": {
Type: schema.TypeString,
Computed: true,
},
"tls_certificate": {
Type: schema.TypeString,
Computed: true,
},
"tls_key": {
Type: schema.TypeString,
Computed: true,
},
"port": {
Type: schema.TypeInt,
Computed: true,
},
"max_request_size": {
Type: schema.TypeInt,
Computed: true,
},
"policy": {
Type: schema.TypeString,
Computed: true,
},
"health_check_path": {
Type: schema.TypeString,
Computed: true,
},
"fail_timeout": {
Type: schema.TypeInt,
Computed: true,
},
"max_conns": {
Type: schema.TypeInt,
Computed: true,
},
"ignore_invalid_backend_tls": {
Type: schema.TypeBool,
Computed: true,
},
"backend": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Computed: true,
},
"protocol": {
Type: schema.TypeString,
Computed: true,
},
"port": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
}
}

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

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
}
1 change: 1 addition & 0 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func Provider() terraform.ResourceProvider {
"civo_dns_domain_record": dataSourceDnsDomainRecord(),
"civo_network": dataSourceNetwork(),
"civo_volume": dataSourceVolume(),
"civo_loadbalancer": dataSourceLoadBalancer(),
},
ResourcesMap: map[string]*schema.Resource{
"civo_instance": resourceInstance(),
Expand Down
54 changes: 54 additions & 0 deletions website/docs/d/loadbalancer.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
layout: "civo"
page_title: "Civo: civo_loadbalancer"
sidebar_current: "docs-civo-datasource-loadbalancer"
description: |-
Get information on a loadbalancer.
---

# civo_loadbalancer

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. This is useful if the load balancer in question is not managed by
Terraform or you need to utilize any of the load balancers data.

An error is triggered if the provided load balancer name does not exist.

## Example Usage

Get the load balancer:

```hcl
data "civo_loadbalancer" "example" {
hostname = "example.com"
}
```

## Argument Reference

The following arguments are supported:

* `id` - (Optional) The ID of the Load Balancer.
* `hostname` - (Optional) The hostname of the Load Balancer.

## Attributes Reference

The following attributes are exported:

* `id` - The ID of the Load Balancer
* `hostname` - The hostname of the Load Balancer
* `protocol` - The protocol used
* `tls_certificate` - If is set will be returned
* `tls_key` - If is set will be returned
* `port` - The port set in the configuration
* `max_request_size` - The max request size set in the configuration
* `policy` - The policy set in the Load Balancer
* `health_check_path` - The path to check the health of the backend
* `fail_timeout` - The wait time until the backend is marked as a failure
* `max_conns` - How many concurrent connections can each backend handle
* `ignore_invalid_backend_tls` - Should self-signed/invalid certificates be ignored from the backend servers
* `backend` - A list of backend instances
- `instance_id` - The instance id
- `protocol` - The protocol used in the configuration.
- `port` - The port set in the configuration.

0 comments on commit 255aaf4

Please sign in to comment.