Skip to content

Commit 1838695

Browse files
committed
Fix load balancer backendset creation without certificates. Closes #85
1 parent 95eea02 commit 1838695

File tree

5 files changed

+79
-52
lines changed

5 files changed

+79
-52
lines changed

crud/helpers.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ func (s *BaseCrud) State() string {
6868
}
6969

7070
func handleMissingResourceError(sync ResourceVoider, err *error) {
71-
if err != nil && (
72-
strings.Contains((*err).Error(), "does not exist") ||
71+
72+
if err != nil {
73+
if strings.Contains((*err).Error(), "does not exist") ||
7374
strings.Contains((*err).Error(), " not present in ") ||
7475
strings.Contains((*err).Error(), "resource not found") ||
75-
(strings.Contains((*err).Error(), "Load balancer") && strings.Contains((*err).Error(), " has no "))) {
76-
log.Println("[DEBUG] Object does not exist, voiding resource and nullifying error")
77-
sync.VoidState()
78-
*err = nil
76+
(strings.Contains((*err).Error(), "Load balancer") && strings.Contains((*err).Error(), " has no ")) {
77+
log.Println("[DEBUG] Object does not exist, voiding resource and nullifying error")
78+
sync.VoidState()
79+
*err = nil
80+
}
7981
}
8082
}
8183

@@ -116,7 +118,6 @@ func LoadBalancerResourceID(res interface{}, workReq *baremetal.WorkRequest) (id
116118

117119
func LoadBalancerResourceGet(s BaseCrud, workReq *baremetal.WorkRequest) (id string, stillWorking bool, err error) {
118120
id = s.D.Id()
119-
log.Printf("================== ID in LoadbalancerResourceGet: %s\n", id)
120121
// NOTE: if the id is for a work request, refresh its state and loadBalancerID.
121122
if strings.HasPrefix(id, "ocid1.loadbalancerworkrequest.") {
122123
updatedWorkReq, err := s.Client.GetWorkRequest(id, nil)
@@ -169,8 +170,20 @@ func ReadResource(sync ResourceReader) (e error) {
169170
handleMissingResourceError(sync, &e)
170171
return
171172
}
173+
172174
sync.SetData()
173175

176+
/* Attempt at #113, but this breaks everything. Probably because this is used internally by other state checking mechanisms.
177+
178+
if dr, ok := sync.(StatefullyDeletedResource); ok {
179+
for _, target := range dr.DeletedTarget() {
180+
if dr.State() == target {
181+
dr.VoidState()
182+
return
183+
}
184+
}
185+
}*/
186+
174187
return
175188
}
176189

@@ -191,12 +204,14 @@ func UpdateResource(d *schema.ResourceData, sync ResourceUpdater) (e error) {
191204
// Finally, sets the ResourceData state to empty.
192205
func DeleteResource(d *schema.ResourceData, sync ResourceDeleter) (e error) {
193206
if e = sync.Delete(); e != nil {
194-
return
207+
handleMissingResourceError(sync, &e)
208+
if e != nil {
209+
return
210+
}
195211
}
196212

197213
if stateful, ok := sync.(StatefullyDeletedResource); ok {
198214
e = waitForStateRefresh(stateful, d.Timeout(schema.TimeoutDelete), stateful.DeletedPending(), stateful.DeletedTarget())
199-
200215
}
201216

202217
if ew, waitOK := sync.(ExtraWaitPostCreateDelete); waitOK {

resource_obmcs_loadbalancer_backend_set.go

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,11 @@ func (s *LoadBalancerBackendSetResourceCrud) Get() (e error) {
158158
}
159159

160160
func (s *LoadBalancerBackendSetResourceCrud) Update() (e error) {
161-
healthChecker := baremetal.HealthChecker{
162-
IntervalInMS: s.D.Get("health_checker.interval_ms").(int),
163-
Port: s.D.Get("health_checker.port").(int),
164-
Protocol: s.D.Get("health_checker.protocol").(string),
165-
ResponseBodyRegex: s.D.Get("health_checker.response_body_regex").(string),
166-
URLPath: s.D.Get("health_checker.url_path").(string),
167-
}
168-
sslConfig := baremetal.SSLConfiguration{
169-
CertificateName: s.D.Get("ssl_configuration.certificate_name").(string),
170-
VerifyDepth: s.D.Get("ssl_configuration.verify_depth").(int),
171-
VerifyPeerCertificate: s.D.Get("ssl_configuration.verify_peer_certificate").(bool),
172-
}
173-
opts := &baremetal.UpdateLoadBalancerBackendSetOptions{
174-
Policy: s.D.Get("policy").(string),
175-
SSLConfig: sslConfig,
176-
HealthChecker: healthChecker,
177-
}
161+
opts := &baremetal.UpdateLoadBalancerBackendSetOptions{}
162+
163+
opts.HealthChecker = s.healthChecker()
164+
opts.SSLConfig = s.sslConfig()
165+
opts.Policy = s.D.Get("policy").(string)
178166

179167
var workReqID string
180168
workReqID, e = s.Client.UpdateBackendSet(s.D.Get("load_balancer_id").(string), s.D.Id(), opts)
@@ -191,18 +179,24 @@ func (s *LoadBalancerBackendSetResourceCrud) SetData() {
191179
}
192180
s.D.Set("policy", s.Resource.Policy)
193181
s.D.Set("name", s.Resource.Name)
194-
s.D.Set("health_checker", map[string]interface{}{
195-
"interval_ms": s.Resource.HealthChecker.IntervalInMS,
196-
"port": s.Resource.HealthChecker.Port,
197-
"protocol": s.Resource.HealthChecker.Protocol,
198-
"response_body_regex": s.Resource.HealthChecker.ResponseBodyRegex,
199-
"url_path": s.Resource.HealthChecker.URLPath,
200-
})
201-
s.D.Set("ssl_configuration", map[string]interface{}{
202-
"certificate_name": s.Resource.SSLConfig.CertificateName,
203-
"verify_depth": s.Resource.SSLConfig.VerifyDepth,
204-
"verify_peer_certificate": s.Resource.SSLConfig.VerifyPeerCertificate,
205-
})
182+
if s.Resource.HealthChecker != nil {
183+
s.D.Set("health_checker", map[string]interface{}{
184+
"interval_ms": s.Resource.HealthChecker.IntervalInMS,
185+
"port": s.Resource.HealthChecker.Port,
186+
"protocol": s.Resource.HealthChecker.Protocol,
187+
"response_body_regex": s.Resource.HealthChecker.ResponseBodyRegex,
188+
"url_path": s.Resource.HealthChecker.URLPath,
189+
})
190+
}
191+
192+
if s.Resource.SSLConfig != nil {
193+
s.D.Set("ssl_configuration", map[string]interface{}{
194+
"certificate_name": s.Resource.SSLConfig.CertificateName,
195+
"verify_depth": s.Resource.SSLConfig.VerifyDepth,
196+
"verify_peer_certificate": s.Resource.SSLConfig.VerifyPeerCertificate,
197+
})
198+
}
199+
206200
backends := make([]map[string]interface{}, len(s.Resource.Backends))
207201
for i, v := range s.Resource.Backends {
208202
backends[i] = map[string]interface{}{
@@ -229,30 +223,33 @@ func (s *LoadBalancerBackendSetResourceCrud) Delete() (e error) {
229223
}
230224

231225
func (s *LoadBalancerBackendSetResourceCrud) sslConfig() (sslConfig *baremetal.SSLConfiguration) {
232-
sslConfig = new(baremetal.SSLConfiguration)
233226
vs := s.D.Get("ssl_configuration").([]interface{})
234227
if len(vs) == 1 {
228+
sslConfig = new(baremetal.SSLConfiguration)
235229
v := vs[0].(map[string]interface{})
236230
sslConfig.CertificateName = v["certificate_name"].(string)
237231
sslConfig.VerifyDepth = v["verify_depth"].(int)
238232
sslConfig.VerifyPeerCertificate = v["verify_peer_certificate"].(bool)
233+
return sslConfig
239234
}
240235

241-
return
236+
return nil
242237
}
243238

244239
func (s *LoadBalancerBackendSetResourceCrud) healthChecker() *baremetal.HealthChecker {
245-
healthChecker := new(baremetal.HealthChecker)
240+
246241
vs := s.D.Get("health_checker").([]interface{})
247242
if len(vs) == 1 {
243+
healthChecker := new(baremetal.HealthChecker)
248244
v := vs[0].(map[string]interface{})
249245
healthChecker.IntervalInMS = v["interval_ms"].(int)
250246
healthChecker.Port = v["port"].(int)
251247
healthChecker.Protocol = v["protocol"].(string)
252248
healthChecker.ResponseBodyRegex = v["response_body_regex"].(string)
253249
healthChecker.URLPath = v["url_path"].(string)
250+
return healthChecker
254251
}
255-
return healthChecker
252+
return nil
256253
}
257254
func (s *LoadBalancerBackendSetResourceCrud) backends() []baremetal.Backend {
258255
vs := s.D.Get("backend").([]interface{})

resource_obmcs_loadbalancer_loadbalancer.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ type LoadBalancerResourceCrud struct {
100100
// ID delegates to the load balancer ID, falling back to the work request ID
101101
func (s *LoadBalancerResourceCrud) ID() string {
102102
id, workSuccess := crud.LoadBalancerResourceID(s.Resource, s.WorkRequest)
103-
log.Printf("==================\n%s,%v\n", *id, workSuccess)
104103
if id != nil {
105104
return *id
106105
}
@@ -175,7 +174,6 @@ func (s *LoadBalancerResourceCrud) Create() (e error) {
175174
func (s *LoadBalancerResourceCrud) Get() (e error) {
176175
// key: {workRequestID} || {loadBalancerID}
177176
id, stillWorking, err := crud.LoadBalancerResourceGet(s.BaseCrud, s.WorkRequest)
178-
log.Printf("==================\n%s,%v,%v,%v\n", id, stillWorking, err, s.WorkRequest)
179177
if err != nil {
180178
return err
181179
}
@@ -212,7 +210,7 @@ func (s *LoadBalancerResourceCrud) Update() (e error) {
212210
func (s *LoadBalancerResourceCrud) SetData() {
213211
// The first time this is called, we haven't actually fetched the resource yet, we just got a work request
214212
if s.Resource != nil && s.Resource.ID != "" {
215-
s.D.SetId(s.Resource.ID)
213+
//s.D.SetId(s.Resource.ID)
216214
s.D.Set("compartment_id", s.Resource.CompartmentID)
217215
s.D.Set("display_name", s.Resource.DisplayName)
218216
s.D.Set("shape", s.Resource.Shape)

resource_obmcs_loadbalancer_test.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ func (s *ResourceLoadBalancerTestSuite) SetupTest() {
3636
s.ResourceName = "baremetal_load_balancer.t"
3737
s.Config = loadbalancerConfig + certificateConfig + `
3838
39+
resource "baremetal_load_balancer_backendset" "no_cert" {
40+
load_balancer_id = "${baremetal_load_balancer.t.id}"
41+
name = "stub_backendset_name_no_cert"
42+
policy = "ROUND_ROBIN"
43+
44+
health_checker {
45+
interval_ms = 30000
46+
port = 1234
47+
protocol = "HTTP"
48+
response_body_regex = ".*"
49+
url_path = "/"
50+
}
51+
}
52+
3953
resource "baremetal_load_balancer_backendset" "t" {
4054
load_balancer_id = "${baremetal_load_balancer.t.id}"
4155
name = "stub_backendset_name"
@@ -80,7 +94,7 @@ resource "baremetal_load_balancer_backend" "t" {
8094
offline = true
8195
weight = 1
8296
}
83-
`
97+
`
8498
s.Config += testProviderConfig()
8599
}
86100

@@ -99,18 +113,22 @@ func (s *ResourceLoadBalancerTestSuite) TestCreateResourceLoadBalancerMaximal()
99113
resource.TestCheckResourceAttrSet("baremetal_load_balancer.t", "ip_addresses.#"),
100114

101115
resource.TestCheckResourceAttr("baremetal_load_balancer_listener.t", "ssl_configuration.#", "1"),
102-
/*
116+
103117
// Certificate
104-
resource.TestCheckResourceAttrSet("baremetal_load_balancer_certificate.t.certificate_name", "stub_certificate_name"),
118+
resource.TestCheckResourceAttr("baremetal_load_balancer_certificate.t", "certificate_name", "stub_certificate_name"),
105119

106120
// BackendSet
107121
resource.TestCheckResourceAttr("baremetal_load_balancer_backendset.t", "name", "stub_backendset_name"),
108-
resource.TestCheckResourceAttr("baremetal_load_balancer_backendset.t", "health_checker.port", "1234"),
122+
resource.TestCheckResourceAttr("baremetal_load_balancer_backendset.t", "health_checker.0.port", "1234"),
109123
resource.TestCheckResourceAttr("baremetal_load_balancer_backendset.t", "ssl_configuration.#", "1"),
110124
resource.TestCheckResourceAttr("baremetal_load_balancer_backendset.t", "ssl_configuration.0.certificate_name", "stub_certificate_name"),
111125
resource.TestCheckResourceAttr("baremetal_load_balancer_backendset.t", "ssl_configuration.0.verify_depth", "6"),
112126
resource.TestCheckResourceAttr("baremetal_load_balancer_backendset.t", "ssl_configuration.0.verify_peer_certificate", "false"),
113127

128+
resource.TestCheckResourceAttr("baremetal_load_balancer_backendset.no_cert", "name", "stub_backendset_name_no_cert"),
129+
resource.TestCheckResourceAttr("baremetal_load_balancer_backendset.no_cert", "health_checker.0.port", "1234"),
130+
131+
114132
// Listener
115133
resource.TestCheckResourceAttr("baremetal_load_balancer_listener.t", "name", "stub_listener_name"),
116134

@@ -124,7 +142,6 @@ func (s *ResourceLoadBalancerTestSuite) TestCreateResourceLoadBalancerMaximal()
124142
resource.TestCheckResourceAttr("baremetal_load_balancer_backend.t", "drain", "true"),
125143
resource.TestCheckResourceAttr("baremetal_load_balancer_backend.t", "offline", "true"),
126144
resource.TestCheckResourceAttr("baremetal_load_balancer_backend.t", "weight", "1"),
127-
*/
128145
),
129146
},
130147
},

vendor/github.com/MustWin/baremetal-sdk-go/request_options.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)