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

SDK-80: Zone to zone_id refactoring #472

Merged
merged 16 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from 12 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
34 changes: 1 addition & 33 deletions cloudflare/resource_cloudflare_access_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ func resourceCloudflareAccessRule() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"zone": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Deprecated: "`zone` is deprecated in favour of explicit `zone_id` and will be removed in the next major release",
},
"zone_id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -68,7 +61,6 @@ func resourceCloudflareAccessRule() *schema.Resource {

func resourceCloudflareAccessRuleCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zone := d.Get("zone").(string)
zoneID := d.Get("zone_id").(string)

newRule := cloudflare.AccessRule{
Expand All @@ -88,22 +80,13 @@ func resourceCloudflareAccessRuleCreate(d *schema.ResourceData, meta interface{}
var r *cloudflare.AccessRuleResponse
var err error

if zone == "" && zoneID == "" {
if zoneID == "" {
if client.AccountID != "" {
r, err = client.CreateAccountAccessRule(client.AccountID, newRule)
} else {
r, err = client.CreateUserAccessRule(newRule)
}
} else {
if zoneID == "" {
zoneID, err = client.ZoneIDByName(zone)
if err != nil {
return fmt.Errorf("Error finding zone %q: %s", zone, err)
}

d.Set("zone_id", zoneID)
}

r, err = client.CreateZoneAccessRule(zoneID, newRule)
}

Expand All @@ -123,20 +106,6 @@ func resourceCloudflareAccessRuleCreate(d *schema.ResourceData, meta interface{}
func resourceCloudflareAccessRuleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
zoneName := d.Get("zone").(string)
if zoneID == "" {
zoneID, _ = client.ZoneIDByName(zoneName)
} else {
zones, err := client.ListZones()
if err != nil {
return fmt.Errorf("failed to lookup all zones: %s", err)
}
for _, zone := range zones {
if zone.ID == zoneID {
zoneName = zone.Name
}
}
}

var accessRuleResponse *cloudflare.AccessRuleResponse
var err error
Expand Down Expand Up @@ -166,7 +135,6 @@ func resourceCloudflareAccessRuleRead(d *schema.ResourceData, meta interface{})

log.Printf("[DEBUG] Cloudflare Access Rule read configuration: %#v", accessRuleResponse)

d.Set("zone", zoneName)
d.Set("zone_id", zoneID)
d.Set("mode", accessRuleResponse.Result.Mode)
d.Set("notes", accessRuleResponse.Result.Notes)
Expand Down
39 changes: 1 addition & 38 deletions cloudflare/resource_cloudflare_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,10 @@ func resourceCloudflareFilter() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"zone": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Deprecated: "`zone` is deprecated in favour of explicit `zone_id` and will be removed in the next major release",
},
"zone_id": {
Type: schema.TypeString,
Optional: true,
Required: true,
ForceNew: true,
Computed: true,
},
"paused": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -61,21 +53,10 @@ func resourceCloudflareFilter() *schema.Resource {

func resourceCloudflareFilterCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneName := d.Get("zone").(string)
zoneID := d.Get("zone_id").(string)

var err error

if zoneName == "" && zoneID == "" {
return fmt.Errorf("'zone' or 'zone_id' is required")
} else if zoneID == "" {
zoneID, err = client.ZoneIDByName(zoneName)
if err != nil {
return fmt.Errorf("Error finding zone %q: %s", zoneName, err)
}
d.Set("zone_id", zoneID)
}

var newFilter cloudflare.Filter

if paused, ok := d.GetOk("paused"); ok {
Expand Down Expand Up @@ -116,22 +97,6 @@ func resourceCloudflareFilterCreate(d *schema.ResourceData, meta interface{}) er
func resourceCloudflareFilterRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
zoneName := d.Get("zone").(string)

if zoneID == "" {
zoneID, _ = client.ZoneIDByName(zoneName)
} else {
zones, err := client.ListZones()
if err != nil {
return fmt.Errorf("failed to lookup all zones: %s", err)
}

for _, zone := range zones {
if zone.ID == zoneID {
zoneName = zone.Name
}
}
}

log.Printf("[DEBUG] Getting a Filter record for zone %q, id %s", zoneID, d.Id())
filter, err := client.Filter(zoneID, d.Id())
Expand All @@ -150,8 +115,6 @@ func resourceCloudflareFilterRead(d *schema.ResourceData, meta interface{}) erro

log.Printf("[DEBUG] Cloudflare Filter read configuration: %#v", filter)

d.Set("zone", zoneName)
d.Set("zone_id", zoneID)
d.Set("paused", filter.Paused)
d.Set("description", filter.Description)
d.Set("expression", filter.Expression)
Expand Down
19 changes: 9 additions & 10 deletions cloudflare/resource_cloudflare_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestAccFilterSimple(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_filter." + rnd
zone := os.Getenv("CLOUDFLARE_DOMAIN")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")

filterQuoted := `(http.request.uri.path ~ \".*wp-login-` + rnd + `.php\" or http.request.uri.path ~ \".*xmlrpc.php\") and ip.src ne 192.0.2.1`
filterUnquoted := `(http.request.uri.path ~ ".*wp-login-` + rnd + `.php" or http.request.uri.path ~ ".*xmlrpc.php") and ip.src ne 192.0.2.1`
Expand All @@ -21,33 +21,32 @@ func TestAccFilterSimple(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testFilterConfig(rnd, zone, "true", "this is notes", filterQuoted),
Config: testFilterConfig(rnd, zoneID, "true", "this is notes", filterQuoted),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "description", "this is notes"),
resource.TestCheckResourceAttr(name, "expression", filterUnquoted),
resource.TestCheckResourceAttr(name, "paused", "true"),
resource.TestCheckResourceAttr(name, "zone", zone),
resource.TestCheckResourceAttrSet(name, "zone_id"),
resource.TestCheckResourceAttr(name, "zone_id", zoneID),
),
},
},
})
}

