Skip to content

Commit

Permalink
Add network rm (#507)
Browse files Browse the repository at this point in the history
* Add network rm
  • Loading branch information
jdewinne authored Feb 12, 2025
1 parent ea83702 commit bc9f91b
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 3 deletions.
96 changes: 93 additions & 3 deletions cli/cmd/network_rm.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,112 @@
package cmd

import (
"fmt"

"github.com/pkg/errors"
"github.com/replicatedhq/replicated/pkg/platformclient"
"github.com/spf13/cobra"
)

func (r *runners) InitNetworkRemove(parent *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "rm ID [ID …]",
Aliases: []string{"delete"},
Short: "Remove test network",
Long: ``,
RunE: r.removeNetworks,
Short: "Remove test network(s) immediately, with options to filter by name or remove all networks.",
Long: `The 'rm' command allows you to remove test networks from your account immediately. You can specify one or more network IDs directly, or use flags to filter which networks to remove based on their name or simply remove all networks at once.
This command supports multiple filtering options, including removing networks by their name or by specifying the '--all' flag to remove all networks in your account.
You can also use the '--dry-run' flag to simulate the removal without actually deleting the networks.`,
Example: `# Remove a network by ID
replicated network rm aaaaa11
# Remove multiple networks by ID
replicated network rm aaaaa11 bbbbb22 ccccc33
# Remove all networks with a specific name
replicated network rm --name test-network
# Remove all networks
replicated network rm --all
# Perform a dry run of removing all networks
replicated network rm --all --dry-run`,
RunE: r.removeNetworks,
ValidArgsFunction: r.completeNetworkIDs,
}
parent.AddCommand(cmd)

cmd.Flags().StringArrayVar(&r.args.removeNetworkNames, "name", []string{}, "Name of the network to remove (can be specified multiple times)")
cmd.RegisterFlagCompletionFunc("name", r.completeNetworkNames)

cmd.Flags().BoolVar(&r.args.removeNetworkAll, "all", false, "remove all networks")
cmd.Flags().BoolVar(&r.args.removeNetworkDryRun, "dry-run", false, "Dry run")

return cmd
}

func (r *runners) removeNetworks(_ *cobra.Command, args []string) error {
if len(args) == 0 && !r.args.removeNetworkAll && len(r.args.removeNetworkNames) == 0 {
return errors.New("One of ID, --all, or --name flag required")
} else if len(args) > 0 && (r.args.removeNetworkAll || len(r.args.removeNetworkNames) > 0) {
return errors.New("cannot specify ID and --all or --name flag")
} else if len(args) == 0 && r.args.removeNetworkAll && len(r.args.removeNetworkNames) > 0 {
return errors.New("cannot specify --all and --name flag")
}

if len(r.args.removeNetworkNames) > 0 {
networks, err := r.kotsAPI.ListNetworks(nil, nil)
if err != nil {
return errors.Wrap(err, "list networks")
}
for _, network := range networks {
for _, name := range r.args.removeNetworkNames {
if network.Name == name {
err := removeNetwork(r, network.ID)
if err != nil {
return errors.Wrap(err, "remove network")
}
}
}
}
}

if r.args.removeNetworkAll {
networks, err := r.kotsAPI.ListNetworks(nil, nil)
if err != nil {
return errors.Wrap(err, "list networks")
}
for _, network := range networks {
err := removeNetwork(r, network.ID)
if err != nil {
return errors.Wrap(err, "remove network")
}
}
}

for _, arg := range args {
err := removeNetwork(r, arg)
if err != nil {
return errors.Wrap(err, "remove network")
}
}

return nil
}

func removeNetwork(r *runners, networkID string) error {
if r.args.removeNetworkDryRun {
fmt.Printf("would remove network %s\n", networkID)
return nil
}
err := r.kotsAPI.RemoveNetwork(networkID)
if errors.Cause(err) == platformclient.ErrForbidden {
return ErrCompatibilityMatrixTermsNotAccepted
} else if err != nil {
return errors.Wrap(err, "remove network")
} else {
fmt.Printf("removed network %s\n", networkID)
}
return nil
}
4 changes: 4 additions & 0 deletions cli/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ type runnerArgs struct {
updateNetworkName string
updateNetworkID string

removeNetworkAll bool
removeNetworkNames []string
removeNetworkDryRun bool

clusterAddonCreateObjectStoreBucket string
clusterAddonCreateObjectStoreClusterID string
clusterAddonCreateObjectStoreDuration time.Duration
Expand Down
47 changes: 47 additions & 0 deletions pkg/kotsclient/network_rm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package kotsclient

import (
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/pkg/errors"
"github.com/replicatedhq/replicated/pkg/platformclient"
)

type RemoveNetworkRequest struct {
ID string `json:"id"`
}

type RemoveNetworkResponse struct {
Error string `json:"error"`
}

type RemoveNetworkErrorResponse struct {
Error struct {
Message string `json:"message"`
}
}

func (c *VendorV3Client) RemoveNetwork(id string) error {
resp := RemoveNetworkResponse{}

url := fmt.Sprintf("/v3/network/%s", id)
err := c.DoJSON(context.TODO(), "DELETE", url, http.StatusOK, nil, &resp)
if err != nil {
// if err is APIError and the status code is 400, then we have a validation error
// and we can return the validation error
if apiErr, ok := errors.Cause(err).(platformclient.APIError); ok {
if apiErr.StatusCode == http.StatusBadRequest {
errResp := &RemoveNetworkErrorResponse{}
if jsonErr := json.Unmarshal(apiErr.Body, errResp); jsonErr != nil {
return fmt.Errorf("unmarshal error response: %w", err)
}
return errors.New(errResp.Error.Message)
}
}
return err
}
return nil
}

0 comments on commit bc9f91b

Please sign in to comment.