Skip to content

Commit

Permalink
Add vnet param
Browse files Browse the repository at this point in the history
  • Loading branch information
vavsab committed Jun 8, 2022
1 parent 485630a commit 5cc56a8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
55 changes: 46 additions & 9 deletions internal/provider/resource_cloudflare_tunnel_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ func resourceCloudflareTunnelRouteRead(ctx context.Context, d *schema.ResourceDa
accountID := d.Get("account_id").(string)
network := d.Get("network").(string)

tunnelRoutes, err := client.ListTunnelRoutes(ctx, cloudflare.TunnelRoutesListParams{
resource := cloudflare.TunnelRoutesListParams{
AccountID: accountID,
IsDeleted: cloudflare.BoolPtr(false),
NetworkSubset: network,
NetworkSuperset: network,
})
}

if virtualNetworkID, ok := d.Get("virtual_network_id").(string); ok {
resource.VirtualNetworkID = virtualNetworkID
}

tunnelRoutes, err := client.ListTunnelRoutes(ctx, resource)

if err != nil {
return diag.FromErr(fmt.Errorf("failed to fetch Tunnel Route: %w", err))
Expand All @@ -55,6 +61,11 @@ func resourceCloudflareTunnelRouteRead(ctx context.Context, d *schema.ResourceDa
d.Set("comment", tunnelRoute.Comment)
}

// vnet is optional. Do not set it unless it was specified explicitly
if _, ok := d.GetOk("virtual_network_id"); ok {
d.Set("virtual_network_id", tunnelRoute.VirtualNetworkID)
}

return nil
}

Expand All @@ -71,12 +82,21 @@ func resourceCloudflareTunnelRouteCreate(ctx context.Context, d *schema.Resource
resource.Comment = comment
}

if virtualNetworkID, ok := d.Get("virtual_network_id").(string); ok {
resource.VirtualNetworkID = virtualNetworkID
}

newTunnelRoute, err := client.CreateTunnelRoute(ctx, resource)
if err != nil {
return diag.FromErr(fmt.Errorf("error creating Tunnel Route for Network %q: %w", d.Get("network").(string), err))
}

d.SetId(newTunnelRoute.Network)
if virtualNetworkID, ok := d.Get("virtual_network_id").(string); ok {
// It's possible to create several routes with the same network but different virtual network ids.
d.SetId(fmt.Sprintf("%s/%s", newTunnelRoute.Network, virtualNetworkID))
} else {
d.SetId(newTunnelRoute.Network)
}

return resourceCloudflareTunnelRouteRead(ctx, d, meta)
}
Expand All @@ -95,6 +115,10 @@ func resourceCloudflareTunnelRouteUpdate(ctx context.Context, d *schema.Resource
resource.Comment = comment
}

if virtualNetworkID, ok := d.Get("virtual_network_id").(string); ok {
resource.VirtualNetworkID = virtualNetworkID
}

_, err := client.UpdateTunnelRoute(ctx, resource)
if err != nil {
return diag.FromErr(fmt.Errorf("error updating Tunnel Route for Network %q: %w", d.Get("network").(string), err))
Expand All @@ -106,10 +130,16 @@ func resourceCloudflareTunnelRouteUpdate(ctx context.Context, d *schema.Resource
func resourceCloudflareTunnelRouteDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)

err := client.DeleteTunnelRoute(ctx, cloudflare.TunnelRoutesDeleteParams{
resource := cloudflare.TunnelRoutesDeleteParams{
AccountID: d.Get("account_id").(string),
Network: d.Get("network").(string),
})
}

if virtualNetworkID, ok := d.Get("virtual_network_id").(string); ok {
resource.VirtualNetworkID = virtualNetworkID
}

err := client.DeleteTunnelRoute(ctx, resource)
if err != nil {
return diag.FromErr(fmt.Errorf("error deleting Tunnel Route for Network %q: %w", d.Get("network").(string), err))
}
Expand All @@ -118,15 +148,22 @@ func resourceCloudflareTunnelRouteDelete(ctx context.Context, d *schema.Resource
}

func resourceCloudflareTunnelRouteImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
attributes := strings.SplitN(d.Id(), "/", 2)
attributes := strings.SplitN(d.Id(), "/", 3)

if len(attributes) != 2 {
return nil, fmt.Errorf(`invalid id (%q) specified, should be in format "accountID/network"`, d.Id())
if len(attributes) != 2 && len(attributes) != 3 {
return nil, fmt.Errorf(`invalid id (%q) specified, should be in format "accountID/network" or "accountID/network/virtual_network_id"`, d.Id())
}

accountID, network := attributes[0], attributes[1]

d.SetId(network)
if len(attributes) == 3 {
// It's possible to create several routes with the same network but different virtual network ids.
d.SetId(fmt.Sprintf("%s/%s", network, attributes[2]))
d.Set("virtual_network_id", accountID)
} else {
d.SetId(network)
}

d.Set("account_id", accountID)
d.Set("network", network)

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_cloudflare_tunnel_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func testSweepCloudflareTunnelRoute(r string) error {
for _, tunnel := range tunnelRoutes {
tflog.Info(ctx, fmt.Sprintf("Deleting Cloudflare Tunnel Route network: %s", tunnel.Network))
//nolint:errcheck
client.DeleteTunnelRoute(context.Background(), cloudflare.TunnelRoutesDeleteParams{AccountID: accountID, Network: tunnel.Network})
client.DeleteTunnelRoute(context.Background(), cloudflare.TunnelRoutesDeleteParams{AccountID: accountID, Network: tunnel.Network, VirtualNetworkID: tunnel.TunnelID})
}

return nil
Expand Down

0 comments on commit 5cc56a8

Please sign in to comment.