Skip to content

Commit

Permalink
resource/cloudflare_load_balancer: fix test assertions for pools
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz committed Sep 13, 2021
1 parent 19e4d89 commit 77a7ce6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 35 deletions.
31 changes: 15 additions & 16 deletions cloudflare/resource_cloudflare_load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,23 +618,24 @@ func flattenRules(d *schema.ResourceData, rules []*cloudflare.LoadBalancerRule)

if fr := r.FixedResponse; fr != nil {
frm := map[string]interface{}{}
if _, ok := d.GetOkExists(fmt.Sprintf("rules.%d.fixed_response.message_body", idx)); ok {
if _, ok := d.GetOkExists(fmt.Sprintf("rules.%d.fixed_response.0.message_body", idx)); ok {
frm["message_body"] = fr.MessageBody
m["fixed_response"] = frm // only set if one of these has is true
m["fixed_response"] = []interface{}{frm} // only set if one of these has is true
}
if _, ok := d.GetOkExists(fmt.Sprintf("rules.%d.fixed_response.status_code", idx)); ok {
frm["status_code"] = strconv.FormatInt(int64(fr.StatusCode), 10)
m["fixed_response"] = frm // only set if one of these has is true
if _, ok := d.GetOkExists(fmt.Sprintf("rules.%d.fixed_response.0.status_code", idx)); ok {
frm["status_code"] = fr.StatusCode
m["fixed_response"] = []interface{}{frm} // only set if one of these has is true
}
if _, ok := d.GetOkExists(fmt.Sprintf("rules.%d.fixed_response.content_type", idx)); ok {
if _, ok := d.GetOkExists(fmt.Sprintf("rules.%d.fixed_response.0.content_type", idx)); ok {
frm["content_type"] = fr.ContentType
m["fixed_response"] = frm // only set if one of these has is true
m["fixed_response"] = []interface{}{frm} // only set if one of these has is true
}
if _, ok := d.GetOkExists(fmt.Sprintf("rules.%d.fixed_response.location", idx)); ok {
if _, ok := d.GetOkExists(fmt.Sprintf("rules.%d.fixed_response.0.location", idx)); ok {
frm["location"] = fr.Location
m["fixed_response"] = frm // only set if one of these has is true
m["fixed_response"] = []interface{}{frm} // only set if one of these has is true
}
}

if _, ok := d.GetOkExists(fmt.Sprintf("rules.%d.overrides", idx)); ok {
o := r.Overrides
om := map[string]interface{}{}
Expand Down Expand Up @@ -772,20 +773,17 @@ func expandRules(rdata interface{}) ([]*cloudflare.LoadBalancerRule, error) {
}
}

if fixedResponseData, ok := r["fixed_response"]; ok {
for _, fixedResponseData := range r["fixed_response"].([]interface{}) {
frd := fixedResponseData.(map[string]interface{})
// we don't add this into our LB unless one of the cases below is true
fr := &cloudflare.LoadBalancerFixedResponseData{}

if mb, ok := frd["message_body"]; ok {
fr.MessageBody = mb.(string)
lbr.FixedResponse = fr
}
if sc, ok := frd["status_code"]; ok {
scint, err := strconv.ParseInt(sc.(string), 10, 64)
if err != nil {
return nil, err
}
fr.StatusCode = int(scint)
fr.StatusCode = sc.(int)
lbr.FixedResponse = fr
}
if ct, ok := frd["content_type"]; ok {
Expand All @@ -796,10 +794,11 @@ func expandRules(rdata interface{}) ([]*cloudflare.LoadBalancerRule, error) {
fr.Location = l.(string)
lbr.FixedResponse = fr
}

}

rules = append(rules, lbr)
}

return rules, nil
}

Expand Down
10 changes: 6 additions & 4 deletions cloudflare/resource_cloudflare_load_balancer_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ func TestAccCloudflareLoadBalancerPool_FullySpecified(t *testing.T) {
// checking our overrides of default values worked
resource.TestCheckResourceAttr(name, "enabled", "false"),
resource.TestCheckResourceAttr(name, "load_shedding.#", "1"),
resource.TestCheckResourceAttr(name, "load_shedding.2749995019.default_percent", "55"),
resource.TestCheckResourceAttr(name, "load_shedding.2749995019.default_policy", "random"),
resource.TestCheckResourceAttr(name, "load_shedding.2749995019.session_percent", "12"),
resource.TestCheckResourceAttr(name, "load_shedding.2749995019.session_policy", "hash"),
resource.TestCheckTypeSetElemNestedAttrs(name, "load_shedding.*", map[string]string{
"default_percent": "55",
"default_policy": "random",
"session_percent": "12",
"session_policy": "hash",
}),
resource.TestCheckResourceAttr(name, "description", "tfacc-fully-specified"),
resource.TestCheckResourceAttr(name, "check_regions.#", "1"),
resource.TestCheckResourceAttr(name, "minimum_origins", "2"),
Expand Down
18 changes: 3 additions & 15 deletions cloudflare/resource_cloudflare_load_balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestAccCloudflareLoadBalancer_Rules(t *testing.T) {
resource.TestCheckResourceAttr(name, "rules.0.overrides.0.session_affinity_attributes.samesite", "Auto"),
resource.TestCheckResourceAttr(name, "rules.0.overrides.0.session_affinity_attributes.secure", "Auto"),
resource.TestCheckResourceAttr(name, "rules.#", "3"),
resource.TestCheckResourceAttr(name, "rules.1.fixed_response.message_body", "hello"),
resource.TestCheckResourceAttr(name, "rules.1.fixed_response.0.message_body", "hello"),
resource.TestCheckResourceAttr(name, "rules.2.overrides.0.region_pools.#", "1"),
),
},
Expand Down Expand Up @@ -487,9 +487,9 @@ resource "cloudflare_load_balancer" "%[3]s" {
rules {
name = "test rule 2"
condition = "dns.qry.type == 28"
fixed_response = {
fixed_response {
message_body = "hello"
status_code = "200"
status_code = 200
content_type = "html"
location = "www.example.com"
}
Expand All @@ -506,15 +506,3 @@ resource "cloudflare_load_balancer" "%[3]s" {
}
}`, zoneID, zone, id)
}

func testAccCheckCloudflareLoadBalancerConfigLatitudeLongitude(zoneID, zone, id string) string {
return testAccCheckCloudflareLoadBalancerPoolConfigBasic(id) + fmt.Sprintf(`
resource "cloudflare_load_balancer" "%[3]s" {
zone_id = "%[1]s"
name = "tf-testacc-lb-latitude-longitude-%[3]s.%[2]s"
fallback_pool_id = "${cloudflare_load_balancer_pool.%[3]s.id}"
default_pool_ids = ["${cloudflare_load_balancer_pool.%[3]s.id}"]
latitude = 23.5
longitude = -11.1
}`, zoneID, zone, id)
}

0 comments on commit 77a7ce6

Please sign in to comment.