Skip to content

Commit

Permalink
Add support for changing TieredCacheSmartTopology (#46)
Browse files Browse the repository at this point in the history
* create provider for TieredCacheSmartTopology

* use latest shopify cloudflare-go fork
  • Loading branch information
yknx4 authored and sbfaulkner committed Jun 20, 2022
1 parent 3b774a6 commit b04f2b4
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/cloudflare/terraform-provider-cloudflare

go 1.17

replace github.com/cloudflare/cloudflare-go => github.com/shopify/cloudflare-go v0.7.4-0.20220524200708-1c6eaaa9b6b6
replace github.com/cloudflare/cloudflare-go => github.com/shopify/cloudflare-go v0.7.4-0.20220601181031-08b76a8c8a1a

require (
github.com/agext/levenshtein v1.2.3 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/antihax/optional v0.0.0-20180407024304-ca021399b1a6/go.mod h1:V8iCPQYkqmusNa815XgQio277wI47sdRh1dUOLdyC6Q=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/antzucaro/matchr v0.0.0-20210222213004-b04723ef80f0/go.mod h1:v3ZDlfVAL1OrkKHbGSFFK60k0/7hruHPDq2XMs9Gu6U=
github.com/aokoli/goutils v1.0.1/go.mod h1:SijmP0QR8LtwsmDs8Yii5Z/S4trXFGFC2oO5g9DP+DQ=
github.com/apparentlymart/go-cidr v1.0.1/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4tdgBZjnU=
Expand Down Expand Up @@ -953,6 +952,8 @@ github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5g
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopify/cloudflare-go v0.7.4-0.20220524200708-1c6eaaa9b6b6 h1:qVNN2tyDhclFmVmIDWzNHjiwbhG9Bk3y7LoO5UIgqfU=
github.com/shopify/cloudflare-go v0.7.4-0.20220524200708-1c6eaaa9b6b6/go.mod h1:6VPJV6VD+42Q85HQWv9ITAy6P01QOCwuxJ+3UetJelw=
github.com/shopify/cloudflare-go v0.7.4-0.20220601181031-08b76a8c8a1a h1:EgSmk22v0UR390WXr2oZ+MegV7puX8jsEg65eZBW+LI=
github.com/shopify/cloudflare-go v0.7.4-0.20220601181031-08b76a8c8a1a/go.mod h1:MmAqiRfD8rjKEuUe4MYNHfHjYhFWfW7PNe12CCQWqPY=
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
Expand Down Expand Up @@ -1054,7 +1055,7 @@ github.com/ultraware/whitespace v0.0.5 h1:hh+/cpIcopyMYbZNVov9iSxvJU3OYQg78Sfaqz
github.com/ultraware/whitespace v0.0.5/go.mod h1:aVMh/gQve5Maj9hQ/hg+F75lr/X5A89uZnzAmWSineA=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli/v2 v2.8.0/go.mod h1:TYFbtzt/azQoJOrGH5mDfZtS0jIkl/OeFwlRWPR9KRM=
github.com/urfave/cli/v2 v2.8.1/go.mod h1:Z41J9TPoffeoqP0Iza0YbAhGvymRdZAd2uPmZ5JxRdY=
github.com/uudashr/gocognit v1.0.5 h1:rrSex7oHr3/pPLQ0xoWq108XMU8s678FJcQ+aSfOHa4=
github.com/uudashr/gocognit v1.0.5/go.mod h1:wgYz0mitoKOTysqxTDMOUXg+Jb5SvtihkfmugIZYpEA=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
Expand All @@ -1075,6 +1076,7 @@ github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6e
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yagipy/maintidx v1.0.0 h1:h5NvIsCz+nRDapQ0exNv4aJ0yXSI0420omVANTv3GJM=
github.com/yagipy/maintidx v1.0.0/go.mod h1:0qNf/I/CCZXSMhsRsrEPDZ+DkekpKLXAJfsTACwgXLk=
github.com/yeya24/promlinter v0.2.0 h1:xFKDQ82orCU5jQujdaD8stOHiv8UN68BSdn2a8u8Y3o=
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ func New(version string) func() *schema.Provider {
"cloudflare_zone_lockdown": resourceCloudflareZoneLockdown(),
"cloudflare_zone_settings_override": resourceCloudflareZoneSettingsOverride(),
"cloudflare_zone": resourceCloudflareZone(),
/* Non Public below this line*/
"cloudflare_zone_cache_tiered_cache_smart_topology": resourceCloudflareZoneCacheTieredCacheSmartTopology(),
},
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package provider

import (
"context"
"errors"
"fmt"
"strings"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceCloudflareZoneCacheTieredCacheSmartTopology() *schema.Resource {
return &schema.Resource{
Schema: resourceCloudflareZoneCacheTieredCacheSmartTopologySchema(),
CreateContext: resourceCloudflareZoneCacheTieredCacheSmartTopologyUpdate,
ReadContext: resourceCloudflareZoneCacheTieredCacheSmartTopologyRead,
UpdateContext: resourceCloudflareZoneCacheTieredCacheSmartTopologyUpdate,
DeleteContext: resourceCloudflareZoneCacheTieredCacheSmartTopologyDelete,
}
}

func resourceCloudflareZoneCacheTieredCacheSmartTopologyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)

tflog.Info(ctx, fmt.Sprintf("Reading Zone Cache TieredCacheSmartTopology %q", d.Id()))

topology, err := client.TieredCacheSmartTopology(ctx, d.Id())

if err != nil {
if strings.Contains(err.Error(), "HTTP status 404") {
tflog.Info(ctx, fmt.Sprintf("Zone Cache TieredCacheSmartTopology for zone %q not found", d.Id()))
d.SetId("")
return nil
} else {
return diag.FromErr(fmt.Errorf("error reading cache TieredCacheSmartTopology for zone %q: %w", d.Id(), err))
}
}

value := topology.Value

if err := d.Set("value", value); err != nil {
return diag.FromErr(fmt.Errorf("failed to set value: %w", err))
}

return nil
}

func parseValue(value string) (cloudflare.TieredCacheSmartTopologyValue, error) {
switch value {
case "on":
return cloudflare.TieredCacheSmartTopologyOn, nil
case "off":
return cloudflare.TieredCacheSmartTopologyOff, nil
default:
return cloudflare.TieredCacheSmartTopologyOff, errors.New("invalid value for TieredCacheSmartTopologyValue")
}
}

func resourceCloudflareZoneCacheTieredCacheSmartTopologyUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)

