Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for turnstile_widget #563

Merged
merged 1 commit into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ Any resources not listed are currently not supported.
| [cloudflare_ruleset](https://www.terraform.io/docs/providers/cloudflare/r/ruleset) | Account or Zone | ✅ | ✅ |
| [cloudflare_spectrum_application](https://www.terraform.io/docs/providers/cloudflare/r/spectrum_application) | Zone | ✅ | ✅ |
| [cloudflare_tunnel](https://www.terraform.io/docs/providers/cloudflare/r/tunnel) | Account | ✅ | ✅ |
| [cloudflare_turnstile_widget](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/turnstile_widget) | Account | ✅ | ✅ |
| [cloudflare_url_normalization_settings](https://www.terraform.io/docs/providers/cloudflare/r/url_normalization_settings) | Zone | ✅ | ❌ |
| [cloudflare_waf_group](https://www.terraform.io/docs/providers/cloudflare/r/waf_group) | Zone | ❌ | ❌ |
| [cloudflare_waf_override](https://www.terraform.io/docs/providers/cloudflare/r/waf_override) | Zone | ✅ | ✅ |
Expand Down
16 changes: 16 additions & 0 deletions internal/app/cf-terraforming/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,22 @@ func generateResources() func(cmd *cobra.Command, args []string) {

jsonStructData[i].(map[string]interface{})["connections"] = nil
}
case "cloudflare_turnstile_widget":
jsonPayload, _, err := api.ListTurnstileWidgets(context.Background(), cloudflare.AccountIdentifier(accountID), cloudflare.ListTurnstileWidgetParams{})
punkeel marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatal(err)
}

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

for i := 0; i < resourceCount; i++ {
jsonStructData[i].(map[string]interface{})["id"] = jsonStructData[i].(map[string]interface{})["sitekey"]
}
case "cloudflare_url_normalization_settings":
jsonPayload, err := api.URLNormalizationSettings(context.Background(), &cloudflare.ResourceContainer{Identifier: zoneID, Level: cloudflare.ZoneRouteLevel})
if err != nil {
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 @@ -140,6 +140,7 @@ func TestResourceGeneration(t *testing.T) {
"cloudflare ruleset": {identiferType: "zone", resourceType: "cloudflare_ruleset", testdataFilename: "cloudflare_ruleset_zone"},
"cloudflare spectrum application": {identiferType: "zone", resourceType: "cloudflare_spectrum_application", testdataFilename: "cloudflare_spectrum_application"},
"cloudflare tunnel": {identiferType: "account", resourceType: "cloudflare_tunnel", testdataFilename: "cloudflare_tunnel"},
"cloudflare turnstile_widget": {identiferType: "account", resourceType: "cloudflare_turnstile_widget", testdataFilename: "cloudflare_turnstile_widget"},
"cloudflare url normalization settings": {identiferType: "zone", resourceType: "cloudflare_url_normalization_settings", testdataFilename: "cloudflare_url_normalization_settings"},
"cloudflare user agent blocking rule": {identiferType: "zone", resourceType: "cloudflare_user_agent_blocking_rule", testdataFilename: "cloudflare_user_agent_blocking_rule"},
"cloudflare waiting room": {identiferType: "zone", resourceType: "cloudflare_waiting_room", testdataFilename: "cloudflare_waiting_room"},
Expand Down
20 changes: 17 additions & 3 deletions internal/app/cf-terraforming/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"
"crypto/md5"
"encoding/json"
"fmt"
"strings"
"time"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/cloudflare/cloudflare-go"
"github.com/spf13/cobra"

"fmt"
)

// resourceImportStringFormats contains a mapping of the resource type to the
Expand Down Expand Up @@ -38,6 +37,7 @@ var resourceImportStringFormats = map[string]string{
"cloudflare_ruleset": ":identifier_type/:identifier_value/:id",
"cloudflare_spectrum_application": ":zone_id/:id",
"cloudflare_tunnel": ":account_id/:id",
"cloudflare_turnstile_widget": ":account_id/:id",
"cloudflare_waf_override": ":zone_id/:id",
"cloudflare_waiting_room": ":zone_id/:id",
"cloudflare_worker_route": ":zone_id/:id",
Expand Down Expand Up @@ -361,6 +361,20 @@ func runImport() func(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatal(err)
}
case "cloudflare_turnstile_widget":
jsonPayload, _, err := api.ListTurnstileWidgets(context.Background(), cloudflare.AccountIdentifier(accountID), cloudflare.ListTurnstileWidgetParams{})
if err != nil {
log.Fatal(err)
}

m, _ := json.Marshal(jsonPayload)
err = json.Unmarshal(m, &jsonStructData)
if err != nil {
log.Fatal(err)
}
for i := 0; i < len(jsonStructData); i++ {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the ID hack here

jsonStructData[i].(map[string]interface{})["id"] = jsonStructData[i].(map[string]interface{})["sitekey"]
}
case "cloudflare_waf_override":
jsonPayload, err := api.ListWAFOverrides(context.Background(), zoneID)
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions testdata/cloudflare/cloudflare_turnstile_widget.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
Content-Type:
- application/json
url: https://api.cloudflare.com/client/v4/accounts/f037e56e89293a057740de681ac9abbe/challenges/widgets?page=1&per_page=25
method: GET
response:
body: |
{"result":[{"sitekey":"0x4AAAAAAAEg5sP3rwf91fe8","modified_on":"2023-05-08T11:49:41.862676Z","created_on":"2023-05-08T11:49:41.862676Z","mode":"managed","domains":["example.com"],"name":"test site","bot_fight_mode":false,"region":"world","offlabel":false},{"sitekey":"0x4AAAAAAAE0gwg0H1StXlOx","modified_on":"2023-05-17T02:08:18.399564Z","created_on":"2023-05-17T02:08:18.399564Z","mode":"managed","domains":["example.org"],"name":"My Terraform-managed widget","bot_fight_mode":false,"region":"world","offlabel":false},{"sitekey":"0x4AAAAAAAE2z4LbxEka5UBh","modified_on":"2023-05-17T19:11:26.151063Z","created_on":"2023-05-17T19:11:26.151063Z","mode":"managed","domains":["example.net"],"name":"My website","bot_fight_mode":false,"region":"world","offlabel":false}],"success":true,"errors":[],"messages":[],"result_info":{"page":1,"per_page":25,"count":3,"total_count":3,"total_pages":1}}
headers:
Content-Type:
- application/json
Vary:
- Accept-Encoding
status: 200 OK
code: 200
duration: ""
7 changes: 7 additions & 0 deletions testdata/terraform/cloudflare_turnstile_widget/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
}
}
}
23 changes: 23 additions & 0 deletions testdata/terraform/cloudflare_turnstile_widget/test.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
resource "cloudflare_turnstile_widget" "terraform_managed_resource" {
account_id = "f037e56e89293a057740de681ac9abbe"
domains = ["example.com"]
mode = "managed"
name = "test site"
region = "world"
}

resource "cloudflare_turnstile_widget" "terraform_managed_resource" {
account_id = "f037e56e89293a057740de681ac9abbe"
domains = ["example.org"]
mode = "managed"
name = "My Terraform-managed widget"
region = "world"
}

resource "cloudflare_turnstile_widget" "terraform_managed_resource" {
account_id = "f037e56e89293a057740de681ac9abbe"
domains = ["example.net"]
mode = "managed"
name = "My website"
region = "world"
}