Skip to content

Commit 7cfad70

Browse files
committed
More acceptance tests. Mostly fix load balancer endpoints
1 parent 8b66b59 commit 7cfad70

16 files changed

+261
-373
lines changed

crud/helpers.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"github.com/hashicorp/terraform/helper/schema"
1313

1414
"github.com/oracle/terraform-provider-baremetal/client"
15+
"github.com/MustWin/baremetal-sdk-go"
16+
"errors"
1517
)
1618

1719
var (
@@ -65,13 +67,62 @@ func (s *BaseCrud) State() string {
6567
}
6668

6769
func handleMissingResourceError(sync ResourceVoider, err *error) {
68-
if err != nil && strings.Contains((*err).Error(), "does not exist") {
70+
if err != nil && (
71+
strings.Contains((*err).Error(), "does not exist") ||
72+
strings.Contains((*err).Error(), " not present in ") ||
73+
strings.Contains((*err).Error(), "resource not found") ||
74+
(strings.Contains((*err).Error(), "Load balancer") && strings.Contains((*err).Error(), " has no "))) {
6975
log.Println("[DEBUG] Object does not exist, voiding resource and nullifying error")
7076
sync.VoidState()
7177
*err = nil
7278
}
7379
}
7480

81+
func LoadBalancerResourceID(res interface{}, workReq *baremetal.WorkRequest) (id *string, workReqSucceeded bool) {
82+
v := reflect.ValueOf(res).Elem()
83+
if v.IsValid() {
84+
id := v.FieldByName("ID")
85+
if id.IsValid() {
86+
s := id.String()
87+
return &s, false
88+
}
89+
name := v.FieldByName("Name")
90+
if name.IsValid() {
91+
s := name.String()
92+
return &s, false
93+
}
94+
}
95+
if workReq != nil {
96+
if workReq.State == baremetal.WorkRequestSucceeded {
97+
return nil, true
98+
} else {
99+
return &workReq.ID, false
100+
}
101+
}
102+
return nil, false
103+
}
104+
105+
func LoadBalancerResourceGet(s BaseCrud, workReq *baremetal.WorkRequest) (id string, stillWorking bool, err error) {
106+
id = s.D.Id()
107+
log.Printf("================== ID in LoadbalancerResourceGet: %s\n", id)
108+
// NOTE: if the id is for a work request, refresh its state and loadBalancerID.
109+
if strings.HasPrefix(id, "ocid1.loadbalancerworkrequest.") {
110+
updatedWorkReq, err := s.Client.GetWorkRequest(id, nil)
111+
if err != nil {
112+
return "", false, err
113+
}
114+
if workReq != nil {
115+
*workReq = *updatedWorkReq
116+
s.D.Set("state", workReq.State)
117+
if workReq.State == baremetal.WorkRequestSucceeded {
118+
return "", false, nil
119+
}
120+
}
121+
return "", true, nil
122+
}
123+
return id, false, nil
124+
}
125+
75126
func CreateResource(d *schema.ResourceData, sync ResourceCreator) (e error) {
76127
if e = sync.Create(); e != nil {
77128
// Check for conflicts and retry
@@ -180,6 +231,9 @@ func waitForStateRefresh(sync StatefulResource, timeout time.Duration, pending,
180231
handleMissingResourceError(sync, &e)
181232
return
182233
}
234+
if sync.State() == baremetal.ResourceFailed {
235+
return errors.New("Resource creation failed, state FAILED")
236+
}
183237

184238
return
185239
}

helpers_loadbalancer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ var HealthCheckerSchema = &schema.Schema{
2727
Type: schema.TypeString,
2828
Required: true,
2929
},
30+
"url_path": {
31+
Type: schema.TypeString,
32+
Optional: true,
33+
},
3034
},
3135
},
3236
}

