[VOL-3208]Additional error handling in voltctl pmconfig diagnostics

Change-Id: I469d21c98a6336edf181e22b89b0eed7742cc7ef
diff --git a/internal/pkg/commands/devices.go b/internal/pkg/commands/devices.go
index 3068902..85cba8b 100644
--- a/internal/pkg/commands/devices.go
+++ b/internal/pkg/commands/devices.go
@@ -851,31 +851,30 @@
 				metric.SampleFreq = pmConfigs.DefaultFreq
 			}
 		}
+		outputFormat := CharReplacer.Replace(options.Format)
+		if outputFormat == "" {
+			outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
+		}
 
+		orderBy := options.OrderBy
+		if orderBy == "" {
+			orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
+		}
+
+		result := CommandResult{
+			Format:    format.Format(outputFormat),
+			Filter:    options.Filter,
+			OrderBy:   orderBy,
+			OutputAs:  toOutputType(options.OutputAs),
+			NameLimit: options.NameLimit,
+			Data:      pmConfigs.Metrics,
+		}
+
+		GenerateOutput(&result)
+		return nil
+	} else {
+		return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
 	}
-
-	outputFormat := CharReplacer.Replace(options.Format)
-	if outputFormat == "" {
-		outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
-	}
-
-	orderBy := options.OrderBy
-	if orderBy == "" {
-		orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
-	}
-
-	result := CommandResult{
-		Format:    format.Format(outputFormat),
-		Filter:    options.Filter,
-		OrderBy:   orderBy,
-		OutputAs:  toOutputType(options.OutputAs),
-		NameLimit: options.NameLimit,
-		Data:      pmConfigs.Metrics,
-	}
-
-	GenerateOutput(&result)
-	return nil
-
 }
 
 func (options *DevicePmConfigMetricEnable) Execute(args []string) error {
@@ -899,8 +898,17 @@
 	}
 
 	if !pmConfigs.Grouped {
+		metrics := make(map[string]struct{})
+		for _, metric := range pmConfigs.Metrics {
+			metrics[metric.Name] = struct{}{}
+		}
+
 		for _, metric := range pmConfigs.Metrics {
 			for _, mName := range options.Args.Metrics {
+				if _, exist := metrics[string(mName)]; !exist {
+					return fmt.Errorf("Metric Name '%s' does not exist", mName)
+				}
+
 				if string(mName) == metric.Name && !metric.Enabled {
 					metric.Enabled = true
 					_, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
@@ -910,6 +918,8 @@
 				}
 			}
 		}
+	} else {
+		return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
 	}
 	return nil
 }
@@ -935,17 +945,29 @@
 	}
 
 	if !pmConfigs.Grouped {
+		metrics := make(map[string]struct{})
+		for _, metric := range pmConfigs.Metrics {
+			metrics[metric.Name] = struct{}{}
+		}
+
 		for _, metric := range pmConfigs.Metrics {
 			for _, mName := range options.Args.Metrics {
+				if _, have := metrics[string(mName)]; !have {
+					return fmt.Errorf("Metric Name '%s' does not exist", mName)
+				}
 				if string(mName) == metric.Name && metric.Enabled {
 					metric.Enabled = false
 					_, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
 					if err != nil {
 						return err
 					}
+				} else {
+					return fmt.Errorf("Metric '%s' cannot be disabled", string(mName))
 				}
 			}
 		}
+	} else {
+		return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
 	}
 	return nil
 }
@@ -971,8 +993,15 @@
 	}
 
 	if pmConfigs.Grouped {
+		groups := make(map[string]struct{})
+		for _, group := range pmConfigs.Groups {
+			groups[group.GroupName] = struct{}{}
+		}
 		for _, group := range pmConfigs.Groups {
 			for _, gName := range options.Args.Groups {
+				if _, have := groups[string(gName)]; !have {
+					return fmt.Errorf("Group Name '%s' does not exist", gName)
+				}
 				if string(gName) == group.GroupName && !group.Enabled {
 					group.Enabled = true
 					_, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
@@ -982,6 +1011,8 @@
 				}
 			}
 		}
+	} else {
+		return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
 	}
 	return nil
 }
@@ -1007,8 +1038,17 @@
 	}
 
 	if pmConfigs.Grouped {
+		groups := make(map[string]struct{})
+		for _, group := range pmConfigs.Groups {
+			groups[group.GroupName] = struct{}{}
+		}
+
 		for _, group := range pmConfigs.Groups {
 			for _, gName := range options.Args.Groups {
+				if _, have := groups[string(gName)]; !have {
+					return fmt.Errorf("Group Name '%s' does not exist", gName)
+				}
+
 				if string(gName) == group.GroupName && group.Enabled {
 					group.Enabled = false
 					_, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
@@ -1018,6 +1058,8 @@
 				}
 			}
 		}
+	} else {
+		return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
 	}
 	return nil
 }
@@ -1048,31 +1090,30 @@
 				group.GroupFreq = pmConfigs.DefaultFreq
 			}
 		}
+		outputFormat := CharReplacer.Replace(options.Format)
+		if outputFormat == "" {
+			outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT)
+		}
 
+		orderBy := options.OrderBy
+		if orderBy == "" {
+			orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
+		}
+
+		result := CommandResult{
+			Format:    format.Format(outputFormat),
+			Filter:    options.Filter,
+			OrderBy:   orderBy,
+			OutputAs:  toOutputType(options.OutputAs),
+			NameLimit: options.NameLimit,
+			Data:      pmConfigs.Groups,
+		}
+
+		GenerateOutput(&result)
+	} else {
+		return fmt.Errorf("Device '%s' does not have Group Metrics", string(options.Args.Id))
 	}
-
-	outputFormat := CharReplacer.Replace(options.Format)
-	if outputFormat == "" {
-		outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT)
-	}
-
-	orderBy := options.OrderBy
-	if orderBy == "" {
-		orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
-	}
-
-	result := CommandResult{
-		Format:    format.Format(outputFormat),
-		Filter:    options.Filter,
-		OrderBy:   orderBy,
-		OutputAs:  toOutputType(options.OutputAs),
-		NameLimit: options.NameLimit,
-		Data:      pmConfigs.Groups,
-	}
-
-	GenerateOutput(&result)
 	return nil
-
 }
 
 func (options *DevicePmConfigGroupMetricList) Execute(args []string) error {