forked from oracle/terraform-provider-oci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_obmcs_loadbalancer_backendsets.go
118 lines (106 loc) · 3.03 KB
/
data_source_obmcs_loadbalancer_backendsets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
package main
import (
"log"
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/oracle/bmcs-go-sdk"
"github.com/oracle/terraform-provider-oci/crud"
)
func BackendSetDatasource() *schema.Resource {
return &schema.Resource{
Read: readBackendSets,
Schema: map[string]*schema.Schema{
"load_balancer_id": {
Type: schema.TypeString,
Required: true,
},
"backendsets": {
Type: schema.TypeList,
Computed: true,
Elem: LoadBalancerBackendSetResource(),
},
},
}
}
func readBackendSets(d *schema.ResourceData, m interface{}) (e error) {
client := m.(*OracleClients)
sync := &BackendSetDatasourceCrud{}
sync.D = d
sync.Client = client.client
return crud.ReadResource(sync)
}
type BackendSetDatasourceCrud struct {
crud.BaseCrud
Res *baremetal.ListBackendSets
}
func (s *BackendSetDatasourceCrud) Get() (e error) {
lbID := s.D.Get("load_balancer_id").(string)
s.Res, e = s.Client.ListBackendSets(lbID, nil)
return
}
func (s *BackendSetDatasourceCrud) SetData() {
if s.Res == nil {
panic("LoadBalancer Backend Resource is nil, cannot SetData")
}
s.D.SetId(time.Now().UTC().String())
resources := []map[string]interface{}{}
for _, v := range s.Res.BackendSets {
var healthChecker []map[string]interface{}
if hc := v.HealthChecker; hc != nil {
healthChecker = []map[string]interface{}{
{
"interval_ms": hc.IntervalInMS,
"port": hc.Port,
"protocol": hc.Protocol,
"response_body_regex": hc.ResponseBodyRegex,
},
}
}
var sslConfig []map[string]interface{}
if ssl := v.SSLConfig; ssl != nil {
sslConfig = []map[string]interface{}{
{
"certificate_name": ssl.CertificateName,
"verify_depth": ssl.VerifyDepth,
"verify_peer_certificate": ssl.VerifyPeerCertificate,
},
}
}
var sessionConfig []map[string]interface{}
if session := v.SessionPersistenceConfig; session != nil {
sessionConfig = []map[string]interface{}{
{
"cookie_name": session.CookieName,
"disable_fallback": session.DisableFallback,
},
}
}
backends := []map[string]interface{}{}
for _, backend := range v.Backends {
res := map[string]interface{}{
"ip_address": backend.IPAddress,
"port": backend.Port,
"backup": backend.Backup,
"drain": backend.Drain,
"offline": backend.Offline,
"weight": backend.Weight,
}
backends = append(backends, res)
}
res := map[string]interface{}{
"name": v.Name,
"policy": v.Policy,
"health_checker": healthChecker,
"ssl_configuration": sslConfig,
"session_persistence_configuration": sessionConfig,
"backend": backends,
}
resources = append(resources, res)
}
err := s.D.Set("backendsets", resources)
if err != nil {
log.Printf("[ERROR] Failed to set load_balancers: %v", err)
}
return
}