Skip to content

Commit

Permalink
Cleanup and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
kishen-v committed Sep 12, 2024
1 parent c0e80d3 commit 4cc95c1
Show file tree
Hide file tree
Showing 26 changed files with 71 additions and 107 deletions.
6 changes: 1 addition & 5 deletions cmd/get/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package events

import (
"fmt"
"time"

"github.com/spf13/cobra"
Expand All @@ -35,10 +34,7 @@ var Cmd = &cobra.Command{
Short: "Get Powervs events",
Long: `Get the PowerVS events`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if pkg.Options.WorkspaceID == "" && pkg.Options.WorkspaceName == "" {
return fmt.Errorf("--workspace-id or --workspace-name required")
}
return nil
return utils.EnsureWorkspaceIDorNameIsSet(pkg.Options.WorkspaceID, pkg.Options.WorkspaceName)
},
RunE: func(cmd *cobra.Command, args []string) error {
opt := pkg.Options
Expand Down
9 changes: 3 additions & 6 deletions cmd/get/peravailability/peravailability.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
package peravailability

import (
"fmt"
"sort"

"github.com/spf13/cobra"
"k8s.io/klog/v2"

"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg/client"
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
)

const powerEdgeRouter = "power-edge-router"
Expand All @@ -32,13 +32,10 @@ var Cmd = &cobra.Command{
Short: "List regions that support PER",
Long: "List regions that support Power Edge Router (PER)",
PreRunE: func(cmd *cobra.Command, args []string) error {
if pkg.Options.WorkspaceID == "" && pkg.Options.WorkspaceName == "" {
return fmt.Errorf("--workspace-id or --workspace-name required")
}
return nil
return utils.EnsureWorkspaceIDorNameIsSet(pkg.Options.WorkspaceID, pkg.Options.WorkspaceName)
},
RunE: func(cmd *cobra.Command, args []string) error {
var perEnabledRegions = []string{}
var perEnabledRegions []string
opt := pkg.Options
c, err := client.NewClientWithEnv(opt.APIKey, opt.Environment, opt.Debug)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions cmd/get/ports/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ var Cmd = &cobra.Command{
Short: "Get PowerVS network ports",
Long: `Get PowerVS network ports`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if pkg.Options.WorkspaceID == "" && pkg.Options.WorkspaceName == "" {
return fmt.Errorf("--workspace-id or --workspace-name required")
}
return nil
return utils.EnsureWorkspaceIDorNameIsSet(pkg.Options.WorkspaceID, pkg.Options.WorkspaceName)
},
RunE: func(cmd *cobra.Command, args []string) error {
opt := pkg.Options
Expand Down
2 changes: 1 addition & 1 deletion cmd/image/import/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ pvsadm image import -n upstream-core-lon04 -b <BUCKETNAME> --object rhel-83-1003
return err
}

var image *pmodels.ImageReference = &pmodels.ImageReference{}
var image = &pmodels.ImageReference{}
klog.Info("Retrieving image details")

if image.ImageID == nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/image/qcow2ova/ova/ova.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func RenderMeta(imageName string) (string, error) {
return wr.String(), nil
}

// bundles the dir into a OVA image
// CreateTarArchive bundles the dir into a OVA image
func CreateTarArchive(dir string, target string, targetDiskSize int64, imageDist string, volumeDiskName string) error {
ovf := filepath.Join(dir, volumeDiskName)
info, err := os.Stat(ovf)
Expand Down
2 changes: 1 addition & 1 deletion cmd/image/qcow2ova/password-generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"encoding/base64"
)

// Generates the password of length n
// GeneratePassword generates the password of length n
func GeneratePassword(n int) (b64Password string, err error) {
b := make([]byte, n)
_, err = rand.Read(b)
Expand Down
1 change: 0 additions & 1 deletion cmd/image/qcow2ova/prep/chroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

//go:build !windows
// +build !windows

package prep

Expand Down
3 changes: 1 addition & 2 deletions cmd/image/qcow2ova/qcow2ova.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package qcow2ova

import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -98,7 +97,7 @@ Qcow2 images location:
return fmt.Errorf("--prep-template option is not supported for coreos distro")
} else {
klog.V(2).Info("Overriding with the user defined image preparation template.")
content, err := ioutil.ReadFile(opt.PrepTemplate)
content, err := os.ReadFile(opt.PrepTemplate)
if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion cmd/image/qcow2ova/validate/diskspace/diskspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

//go:build !windows
// +build !windows

package diskspace

Expand Down
5 changes: 2 additions & 3 deletions cmd/image/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,11 @@ Sample spec.yaml file:
`,
RunE: func(cmd *cobra.Command, args []string) error {

var apikey string = pkg.Options.APIKey
opt := pkg.ImageCMDOptions
start := time.Now()

//Create bluemix client
pvsClient, err := client.NewClientWithEnv(apikey, pkg.Options.Environment, pkg.Options.Debug)
// Create resource controller client
pvsClient, err := client.NewClientWithEnv(pkg.Options.APIKey, pkg.Options.Environment, pkg.Options.Debug)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/image/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"testing"

mocksync "github.com/ppc64le-cloud/pvsadm/cmd/image/sync/mock"
pkg "github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
gomock "go.uber.org/mock/gomock"
"go.uber.org/mock/gomock"
"gopkg.in/yaml.v2"
"k8s.io/klog/v2"
)
Expand Down
5 changes: 1 addition & 4 deletions cmd/image/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ const (
UseExistingPromptMessage = "Would you like to use an existing COS Instance for creating bucket?"
CreatePromptMessage = "Would you like to create a new COS Instance?"
ResourceGroupAPIRegion = "global"
// CosResourceID is IBM COS service id, can be retrieved using ibmcloud cli
// ibmcloud catalog service cloud-object-storage.
CosResourceID = "dff97f5c-bc5e-4455-b470-411c3edbe49c"
)

var Cmd = &cobra.Command{
Expand Down Expand Up @@ -113,7 +110,7 @@ pvsadm image upload --bucket bucket1320 -f centos-8-latest.ova.gz --bucket-regio
return err
}

instances, err := pvsClient.ListServiceInstances(CosResourceID)
instances, err := pvsClient.ListServiceInstances(utils.CosResourceID)
if err != nil {
return err
}
Expand Down
16 changes: 10 additions & 6 deletions cmd/purge/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,28 @@ package images
import (
"fmt"

"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"
)

const deletePromptMessage = "Deleting all the above images, images can't be claimed back once deleted. Do you really want to continue?"

var Cmd = &cobra.Command{
Use: "images",
Short: "Purge the PowerVS images",
Long: `Purge the PowerVS images!
pvsadm purge --help for information
`,
RunE: func(cmd *cobra.Command, args []string) error {
klog.Infof("Purge the images for the workspace: %v", pkg.Options.WorkspaceID)
if pkg.Options.WorkspaceName != "" {
klog.Infof("Purge images for the workspace: %s", pkg.Options.WorkspaceName)
} else {
klog.Infof("Purge images for the workspace ID: %s", pkg.Options.WorkspaceID)
}

opt := pkg.Options

c, err := client.NewClientWithEnv(opt.APIKey, opt.Environment, opt.Debug)
Expand All @@ -56,7 +60,7 @@ pvsadm purge --help for information

table.Render(images, []string{"href", "specifications"})
if !opt.DryRun && len(images) != 0 {
if opt.NoPrompt || utils.AskConfirmation(deletePromptMessage) {
if opt.NoPrompt || utils.AskConfirmation(fmt.Sprintf(utils.DeletePromptMessage, "images")) {
for _, image := range images {
klog.Infof("Deleting image: %s with ID: %s", *image.Name, *image.ImageID)
err = pvmclient.ImgClient.Delete(*image.ImageID)
Expand Down
14 changes: 9 additions & 5 deletions cmd/purge/keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ import (
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
)

const deletePromptMessage = "Deleting all the above ssh key/key's, Do you really want to continue?"

var Cmd = &cobra.Command{
Use: "keys",
Short: "Delete PowerVS ssh key/keys",
Long: `Delete PowerVS ssh key/keys matching regex
Short: "Delete PowerVS SSH key(s)",
Long: `Delete PowerVS SSH key(s) matching regex
pvsadm purge --help for information
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -44,6 +42,12 @@ pvsadm purge --help for information
RunE: func(cmd *cobra.Command, args []string) error {
opt := pkg.Options

if pkg.Options.WorkspaceName != "" {
klog.Infof("Purge SSH keys for the workspace: %s", pkg.Options.WorkspaceName)
} else {
klog.Infof("Purge SSH keys for the workspace ID: %s", pkg.Options.WorkspaceID)
}

c, err := client.NewClientWithEnv(opt.APIKey, opt.Environment, opt.Debug)
if err != nil {
klog.Errorf("failed to create a session with IBM cloud, err: %v", err)
Expand All @@ -62,7 +66,7 @@ pvsadm purge --help for information

klog.Infof("keys matched are %s", keys)
if len(keys) != 0 {
if opt.NoPrompt || utils.AskConfirmation(deletePromptMessage) {
if opt.NoPrompt || utils.AskConfirmation(fmt.Sprintf(utils.DeletePromptMessage, "keys")) {
for _, key := range keys {
err = pvmclient.KeyClient.Delete(key)
if err != nil {
Expand Down
17 changes: 10 additions & 7 deletions cmd/purge/networks/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ package networks
import (
"fmt"

"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"
)

const deletePromptMessage = "Deleting all the above networks, networks can't be claimed back once deleted. Do you really want to continue?"

var (
deletePorts, deleteInstances bool
)
Expand All @@ -49,17 +48,21 @@ pvsadm purge --help for information
if err != nil {
return err
}
klog.Infof("Purging the networks for the instance: %v", pvmclient.InstanceID)
if pkg.Options.WorkspaceName != "" {
klog.Infof("Purge networks for the workspace: %s", pkg.Options.WorkspaceName)
} else {
klog.Infof("Purge networks for the workspace ID: %s", pkg.Options.WorkspaceID)
}

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

table.Render(networks, []string{"href"})
if !opt.DryRun && len(networks) != 0 {
if opt.NoPrompt || utils.AskConfirmation(deletePromptMessage) {
if opt.NoPrompt || utils.AskConfirmation(fmt.Sprintf(utils.DeletePromptMessage, "networks")) {
for _, network := range networks {
if deleteInstances || deletePorts {
ports, err := pvmclient.NetworkClient.GetAllPorts(*network.NetworkID)
Expand Down
6 changes: 2 additions & 4 deletions cmd/purge/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/ppc64le-cloud/pvsadm/cmd/purge/vms"
"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"
)

Expand Down Expand Up @@ -84,10 +85,7 @@ Examples:
if pkg.Options.Since != 0 && pkg.Options.Before != 0 {
return fmt.Errorf("--since and --before options can not be set at a time")
}
if pkg.Options.WorkspaceID == "" && pkg.Options.WorkspaceName == "" {
return fmt.Errorf("--workspace-id or --workspace-name required")
}
return nil
return utils.EnsureWorkspaceIDorNameIsSet(pkg.Options.WorkspaceID, pkg.Options.WorkspaceName)
},
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/purge/vms/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (
"k8s.io/klog/v2"
)

const deletePromptMessage = "Deleting all the above instances, instances can't be claimed back once deleted. Do you really want to continue?"

var Cmd = &cobra.Command{
Use: "vms",
Short: "Purge the PowerVS vms",
Expand Down Expand Up @@ -74,7 +72,7 @@ pvsadm purge --help for information
}
t.Table.Render()
if !opt.DryRun && len(instances) != 0 {
if opt.NoPrompt || utils.AskConfirmation(deletePromptMessage) {
if opt.NoPrompt || utils.AskConfirmation(fmt.Sprintf(utils.DeletePromptMessage, "instances")) {
for _, instance := range instances {
klog.Infof("Deleting instance: %s with ID: %s", *instance.ServerName, *instance.PvmInstanceID)
err = pvmclient.InstanceClient.Delete(*instance.PvmInstanceID)
Expand Down
10 changes: 2 additions & 8 deletions cmd/purge/volumes/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package volumes

import (
"fmt"
"time"

"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg/audit"
"github.com/ppc64le-cloud/pvsadm/pkg/client"
Expand All @@ -26,10 +24,6 @@ import (
"k8s.io/klog/v2"
)

var before time.Duration

const deletePromptMessage = "Deleting all the volumes in available state, volumes can't be claimed back once deleted. Do you really want to continue?"

var Cmd = &cobra.Command{
Use: "volumes",
Short: "Purge the PowerVS volumes",
Expand Down Expand Up @@ -62,8 +56,8 @@ pvsadm purge --help for information
t.Table.Render()

if !opt.DryRun && len(volumes) != 0 {
klog.Infof("Deleting all the volumes in available state")
if opt.NoPrompt || utils.AskConfirmation(deletePromptMessage) {
if opt.NoPrompt || utils.AskConfirmation(fmt.Sprintf(utils.DeletePromptMessage, "volumes")) {
klog.Infof("Deleting all the volumes in available state")
for _, volume := range volumes {
if *volume.State == "available" {
klog.Infof("Deleting volume: %s with ID: %s", *volume.Name, *volume.VolumeID)
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (c *Client) CreateCosImage(body models.CreateCosImageImportJob) (*models.Jo
return c.client.CreateCosImage(&body)
}

// func ImportImage imports image from S3 Instance
// ImportImage imports image from S3 Instance
func (c *Client) ImportImage(imageName, s3Filename, region, accessKey, secretKey, bucketName, storageType, bucketAccess string) (*models.JobReference, error) {

var body = models.CreateCosImageImportJob{
Expand All @@ -83,7 +83,7 @@ func (c *Client) ImportImage(imageName, s3Filename, region, accessKey, secretKey
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)
return nil, fmt.Errorf("failed to get the list of images: %v", err)
}

var candidates []*models.ImageReference
Expand Down
Loading

0 comments on commit 4cc95c1

Please sign in to comment.