Skip to content

Commit ae06159

Browse files
committed
Added optional config_src parameter to cloudflare_tunnel resource
1 parent 42d49ac commit ae06159

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

.changelog/2369.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/cloudflare_tunnel: Adds config_src parameter
3+
```

docs/resources/tunnel.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ resource "cloudflare_tunnel" "example" {
3030
- `account_id` (String) The account identifier to target for the resource. **Modifying this attribute will force creation of a new resource.**
3131
- `name` (String) A user-friendly name chosen when the tunnel is created. **Modifying this attribute will force creation of a new resource.**
3232
- `secret` (String, Sensitive) 32 or more bytes, encoded as a base64 string. The Create Argo Tunnel endpoint sets this as the tunnel's password. Anyone wishing to run the tunnel needs this password. **Modifying this attribute will force creation of a new resource.**
33+
- `config_src` (String, Optional) Indicates if this is a locally or remotely configured tunnel. If `local`, manage the tunnel using a YAML file on the origin machine. If `cloudflare`, manage the tunnel on the Zero Trust dashboard or using `tunnel_config`, `tunnel_route` or `tunnel_virtual_network` resources. **Modifying this attribute will force creation of a new resource.**
3334

3435
### Read-Only
3536

internal/sdkv2provider/resource_cloudflare_tunnel.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ func resourceCloudflareTunnelCreate(ctx context.Context, d *schema.ResourceData,
3838
accID := d.Get(consts.AccountIDSchemaKey).(string)
3939
name := d.Get("name").(string)
4040
secret := d.Get("secret").(string)
41+
configSrc := d.Get("config_src").(string)
4142

42-
tunnel, err := client.CreateTunnel(ctx, cloudflare.AccountIdentifier(accID), cloudflare.TunnelCreateParams{Name: name, Secret: secret})
43+
tunnel, err := client.CreateTunnel(ctx, cloudflare.AccountIdentifier(accID), cloudflare.TunnelCreateParams{Name: name, Secret: secret, ConfigSrc: configSrc})
4344
if err != nil {
4445
return diag.FromErr(errors.Wrap(err, fmt.Sprintf("failed to create Argo Tunnel")))
4546
}

internal/sdkv2provider/resource_cloudflare_tunnel_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,86 @@ func testAccCheckCloudflareTunnelBasic(accID, name string) string {
5252
}`, accID, name)
5353
}
5454

55+
func TestAccCloudflareTunnelCreate_Managed(t *testing.T) {
56+
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Argo Tunnel
57+
// endpoint does not yet support the API tokens.
58+
if os.Getenv("CLOUDFLARE_API_TOKEN") != "" {
59+
t.Setenv("CLOUDFLARE_API_TOKEN", "")
60+
}
61+
62+
accID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
63+
rnd := generateRandomResourceName()
64+
name := fmt.Sprintf("cloudflare_tunnel.%s", rnd)
65+
66+
resource.Test(t, resource.TestCase{
67+
PreCheck: func() {
68+
testAccPreCheck(t)
69+
},
70+
ProviderFactories: providerFactories,
71+
CheckDestroy: testAccCheckCloudflareTunnelDestroy,
72+
Steps: []resource.TestStep{
73+
{
74+
Config: testAccCheckCloudflareTunnelManaged(accID, rnd),
75+
Check: resource.ComposeTestCheckFunc(
76+
resource.TestCheckResourceAttr(name, "name", rnd),
77+
resource.TestCheckResourceAttr(name, "secret", "AQIDBAUGBwgBAgMEBQYHCAECAwQFBgcIAQIDBAUGBwg="),
78+
resource.TestMatchResourceAttr(name, "cname", regexp.MustCompile(".*\\.cfargotunnel\\.com")),
79+
),
80+
},
81+
},
82+
})
83+
}
84+
85+
func testAccCheckCloudflareTunnelManaged(accID, name string) string {
86+
return fmt.Sprintf(`
87+
resource "cloudflare_tunnel" "%[2]s" {
88+
account_id = "%[1]s"
89+
name = "%[2]s"
90+
secret = "AQIDBAUGBwgBAgMEBQYHCAECAwQFBgcIAQIDBAUGBwg="
91+
config_src = "cloudflare"
92+
}`, accID, name)
93+
}
94+
95+
func TestAccCloudflareTunnelCreate_Unmanaged(t *testing.T) {
96+
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Argo Tunnel
97+
// endpoint does not yet support the API tokens.
98+
if os.Getenv("CLOUDFLARE_API_TOKEN") != "" {
99+
t.Setenv("CLOUDFLARE_API_TOKEN", "")
100+
}
101+
102+
accID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
103+
rnd := generateRandomResourceName()
104+
name := fmt.Sprintf("cloudflare_tunnel.%s", rnd)
105+
106+
resource.Test(t, resource.TestCase{
107+
PreCheck: func() {
108+
testAccPreCheck(t)
109+
},
110+
ProviderFactories: providerFactories,
111+
CheckDestroy: testAccCheckCloudflareTunnelDestroy,
112+
Steps: []resource.TestStep{
113+
{
114+
Config: testAccCheckCloudflareTunnelUnmanaged(accID, rnd),
115+
Check: resource.ComposeTestCheckFunc(
116+
resource.TestCheckResourceAttr(name, "name", rnd),
117+
resource.TestCheckResourceAttr(name, "secret", "AQIDBAUGBwgBAgMEBQYHCAECAwQFBgcIAQIDBAUGBwg="),
118+
resource.TestMatchResourceAttr(name, "cname", regexp.MustCompile(".*\\.cfargotunnel\\.com")),
119+
),
120+
},
121+
},
122+
})
123+
}
124+
125+
func testAccCheckCloudflareTunnelUnmanaged(accID, name string) string {
126+
return fmt.Sprintf(`
127+
resource "cloudflare_tunnel" "%[2]s" {
128+
account_id = "%[1]s"
129+
name = "%[2]s"
130+
secret = "AQIDBAUGBwgBAgMEBQYHCAECAwQFBgcIAQIDBAUGBwg="
131+
config_src = "local"
132+
}`, accID, name)
133+
}
134+
55135
func testAccCheckCloudflareTunnelDestroy(s *terraform.State) error {
56136
for _, rs := range s.RootModule().Resources {
57137
if rs.Type != "cloudflare_tunnel" {

internal/sdkv2provider/schema_cloudflare_tunnel.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ func resourceCloudflareTunnelSchema() map[string]*schema.Schema {
2626
ForceNew: true,
2727
Description: "32 or more bytes, encoded as a base64 string. The Create Argo Tunnel endpoint sets this as the tunnel's password. Anyone wishing to run the tunnel needs this password.",
2828
},
29+
"config_src": {
30+
Type: schema.TypeString,
31+
Optional: true,
32+
Sensitive: false,
33+
ForceNew: true,
34+
Description: "Indicates if this is a locally or remotely configured tunnel. If `local`, manage the tunnel using a YAML file on the origin machine. If `cloudflare`, manage the tunnel on the Zero Trust dashboard or using tunnel_config, tunnel_route or tunnel_virtual_network resources.",
35+
},
2936
"cname": {
3037
Type: schema.TypeString,
3138
Computed: true,

0 commit comments

Comments
 (0)