-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the new data source loadbalancer to get one loadbalancer from t…
…he civo cloud Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
- Loading branch information
1 parent
c97d499
commit 255aaf4
Showing
3 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |