Skip to content

Commit

Permalink
challenge_widget -> turnstile
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyb3r-Jak3 committed Apr 17, 2023
1 parent de84ba5 commit 0088a27
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .changelog/2380.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```release-note:new-resource
resource/challenge_widget: add support for challenge widgets / Turnstile
resource/turnstile_widget: add support for Turnstile
```
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ require (
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/cloudflare/cloudflare-go => github.com/Cyb3r-Jak3/cloudflare-go v0.42.1-0.20230417212430-cf6ed3d929d8
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/Cyb3r-Jak3/cloudflare-go v0.42.1-0.20230417212430-cf6ed3d929d8 h1:WQ1FgYZN91jtpUbN6yUxxNq3AxbSyvamEzV1nMVjOB8=
github.com/Cyb3r-Jak3/cloudflare-go v0.42.1-0.20230417212430-cf6ed3d929d8/go.mod h1:tA44hjU9FfycofKT+lWWMHb/dEq1pRbiVPGuJo1WzLQ=
github.com/MakeNowJust/heredoc/v2 v2.0.1 h1:rlCHh70XXXv7toz95ajQWOWQnN4WNLt0TdpZYIR/J6A=
github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRrqsyY9MWy+4JdRM=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
Expand Down
6 changes: 3 additions & 3 deletions internal/framework/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package provider
import (
"context"
"fmt"
"github.com/cloudflare/cloudflare-go"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/service/turnstile"
"log"
"math"
"regexp"
"strconv"
"testing"

"github.com/cloudflare/cloudflare-go"
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/service/challenge_widget"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/service/rulesets"
"github.com/cloudflare/terraform-provider-cloudflare/internal/sdkv2provider"
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
Expand Down Expand Up @@ -308,7 +308,7 @@ func (p *CloudflareProvider) Configure(ctx context.Context, req provider.Configu
func (p *CloudflareProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
rulesets.NewResource,
challenge_widget.NewResource,
turnstile.NewResource,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package challenge_widget
package turnstile

import "github.com/hashicorp/terraform-plugin-framework/types"

type ChallengeWidgetModel struct {
type TurnstileWidgetModel struct {
AccountID types.String `tfsdk:"account_id"`
ID types.String `tfsdk:"id"`
Domains types.Set `tfsdk:"domains"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package challenge_widget
package turnstile

import (
"context"
"fmt"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/expanders"
"strings"

"github.com/cloudflare/cloudflare-go"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/expanders"

"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/flatteners"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand All @@ -15,23 +16,23 @@ import (
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ resource.Resource = &ChallengeWidgetResource{}
var _ resource.ResourceWithImportState = &ChallengeWidgetResource{}
var _ resource.Resource = &TurnstileWidgetResource{}
var _ resource.ResourceWithImportState = &TurnstileWidgetResource{}

func NewResource() resource.Resource {
return &ChallengeWidgetResource{}
return &TurnstileWidgetResource{}
}

// ChallengeWidgetResource defines the resource implementation for challenge widgets.
type ChallengeWidgetResource struct {
// TurnstileWidgetResource defines the resource implementation for challenge widgets.
type TurnstileWidgetResource struct {
client *cloudflare.API
}

func (r *ChallengeWidgetResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_challenge_widget"
func (r *TurnstileWidgetResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_turnstile_widget"
}

func (r *ChallengeWidgetResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
func (r *TurnstileWidgetResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
Expand All @@ -50,8 +51,8 @@ func (r *ChallengeWidgetResource) Configure(ctx context.Context, req resource.Co
r.client = client
}

func (r *ChallengeWidgetResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data *ChallengeWidgetModel
func (r *TurnstileWidgetResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data *TurnstileWidgetModel

resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

Expand All @@ -61,9 +62,9 @@ func (r *ChallengeWidgetResource) Create(ctx context.Context, req resource.Creat

widget := buildChallengeWidgetFromModel(data)

createWidget, err := r.client.CreateChallengeWidget(ctx, cloudflare.AccountIdentifier(data.AccountID.ValueString()),
cloudflare.CreateChallengeWidgetRequest{
OffLabel: data.OffLabel.ValueBool(),
createWidget, err := r.client.CreateTurnstileWidget(ctx, cloudflare.AccountIdentifier(data.AccountID.ValueString()),
cloudflare.CreateTurnstileWidgetRequest{
OffLabel: widget.OffLabel,
Name: widget.Name,
Domains: widget.Domains,
Mode: widget.Mode,
Expand All @@ -82,16 +83,16 @@ func (r *ChallengeWidgetResource) Create(ctx context.Context, req resource.Creat
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (r *ChallengeWidgetResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data *ChallengeWidgetModel
func (r *TurnstileWidgetResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data *TurnstileWidgetModel

resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

widget, err := r.client.GetChallengeWidget(ctx, cloudflare.AccountIdentifier(data.AccountID.ValueString()), data.ID.ValueString())
widget, err := r.client.GetTurnstileWidget(ctx, cloudflare.AccountIdentifier(data.AccountID.ValueString()), data.ID.ValueString())

if err != nil {
resp.Diagnostics.AddError("Error reading challenge widget", err.Error())
Expand All @@ -105,8 +106,8 @@ func (r *ChallengeWidgetResource) Read(ctx context.Context, req resource.ReadReq
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (r *ChallengeWidgetResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data *ChallengeWidgetModel
func (r *TurnstileWidgetResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data *TurnstileWidgetModel

resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

Expand All @@ -116,7 +117,7 @@ func (r *ChallengeWidgetResource) Update(ctx context.Context, req resource.Updat

widget := buildChallengeWidgetFromModel(data)

updatedWidget, err := r.client.UpdateChallengeWidget(ctx, cloudflare.AccountIdentifier(data.AccountID.ValueString()), widget)
updatedWidget, err := r.client.UpdateTurnstileWidget(ctx, cloudflare.AccountIdentifier(data.AccountID.ValueString()), widget)

if err != nil {
resp.Diagnostics.AddError("Error reading challenge widget", err.Error())
Expand All @@ -130,22 +131,22 @@ func (r *ChallengeWidgetResource) Update(ctx context.Context, req resource.Updat
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (r *ChallengeWidgetResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data *ChallengeWidgetModel
func (r *TurnstileWidgetResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data *TurnstileWidgetModel

resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

err := r.client.DeleteChallengeWidget(ctx, cloudflare.AccountIdentifier(data.AccountID.ValueString()), data.ID.ValueString())
err := r.client.DeleteTurnstileWidget(ctx, cloudflare.AccountIdentifier(data.AccountID.ValueString()), data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Error deleting challenge widget", err.Error())
}
}

func (r *ChallengeWidgetResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
func (r *TurnstileWidgetResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, "/")
if len(idParts) != 2 {
resp.Diagnostics.AddError("Error importing challenge widget", "Invalid ID specified. Please specify the ID as \"accounts_id/sitekey\"")
Expand All @@ -154,28 +155,30 @@ func (r *ChallengeWidgetResource) ImportState(ctx context.Context, req resource.
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), idParts[1])...)
}

func buildChallengeWidgetFromModel(widget *ChallengeWidgetModel) cloudflare.ChallengeWidget {
built := cloudflare.ChallengeWidget{
func buildChallengeWidgetFromModel(widget *TurnstileWidgetModel) cloudflare.TurnstileWidget {
built := cloudflare.TurnstileWidget{
SiteKey: widget.ID.ValueString(),
Name: widget.Name.ValueString(),
BotFightMode: widget.BotFightMode.ValueBool(),
Mode: widget.Mode.ValueString(),
Region: widget.Region.ValueString(),
Domains: expanders.StringSet(widget.Domains),
OffLabel: widget.OffLabel.ValueBool(),
}

return built
}

func buildChallengeModelFromWidget(accountID types.String, widget cloudflare.ChallengeWidget) *ChallengeWidgetModel {
built := ChallengeWidgetModel{
func buildChallengeModelFromWidget(accountID types.String, widget cloudflare.TurnstileWidget) *TurnstileWidgetModel {
built := TurnstileWidgetModel{
AccountID: accountID,
ID: flatteners.String(widget.SiteKey),
Secret: flatteners.String(widget.Secret),
BotFightMode: types.BoolValue(widget.BotFightMode),
Name: flatteners.String(widget.Name),
Mode: flatteners.String(widget.Mode),
Region: flatteners.String(widget.Region),
OffLabel: types.BoolValue(widget.OffLabel),
}

var domains []attr.Value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package challenge_widget_test
package turnstile_test

import (
"fmt"
Expand All @@ -10,17 +10,17 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccCloudflareChallengeWidgetBasic(t *testing.T) {
func TestAccCloudflareTurnstileWidgetBasic(t *testing.T) {
rnd := utils.GenerateRandomResourceName()
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
resourceName := "cloudflare_challenge_widget." + rnd
resourceName := "cloudflare_turnstile_widget." + rnd

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareChallengeWidgetBasic(rnd, accountID),
Config: testAccCheckCloudflareTurnstileWidgetBasic(rnd, accountID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", rnd),
resource.TestCheckResourceAttr(resourceName, "account_id", accountID),
Expand All @@ -41,9 +41,9 @@ func TestAccCloudflareChallengeWidgetBasic(t *testing.T) {
})
}

func testAccCheckCloudflareChallengeWidgetBasic(rnd, accountID string) string {
func testAccCheckCloudflareTurnstileWidgetBasic(rnd, accountID string) string {
return fmt.Sprintf(`
resource "cloudflare_challenge_widget" "%[1]s" {
resource "cloudflare_turnstile_widget" "%[1]s" {
account_id = "%[2]s"
name = "%[1]s"
bot_fight_mode = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package challenge_widget
package turnstile

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"

"github.com/MakeNowJust/heredoc/v2"
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
Expand All @@ -13,7 +16,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

func (r *ChallengeWidgetResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
func (r *TurnstileWidgetResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: heredoc.Doc(`
The [Challenge Widget](https://developers.cloudflare.com/turnstile/) resource allows you to manage Cloudflare Turnstile Widgets.
Expand Down Expand Up @@ -64,6 +67,12 @@ func (r *ChallengeWidgetResource) Schema(ctx context.Context, req resource.Schem
MarkdownDescription: "If bot_fight_mode is set to true, Cloudflare issues computationally expensive challenges in response to malicious bots (ENT only).",
Optional: true,
},
"off_label": schema.BoolAttribute{
MarkdownDescription: "Do not show any Cloudflare branding on the widget (ENT only).",
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
},
},
}
}

0 comments on commit 0088a27

Please sign in to comment.