Skip to content

Commit

Permalink
Merge pull request #11 from ppc64le-cloud/regexp
Browse files Browse the repository at this point in the history
Print usage table and regex filtering
  • Loading branch information
mkumatag authored Oct 12, 2020
2 parents c00b7db + 4e92367 commit 0567f32
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 16 deletions.
8 changes: 4 additions & 4 deletions cmd/purge/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ var Cmd = &cobra.Command{
return err
}

images, err := pvmclient.ImgClient.GetAll()
images, err := pvmclient.ImgClient.GetAllPurgeable(opt.Before, opt.Since, opt.Expr)
if err != nil {
return fmt.Errorf("failed to get the list of images: %v", err)
}
table := utils.NewTable()

table.Render(images.Images, []string{})
if !opt.DryRun && len(images.Images) != 0 {
table.Render(images, []string{"href", "specifications"})
if !opt.DryRun && len(images) != 0 {
if opt.NoPrompt || utils.AskYesOrNo(deletePromptMessage) {
for _, image := range images.Images {
for _, image := range images {
klog.Infof("Deleting the %s, and ID: %s", *image.Name, *image.ImageID)
err = pvmclient.ImgClient.Delete(*image.ImageID)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions cmd/purge/networks/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ var Cmd = &cobra.Command{
}
klog.Infof("Purging the networks for the instance: %v", pvmclient.InstanceID)

networks, err := pvmclient.NetworkClient.GetAll()
networks, err := pvmclient.NetworkClient.GetAllPurgeable(opt.Before, opt.Since, opt.Expr)
if err != nil {
return fmt.Errorf("failed to get the list of networks: %v", err)
}
table := utils.NewTable()

table.Render(networks.Networks, []string{})
if !opt.DryRun && len(networks.Networks) != 0 {
table.Render(networks, []string{"href"})
if !opt.DryRun && len(networks) != 0 {
if opt.NoPrompt || utils.AskYesOrNo(deletePromptMessage) {
for _, network := range networks.Networks {
for _, network := range networks {
klog.Infof("Deleting the %s, and ID: %s", *network.Name, *network.NetworkID)
err = pvmclient.NetworkClient.Delete(*network.NetworkID)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/purge/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ func init() {
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")
}
28 changes: 27 additions & 1 deletion cmd/purge/vms/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package vms

import (
"fmt"
"github.com/IBM-Cloud/power-go-client/errors"
"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_instances"
"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"
"strconv"
"strings"
)

Expand All @@ -30,7 +34,29 @@ var Cmd = &cobra.Command{
return err
}

instances, err := pvmclient.InstanceClient.GetAllPurgeable(pkg.Options.Before, pkg.Options.Since)
param := p_cloud_instances.NewPcloudCloudinstancesGetParamsWithTimeout(pkg.TIMEOUT).WithCloudInstanceID(pvmclient.InstanceID)
resp, err := pvmclient.PISession.Power.PCloudInstances.PcloudCloudinstancesGet(param, ibmpisession.NewAuth(pvmclient.PISession, pvmclient.InstanceID))

if err != nil || resp.Payload == nil {
klog.Infof("Failed to perform the operation... %v", err)
return errors.ToError(err)
}
usage := resp.Payload.Usage

fmt.Println("Usage:")
tu := utils.NewTable()
tu.SetHeader([]string{"Instances", "Memory", "Proc Units", "processors", "storage", "storageSSD", "storageStandard"})
tu.Append([]string{strconv.FormatFloat(*usage.Instances, 'f', -1, 64),
strconv.FormatFloat(*usage.Memory, 'f', -1, 64),
strconv.FormatFloat(*usage.ProcUnits, 'f', 1, 64),
strconv.FormatFloat(*usage.Processors, 'f', -1, 64),
strconv.FormatFloat(*usage.Storage, 'f', 2, 64),
strconv.FormatFloat(*usage.StorageSSD, 'f', 2, 64),
strconv.FormatFloat(*usage.StorageStandard, 'f', 2, 64),
})
tu.Table.Render()

instances, err := pvmclient.InstanceClient.GetAllPurgeable(pkg.Options.Before, pkg.Options.Since, pkg.Options.Expr)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/purge/volumes/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var Cmd = &cobra.Command{
if err != nil {
return err
}
volumes, err := pvmclient.VolumeClient.GetAllPurgeableByLastUpdateDate(opt.Before, opt.Since)
volumes, err := pvmclient.VolumeClient.GetAllPurgeableByLastUpdateDate(opt.Before, opt.Since, opt.Expr)
if err != nil {
return fmt.Errorf("failed to get the list of volumes: %v", err)
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/client/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/ppc64le-cloud/pvsadm/pkg"
"regexp"
"time"
)

Expand Down Expand Up @@ -34,14 +35,19 @@ func (c *Client) Delete(id string) error {
return c.client.Delete(id, c.instanceID)
}

func (c *Client) GetAllPurgeable(before, since time.Duration) ([]*models.ImageReference, error) {
func (c *Client) GetAllPurgeable(before, since time.Duration, expr string) ([]*models.ImageReference, error) {
images, err := c.GetAll()
if err != nil {
return nil, fmt.Errorf("failed to get the list of instances: %v", err)
}

var candidates []*models.ImageReference
for _, image := range images.Images {
if expr != "" {
if r, _ := regexp.Compile(expr); !r.MatchString(*image.Name) {
continue
}
}
if !pkg.IsPurgeable(time.Time(*image.CreationDate), before, since) {
continue
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/client/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/ppc64le-cloud/pvsadm/pkg"
"regexp"
"time"
)

Expand Down Expand Up @@ -34,14 +35,19 @@ func (c *Client) Delete(id string) error {
return c.client.Delete(id, c.instanceID, pkg.TIMEOUT)
}

func (c *Client) GetAllPurgeable(before, since time.Duration) ([]*models.PVMInstanceReference, error) {
func (c *Client) GetAllPurgeable(before, since time.Duration, expr string) ([]*models.PVMInstanceReference, error) {
instances, err := c.GetAll()
if err != nil {
return nil, fmt.Errorf("failed to get the list of instances: %v", err)
}

var candidates []*models.PVMInstanceReference
for _, ins := range instances.PvmInstances {
if expr != "" {
if r, _ := regexp.Compile(expr); !r.MatchString(*ins.ServerName) {
continue
}
}
if !pkg.IsPurgeable(time.Time(ins.CreationDate), before, since) {
continue
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/client/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/ppc64le-cloud/pvsadm/pkg"
"k8s.io/klog/v2"
"regexp"
"time"
)

Expand Down Expand Up @@ -51,14 +52,19 @@ func (c *Client) Delete(id string) error {
return c.client.Delete(id, c.instanceID, pkg.TIMEOUT)
}

func (c *Client) GetAllPurgeable(before, since time.Duration) ([]*models.NetworkReference, error) {
func (c *Client) GetAllPurgeable(before, since time.Duration, expr string) ([]*models.NetworkReference, error) {
networks, err := c.GetAll()
if err != nil {
return nil, fmt.Errorf("failed to get the list of instances: %v", err)
}

var candidates []*models.NetworkReference
for _, network := range networks.Networks {
if expr != "" {
if r, _ := regexp.Compile(expr); !r.MatchString(*network.Name) {
continue
}
}
candidates = append(candidates, network)
}
return candidates, nil
Expand Down
12 changes: 9 additions & 3 deletions pkg/client/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ppc64le-cloud/pvsadm/pkg"
"k8s.io/klog/v2"
"reflect"
"regexp"
"time"
)

Expand Down Expand Up @@ -47,14 +48,19 @@ func (c *Client) GetAll() (*models.Volumes, error) {
return resp.Payload, nil
}

func (c *Client) getAllPurgeable(field string, before, since time.Duration) ([]*models.VolumeReference, error) {
func (c *Client) getAllPurgeable(field string, before, since time.Duration, expr string) ([]*models.VolumeReference, error) {
volumes, err := c.GetAll()
if err != nil {
return nil, fmt.Errorf("failed to get the list of volumes: %v", err)
}

var candidates []*models.VolumeReference
for _, vol := range volumes.Volumes {
if expr != "" {
if r, _ := regexp.Compile(expr); !r.MatchString(*vol.Name) {
continue
}
}
r := reflect.ValueOf(vol)
f := reflect.Indirect(r).FieldByName(field)
fieldValue := f.Interface()
Expand All @@ -67,6 +73,6 @@ func (c *Client) getAllPurgeable(field string, before, since time.Duration) ([]*
}

// Returns all the Purgeable volumes by Last Update Date
func (c *Client) GetAllPurgeableByLastUpdateDate(before, since time.Duration) ([]*models.VolumeReference, error) {
return c.getAllPurgeable("LastUpdateDate", before, since)
func (c *Client) GetAllPurgeableByLastUpdateDate(before, since time.Duration, expr string) ([]*models.VolumeReference, error) {
return c.getAllPurgeable("LastUpdateDate", before, since, expr)
}
1 change: 1 addition & 0 deletions pkg/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ type options struct {
NoPrompt bool
IgnoreErrors bool
AuditFile string
Expr string
}

0 comments on commit 0567f32

Please sign in to comment.