provider_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ resource "baremetal_core_instance" "t" {
134134
}
135135
`
136136

137+
var certificateConfig = `
138+
resource "baremetal_load_balancer_certificate" "t" {
139+
load_balancer_id = "${baremetal_load_balancer.t.id}"
140+
ca_certificate = "-----BEGIN CERTIFICATE-----\nMIIBNzCB4gIJAKtwJkxUgNpzMA0GCSqGSIb3DQEBCwUAMCMxITAfBgNVBAoTGElu\ndGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0xNzA0MTIyMTU3NTZaFw0xODA0MTIy\nMTU3NTZaMCMxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDBcMA0G\nCSqGSIb3DQEBAQUAA0sAMEgCQQDlM8lz3BFJA6zBlsF63k9ajPVq3Q1WQoHQ3j35\n08DRKIfwqfV+CxL63W3dZrwL4TrjqorP5CQ36+I6OWALH2zVAgMBAAEwDQYJKoZI\nhvcNAQELBQADQQCEjHVQJoiiVpIIvDWF+4YDRReVuwzrvq2xduWw7CIsDWlYuGZT\nQKVY6tnTy2XpoUk0fqUvMB/M2HGQ1WqZGHs6\n-----END CERTIFICATE-----"
141+
certificate_name = "stub_certificate_name"
142+
private_key = "-----BEGIN RSA PRIVATE KEY-----\nMIIBOgIBAAJBAOUzyXPcEUkDrMGWwXreT1qM9WrdDVZCgdDePfnTwNEoh/Cp9X4L\nEvrdbd1mvAvhOuOqis/kJDfr4jo5YAsfbNUCAwEAAQJAJz8k4bfvJceBT2zXGIj0\noZa9d1z+qaSdwfwsNJkzzRyGkj/j8yv5FV7KNdSfsBbStlcuxUm4i9o5LXhIA+iQ\ngQIhAPzStAN8+Rz3dWKTjRWuCfy+Pwcmyjl3pkMPSiXzgSJlAiEA6BUZWHP0b542\nu8AizBT3b3xKr1AH2nkIx9OHq7F/QbECIHzqqpDypa8/QVuUZegpVrvvT/r7mn1s\nddS6cDtyJgLVAiEA1Z5OFQeuL2sekBRbMyP9WOW7zMBKakLL3TqL/3JCYxECIAkG\nl96uo1MjK/66X5zQXBG7F2DN2CbcYEz0r3c3vvfq\n-----END RSA PRIVATE KEY-----"
143+
public_certificate = "-----BEGIN CERTIFICATE-----\nMIIBNzCB4gIJAKtwJkxUgNpzMA0GCSqGSIb3DQEBCwUAMCMxITAfBgNVBAoTGElu\ndGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0xNzA0MTIyMTU3NTZaFw0xODA0MTIy\nMTU3NTZaMCMxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDBcMA0G\nCSqGSIb3DQEBAQUAA0sAMEgCQQDlM8lz3BFJA6zBlsF63k9ajPVq3Q1WQoHQ3j35\n08DRKIfwqfV+CxL63W3dZrwL4TrjqorP5CQ36+I6OWALH2zVAgMBAAEwDQYJKoZI\nhvcNAQELBQADQQCEjHVQJoiiVpIIvDWF+4YDRReVuwzrvq2xduWw7CIsDWlYuGZT\nQKVY6tnTy2XpoUk0fqUvMB/M2HGQ1WqZGHs6\n-----END CERTIFICATE-----"
144+
}
145+
`
146+
137147
var loadbalancerConfig = subnetConfig + `
138148
139149
resource "baremetal_core_subnet" "WebSubnetAD2" {

resource_obmcs_loadbalancer_backend.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ func LoadBalancerBackendResource() *schema.Resource {
2929
},
3030
"name": {
3131
Type: schema.TypeString,
32-
Required: true,
33-
ForceNew: true,
32+
Computed: true,
3433
},
3534
"ip_address": {
3635
Type: schema.TypeString,
@@ -116,7 +115,11 @@ func (s *LoadBalancerBackendResourceCrud) RefreshWorkRequest() (*baremetal.WorkR
116115
}
117116

118117
func (s *LoadBalancerBackendResourceCrud) ID() string {
119-
return s.D.Get("name").(string)
118+
id, _ := crud.LoadBalancerResourceID(s.Resource, s.WorkRequest)
119+
if id != nil {
120+
return *id
121+
}
122+
return ""
120123
}
121124

122125
func (s *LoadBalancerBackendResourceCrud) CreatedPending() []string {
@@ -131,6 +134,7 @@ func (s *LoadBalancerBackendResourceCrud) CreatedTarget() []string {
131134
return []string{
132135
baremetal.ResourceSucceededWorkRequest,
133136
baremetal.WorkRequestSucceeded,
137+
baremetal.ResourceFailed,
134138
}
135139
}
136140

@@ -204,7 +208,7 @@ func (s *LoadBalancerBackendResourceCrud) Update() (e error) {
204208

205209
func (s *LoadBalancerBackendResourceCrud) SetData() {
206210
if s.Resource == nil {
207-
panic("BackendSet Resource is nil, cannot SetData")
211+
return
208212
}
209213
s.D.Set("backup", s.Resource.Backup)
210214
s.D.Set("drain", s.Resource.Drain)

resource_obmcs_loadbalancer_backend_set.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ type LoadBalancerBackendSetResourceCrud struct {
8383
}
8484

8585
func (s *LoadBalancerBackendSetResourceCrud) ID() string {
86-
if s.Resource != nil && s.Resource.Name != "" {
87-
return s.Resource.Name
86+
id, workSuccess := crud.LoadBalancerResourceID(s.Resource, s.WorkRequest)
87+
if id != nil {
88+
return *id
8889
}
89-
if s.WorkRequest != nil && s.WorkRequest.State == baremetal.WorkRequestSucceeded {
90-
return s.ResourceName
90+
if workSuccess {
91+
return s.D.Get("name").(string)
9192
}
92-
return s.D.Get("name").(string)
93+
return ""
9394
}
9495

9596
// RefreshWorkRequest returns the last updated workRequest
@@ -117,6 +118,7 @@ func (s *LoadBalancerBackendSetResourceCrud) CreatedTarget() []string {
117118
return []string{
118119
baremetal.ResourceSucceededWorkRequest,
119120
baremetal.WorkRequestSucceeded,
121+
baremetal.ResourceFailed,
120122
}
121123
}
122124

@@ -153,6 +155,13 @@ func (s *LoadBalancerBackendSetResourceCrud) Create() (e error) {
153155
}
154156

155157
func (s *LoadBalancerBackendSetResourceCrud) Get() (e error) {
158+
_, stillWorking, err := crud.LoadBalancerResourceGet(s.BaseCrud, s.WorkRequest)
159+
if err != nil {
160+
return err
161+
}
162+
if stillWorking {
163+
return nil
164+
}
156165
s.Resource, e = s.Client.GetBackendSet(
157166
s.D.Get("load_balancer_id").(string),
158167
s.D.Get("name").(string),
@@ -167,6 +176,7 @@ func (s *LoadBalancerBackendSetResourceCrud) Update() (e error) {
167176
Port: s.D.Get("health_checker.port").(int),
168177
Protocol: s.D.Get("health_checker.protocol").(string),
169178
ResponseBodyRegex: s.D.Get("health_checker.response_body_regex").(string),
179+
URLPath: s.D.Get("health_checker.url_path").(string),
170180
}
171181
sslConfig := baremetal.SSLConfiguration{
172182
CertificateName: s.D.Get("ssl_configuration.certificate_name").(string),
@@ -190,7 +200,7 @@ func (s *LoadBalancerBackendSetResourceCrud) Update() (e error) {
190200

191201
func (s *LoadBalancerBackendSetResourceCrud) SetData() {
192202
if s.Resource == nil {
193-
panic("BackendSet Resource is nil, cannot SetData")
203+
return
194204
}
195205
s.D.Set("policy", s.Resource.Policy)
196206
s.D.Set("name", s.Resource.Name)
@@ -199,6 +209,7 @@ func (s *LoadBalancerBackendSetResourceCrud) SetData() {
199209
"port": s.Resource.HealthChecker.Port,
200210
"protocol": s.Resource.HealthChecker.Protocol,
201211
"response_body_regex": s.Resource.HealthChecker.ResponseBodyRegex,
212+
"url_path": s.Resource.HealthChecker.URLPath,
202213
})
203214
s.D.Set("ssl_configuration", map[string]interface{}{
204215
"certificate_name": s.Resource.SSLConfig.CertificateName,
@@ -252,6 +263,7 @@ func (s *LoadBalancerBackendSetResourceCrud) healthChecker() *baremetal.HealthCh
252263
healthChecker.Port = v["port"].(int)
253264
healthChecker.Protocol = v["protocol"].(string)
254265
healthChecker.ResponseBodyRegex = v["response_body_regex"].(string)
266+
healthChecker.URLPath = v["url_path"].(string)
255267
}
256268
return healthChecker
257269
}

resource_obmcs_loadbalancer_backend_test.go

Lines changed: 0 additions & 78 deletions
This file was deleted.

resource_obmcs_loadbalancer_backendset_test.go

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)