Skip to content

Commit 47e4fae

Browse files
committed
LB: Datasources for BackendHealth, BackendSetHealth, LoadBalancerHealth
1 parent c91e49b commit 47e4fae

12 files changed

+693
-4
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# oci_load_balancer_backend_health
2+
3+
## BackendHealth Singular DataSource
4+
5+
### BackendHealth Reference
6+
7+
The following attributes are exported:
8+
9+
* `health_check_results` - A list of the most recent health check results returned for the specified backend server.
10+
* `health_check_status` - The result of the most recent health check.
11+
* `source_ip_address` - The IP address of the health check status report provider. This identifier helps you differentiate same-subnet (private) load balancers that report health check status. Example: `10.0.0.7`
12+
* `subnet_id` - The [OCID](https://docs.us-phoenix-1.oraclecloud.com/Content/General/Concepts/identifiers.htm) of the subnet hosting the load balancer that reported this health check status.
13+
* `timestamp` - The date and time the data was retrieved, in the format defined by RFC3339. Example: `2017-06-02T18:28:11+00:00`
14+
* `status` - The general health status of the specified backend server as reported by the primary and standby load balancers. * **OK:** Both health checks returned `OK`. * **WARNING:** One health check returned `OK` and one did not. * **CRITICAL:** Neither health check returned `OK`. * **UNKNOWN:** One or both health checks returned `UNKNOWN`, or the system was unable to retrieve metrics at this time.
15+
16+
17+
18+
### Get Operation
19+
Gets the current health status of the specified backend server.
20+
21+
The following arguments are supported:
22+
23+
* `backend_name` - (Required) The IP address and port of the backend server to retrieve the health status for. Example: `10.0.0.3:8080`
24+
* `backend_set_name` - (Required) The name of the backend set associated with the backend server to retrieve the health status for. Example: `example_backend_set`
25+
* `load_balancer_id` - (Required) The [OCID](https://docs.us-phoenix-1.oraclecloud.com/Content/General/Concepts/identifiers.htm) of the load balancer associated with the backend server health status to be retrieved.
26+
27+
28+
### Example Usage
29+
30+
```hcl
31+
data "oci_load_balancer_backend_health" "test_backend_health" {
32+
#Required
33+
backend_name = "${var.backend_health_backend_name}"
34+
backend_set_name = "${var.backend_health_backend_set_name}"
35+
load_balancer_id = "${oci_load_balancer_load_balancer.test_load_balancer.id}"
36+
}
37+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# oci_load_balancer_backend_set_health
2+
3+
## BackendSetHealth Singular DataSource
4+
5+
### BackendSetHealth Reference
6+
7+
The following attributes are exported:
8+
9+
* `critical_state_backend_names` - A list of backend servers that are currently in the `CRITICAL` health state. The list identifies each backend server by IP address and port. Example: `10.0.0.4:8080`
10+
* `status` - Overall health status of the backend set. * **OK:** All backend servers in the backend set return a status of `OK`. * **WARNING:** Half or more of the backend set's backend servers return a status of `OK` and at least one backend server returns a status of `WARNING`, `CRITICAL`, or `UNKNOWN`. * **CRITICAL:** Fewer than half of the backend set's backend servers return a status of `OK`. * **UNKNOWN:** More than half of the backend set's backend servers return a status of `UNKNOWN`, the system was unable to retrieve metrics, or the backend set does not have a listener attached.
11+
* `total_backend_count` - The total number of backend servers in this backend set. Example: `7`
12+
* `unknown_state_backend_names` - A list of backend servers that are currently in the `UNKNOWN` health state. The list identifies each backend server by IP address and port. Example: `10.0.0.5:8080`
13+
* `warning_state_backend_names` - A list of backend servers that are currently in the `WARNING` health state. The list identifies each backend server by IP address and port. Example: `10.0.0.3:8080`
14+
15+
16+
17+
### Get Operation
18+
Gets the health status for the specified backend set.
19+
20+
The following arguments are supported:
21+
22+
* `backend_set_name` - (Required) The name of the backend set to retrieve the health status for. Example: `example_backend_set`
23+
* `load_balancer_id` - (Required) The [OCID](https://docs.us-phoenix-1.oraclecloud.com/Content/General/Concepts/identifiers.htm) of the load balancer associated with the backend set health status to be retrieved.
24+
25+
26+
### Example Usage
27+
28+
```hcl
29+
data "oci_load_balancer_backend_set_health" "test_backend_set_health" {
30+
#Required
31+
backend_set_name = "${var.backend_set_health_backend_set_name}"
32+
load_balancer_id = "${oci_load_balancer_load_balancer.test_load_balancer.id}"
33+
}
34+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# oci_load_balancer_health
2+
3+
## LoadBalancerHealth Singular DataSource
4+
5+
### LoadBalancerHealth Reference
6+
7+
The following attributes are exported:
8+
9+
* `critical_state_backend_set_names` - A list of backend sets that are currently in the `CRITICAL` health state. The list identifies each backend set by the friendly name you assigned when you created it. Example: `example_backend_set`
10+
* `status` - The overall health status of the load balancer. * **OK:** All backend sets associated with the load balancer return a status of `OK`. * **WARNING:** At least one of the backend sets associated with the load balancer returns a status of `WARNING`, no backend sets return a status of `CRITICAL`, and the load balancer life cycle state is `ACTIVE`. * **CRITICAL:** One or more of the backend sets associated with the load balancer return a status of `CRITICAL`. * **UNKNOWN:** If any one of the following conditions is true: * The load balancer life cycle state is not `ACTIVE`. * No backend sets are defined for the load balancer. * More than half of the backend sets associated with the load balancer return a status of `UNKNOWN`, none of the backend sets return a status of `WARNING` or `CRITICAL`, and the load balancer life cycle state is `ACTIVE`. * The system could not retrieve metrics for any reason.
11+
* `total_backend_set_count` - The total number of backend sets associated with this load balancer. Example: `4`
12+
* `unknown_state_backend_set_names` - A list of backend sets that are currently in the `UNKNOWN` health state. The list identifies each backend set by the friendly name you assigned when you created it. Example: `example_backend_set2`
13+
* `warning_state_backend_set_names` - A list of backend sets that are currently in the `WARNING` health state. The list identifies each backend set by the friendly name you assigned when you created it. Example: `example_backend_set3`
14+
15+
16+
17+
### Get Operation
18+
Gets the health status for the specified load balancer.
19+
20+
The following arguments are supported:
21+
22+
* `load_balancer_id` - (Required) The [OCID](https://docs.us-phoenix-1.oraclecloud.com/Content/General/Concepts/identifiers.htm) of the load balancer to return health status for.
23+
24+
25+
### Example Usage
26+
27+
```hcl
28+
data "oci_load_balancer_health" "test_load_balancer_health" {
29+
#Required
30+
load_balancer_id = "${oci_load_balancer_load_balancer.test_load_balancer.id}"
31+
}
32+
```
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+
3+
package provider
4+
5+
import (
6+
"context"
7+
8+
"github.com/hashicorp/terraform/helper/schema"
9+
oci_load_balancer "github.com/oracle/oci-go-sdk/loadbalancer"
10+
11+
"github.com/oracle/terraform-provider-oci/crud"
12+
)
13+
14+
func BackendHealthDataSource() *schema.Resource {
15+
return &schema.Resource{
16+
Read: readSingularBackendHealth,
17+
Schema: map[string]*schema.Schema{
18+
"backend_name": {
19+
Type: schema.TypeString,
20+
Required: true,
21+
},
22+
"backend_set_name": {
23+
Type: schema.TypeString,
24+
Required: true,
25+
},
26+
"load_balancer_id": {
27+
Type: schema.TypeString,
28+
Required: true,
29+
},
30+
// Computed
31+
"health_check_results": {
32+
Type: schema.TypeList,
33+
Computed: true,
34+
Elem: &schema.Resource{
35+
Schema: map[string]*schema.Schema{
36+
// Required
37+
38+
// Optional
39+
40+
// Computed
41+
"health_check_status": {
42+
Type: schema.TypeString,
43+
Computed: true,
44+
},
45+
"source_ip_address": {
46+
Type: schema.TypeString,
47+
Computed: true,
48+
},
49+
"subnet_id": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
},
53+
"timestamp": {
54+
Type: schema.TypeString,
55+
Computed: true,
56+
},
57+
},
58+
},
59+
},
60+
"status": {
61+
Type: schema.TypeString,
62+
Computed: true,
63+
},
64+
},
65+
}
66+
}
67+
68+
func readSingularBackendHealth(d *schema.ResourceData, m interface{}) error {
69+
sync := &BackendHealthDataSourceCrud{}
70+
sync.D = d
71+
sync.Client = m.(*OracleClients).loadBalancerClient
72+
73+
return crud.ReadResource(sync)
74+
}
75+
76+
type BackendHealthDataSourceCrud struct {
77+
D *schema.ResourceData
78+
Client *oci_load_balancer.LoadBalancerClient
79+
Res *oci_load_balancer.GetBackendHealthResponse
80+
}
81+
82+
func (s *BackendHealthDataSourceCrud) VoidState() {
83+
s.D.SetId("")
84+
}
85+
86+
func (s *BackendHealthDataSourceCrud) Get() error {
87+
request := oci_load_balancer.GetBackendHealthRequest{}
88+
89+
if backendName, ok := s.D.GetOkExists("backend_name"); ok {
90+
tmp := backendName.(string)
91+
request.BackendName = &tmp
92+
}
93+
94+
if backendSetName, ok := s.D.GetOkExists("backend_set_name"); ok {
95+
tmp := backendSetName.(string)
96+
request.BackendSetName = &tmp
97+
}
98+
99+
if loadBalancerId, ok := s.D.GetOkExists("load_balancer_id"); ok {
100+
tmp := loadBalancerId.(string)
101+
request.LoadBalancerId = &tmp
102+
}
103+
104+
request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "load_balancer")
105+
106+
response, err := s.Client.GetBackendHealth(context.Background(), request)
107+
if err != nil {
108+
return err
109+
}
110+
111+
s.Res = &response
112+
return nil
113+
}
114+
115+
func (s *BackendHealthDataSourceCrud) SetData() {
116+
if s.Res == nil {
117+
return
118+
}
119+
120+
s.D.SetId(crud.GenerateDataSourceID())
121+
122+
healthCheckResults := []interface{}{}
123+
for _, item := range s.Res.HealthCheckResults {
124+
healthCheckResults = append(healthCheckResults, HealthCheckResultToMap(item))
125+
}
126+
s.D.Set("health_check_results", healthCheckResults)
127+
128+
s.D.Set("status", s.Res.Status)
129+
130+
return
131+
}
132+
133+
func HealthCheckResultToMap(obj oci_load_balancer.HealthCheckResult) map[string]interface{} {
134+
result := map[string]interface{}{}
135+
136+
result["health_check_status"] = string(obj.HealthCheckStatus)
137+
138+
if obj.SourceIpAddress != nil {
139+
result["source_ip_address"] = string(*obj.SourceIpAddress)
140+
}
141+
142+
if obj.SubnetId != nil {
143+
result["subnet_id"] = string(*obj.SubnetId)
144+
}
145+
146+
if obj.Timestamp != nil {
147+
result["timestamp"] = obj.Timestamp.String()
148+
}
149+
150+
return result
151+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+
3+
package provider
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform/helper/resource"
10+
"github.com/hashicorp/terraform/terraform"
11+
)
12+
13+
const (
14+
BackendHealthResourceConfig = BackendHealthResourceDependencies + `
15+
16+
`
17+
BackendHealthPropertyVariables = `
18+
variable "backend_health_backend_name" { default = "backendName" }
19+
variable "backend_health_backend_set_name" { default = "backendSetName" }
20+
variable "backend_health_load_balancer_id" { default = "loadBalancerId" }
21+
22+
`
23+
BackendHealthResourceDependencies = BackendRequiredOnlyResource + BackendPropertyVariables
24+
)
25+
26+
func TestLoadBalancerBackendHealthResource_basic(t *testing.T) {
27+
provider := testAccProvider
28+
config := testProviderConfig()
29+
30+
compartmentId := getRequiredEnvSetting("compartment_ocid")
31+
compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId)
32+
33+
singularDatasourceName := "data.oci_load_balancer_backend_health.test_backend_health"
34+
35+
resource.Test(t, resource.TestCase{
36+
Providers: map[string]terraform.ResourceProvider{
37+
"oci": provider,
38+
},
39+
Steps: []resource.TestStep{
40+
// verify singular datasource
41+
{
42+
Config: config + `
43+
variable "backend_health_backend_name" { default = "backendName" }
44+
variable "backend_health_backend_set_name" { default = "backendSetName" }
45+
variable "backend_health_load_balancer_id" { default = "loadBalancerId" }
46+
47+
data "oci_load_balancer_backend_health" "test_backend_health" {
48+
#Required
49+
backend_name = "${oci_load_balancer_backend.test_backend.name}"
50+
backend_set_name = "${oci_load_balancer_backend_set.test_backend_set.name}"
51+
load_balancer_id = "${oci_load_balancer_load_balancer.test_load_balancer.id}"
52+
}
53+
` + compartmentIdVariableStr + BackendHealthResourceConfig,
54+
Check: resource.ComposeAggregateTestCheckFunc(
55+
resource.TestCheckResourceAttr(singularDatasourceName, "backend_name", "10.0.0.3:10"),
56+
resource.TestCheckResourceAttr(singularDatasourceName, "backend_set_name", "backendSet1"),
57+
resource.TestCheckResourceAttrSet(singularDatasourceName, "load_balancer_id"),
58+
59+
resource.TestCheckResourceAttr(singularDatasourceName, "health_check_results.#", "0"),
60+
resource.TestCheckResourceAttrSet(singularDatasourceName, "status"),
61+
),
62+
},
63+
},
64+
})
65+
}

0 commit comments

Comments
 (0)