From ece6df52effb497e07a8f7ff408ad0f81ca59c0d Mon Sep 17 00:00:00 2001 From: Kishen V Date: Mon, 14 Oct 2024 13:34:11 +0530 Subject: [PATCH] Limit --since and --after options to keys and VMs --- cmd/purge/keys/keys.go | 7 +++++++ cmd/purge/networks/networks.go | 23 +++++++++++++++-------- cmd/purge/purge.go | 11 +---------- cmd/purge/vms/vms.go | 12 ++++++++++-- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/cmd/purge/keys/keys.go b/cmd/purge/keys/keys.go index 883a95d5..22fad80f 100644 --- a/cmd/purge/keys/keys.go +++ b/cmd/purge/keys/keys.go @@ -16,6 +16,7 @@ package keys import ( "fmt" + "time" "github.com/spf13/cobra" "k8s.io/klog/v2" @@ -83,3 +84,9 @@ pvsadm purge --help for information return nil }, } + +func init() { + Cmd.PersistentFlags().DurationVar(&pkg.Options.Since, "since", 0*time.Second, "Remove resources since mentioned duration(format: 99h99m00s), mutually exclusive with --before") + Cmd.PersistentFlags().DurationVar(&pkg.Options.Before, "before", 0*time.Second, "Remove resources before mentioned duration(format: 99h99m00s), mutually exclusive with --since") + Cmd.MarkFlagsMutuallyExclusive("since", "before") +} diff --git a/cmd/purge/networks/networks.go b/cmd/purge/networks/networks.go index e28fb1d8..8147c025 100644 --- a/cmd/purge/networks/networks.go +++ b/cmd/purge/networks/networks.go @@ -73,20 +73,27 @@ pvsadm purge --help for information // Clean up instances and ports associated with the network instance for _, port := range ports.Ports { pvminstance := port.PvmInstance - portID := port.PortID - if deleteInstances && pvminstance != nil { + if deleteInstances && (pvminstance != nil) { err = pvmclient.InstanceClient.Delete(pvminstance.PvmInstanceID) if err != nil { - return fmt.Errorf("failed to delete the instance: %v", err) + if opt.IgnoreErrors { + klog.Errorf("error occurred while deleting PVMInstance: %s associated with network %s : %v", pvminstance.PvmInstanceID, *network.Name, err) + } else { + return err + } } klog.Infof("Successfully deleted a instance %s using network '%s'", pvminstance.PvmInstanceID, *network.Name) } if deletePorts { - err = pvmclient.NetworkClient.DeletePort(*network.NetworkID, *portID) + err = pvmclient.NetworkClient.DeletePort(*network.NetworkID, *port.PortID) if err != nil { - return fmt.Errorf("failed to delete a port, err: %v", err) + if opt.IgnoreErrors { + klog.Errorf("error occurred while deleting port: %s associated with network %s : %v", *port.PortID, *network.Name, err) + } else { + return err + } } - klog.Infof("Successfully deleted a port %s using network '%s'", *portID, *network.Name) + klog.Infof("Successfully deleted a port %s using network '%s'", *port.PortID, *network.Name) } } } @@ -108,6 +115,6 @@ pvsadm purge --help for information } func init() { - Cmd.PersistentFlags().BoolVar(&deletePorts, "ports", false, "Delete ports") - Cmd.PersistentFlags().BoolVar(&deleteInstances, "instances", false, "Delete instances") + Cmd.PersistentFlags().BoolVar(&deletePorts, "ports", false, "Delete ports that are associated with the network") + Cmd.PersistentFlags().BoolVar(&deleteInstances, "instances", false, "Delete instances that are associated with the network") } diff --git a/cmd/purge/purge.go b/cmd/purge/purge.go index 1820f521..97bab3ad 100644 --- a/cmd/purge/purge.go +++ b/cmd/purge/purge.go @@ -15,8 +15,7 @@ package purge import ( - "fmt" - "time" + "github.com/spf13/cobra" "github.com/ppc64le-cloud/pvsadm/cmd/purge/images" "github.com/ppc64le-cloud/pvsadm/cmd/purge/keys" @@ -25,7 +24,6 @@ import ( "github.com/ppc64le-cloud/pvsadm/cmd/purge/volumes" "github.com/ppc64le-cloud/pvsadm/pkg" "github.com/ppc64le-cloud/pvsadm/pkg/utils" - "github.com/spf13/cobra" ) var Cmd = &cobra.Command{ @@ -81,11 +79,6 @@ Examples: if err := root.PersistentPreRunE(cmd, args); err != nil { return err } - - if pkg.Options.Since != 0 && pkg.Options.Before != 0 { - return fmt.Errorf("--since and --before options can not be set at a time") - } - return utils.EnsurePrerequisitesAreSet(pkg.Options.APIKey, pkg.Options.WorkspaceID, pkg.Options.WorkspaceName) }, } @@ -103,8 +96,6 @@ func init() { Cmd.PersistentFlags().StringVarP(&pkg.Options.WorkspaceID, "workspace-id", "", "", "Workspace ID of the PowerVS workspace") Cmd.PersistentFlags().StringVarP(&pkg.Options.WorkspaceName, "workspace-name", "", "", "Workspace name of the PowerVS workspace") Cmd.PersistentFlags().BoolVar(&pkg.Options.DryRun, "dry-run", false, "dry run the action and don't delete the actual resources") - Cmd.PersistentFlags().DurationVar(&pkg.Options.Since, "since", 0*time.Second, "Remove resources since mentioned duration(format: 99h99m00s), mutually exclusive with --before") - Cmd.PersistentFlags().DurationVar(&pkg.Options.Before, "before", 0*time.Second, "Remove resources before mentioned duration(format: 99h99m00s), mutually exclusive with --since") Cmd.PersistentFlags().BoolVar(&pkg.Options.NoPrompt, "no-prompt", false, "Show prompt before doing any destructive operations") Cmd.PersistentFlags().BoolVar(&pkg.Options.IgnoreErrors, "ignore-errors", false, "Ignore any errors during the operations") Cmd.PersistentFlags().StringVar(&pkg.Options.Expr, "regexp", "", "Regular Expressions for filtering the selection") diff --git a/cmd/purge/vms/vms.go b/cmd/purge/vms/vms.go index 651ed5c1..94b7cb19 100644 --- a/cmd/purge/vms/vms.go +++ b/cmd/purge/vms/vms.go @@ -17,13 +17,15 @@ package vms import ( "fmt" "strings" + "time" + + "github.com/spf13/cobra" + "k8s.io/klog/v2" "github.com/ppc64le-cloud/pvsadm/pkg" "github.com/ppc64le-cloud/pvsadm/pkg/audit" "github.com/ppc64le-cloud/pvsadm/pkg/client" "github.com/ppc64le-cloud/pvsadm/pkg/utils" - "github.com/spf13/cobra" - "k8s.io/klog/v2" ) var Cmd = &cobra.Command{ @@ -90,3 +92,9 @@ pvsadm purge --help for information return nil }, } + +func init() { + Cmd.PersistentFlags().DurationVar(&pkg.Options.Since, "since", 0*time.Second, "Remove resources since mentioned duration(format: 99h99m00s), mutually exclusive with --before") + Cmd.PersistentFlags().DurationVar(&pkg.Options.Before, "before", 0*time.Second, "Remove resources before mentioned duration(format: 99h99m00s), mutually exclusive with --since") + Cmd.MarkFlagsMutuallyExclusive("since", "before") +}