func testFilterConfig(resourceID, zoneName, paused, description, expression string) string {
func testFilterConfig(resourceID, zoneID, paused, description, expression string) string {
return fmt.Sprintf(`
resource "cloudflare_filter" "%[1]s" {
zone = "%[2]s"
zone_id = "%[2]s"
paused = "%[3]s"
description = "%[4]s"
expression = "%[5]s"
}
`, resourceID, zoneName, paused, description, expression)
`, resourceID, zoneID, paused, description, expression)
}

const multiLineFilter = `
resource "cloudflare_filter" "%[1]s" {
zone = "%[2]s"
zone_id = "%[2]s"
paused = "%[3]s"
description = "%[4]s"
expression = <<EOF
Expand All @@ -58,14 +57,14 @@ EOF

func TestAccFilterWhitespace(t *testing.T) {
rnd := generateRandomResourceName()
zone := os.Getenv("CLOUDFLARE_DOMAIN")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(multiLineFilter, rnd, zone, "true", "multi-line filter",
Config: fmt.Sprintf(multiLineFilter, rnd, zoneID, "true", "multi-line filter",
"\t\nhttp.request.method in {\"PUT\" \"DELETE\"} and\nhttp.request.uri.path eq \"/\" \n"),
},
},
Expand Down
39 changes: 2 additions & 37 deletions cloudflare/resource_cloudflare_firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,10 @@ func resourceCloudflareFirewallRule() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"zone": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Deprecated: "`zone` is deprecated in favour of explicit `zone_id` and will be removed in the next major release",
},
"zone_id": {
Type: schema.TypeString,
Optional: true,
Required: true,
ForceNew: true,
Computed: true,
},
"filter_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -63,21 +55,10 @@ func resourceCloudflareFirewallRule() *schema.Resource {

func resourceCloudflareFirewallRuleCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneName := d.Get("zone").(string)
zoneID := d.Get("zone_id").(string)

var err error

if zoneName == "" && zoneID == "" {
return fmt.Errorf("'zone' or 'zone_id' is required")
} else if zoneID == "" {
zoneID, err = client.ZoneIDByName(zoneName)
if err != nil {
return fmt.Errorf("Error finding zone %q: %s", zoneName, err)
}
d.Set("zone_id", zoneID)
}

var newFirewallRule cloudflare.FirewallRule

if paused, ok := d.GetOk("paused"); ok {
Expand Down Expand Up @@ -109,7 +90,7 @@ func resourceCloudflareFirewallRuleCreate(d *schema.ResourceData, meta interface
r, err = client.CreateFirewallRules(zoneID, []cloudflare.FirewallRule{newFirewallRule})

if err != nil {
return fmt.Errorf("error creating Firewall Rule for zone %q: %s", zoneName, err)
return fmt.Errorf("error creating Firewall Rule for zone %q: %s", zoneID, err)
}

if len(r) == 0 {
Expand All @@ -126,20 +107,6 @@ func resourceCloudflareFirewallRuleCreate(d *schema.ResourceData, meta interface
func resourceCloudflareFirewallRuleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
zoneName := d.Get("zone").(string)
if zoneID == "" {
zoneID, _ = client.ZoneIDByName(zoneName)
} else {
zones, err := client.ListZones()
if err != nil {
return fmt.Errorf("failed to lookup all zones: %s", err)
}
for _, zone := range zones {
if zone.ID == zoneID {
zoneName = zone.Name
}
}
}

firewallRule, err := client.FirewallRule(zoneID, d.Id())

Expand All @@ -157,8 +124,6 @@ func resourceCloudflareFirewallRuleRead(d *schema.ResourceData, meta interface{}

log.Printf("[DEBUG] Cloudflare Firewall Rule read configuration: %#v", firewallRule)

d.Set("zone", zoneName)
d.Set("zone_id", zoneID)
d.Set("paused", firewallRule.Paused)
d.Set("description", firewallRule.Description)
d.Set("action", firewallRule.Action)
Expand Down
17 changes: 8 additions & 9 deletions cloudflare/resource_cloudflare_firewall_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,46 @@ import (
func TestAccFirewallRuleSimple(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_firewall_rule." + rnd
zone := os.Getenv("CLOUDFLARE_DOMAIN")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")

filterQuoted := `(http.request.uri.path ~ \".*wp-login-` + rnd + `.php\" or http.request.uri.path ~ \".*xmlrpc.php\") and ip.src ne 192.0.2.1`
// filterUnquoted := `(http.request.uri.path ~ ".*wp-login-` + rnd + `.php" or http.request.uri.path ~ ".*xmlrpc.php") and ip.src ne 192.0.2.1`

fmt.Print(testFirewallRuleConfig(rnd, zone, "true", "this is notes", filterQuoted, "allow", "1"))
fmt.Print(testFirewallRuleConfig(rnd, zoneID, "true", "this is notes", filterQuoted, "allow", "1"))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testFirewallRuleConfig(rnd, zone, "true", "this is notes", filterQuoted, "allow", "1"),
Config: testFirewallRuleConfig(rnd, zoneID, "true", "this is notes", filterQuoted, "allow", "1"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "description", "this is notes"),
resource.TestCheckResourceAttr(name, "paused", "true"),
resource.TestCheckResourceAttr(name, "action", "allow"),
resource.TestCheckResourceAttr(name, "priority", "1"),
resource.TestCheckResourceAttr(name, "zone", zone),
resource.TestCheckResourceAttrSet(name, "zone_id"),
resource.TestCheckResourceAttr(name, "zone_id", zoneID),
),
},
},
})
}

func testFirewallRuleConfig(resourceID, zoneName, paused, description, expression, action, priority string) string {
func testFirewallRuleConfig(resourceID, zoneID, paused, description, expression, action, priority string) string {
return fmt.Sprintf(`
resource "cloudflare_filter" "%[1]s" {
zone = "%[2]s"
zone_id = "%[2]s"
paused = "%[3]s"
description = "%[4]s"
expression = "%[5]s"
}
resource "cloudflare_firewall_rule" "%[1]s" {
zone = "%[2]s"
zone_id = "%[2]s"
paused = "%[3]s"
description = "%[4]s"
filter_id = "${cloudflare_filter.%[1]s.id}"
action = "%[6]s"
priority = %[7]s
}
`, resourceID, zoneName, paused, description, expression, action, priority)
`, resourceID, zoneID, paused, description, expression, action, priority)
}
Loading