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

add import cloudflare_tunnel_config #2298

Merged
merged 3 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions .changelog/2298.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/cloudflare_tunnel_config: add support for import of `cloudflare_tunnel_config`
```
8 changes: 8 additions & 0 deletions docs/resources/tunnel_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,11 @@ Optional:
Optional:

- `enabled` (Boolean) Whether WARP routing is enabled.

jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
## Import

Import is supported using the following syntax:

```shell
$ terraform import cloudflare_tunnel_config.example <account_id>/<tunnel_id>
```
30 changes: 30 additions & 0 deletions internal/sdkv2provider/resource_cloudflare_tunnel_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"reflect"
"strings"
"time"

"github.com/MakeNowJust/heredoc/v2"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
)

func resourceCloudflareTunnelConfig() *schema.Resource {
Expand All @@ -22,6 +24,9 @@ func resourceCloudflareTunnelConfig() *schema.Resource {
CreateContext: resourceCloudflareTunnelConfigUpdate,
UpdateContext: resourceCloudflareTunnelConfigUpdate,
DeleteContext: resourceCloudflareTunnelConfigDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceCloudflareTunnelConfigImport,
},
Description: heredoc.Doc(`
Provides a Cloudflare Tunnel configuration resource.
`),
Expand Down Expand Up @@ -223,3 +228,28 @@ func resourceCloudflareTunnelConfigDelete(ctx context.Context, d *schema.Resourc
d.SetId("")
return nil
}

func resourceCloudflareTunnelConfigImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*cloudflare.API)
attributes := strings.Split(d.Id(), "/")

if len(attributes) != 2 {
return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"accountID/tunnelId\"", d.Id())
}

accountId, tunnelId := attributes[0], attributes[1]

result, err := client.GetTunnelConfiguration(ctx, cloudflare.AccountIdentifier(accountId), tunnelId)
tflog.Debug(ctx, fmt.Sprintf("GetTunnelConfiguration: %+v", result))
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch Cloudflare Tunnel configuration %s", tunnelId))
}

d.Set(consts.AccountIDSchemaKey, accountId)
d.Set("tunnel_id", result.TunnelID)
d.SetId(result.TunnelID)

resourceCloudflareTunnelConfigRead(ctx, d, meta)

return []*schema.ResourceData{d}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ func TestAccCloudflareTunnelConfig_Full(t *testing.T) {
resource.TestCheckResourceAttr(name, "config.0.ingress_rule.1.service", "https://10.0.0.3:8081"),
),
},
{
ResourceName: name,
ImportStateIdPrefix: fmt.Sprintf("%s/", zoneID),
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -153,6 +159,12 @@ func TestAccCloudflareTunnelConfig_Short(t *testing.T) {
resource.TestCheckResourceAttr(name, "config.0.ingress_rule.0.service", "https://10.0.0.1:8081"),
),
},
{
ResourceName: name,
ImportStateIdPrefix: fmt.Sprintf("%s/", zoneID),
ImportState: true,
ImportStateVerify: true,
},
},
})
}