ZTC-446: Allow to force delete a vnet

This commit is contained in:
Nuno Diegues 2023-01-20 11:52:56 +00:00
parent de7ca4be30
commit 4616e9fcc2
4 changed files with 25 additions and 6 deletions

View File

@ -28,7 +28,7 @@ type IPRouteClient interface {
type VnetClient interface { type VnetClient interface {
CreateVirtualNetwork(newVnet NewVirtualNetwork) (VirtualNetwork, error) CreateVirtualNetwork(newVnet NewVirtualNetwork) (VirtualNetwork, error)
ListVirtualNetworks(filter *VnetFilter) ([]*VirtualNetwork, error) ListVirtualNetworks(filter *VnetFilter) ([]*VirtualNetwork, error)
DeleteVirtualNetwork(id uuid.UUID) error DeleteVirtualNetwork(id uuid.UUID, force bool) error
UpdateVirtualNetwork(id uuid.UUID, updates UpdateVirtualNetwork) error UpdateVirtualNetwork(id uuid.UUID, updates UpdateVirtualNetwork) error
} }

View File

@ -80,9 +80,16 @@ func (r *RESTClient) ListVirtualNetworks(filter *VnetFilter) ([]*VirtualNetwork,
return nil, r.statusCodeToError("list virtual networks", resp) return nil, r.statusCodeToError("list virtual networks", resp)
} }
func (r *RESTClient) DeleteVirtualNetwork(id uuid.UUID) error { func (r *RESTClient) DeleteVirtualNetwork(id uuid.UUID, force bool) error {
endpoint := r.baseEndpoints.accountVnets endpoint := r.baseEndpoints.accountVnets
endpoint.Path = path.Join(endpoint.Path, url.PathEscape(id.String())) endpoint.Path = path.Join(endpoint.Path, url.PathEscape(id.String()))
queryParams := url.Values{}
if force {
queryParams.Set("force", strconv.FormatBool(force))
}
endpoint.RawQuery = queryParams.Encode()
resp, err := r.sendRequest("DELETE", endpoint, nil) resp, err := r.sendRequest("DELETE", endpoint, nil)
if err != nil { if err != nil {
return errors.Wrap(err, "REST request failed") return errors.Wrap(err, "REST request failed")

View File

@ -23,12 +23,12 @@ func (sc *subcommandContext) listVirtualNetworks(filter *cfapi.VnetFilter) ([]*c
return client.ListVirtualNetworks(filter) return client.ListVirtualNetworks(filter)
} }
func (sc *subcommandContext) deleteVirtualNetwork(vnetId uuid.UUID) error { func (sc *subcommandContext) deleteVirtualNetwork(vnetId uuid.UUID, force bool) error {
client, err := sc.client() client, err := sc.client()
if err != nil { if err != nil {
return errors.Wrap(err, noClientMsg) return errors.Wrap(err, noClientMsg)
} }
return client.DeleteVirtualNetwork(vnetId) return client.DeleteVirtualNetwork(vnetId, force)
} }
func (sc *subcommandContext) updateVirtualNetwork(vnetId uuid.UUID, updates cfapi.UpdateVirtualNetwork) error { func (sc *subcommandContext) updateVirtualNetwork(vnetId uuid.UUID, updates cfapi.UpdateVirtualNetwork) error {

View File

@ -33,6 +33,12 @@ var (
Aliases: []string{"c"}, Aliases: []string{"c"},
Usage: "A new comment describing the purpose of the virtual network.", Usage: "A new comment describing the purpose of the virtual network.",
} }
vnetForceDeleteFlag = &cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "Force the deletion of the virtual network even if it is being relied upon by other resources. Those" +
"resources will either be deleted (e.g. IP Routes) or moved to the current default virutal network.",
}
) )
func buildVirtualNetworkSubcommand(hidden bool) *cli.Command { func buildVirtualNetworkSubcommand(hidden bool) *cli.Command {
@ -82,6 +88,7 @@ be the current default.`,
UsageText: "cloudflared tunnel [--config FILEPATH] network delete VIRTUAL_NETWORK", UsageText: "cloudflared tunnel [--config FILEPATH] network delete VIRTUAL_NETWORK",
Description: `Deletes the virtual network (given its ID or name). This is only possible if that virtual network is unused. Description: `Deletes the virtual network (given its ID or name). This is only possible if that virtual network is unused.
A virtual network may be used by IP routes or by WARP devices.`, A virtual network may be used by IP routes or by WARP devices.`,
Flags: []cli.Flag{vnetForceDeleteFlag},
Hidden: hidden, Hidden: hidden,
}, },
{ {
@ -188,7 +195,7 @@ func deleteVirtualNetworkCommand(c *cli.Context) error {
if err != nil { if err != nil {
return err return err
} }
if c.NArg() != 1 { if c.NArg() < 1 {
return errors.New("You must supply exactly one argument, either the ID or name of the virtual network to delete") return errors.New("You must supply exactly one argument, either the ID or name of the virtual network to delete")
} }
@ -198,7 +205,12 @@ func deleteVirtualNetworkCommand(c *cli.Context) error {
return err return err
} }
if err := sc.deleteVirtualNetwork(vnetId); err != nil { forceDelete := false
if c.IsSet(vnetForceDeleteFlag.Name) {
forceDelete = c.Bool(vnetForceDeleteFlag.Name)
}
if err := sc.deleteVirtualNetwork(vnetId, forceDelete); err != nil {
return errors.Wrap(err, "API error") return errors.Wrap(err, "API error")
} }
fmt.Printf("Successfully deleted virtual network '%s'\n", input) fmt.Printf("Successfully deleted virtual network '%s'\n", input)