diff --git a/pkg/blockdevice/blockdevice.go b/pkg/blockdevice/blockdevice.go index 6010bde7..68564148 100644 --- a/pkg/blockdevice/blockdevice.go +++ b/pkg/blockdevice/blockdevice.go @@ -87,6 +87,9 @@ func createTreeByNode(k *client.K8sClient, bdNames []string) error { // Add an empty row so that the tree looks neat rows = append(rows, metav1.TableRow{Cells: []interface{}{"", "", "", "", "", "", ""}}) } + if len(rows) == 0 { + return util.HandleEmptyTableError("Block Device", k.Ns, "") + } // Show the output using cli-runtime util.TablePrinter(util.BDTreeListColumnDefinations, rows, printers.PrintOptions{Wide: true}) return nil diff --git a/pkg/storage/lvmlocalpv.go b/pkg/storage/lvmlocalpv.go index 002a3807..3c206268 100644 --- a/pkg/storage/lvmlocalpv.go +++ b/pkg/storage/lvmlocalpv.go @@ -55,8 +55,7 @@ func GetVolumeGroups(c *client.K8sClient, vgs []string) ([]metav1.TableColumnDef } // 3. Actually print the table or return an error if len(rows) == 0 { - // TODO: Improve this in issue #56 - return nil, nil, fmt.Errorf("no lvm volumegroups found") + return nil, nil, util.HandleEmptyTableError("lvm Volumegroups", c.Ns, "") } return util.LVMvolgroupListColumnDefinitions, rows, nil } diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index 06cb11b9..7ee464dd 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -36,20 +36,31 @@ func Get(pools []string, openebsNS string, casType string) error { if err != nil { return err } + if len(rows) == 0 { + return util.HandleEmptyTableError("Storage", openebsNS, casType) + } util.TablePrinter(header, rows, printers.PrintOptions{Wide: true}) } else if casType != "" { return fmt.Errorf("cas-type %s is not supported", casType) } else { + storageResourcesFound := false // 3. Call all functions & exit for _, f := range CasList() { header, row, err := f(k, pools) if err == nil { + if len(row) > 0 { + storageResourcesFound = true + } // 4. Find the correct heading & print the rows util.TablePrinter(header, row, printers.PrintOptions{Wide: true}) // A visual separator for different cas-type pools/storage entities fmt.Println() } } + + if !storageResourcesFound { + return util.HandleEmptyTableError("Storage", openebsNS, casType) + } } return nil } diff --git a/pkg/storage/zfslocalpv.go b/pkg/storage/zfslocalpv.go index 61323e37..0ff27a8f 100644 --- a/pkg/storage/zfslocalpv.go +++ b/pkg/storage/zfslocalpv.go @@ -57,7 +57,7 @@ func GetZFSPools(c *client.K8sClient, zfsnodes []string) ([]metav1.TableColumnDe } // 3. Actually print the table or return an error if len(rows) == 0 { - return nil, nil, fmt.Errorf("no zfspools found") + return nil, nil, util.HandleEmptyTableError("zfs pools", c.Ns, "") } return util.ZFSPoolListColumnDefinitions, rows, nil } diff --git a/pkg/util/error.go b/pkg/util/error.go index 934009c5..0e8fb4e4 100644 --- a/pkg/util/error.go +++ b/pkg/util/error.go @@ -40,3 +40,16 @@ func CheckErr(err error, handleErr func(string)) { } handleErr(err.Error()) } + +// HandleEmptyTableError handles error when resources or set of resources are not found +func HandleEmptyTableError(resource string, ns string, casType string) error { + if ns == "" && casType == "" { + return fmt.Errorf("no %s found in your cluster", resource) + } else if ns != "" && casType != "" { + return fmt.Errorf("no %s %s found in %s namespace", casType, resource, ns) + } else if casType != "" && !IsValidCasType(casType) { + return fmt.Errorf("cas-type %s not supported", casType) + } else { + return fmt.Errorf("no %s found in %s namespace", resource, ns) + } +} diff --git a/pkg/util/error_test.go b/pkg/util/error_test.go index a0f6f5e5..4797d356 100644 --- a/pkg/util/error_test.go +++ b/pkg/util/error_test.go @@ -57,3 +57,69 @@ func TestCheckErr(t *testing.T) { }) } } + +func TestHandleEmptyTableError(t *testing.T) { + type args struct { + resource string + ns string + casType string + } + tests := []struct { + name string + args args + expected error + }{ + { + "No Namespace and cas", + args{ + resource: "ResourceType", + ns: "", + casType: "", + }, + fmt.Errorf("no ResourceType found in your cluster"), + }, + { + "Wrong cas or Namespace", + args{ + resource: "ResourceType", + ns: "InValid", + casType: "jiva", + }, + fmt.Errorf("no jiva ResourceType found in InValid namespace"), + }, + { + "", + args{ + resource: "ResourceType", + ns: "invalid", + casType: "", + }, + fmt.Errorf("no ResourceType found in invalid namespace"), + }, + { + "Wrong cas type in all namespace", + args{ + resource: "ResourceType", + ns: "", + casType: "invalid", + }, + fmt.Errorf("cas-type invalid not supported"), + }, + { + "Wrong Namespace and all cas types", + args{ + resource: "ResourceType", + ns: "InValid", + casType: "", + }, + fmt.Errorf("no ResourceType found in InValid namespace"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tResult := HandleEmptyTableError(tt.args.resource, tt.args.ns, tt.args.casType); tResult.Error() != tt.expected.Error() { + t.Errorf("HandleEmptyTableError(): expected: %s, got: %s", tt.expected, tResult) + } + }) + } +} diff --git a/pkg/volume/volume.go b/pkg/volume/volume.go index 84399a95..7ebd2c8e 100644 --- a/pkg/volume/volume.go +++ b/pkg/volume/volume.go @@ -62,7 +62,11 @@ func Get(vols []string, openebsNS, casType string) error { } } } - // 3. Print the volumes from rows + + // 3. Return Error or Print volumes from rows + if len(rows) == 0 { + return util.HandleEmptyTableError("Volume", openebsNS, casType) + } util.TablePrinter(util.VolumeListColumnDefinations, rows, printers.PrintOptions{Wide: true}) return nil }