zoneID := d.Get("zone_id").(string)
d.SetId(zoneID)

value, err := parseValue(d.Get("value").(string))
if err != nil {
return diag.FromErr(err)
}
tflog.Info(ctx, fmt.Sprintf("Setting Zone Cache TieredCacheSmartTopology to struct: %+v for zone ID: %q", value, d.Id()))

_, err = client.UpdateTieredCacheSmartTopology(ctx, d.Id(), cloudflare.TieredCacheSmartTopologyUpdateOptions{Value: value})

if err != nil {
return diag.FromErr(fmt.Errorf("error setting cache TieredCacheSmartTopology for zone %q: %w", d.Id(), err))
}

return resourceCloudflareZoneCacheTieredCacheSmartTopologyRead(ctx, d, meta)
}

func resourceCloudflareZoneCacheTieredCacheSmartTopologyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
tflog.Info(ctx, fmt.Sprintf("Deleting Zone Cache TieredCacheSmartTopology for zone ID: %q", d.Id()))
d.Set("value", cloudflare.TieredCacheSmartTopologyOff)

return resourceCloudflareZoneCacheTieredCacheSmartTopologyUpdate(ctx, d, meta)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package provider

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

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

func resourceCloudflareZoneCacheTieredCacheSmartTopologySchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"zone_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"value": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"on",
"off",
}, false),
},
}
}

0 comments on commit b04f2b4

Please sign in to comment.