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

resource/cloudflare_argo: Allow settings to be individually customised #703

Merged
merged 2 commits into from
Jun 15, 2020
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
51 changes: 29 additions & 22 deletions cloudflare/resource_cloudflare_argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ func resourceCloudflareArgo() *schema.Resource {
"tiered_caching": {
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{"on", "off"}, false),
Default: "off",
Optional: true,
},
"smart_routing": {
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{"on", "off"}, false),
Default: "off",
Optional: true,
},
},
Expand All @@ -50,22 +48,27 @@ func resourceCloudflareArgoRead(d *schema.ResourceData, meta interface{}) error

log.Printf("[DEBUG] zone ID: %s", zoneID)

tieredCaching, err := client.ArgoTieredCaching(zoneID)
if err != nil {
return errors.Wrap(err, "failed to get tiered caching setting")
}
checksum := stringChecksum(fmt.Sprintf("%s/argo", zoneID))
d.SetId(checksum)
d.Set("zone_id", zoneID)

if _, ok := d.GetOk("tiered_caching"); ok {
tieredCaching, err := client.ArgoTieredCaching(zoneID)
if err != nil {
return errors.Wrap(err, "failed to get tiered caching setting")
}

smartRouting, err := client.ArgoSmartRouting(zoneID)
if err != nil {
return errors.Wrap(err, "failed to get smart routing setting")
d.Set("tiered_caching", tieredCaching.Value)
}

checksum := stringChecksum(fmt.Sprintf("%s/argo", zoneID))
d.SetId(checksum)
if _, ok := d.GetOk("smart_routing"); ok {
smartRouting, err := client.ArgoSmartRouting(zoneID)
if err != nil {
return errors.Wrap(err, "failed to get smart routing setting")
}

d.Set("zone_id", zoneID)
d.Set("tiered_caching", tieredCaching.Value)
d.Set("smart_routing", smartRouting.Value)
d.Set("smart_routing", smartRouting.Value)
}

return nil
}
Expand All @@ -76,17 +79,21 @@ func resourceCloudflareArgoUpdate(d *schema.ResourceData, meta interface{}) erro
tieredCaching := d.Get("tiered_caching").(string)
smartRouting := d.Get("smart_routing").(string)

argoSmartRouting, err := client.UpdateArgoSmartRouting(zoneID, smartRouting)
if err != nil {
return errors.Wrap(err, "failed to update smart routing setting")
if smartRouting != "" {
argoSmartRouting, err := client.UpdateArgoSmartRouting(zoneID, smartRouting)
if err != nil {
return errors.Wrap(err, "failed to update smart routing setting")
}
log.Printf("[DEBUG] Argo Smart Routing set to: %s", argoSmartRouting.Value)
}
log.Printf("[DEBUG] Argo Smart Routing set to: %s", argoSmartRouting.Value)

argoTieredCaching, err := client.UpdateArgoTieredCaching(zoneID, tieredCaching)
if err != nil {
return errors.Wrap(err, "failed to update tiered caching setting")
if tieredCaching != "" {
argoTieredCaching, err := client.UpdateArgoTieredCaching(zoneID, tieredCaching)
if err != nil {
return errors.Wrap(err, "failed to update tiered caching setting")
}
log.Printf("[DEBUG] Argo Tiered Caching set to: %s", argoTieredCaching.Value)
}
log.Printf("[DEBUG] Argo Tiered Caching set to: %s", argoTieredCaching.Value)

return resourceCloudflareArgoRead(d, meta)
}
Expand Down
70 changes: 70 additions & 0 deletions cloudflare/resource_cloudflare_argo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cloudflare

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccCloudflareArgoOnlySetTieredCaching(t *testing.T) {
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
rnd := generateRandomResourceName()
name := fmt.Sprintf("cloudflare_argo.%s", rnd)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareArgoTieredCachingConfig(zoneID, rnd),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "tiered_caching", "on"),
resource.TestCheckNoResourceAttr(name, "smart_routing"),
),
},
},
})
}

func TestAccCloudflareArgoSetTieredAndSmartRouting(t *testing.T) {
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
rnd := generateRandomResourceName()
name := fmt.Sprintf("cloudflare_argo.%s", rnd)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareArgoFullConfig(zoneID, rnd),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "tiered_caching", "on"),
resource.TestCheckResourceAttr(name, "smart_routing", "on"),
),
},
},
})
}

func testAccCheckCloudflareArgoTieredCachingConfig(zoneID, name string) string {
return fmt.Sprintf(`
resource "cloudflare_argo" "%[2]s" {
zone_id = "%[1]s"
tiered_caching = "on"
}`, zoneID, name)
}

func testAccCheckCloudflareArgoFullConfig(zoneID, name string) string {
return fmt.Sprintf(`
resource "cloudflare_argo" "%[2]s" {
zone_id = "%[1]s"
tiered_caching = "on"
smart_routing = "on"
}`, zoneID, name)
}
4 changes: 2 additions & 2 deletions website/docs/r/argo.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ resource "cloudflare_argo" "example" {
The following arguments are supported:

* `zone_id` - (Required) The DNS zone ID that you wish to manage Argo on.
* `tiered_caching` - (Optional) Whether tiered caching is enabled. Valid values: `on` or `off`. Defaults to `off`.
* `smart_routing` - (Optional) Whether smart routing is enabled. Valid values: `on` or `off`. Defaults to `off`.
* `tiered_caching` - (Optional) Whether tiered caching is enabled. Valid values: `on` or `off`.
* `smart_routing` - (Optional) Whether smart routing is enabled. Valid values: `on` or `off`.


## Import
Expand Down