Skip to content

Commit

Permalink
Merge pull request #394 from cloudflare/APIOPS-5165-no-rules
Browse files Browse the repository at this point in the history
Handle nil case when no rules in a ruleset
  • Loading branch information
manatarms authored Apr 13, 2022
2 parents 1bc7441 + 18de85c commit f0c2320
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
29 changes: 19 additions & 10 deletions internal/app/cf-terraforming/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,15 +607,23 @@ func generateResources() func(cmd *cobra.Command, args []string) {
if ruleset.Rules != nil {
for _, rule := range ruleset.Rules {
if rule.ActionParameters != nil && rule.ActionParameters.Headers != nil {

// Sort the headers to have deterministic config output
keys := make([]string, 0, len(rule.ActionParameters.Headers))
for k := range rule.ActionParameters.Headers {
keys = append(keys, k)
}
sort.Strings(keys)

// The structure of the API response for headers differs from the
// structure terraform requires. So we collect all the headers
// indexed by rule.ID to massage the jsonStructData later
for headerName, values := range rule.ActionParameters.Headers {
for _, headerName := range keys {
header := map[string]interface{}{
"name": headerName,
"operation": values.Operation,
"expression": values.Expression,
"value": values.Value,
"operation": rule.ActionParameters.Headers[headerName].Operation,
"expression": rule.ActionParameters.Headers[headerName].Expression,
"value": rule.ActionParameters.Headers[headerName].Value,
}
ruleHeaders[rule.ID] = append(ruleHeaders[rule.ID], header)
}
Expand All @@ -631,15 +639,16 @@ func generateResources() func(cmd *cobra.Command, args []string) {

// Make the rules have the correct header structure
for i, ruleset := range jsonStructData {
for j, rule := range ruleset.(map[string]interface{})["rules"].([]interface{}) {
ID := rule.(map[string]interface{})["id"]
headers, exists := ruleHeaders[ID.(string)]
if exists {
jsonStructData[i].(map[string]interface{})["rules"].([]interface{})[j].(map[string]interface{})["action_parameters"].(map[string]interface{})["headers"] = headers
if ruleset.(map[string]interface{})["rules"] != nil {
for j, rule := range ruleset.(map[string]interface{})["rules"].([]interface{}) {
ID := rule.(map[string]interface{})["id"]
headers, exists := ruleHeaders[ID.(string)]
if exists {
jsonStructData[i].(map[string]interface{})["rules"].([]interface{})[j].(map[string]interface{})["action_parameters"].(map[string]interface{})["headers"] = headers
}
}
}
}

}
case "cloudflare_spectrum_application":
jsonPayload, err := api.SpectrumApplications(context.Background(), zoneID)
Expand Down
21 changes: 20 additions & 1 deletion testdata/cloudflare/cloudflare_ruleset_zone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ interactions:
"version": "4",
"last_updated": "2022-02-07T16:58:54.317608Z",
"phase": "http_request_late_transform"
},
{
"id": "0800fc577294c34e0b28ad2839435945",
"name": "default",
"description": "Some ruleset",
"kind": "zone",
"version": "2",
"last_updated": "2022-04-07T16:58:54.317608Z",
"phase": "http_request_late_transform"
}
],
"success": true,
Expand Down Expand Up @@ -462,6 +471,16 @@ interactions:
},
"example-http-header-2": {
"operation": "remove"
},
"example-http-header-3": {
"operation": "set",
"value": "space-header",
"expression": "(ip.geoip.continent eq \"pluto\")"
}
},
"uri": {
"path": {
"value": "/aquarii_b"
}
}
},
Expand Down Expand Up @@ -506,7 +525,7 @@ interactions:
"last_updated": "2022-05-07T16:58:54.317608Z",
"ref": "d0f1b4fdb4234adf9c6de9b614424836",
"enabled": false
}
}
],
"last_updated": "2022-02-07T16:58:54.317608Z",
"phase": "http_request_late_transform"
Expand Down
19 changes: 19 additions & 0 deletions testdata/terraform/cloudflare_ruleset_zone.tf
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ resource "cloudflare_ruleset" "terraform_managed_resource" {
name = "example-http-header-2"
operation = "remove"
}
headers {
expression = "(ip.geoip.continent eq \"pluto\")"
name = "example-http-header-3"
operation = "set"
value = "space-header"
}
uri {
path {
value = "/aquarii_b"
}
}
}
}
rules {
Expand Down Expand Up @@ -205,3 +216,11 @@ resource "cloudflare_ruleset" "terraform_managed_resource" {
}
}
}

resource "cloudflare_ruleset" "terraform_managed_resource" {
description = "Some ruleset"
kind = "zone"
name = "default"
phase = "http_request_late_transform"
zone_id = "0da42c8d2132a9ddaf714f9e7c920711"
}

0 comments on commit f0c2320

Please sign in to comment.