Skip to content

Commit

Permalink
Merge pull request #3262 from cloudflare/mux-cloudflare-go-versions
Browse files Browse the repository at this point in the history
provider: introduce a muxed client
  • Loading branch information
jacobbednarz authored Apr 24, 2024
2 parents 4e0a09e + 67b9d7f commit f83af2d
Show file tree
Hide file tree
Showing 45 changed files with 551 additions and 435 deletions.
3 changes: 3 additions & 0 deletions .changelog/3262.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:internal
provider: introduce a muxed client to support using cloudflare-go/v0 and cloudflare-go/v2 together
```
7 changes: 4 additions & 3 deletions docs/data-sources/account_roles.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ Use this data source to lookup [Account Roles](https://api.cloudflare.com/#accou

```terraform
data "cloudflare_account_roles" "account_roles" {
account_id = "f037e56e89293a057740de681ac9abbe"
account_id = "f037e56e89293a057740de681ac9abbe"
}
locals {
roles_by_name = {
for role in data.cloudflare_account_roles.account_roles.roles :
role.name => role
role.name => role
}
}
resource "cloudflare_account_member" "member" {
...
account_id = "f037e56e89293a057740de681ac9abbe"
email_address = "user@example.com"
role_ids = [
local.roles_by_name["Administrator"].id
]
Expand Down
8 changes: 4 additions & 4 deletions docs/resources/dlp_profile.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ They are referenced in Zero Trust Gateway rules.
```terraform
# Predefined profile must be imported, cannot be created
resource "cloudflare_dlp_profile" "creds" {
account_id = "0da42c8d2132a9ddaf714f9e7c920711"
account_id = "f037e56e89293a057740de681ac9abbe"
name = "Credentials and Secrets"
type = "predefined"
allowed_match_count = 3
Expand Down Expand Up @@ -52,7 +52,7 @@ resource "cloudflare_dlp_profile" "creds" {
# Custom profile
resource "cloudflare_dlp_profile" "example_custom" {
account_id = "0da42c8d2132a9ddaf714f9e7c920711"
account_id = "f037e56e89293a057740de681ac9abbe"
name = "Example Custom Profile"
description = "A profile with example entries"
type = "custom"
Expand All @@ -62,7 +62,7 @@ resource "cloudflare_dlp_profile" "example_custom" {
name = "Matches visa credit cards"
enabled = true
pattern {
regex = "4\d{3}([-\\. ])?\d{4}([-\\. ])?\d{4}([-\\. ])?\d{4}"
regex = "4\\d{3}([-\\. ])?\\d{4}([-\\. ])?\\d{4}([-\\. ])?\\d{4}"
validation = "luhn"
}
}
Expand All @@ -87,12 +87,12 @@ resource "cloudflare_dlp_profile" "example_custom" {
- `entry` (Block Set, Min: 1) List of entries to apply to the profile. (see [below for nested schema](#nestedblock--entry))
- `name` (String) Name of the profile. **Modifying this attribute will force creation of a new resource.**
- `type` (String) The type of the profile. Available values: `custom`, `predefined`. **Modifying this attribute will force creation of a new resource.**
- `ocr_enabled` (Boolean) If true, scan images via OCR to determine if any text present matches filters.

### Optional

- `context_awareness` (Block List, Max: 1) Scan the context of predefined entries to only return matches surrounded by keywords. (see [below for nested schema](#nestedblock--context_awareness))
- `description` (String) Brief summary of the profile and its intended use.
- `ocr_enabled` (Boolean) If true, scan images via OCR to determine if any text present matches filters.

### Read-Only

Expand Down
2 changes: 1 addition & 1 deletion docs/resources/workers_for_platforms_namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description: |-

# cloudflare_workers_for_platforms_namespace (Resource)

The [Workers for Platforms](https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/) resource allows you
The [Workers for Platforms](https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/) resource allows you
to manage Cloudflare Workers for Platforms namespaces.

## Example Usage
Expand Down
7 changes: 4 additions & 3 deletions examples/data-sources/cloudflare_account_roles/data-source.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
data "cloudflare_account_roles" "account_roles" {
account_id = "f037e56e89293a057740de681ac9abbe"
account_id = "f037e56e89293a057740de681ac9abbe"
}

locals {
roles_by_name = {
for role in data.cloudflare_account_roles.account_roles.roles :
role.name => role
role.name => role
}
}

resource "cloudflare_account_member" "member" {
...
account_id = "f037e56e89293a057740de681ac9abbe"
email_address = "user@example.com"
role_ids = [
local.roles_by_name["Administrator"].id
]
Expand Down
6 changes: 3 additions & 3 deletions examples/resources/cloudflare_dlp_profile/resource.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Predefined profile must be imported, cannot be created
resource "cloudflare_dlp_profile" "creds" {
account_id = "0da42c8d2132a9ddaf714f9e7c920711"
account_id = "f037e56e89293a057740de681ac9abbe"
name = "Credentials and Secrets"
type = "predefined"
allowed_match_count = 3
Expand Down Expand Up @@ -34,7 +34,7 @@ resource "cloudflare_dlp_profile" "creds" {

# Custom profile
resource "cloudflare_dlp_profile" "example_custom" {
account_id = "0da42c8d2132a9ddaf714f9e7c920711"
account_id = "f037e56e89293a057740de681ac9abbe"
name = "Example Custom Profile"
description = "A profile with example entries"
type = "custom"
Expand All @@ -44,7 +44,7 @@ resource "cloudflare_dlp_profile" "example_custom" {
name = "Matches visa credit cards"
enabled = true
pattern {
regex = "4\d{3}([-\\. ])?\d{4}([-\\. ])?\d{4}([-\\. ])?\d{4}"
regex = "4\\d{3}([-\\. ])?\\d{4}([-\\. ])?\\d{4}([-\\. ])?\\d{4}"
validation = "luhn"
}
}
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cloudflare/cloudflare-go/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
Expand All @@ -84,6 +85,10 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vc
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/cloudflare/cloudflare-go v0.93.0 h1:rV0eHb42NUewfK5qa2+LKAD4v4oFA0QGDABn/lMyF78=
github.com/cloudflare/cloudflare-go v0.93.0/go.mod h1:N1u1cLZ4lG6NeezGOWi7P6aq1DK2iVYg9ze7GZbUmZE=
github.com/cloudflare/cloudflare-go/v2 v2.0.0 h1:T73RHu9NbMIRGs1d4NbMHCnFB7semPVd3r8Fd2KuCjg=
github.com/cloudflare/cloudflare-go/v2 v2.0.0/go.mod h1:AoIzb05z/rvdJLztPct4tSa+3IqXJJ6c+pbUFMOlTr8=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
Expand Down Expand Up @@ -201,6 +203,16 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
19 changes: 15 additions & 4 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"os"
"testing"

"github.com/cloudflare/cloudflare-go"
cfv1 "github.com/cloudflare/cloudflare-go"
cfv2 "github.com/cloudflare/cloudflare-go/v2"
"github.com/cloudflare/cloudflare-go/v2/option"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/provider"
"github.com/cloudflare/terraform-provider-cloudflare/internal/sdkv2provider"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
Expand Down Expand Up @@ -77,14 +79,23 @@ func TestAccSkipForDefaultAccount(t *testing.T, reason string) {
}
}

// sharedClient returns a common Cloudflare client setup needed for the
// SharedV1Client returns a common Cloudflare V1 client setup needed for the
// sweeper functions.
func SharedClient() (*cloudflare.API, error) {
client, err := cloudflare.New(os.Getenv("CLOUDFLARE_API_KEY"), os.Getenv("CLOUDFLARE_EMAIL"))
func SharedV1Client() (*cfv1.API, error) {
client, err := cfv1.New(os.Getenv("CLOUDFLARE_API_KEY"), os.Getenv("CLOUDFLARE_EMAIL"))

if err != nil {
return client, err
}

return client, nil
}

// SharedV2Client returns a common Cloudflare V2 client setup needed for the
// sweeper functions.
func SharedV2Client() *cfv2.Client {
return cfv2.NewClient(
option.WithAPIKey("CLOUDFLARE_API_KEY"),
option.WithAPIEmail("CLOUDFLARE_EMAIL"),
)
}
15 changes: 15 additions & 0 deletions internal/framework/muxclient/muxclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package muxclient

import (
cfv1 "github.com/cloudflare/cloudflare-go"
cfv2 "github.com/cloudflare/cloudflare-go/v2"
)

// Client is the intermediatry structure to allow us to run both versions of the
// Go SDK alongside one another without collisions.
//
// The usage is resource dependent and no safe guards are included here.
type Client struct {
V1 *cfv1.API
V2 *cfv2.Client
}
35 changes: 35 additions & 0 deletions internal/framework/muxclient/muxclient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package muxclient_test

import (
"context"
"testing"

cfv1 "github.com/cloudflare/cloudflare-go"
cfv2 "github.com/cloudflare/cloudflare-go/v2"
"github.com/cloudflare/cloudflare-go/v2/option"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/muxclient"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/provider"
"github.com/stretchr/testify/assert"
)

func TestMuxClientInitialised(t *testing.T) {
c1 := muxclient.Client{}
assert.Empty(t, c1)

v1Config := provider.Config{}
v1Config.Email = "user@example.com"
cfv1Client, _ := v1Config.Client(context.TODO())

cfv2Client := cfv2.NewClient(
option.WithAPIEmail("user@example.com"),
)
c2 := &muxclient.Client{
V1: cfv1Client,
V2: cfv2Client,
}

assert.NotEmpty(t, c2)
assert.IsType(t, &cfv1.API{}, cfv1Client)
assert.IsType(t, &cfv2.Client{}, cfv2Client)
assert.IsType(t, &muxclient.Client{}, c2)
}
Loading

0 comments on commit f83af2d

Please sign in to comment.