From e6719386d58c84a05fcf59cf8a613e2650a5fd1a Mon Sep 17 00:00:00 2001 From: Daniel Quackenbush <25692880+danquack@users.noreply.github.com> Date: Sat, 21 Oct 2023 15:18:41 -0400 Subject: [PATCH 1/6] d/cloudflare_tunnel --- docs/data-sources/tunnel.md | 34 ++++++++ internal/sdkv2provider/data_source_tunnel.go | 77 +++++++++++++++++++ .../sdkv2provider/data_source_tunnel_test.go | 41 ++++++++++ internal/sdkv2provider/provider.go | 1 + 4 files changed, 153 insertions(+) create mode 100644 docs/data-sources/tunnel.md create mode 100644 internal/sdkv2provider/data_source_tunnel.go create mode 100644 internal/sdkv2provider/data_source_tunnel_test.go diff --git a/docs/data-sources/tunnel.md b/docs/data-sources/tunnel.md new file mode 100644 index 0000000000..22303588e6 --- /dev/null +++ b/docs/data-sources/tunnel.md @@ -0,0 +1,34 @@ +--- +page_title: "cloudflare_tunnel Data Source - Cloudflare" +subcategory: "" +description: |- + Use this datasource to lookup a tunnel in an account. +--- + +# cloudflare_tunnel (Data Source) + +```terraform +data "cloudflare_rulesets" "example" { + account_id = "f037e56e89293a057740de681ac9abbe" + name = "my-tunnel" +} +``` + + + + +## Schema + +### Required + +- `account_id` (String) The account identifier to target for the resource. **Modifying this attribute will force creation of a new resource.** +- `name` (String) Name of the tunnel. **Modifying this attribute will force creation of a new resource.** + +### Read-Only + +- `id` (String) ID of the tunnel. +- `remote_config` (Boolean) Whether the tunnel can be configured remotely from the Zero Trust dashboard. +- `status` (String) The status of the tunnel. Possible values: inactive, degraded, healthy, down. +- `tunnel_type` (String) The type of the tunnel. Possible values: cfd_tunnel, warp_connector. + + diff --git a/internal/sdkv2provider/data_source_tunnel.go b/internal/sdkv2provider/data_source_tunnel.go new file mode 100644 index 0000000000..f28dcb591e --- /dev/null +++ b/internal/sdkv2provider/data_source_tunnel.go @@ -0,0 +1,77 @@ +package sdkv2provider + +import ( + "context" + "fmt" + + "github.com/cloudflare/cloudflare-go" + "github.com/cloudflare/terraform-provider-cloudflare/internal/consts" + "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 dataSourceCloudflareTunnel() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceCloudflareTunnelRead, + + Schema: map[string]*schema.Schema{ + consts.AccountIDSchemaKey: { + Description: consts.AccountIDSchemaDescription, + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + Description: "Name of the tunnel", + ForceNew: true, + }, + "id": { + Type: schema.TypeString, + Computed: true, + Description: "ID of the tunnel", + }, + "status": { + Type: schema.TypeString, + Computed: true, + Description: "The status of the tunnel. Possible values: inactive, degraded, healthy, down", + }, + "tunnel_type": { + Type: schema.TypeString, + Computed: true, + Description: "The type of the tunnel. Possible values: cfd_tunnel, warp_connector", + }, + "remote_config": { + Type: schema.TypeBool, + Computed: true, + Description: "Whether the tunnel can be configured remotely from the Zero Trust dashboard.", + }, + }, + } +} + +func dataSourceCloudflareTunnelRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + tflog.Debug(ctx, fmt.Sprintf("Reading Tunnel")) + client := meta.(*cloudflare.API) + accID := d.Get(consts.AccountIDSchemaKey).(string) + + name := d.Get("name").(string) + tunnels, _, err := client.ListTunnels(ctx, cloudflare.AccountIdentifier(accID), cloudflare.TunnelListParams{Name: name}) + if err != nil { + return diag.FromErr(fmt.Errorf("failed to fetch Argo Tunnel: %w", err)) + } + if len(tunnels) == 0 { + return diag.FromErr(fmt.Errorf("No tunnels with name: %s", name)) + } + + tunnel := tunnels[0] + + d.SetId(tunnel.ID) + d.Set("status", tunnel.Status) + d.Set("id", tunnel.ID) + d.Set("tunnel_type", tunnel.TunnelType) + d.Set("remote_config", tunnel.RemoteConfig) + return nil +} diff --git a/internal/sdkv2provider/data_source_tunnel_test.go b/internal/sdkv2provider/data_source_tunnel_test.go new file mode 100644 index 0000000000..aef6887e58 --- /dev/null +++ b/internal/sdkv2provider/data_source_tunnel_test.go @@ -0,0 +1,41 @@ +package sdkv2provider + +import ( + "fmt" + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestAccCloudflareTunnelMatchName(t *testing.T) { + rnd := generateRandomResourceName() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: testCloudflareTunnelMatchName(rnd), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.cloudflare_tunnel.example", "status", "inactive"), + ), + }, + }, + }) +} + +func testCloudflareTunnelMatchName(name string) string { + accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID") + return fmt.Sprintf(` +resource "cloudflare_tunnel" "example" { + account_id = "%[1]s" + name = %[2]q + secret = "AQIDBAUGBwgBAgMEBQYHCAECAwQFBgcIAQIDBAUGBwg=" +} +data "cloudflare_tunnel" "example" { + account_id = cloudflare_tunnel.example.account_id + name = cloudflare_tunnel.example.name +} +`, accountID, name) +} diff --git a/internal/sdkv2provider/provider.go b/internal/sdkv2provider/provider.go index 19023f57bd..ab4fd08ff0 100644 --- a/internal/sdkv2provider/provider.go +++ b/internal/sdkv2provider/provider.go @@ -172,6 +172,7 @@ func New(version string) func() *schema.Provider { "cloudflare_record": dataSourceCloudflareRecord(), "cloudflare_rulesets": dataSourceCloudflareRulesets(), "cloudflare_zone_cache_reserve": dataSourceCloudflareZoneCacheReserve(), + "cloudflare_tunnel": dataSourceCloudflareTunnel(), "cloudflare_zone_dnssec": dataSourceCloudflareZoneDNSSEC(), "cloudflare_zone": dataSourceCloudflareZone(), "cloudflare_zones": dataSourceCloudflareZones(), From bf7d5a783d74dfb4eaa83e5e42a4389a3efdc55d Mon Sep 17 00:00:00 2001 From: Daniel Quackenbush <25692880+danquack@users.noreply.github.com> Date: Sat, 21 Oct 2023 15:20:05 -0400 Subject: [PATCH 2/6] changelog --- .changelog/2866.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/2866.txt diff --git a/.changelog/2866.txt b/.changelog/2866.txt new file mode 100644 index 0000000000..27c9ceb8e0 --- /dev/null +++ b/.changelog/2866.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +cloudflare_tunnel +``` From 411fc244bfb5db0ec42cc61e267855713f3f2943 Mon Sep 17 00:00:00 2001 From: Daniel Quackenbush <25692880+danquack@users.noreply.github.com> Date: Sun, 22 Oct 2023 20:29:35 -0400 Subject: [PATCH 3/6] Add example, update descriptions to use renderAvailableDocumentationValuesStringSlice --- .../data-sources/cloudflare_tunnel/data-source.tf | 4 ++++ internal/sdkv2provider/data_source_tunnel.go | 13 +++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 examples/data-sources/cloudflare_tunnel/data-source.tf diff --git a/examples/data-sources/cloudflare_tunnel/data-source.tf b/examples/data-sources/cloudflare_tunnel/data-source.tf new file mode 100644 index 0000000000..68174e7b0f --- /dev/null +++ b/examples/data-sources/cloudflare_tunnel/data-source.tf @@ -0,0 +1,4 @@ +data "cloudflare_tunnel" "example" { + account_id = "f037e56e89293a057740de681ac9abbe" + name = "my-tunnel" +} diff --git a/internal/sdkv2provider/data_source_tunnel.go b/internal/sdkv2provider/data_source_tunnel.go index f28dcb591e..69c98b7fad 100644 --- a/internal/sdkv2provider/data_source_tunnel.go +++ b/internal/sdkv2provider/data_source_tunnel.go @@ -25,23 +25,23 @@ func dataSourceCloudflareTunnel() *schema.Resource { "name": { Type: schema.TypeString, Required: true, - Description: "Name of the tunnel", + Description: "Name of the tunnel.", ForceNew: true, }, "id": { Type: schema.TypeString, Computed: true, - Description: "ID of the tunnel", + Description: "ID of the tunnel.", }, "status": { Type: schema.TypeString, Computed: true, - Description: "The status of the tunnel. Possible values: inactive, degraded, healthy, down", + Description: fmt.Sprintf("The status of the tunnel. %s", renderAvailableDocumentationValuesStringSlice([]string{"inactive", "degraded", "healthy", "down"})), }, "tunnel_type": { Type: schema.TypeString, Computed: true, - Description: "The type of the tunnel. Possible values: cfd_tunnel, warp_connector", + Description: fmt.Sprintf("The type of the tunnel.. %s", renderAvailableDocumentationValuesStringSlice([]string{"cfd_tunnel", "warp_connector"})), }, "remote_config": { Type: schema.TypeBool, @@ -49,18 +49,19 @@ func dataSourceCloudflareTunnel() *schema.Resource { Description: "Whether the tunnel can be configured remotely from the Zero Trust dashboard.", }, }, + Description: "Use this datasource to lookup a tunnel in an account.", } } func dataSourceCloudflareTunnelRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - tflog.Debug(ctx, fmt.Sprintf("Reading Tunnel")) + tflog.Debug(ctx, "Reading Tunnel") client := meta.(*cloudflare.API) accID := d.Get(consts.AccountIDSchemaKey).(string) name := d.Get("name").(string) tunnels, _, err := client.ListTunnels(ctx, cloudflare.AccountIdentifier(accID), cloudflare.TunnelListParams{Name: name}) if err != nil { - return diag.FromErr(fmt.Errorf("failed to fetch Argo Tunnel: %w", err)) + return diag.FromErr(fmt.Errorf("failed to fetch Tunnel: %w", err)) } if len(tunnels) == 0 { return diag.FromErr(fmt.Errorf("No tunnels with name: %s", name)) From f1e76f4474188a15ea46d15c033cb6d59012b442 Mon Sep 17 00:00:00 2001 From: Daniel Quackenbush <25692880+danquack@users.noreply.github.com> Date: Sun, 22 Oct 2023 20:30:01 -0400 Subject: [PATCH 4/6] use make docs --- docs/data-sources/tunnel.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/data-sources/tunnel.md b/docs/data-sources/tunnel.md index 22303588e6..8790facc40 100644 --- a/docs/data-sources/tunnel.md +++ b/docs/data-sources/tunnel.md @@ -2,20 +2,22 @@ page_title: "cloudflare_tunnel Data Source - Cloudflare" subcategory: "" description: |- - Use this datasource to lookup a tunnel in an account. + Use this datasource to lookup a tunnel in an account. --- # cloudflare_tunnel (Data Source) +Use this datasource to lookup a tunnel in an account. + +## Example Usage + ```terraform -data "cloudflare_rulesets" "example" { +data "cloudflare_tunnel" "example" { account_id = "f037e56e89293a057740de681ac9abbe" name = "my-tunnel" } ``` - - ## Schema @@ -28,7 +30,7 @@ data "cloudflare_rulesets" "example" { - `id` (String) ID of the tunnel. - `remote_config` (Boolean) Whether the tunnel can be configured remotely from the Zero Trust dashboard. -- `status` (String) The status of the tunnel. Possible values: inactive, degraded, healthy, down. -- `tunnel_type` (String) The type of the tunnel. Possible values: cfd_tunnel, warp_connector. +- `status` (String) The status of the tunnel. Available values: `inactive`, `degraded`, `healthy`, `down`. +- `tunnel_type` (String) The type of the tunnel.. Available values: `cfd_tunnel`, `warp_connector`. From 3dcb9d6a14ea0c838065a6172d46961a63b1eb1d Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 23 Oct 2023 11:56:00 +1100 Subject: [PATCH 5/6] make test reliable use an explicit depends_on to make sure the resource always exists before attempting to find it --- .../sdkv2provider/data_source_tunnel_test.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/internal/sdkv2provider/data_source_tunnel_test.go b/internal/sdkv2provider/data_source_tunnel_test.go index aef6887e58..abed79c218 100644 --- a/internal/sdkv2provider/data_source_tunnel_test.go +++ b/internal/sdkv2provider/data_source_tunnel_test.go @@ -8,7 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) -func TestAccCloudflareTunnelMatchName(t *testing.T) { +func TestAccCloudflareTunnel_MatchName(t *testing.T) { rnd := generateRandomResourceName() resource.Test(t, resource.TestCase{ @@ -18,7 +18,7 @@ func TestAccCloudflareTunnelMatchName(t *testing.T) { { Config: testCloudflareTunnelMatchName(rnd), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.cloudflare_tunnel.example", "status", "inactive"), + resource.TestCheckResourceAttr("data.cloudflare_tunnel."+rnd, "status", "inactive"), ), }, }, @@ -28,14 +28,16 @@ func TestAccCloudflareTunnelMatchName(t *testing.T) { func testCloudflareTunnelMatchName(name string) string { accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID") return fmt.Sprintf(` -resource "cloudflare_tunnel" "example" { +resource "cloudflare_tunnel" "%[2]s" { account_id = "%[1]s" - name = %[2]q + name = "%[2]s" secret = "AQIDBAUGBwgBAgMEBQYHCAECAwQFBgcIAQIDBAUGBwg=" } -data "cloudflare_tunnel" "example" { - account_id = cloudflare_tunnel.example.account_id - name = cloudflare_tunnel.example.name + +data "cloudflare_tunnel" "%[2]s" { + account_id = cloudflare_tunnel.%[2]s.account_id + name = cloudflare_tunnel.%[2]s.name + depends_on = [cloudflare_tunnel.%[2]s] } `, accountID, name) } From e7ad83de225c18a662e84af194d39ebde4889b57 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 23 Oct 2023 11:57:42 +1100 Subject: [PATCH 6/6] remove extra period in description --- docs/data-sources/tunnel.md | 2 +- internal/sdkv2provider/data_source_tunnel.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/data-sources/tunnel.md b/docs/data-sources/tunnel.md index 8790facc40..3f3f02e292 100644 --- a/docs/data-sources/tunnel.md +++ b/docs/data-sources/tunnel.md @@ -31,6 +31,6 @@ data "cloudflare_tunnel" "example" { - `id` (String) ID of the tunnel. - `remote_config` (Boolean) Whether the tunnel can be configured remotely from the Zero Trust dashboard. - `status` (String) The status of the tunnel. Available values: `inactive`, `degraded`, `healthy`, `down`. -- `tunnel_type` (String) The type of the tunnel.. Available values: `cfd_tunnel`, `warp_connector`. +- `tunnel_type` (String) The type of the tunnel. Available values: `cfd_tunnel`, `warp_connector`. diff --git a/internal/sdkv2provider/data_source_tunnel.go b/internal/sdkv2provider/data_source_tunnel.go index 69c98b7fad..19541d31a0 100644 --- a/internal/sdkv2provider/data_source_tunnel.go +++ b/internal/sdkv2provider/data_source_tunnel.go @@ -41,7 +41,7 @@ func dataSourceCloudflareTunnel() *schema.Resource { "tunnel_type": { Type: schema.TypeString, Computed: true, - Description: fmt.Sprintf("The type of the tunnel.. %s", renderAvailableDocumentationValuesStringSlice([]string{"cfd_tunnel", "warp_connector"})), + Description: fmt.Sprintf("The type of the tunnel. %s", renderAvailableDocumentationValuesStringSlice([]string{"cfd_tunnel", "warp_connector"})), }, "remote_config": { Type: schema.TypeBool,