Skip to content

Commit

Permalink
Merge pull request #598 from cwlowder/master
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz authored Aug 23, 2023
2 parents af65d4c + 5fe384b commit bfdc6be
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Any resources not listed are currently not supported.
| [cloudflare_argo](https://www.terraform.io/docs/providers/cloudflare/r/argo) | Zone |||
| [cloudflare_authenticated_origin_pulls](https://www.terraform.io/docs/providers/cloudflare/r/authenticated_origin_pulls) | Zone |||
| [cloudflare_authenticated_origin_pulls_certificate](https://www.terraform.io/docs/providers/cloudflare/r/authenticated_origin_pulls_certificate) | Zone |||
| [cloudflare_bot_management](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/bot_management) | Zone |||
| [cloudflare_byo_ip_prefix](https://www.terraform.io/docs/providers/cloudflare/r/byo_ip_prefix) | Account |||
| [cloudflare_certificate_pack](https://www.terraform.io/docs/providers/cloudflare/r/certificate_pack) | Zone |||
| [cloudflare_custom_hostname](https://www.terraform.io/docs/providers/cloudflare/r/custom_hostname) | Zone |||
Expand Down
24 changes: 19 additions & 5 deletions internal/app/cf-terraforming/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package cmd
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/go-version"
Expand All @@ -16,9 +18,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/zclconf/go-cty/cty"

"fmt"
"strings"
)

var resourceType string
Expand Down Expand Up @@ -47,7 +46,7 @@ func generateResources() func(cmd *cobra.Command, args []string) {
workingDir = viper.GetString("terraform-install-path")
execPath = viper.GetString("terraform-binary-path")

//Download terraform if no existing binary was provided
// Download terraform if no existing binary was provided
if execPath == "" {
tmpDir, err := ioutil.TempDir("", "tfinstall")
if err != nil {
Expand Down Expand Up @@ -291,6 +290,22 @@ func generateResources() func(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatal(err)
}
case "cloudflare_bot_management":
botManagement, err := api.GetBotManagement(context.Background(), identifier)
if err != nil {
log.Fatal(err)
}
var jsonPayload []cloudflare.BotManagement
jsonPayload = append(jsonPayload, botManagement)

resourceCount = 1
m, _ := json.Marshal(jsonPayload)
err = json.Unmarshal(m, &jsonStructData)
if err != nil {
log.Fatal(err)
}

jsonStructData[0].(map[string]interface{})["id"] = zoneID
case "cloudflare_byo_ip_prefix":
jsonPayload, err := api.ListPrefixes(context.Background(), accountID)
if err != nil {
Expand Down Expand Up @@ -874,7 +889,6 @@ func generateResources() func(cmd *cobra.Command, args []string) {
Page: 1,
},
})

if err != nil {
log.Fatal(err)
}
Expand Down
1 change: 1 addition & 0 deletions internal/app/cf-terraforming/cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func TestResourceGeneration(t *testing.T) {
"cloudflare account member": {identiferType: "account", resourceType: "cloudflare_account_member", testdataFilename: "cloudflare_account_member"},
"cloudflare api shield": {identiferType: "zone", resourceType: "cloudflare_api_shield", testdataFilename: "cloudflare_api_shield"},
"cloudflare argo": {identiferType: "zone", resourceType: "cloudflare_argo", testdataFilename: "cloudflare_argo"},
"cloudflare bot management": {identiferType: "zone", resourceType: "cloudflare_bot_management", testdataFilename: "cloudflare_bot_management"},
"cloudflare BYO IP prefix": {identiferType: "account", resourceType: "cloudflare_byo_ip_prefix", testdataFilename: "cloudflare_byo_ip_prefix"},
"cloudflare certificate pack": {identiferType: "zone", resourceType: "cloudflare_certificate_pack", testdataFilename: "cloudflare_certificate_pack_acm"},
"cloudflare custom hostname fallback origin": {identiferType: "zone", resourceType: "cloudflare_custom_hostname_fallback_origin", testdataFilename: "cloudflare_custom_hostname_fallback_origin"},
Expand Down
17 changes: 16 additions & 1 deletion internal/app/cf-terraforming/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var resourceImportStringFormats = map[string]string{
"cloudflare_access_rule": ":identifier_type/:identifier_value/:id",
"cloudflare_account_member": ":account_id/:id",
"cloudflare_argo": ":zone_id/argo",
"cloudflare_bot_management": ":zone_id",
"cloudflare_byo_ip_prefix": ":id",
"cloudflare_certificate_pack": ":zone_id/:id",
"cloudflare_custom_hostname": ":zone_id/:id",
Expand Down Expand Up @@ -113,6 +114,21 @@ func runImport() func(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatal(err)
}
case "cloudflare_bot_management":
botManagement, err := api.GetBotManagement(context.Background(), identifier)
if err != nil {
log.Fatal(err)
}
var jsonPayload []cloudflare.BotManagement
jsonPayload = append(jsonPayload, botManagement)

m, _ := json.Marshal(jsonPayload)
err = json.Unmarshal(m, &jsonStructData)
if err != nil {
log.Fatal(err)
}

jsonStructData[0].(map[string]interface{})["id"] = zoneID
case "cloudflare_byo_ip_prefix":
jsonPayload, err := api.ListPrefixes(context.Background(), accountID)
if err != nil {
Expand Down Expand Up @@ -353,7 +369,6 @@ func runImport() func(cmd *cobra.Command, args []string) {
Page: 1,
},
})

if err != nil {
log.Fatal(err)
}
Expand Down
36 changes: 36 additions & 0 deletions testdata/cloudflare/cloudflare_bot_management.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
Content-Type:
- application/json
Cloudflare-Version:
- 2.0.0
url: https://api.cloudflare.com/client/v4/zones/0da42c8d2132a9ddaf714f9e7c920711/bot_management
method: GET
response:
body: |
{
"success": true,
"errors": [],
"messages": [],
"result": [
{
"enable_js": true,
"sbfm_definitely_automated": "block",
"sbfm_likely_automated": "managed_challenge",
"sbfm_verified_bots": "allow",
"sbfm_static_resource_protection": false,
"optimize_wordpress": true
}
]
}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: ""
7 changes: 7 additions & 0 deletions testdata/terraform/cloudflare_bot_management/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
}
}
}
9 changes: 9 additions & 0 deletions testdata/terraform/cloudflare_bot_management/test.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "cloudflare_bot_management" "terraform_managed_resource" {
zone_id = "0da42c8d2132a9ddaf714f9e7c920711"
enable_js = true
sbfm_definitely_automated = "block"
sbfm_likely_automated = "managed_challenge"
sbfm_verified_bots = "allow"
sbfm_static_resource_protection = false
optimize_wordpress = true
}

0 comments on commit bfdc6be

Please sign in to comment.