Skip to content

Commit

Permalink
v2.0 (#460)
Browse files Browse the repository at this point in the history
* Update to cloudflare-go 0.10.0

Pulls in the deprecation of `Organization` in favour of `Account`.

Closes #227

* add go-version file

* newer go version

* Last org reference

* Rename "key" to "token"

API "token" is currently used for HTTP authentication requests where the
`X-Auth-Key` needs to be set. Until recently, it was fine to use the
wrong term for this authentication method (as there was only the one)
however there is now a feature from Cloudflare which is actually called
API tokens and it requires a different authentication procedure.

The functionality for API tokens has been released (v1.18.0) and this
cleans up the old references from "token" to "key" to better reflect
it's purpose.

* Remove use_account_from_zone for v2.0 refactor (#468)

* SDK-80: Zone to zone_id refactoring (#472)

* Refactor zone_id for resource_cloudflare_access_rule

* Refactor zone_id for cloudflare_filter

* Refactor zone_id for cloudflare_firewall_rule

* Refactor zone_id for cloudflare_load_balancer

* Refactor zone_id for cloudflare_page_rule

* Refactor zone_id for cloudflare_rate_limit

* Refactor zone_id for cloudflare_record

* Refactor zone_id for cloudflare_waf_rule

* Refactor zone_id for cloudflare_worker_route

* Refactor zone_id for cloudflare_worker_script

* Refactor zone_id for cloudflare_zone_lockdown

* Refactor zone_id for cloudflare_zone_settings_override

* Make tests `zone_id` compatible

Gets the build green by updating all tests to match the expected
behaviour of using zone ID instead of relying on zone from the
schema.

* Get rid of additional test output

* Add CLOUDFLARE_ALT_ZONE_ID to precheck

* Don't use API token authentication for Workers

The Workers service does not currently support the use of API tokens
(which is used by default when the `CLOUDFLARE_API_TOKEN` environment
variable is set[1]) and throws a very misleading exception.

```
=== RUN   TestAccCloudflareWorkerScript_SingleScriptNonEnt
--- FAIL: TestAccCloudflareWorkerScript_SingleScriptNonEnt (0.59s)
    testing.go:569: Step 0 error: errors during apply:

        Error: script already exists.

          on /var/folders/d4/5sgps61s2jg8f0_71663vw800000gn/T/tf-test617106443/main.tf line 2:
          (source code not available)
```

The reason we hit this exception is due to the `WorkerScript.Script`
being populated with the API error instead of being empty which falls
into this `if` block[2].

```
(cloudflare.WorkerScript) {
 WorkerMetaData: (cloudflare.WorkerMetaData) {
  ID: (string) "",
  ETAG: (string) "",
  Size: (int) 0,
  CreatedOn: (time.Time) 0001-01-01 00:00:00 +0000 UTC,
  ModifiedOn: (time.Time) 0001-01-01 00:00:00 +0000 UTC
 },
 Script: (string) (len=105) "{\"success\":false,\"errors\":[{\"code\":10000,\"message\":\"API Tokens are not supported by this API for now\"}]}\n"
}
```

This was initially puzzling as `err` is handled in `cloudflare-go` where
this field is defined[3] but looking deeper, the HTTP response is coming
back as a 200 OK response, hence the `Script` value being populated
and not falling into the `err` handling.

To fix this, it is two fold. The first is unsetting the
`CLOUDFLARE_API_TOKEN` environment variable in CI. This gets the tests
passing again however to address the underlying issue, we've contacted
Cloudflare to fix the HTTP response code which will then made everything
in the library work as intended. Depending on the length of time before
the fix is deployed, we might also hotfix the library to instead check
`r.Success = true` as well to fail in the correct spot instead of
allowing this to bubble up in unexpected ways.

[1]: https://git.io/Jes9l
[2]: https://git.io/Jes9A
[3]: https://git.io/Jes9h

* SDK-81: Refactor Workers resources to remove single-script code. (#478)

* SDK-81: Refactor Workers resources to remove single-script code.
Closes #470
Closes #387

* Version 2 Upgrade Guide (#480)

* Version 2 Upgrade Guide

* Update website/docs/guides/version-2-upgrade.html.markdown

Co-Authored-By: Jacob Bednarz <jacob.bednarz@gmail.com>

* Apply suggestions from code review

Co-Authored-By: Jacob Bednarz <jacob.bednarz@gmail.com>

* Drop go 1.11, add 1.13
  • Loading branch information
jacobbednarz authored and patryk committed Sep 30, 2019
1 parent 8d10741 commit 0995af4
Show file tree
Hide file tree
Showing 71 changed files with 1,057 additions and 1,703 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ env:
matrix:
fast_finish: true
include:
- go: 1.11.x
- go: 1.12.x
- go: 1.13.x
- go: 1.x
- go: tip
allow_failures:
Expand Down
2 changes: 1 addition & 1 deletion cloudflare/cloudflare_sweeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestMain(m *testing.M) {
// sharedClient returns a common Cloudflare client setup needed for the
// sweeper functions.
func sharedClient() (*cloudflare.API, error) {
client, err := cloudflare.New(os.Getenv("CLOUDFLARE_TOKEN"), os.Getenv("CLOUDFLARE_EMAIL"))
client, err := cloudflare.New(os.Getenv("CLOUDFLARE_API_KEY"), os.Getenv("CLOUDFLARE_EMAIL"))

if err != nil {
return client, err
Expand Down
4 changes: 2 additions & 2 deletions cloudflare/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type Config struct {
Email string
Token string
APIKey string
APIToken string
Options []cloudflare.Option
}
Expand All @@ -22,7 +22,7 @@ func (c *Config) Client() (*cloudflare.API, error) {
if c.APIToken != "" {
client, err = cloudflare.NewWithAPIToken(c.APIToken, c.Options...)
} else {
client, err = cloudflare.New(c.Token, c.Email, c.Options...)
client, err = cloudflare.New(c.APIKey, c.Email, c.Options...)
}
if err != nil {
return nil, fmt.Errorf("Error creating new Cloudflare client: %s", err)
Expand Down
3 changes: 0 additions & 3 deletions cloudflare/data_source_ip_ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cloudflare
import (
"fmt"
"net"
"os"
"sort"
"strconv"
"testing"
Expand Down Expand Up @@ -32,8 +31,6 @@ func testAccCloudflareIPRanges(n string) resource.TestCheckFunc {
r := s.RootModule().Resources[n]
a := r.Primary.Attributes

fmt.Fprintf(os.Stderr, "%#v", a)

var (
cidrBlockSize int
err error
Expand Down
12 changes: 6 additions & 6 deletions cloudflare/import_cloudflare_load_balancer_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package cloudflare

import (
"fmt"
"os"
"testing"

"fmt"

"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform/helper/resource"
)
Expand All @@ -14,6 +13,7 @@ func TestAccCloudflareLoadBalancer_Import(t *testing.T) {
t.Parallel()
var loadBalancer cloudflare.LoadBalancer
zone := os.Getenv("CLOUDFLARE_DOMAIN")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
rnd := generateRandomResourceName()
name := "cloudflare_load_balancer." + rnd

Expand All @@ -22,20 +22,20 @@ func TestAccCloudflareLoadBalancer_Import(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareLoadBalancerConfigBasic(zone, rnd),
Config: testAccCheckCloudflareLoadBalancerConfigBasic(zoneID, zone, rnd),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflareLoadBalancerExists(name, &loadBalancer),
testAccCheckCloudflareLoadBalancerIDIsValid(name, zone),
testAccCheckCloudflareLoadBalancerIDIsValid(name, zoneID),
),
},
{
ResourceName: name,
ImportStateIdPrefix: fmt.Sprintf("%s/", zone),
ImportStateIdPrefix: fmt.Sprintf("%s/", zoneID),
ImportState: true,
ImportStateVerify: true,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflareLoadBalancerExists(name, &loadBalancer),
testAccCheckCloudflareLoadBalancerIDIsValid(name, zone),
testAccCheckCloudflareLoadBalancerIDIsValid(name, zoneID),
),
},
},
Expand Down
16 changes: 8 additions & 8 deletions cloudflare/import_cloudflare_page_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ import (
func TestAccCloudflarePageRule_Import(t *testing.T) {
t.Parallel()
var pageRule cloudflare.PageRule
zone := os.Getenv("CLOUDFLARE_DOMAIN")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
name := "cloudflare_page_rule.test"
target := fmt.Sprintf("test-import.%s", zone)
target := fmt.Sprintf("test-import.%s", os.Getenv("CLOUDFLARE_DOMAIN"))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudflarePageRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflarePageRuleConfigFullySpecified(zone, target),
Config: testAccCheckCloudflarePageRuleConfigFullySpecified(zoneID, target),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists(name, &pageRule),
),
},
{
ResourceName: name,
ImportStateIdPrefix: fmt.Sprintf("%s/", zone),
ImportStateIdPrefix: fmt.Sprintf("%s/", zoneID),
ImportState: true,
ImportStateVerify: true,
Check: resource.ComposeTestCheckFunc(
Expand All @@ -43,7 +43,7 @@ func TestAccCloudflarePageRule_Import(t *testing.T) {

func TestAccCloudflarePageRule_ImportWithBrowserCacheTTL30(t *testing.T) {
var pageRule cloudflare.PageRule
zone := os.Getenv("CLOUDFLARE_DOMAIN")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
name := "cloudflare_page_rule.test"
testAccRunResourceTestSteps(t, []resource.TestStep{
{
Expand All @@ -54,7 +54,7 @@ func TestAccCloudflarePageRule_ImportWithBrowserCacheTTL30(t *testing.T) {
},
{
ResourceName: name,
ImportStateIdPrefix: fmt.Sprintf("%s/", zone),
ImportStateIdPrefix: fmt.Sprintf("%s/", zoneID),
ImportState: true,
ImportStateVerify: true,
Check: resource.ComposeTestCheckFunc(
Expand All @@ -66,7 +66,7 @@ func TestAccCloudflarePageRule_ImportWithBrowserCacheTTL30(t *testing.T) {

func TestAccCloudflarePageRule_ImportWithoutBrowserCacheTTL(t *testing.T) {
var pageRule cloudflare.PageRule
zone := os.Getenv("CLOUDFLARE_DOMAIN")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
name := "cloudflare_page_rule.test"
testAccRunResourceTestSteps(t, []resource.TestStep{
{
Expand All @@ -77,7 +77,7 @@ func TestAccCloudflarePageRule_ImportWithoutBrowserCacheTTL(t *testing.T) {
},
{
ResourceName: name,
ImportStateIdPrefix: fmt.Sprintf("%s/", zone),
ImportStateIdPrefix: fmt.Sprintf("%s/", zoneID),
ImportState: true,
ImportStateVerify: true,
Check: resource.ComposeTestCheckFunc(
Expand Down
11 changes: 6 additions & 5 deletions cloudflare/import_cloudflare_rate_limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
func TestAccCloudflareRateLimit_Import(t *testing.T) {
t.Parallel()
var rateLimit cloudflare.RateLimit
zone := os.Getenv("CLOUDFLARE_DOMAIN")
domain := os.Getenv("CLOUDFLARE_DOMAIN")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
rnd := generateRandomResourceName()
name := "cloudflare_rate_limit." + rnd

Expand All @@ -22,20 +23,20 @@ func TestAccCloudflareRateLimit_Import(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareRateLimitConfigMatchingUrl(zone, rnd),
Config: testAccCheckCloudflareRateLimitConfigMatchingUrl(zoneID, rnd, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflareRateLimitExists(name, &rateLimit),
testAccCheckCloudflareRateLimitIDIsValid(name, zone),
testAccCheckCloudflareRateLimitIDIsValid(name, zoneID),
),
},
{
ResourceName: name,
ImportStateIdPrefix: fmt.Sprintf("%s/", zone),
ImportStateIdPrefix: fmt.Sprintf("%s/", zoneID),
ImportState: true,
ImportStateVerify: true,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflareRateLimitExists(name, &rateLimit),
testAccCheckCloudflareRateLimitIDIsValid(name, zone),
testAccCheckCloudflareRateLimitIDIsValid(name, zoneID),
),
},
},
Expand Down
6 changes: 3 additions & 3 deletions cloudflare/import_cloudflare_waf_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestAccCloudflareWAFRule_Import(t *testing.T) {
t.Parallel()
zone := os.Getenv("CLOUDFLARE_DOMAIN")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
ruleID := "100000"
name := generateRandomResourceName()

Expand All @@ -20,11 +20,11 @@ func TestAccCloudflareWAFRule_Import(t *testing.T) {
CheckDestroy: testAccCheckCloudflareWAFRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareWAFRuleConfig(zone, ruleID, "block", name),
Config: testAccCheckCloudflareWAFRuleConfig(zoneID, ruleID, "block", name),
},
{
ResourceName: "cloudflare_waf_rule." + name,
ImportStateIdPrefix: fmt.Sprintf("%s/", zone),
ImportStateIdPrefix: fmt.Sprintf("%s/", zoneID),
ImportState: true,
ImportStateVerify: true,
},
Expand Down
17 changes: 14 additions & 3 deletions cloudflare/import_cloudflare_worker_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ import (
"os"
"testing"

"github.com/cloudflare/cloudflare-go"
cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccCloudflareWorkerRoute_Import(t *testing.T) {
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Workers
// service does not yet support the API tokens and it results in
// misleading state error messages.
if os.Getenv("CLOUDFLARE_API_TOKEN") != "" {
defer func(apiToken string) {
os.Setenv("CLOUDFLARE_API_TOKEN", apiToken)
}(os.Getenv("CLOUDFLARE_API_TOKEN"))
os.Setenv("CLOUDFLARE_API_TOKEN", "")
}

var route cloudflare.WorkerRoute
zone := os.Getenv("CLOUDFLARE_DOMAIN")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
routeRnd := generateRandomResourceName()
routeName := "cloudflare_worker_route." + routeRnd
pattern := fmt.Sprintf("%s/%s", zone, generateRandomResourceName())
Expand All @@ -26,14 +37,14 @@ func TestAccCloudflareWorkerRoute_Import(t *testing.T) {
CheckDestroy: testAccCheckCloudflareWorkerRouteDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareWorkerRouteConfigMultiScriptInitial(zone, routeRnd, scriptRnd, pattern),
Config: testAccCheckCloudflareWorkerRouteConfigMultiScriptInitial(zoneID, routeRnd, scriptRnd, pattern),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflareWorkerRouteExists(routeName, &route),
),
},
{
ResourceName: routeName,
ImportStateIdPrefix: fmt.Sprintf("%s/", zone),
ImportStateIdPrefix: fmt.Sprintf("%s/", zoneID),
ImportState: true,
ImportStateVerify: true,
Check: resource.ComposeTestCheckFunc(
Expand Down
15 changes: 12 additions & 3 deletions cloudflare/import_cloudflare_worker_script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ import (
"os"
"testing"

"github.com/cloudflare/cloudflare-go"
cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccCloudflareWorkerScript_Import(t *testing.T) {
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Workers
// service does not yet support the API tokens and it results in
// misleading state error messages.
if os.Getenv("CLOUDFLARE_API_TOKEN") != "" {
defer func(apiToken string) {
os.Setenv("CLOUDFLARE_API_TOKEN", apiToken)
}(os.Getenv("CLOUDFLARE_API_TOKEN"))
os.Setenv("CLOUDFLARE_API_TOKEN", "")
}

var script cloudflare.WorkerScript
zone := os.Getenv("CLOUDFLARE_DOMAIN")
rnd := generateRandomResourceName()
name := "cloudflare_worker_script." + rnd

Expand All @@ -20,7 +29,7 @@ func TestAccCloudflareWorkerScript_Import(t *testing.T) {
CheckDestroy: testAccCheckCloudflareWorkerScriptDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareWorkerScriptConfigSingleScriptInitial(zone, rnd),
Config: testAccCheckCloudflareWorkerScriptConfigMultiScriptInitial(rnd),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflareWorkerScriptExists(name, &script),
),
Expand Down
6 changes: 3 additions & 3 deletions cloudflare/import_resource_cloudflare_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import (
)

func TestAccCloudflareRecord_Import(t *testing.T) {
zone := os.Getenv("CLOUDFLARE_DOMAIN")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
name := "cloudflare_record.foobar"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareRecordConfigBasic(zone, name),
Config: testAccCheckCloudflareRecordConfigBasic(zoneID, name),
},
{
ResourceName: name,
ImportStateIdPrefix: fmt.Sprintf("%s/", zone),
ImportStateIdPrefix: fmt.Sprintf("%s/", zoneID),
ImportState: true,
ImportStateVerify: true,
},
Expand Down
Loading

0 comments on commit 0995af4

Please sign in to comment.