|
| 1 | +// This source code is licensed under the license found in the LICENSE file at |
| 2 | +// the root directory of this source tree. |
| 3 | +package cmd |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/spf13/cobra" |
| 13 | + |
| 14 | + "github.com/OpenCHAMI/ochami/internal/log" |
| 15 | + "github.com/OpenCHAMI/ochami/pkg/client" |
| 16 | + "github.com/OpenCHAMI/ochami/pkg/format" |
| 17 | +) |
| 18 | + |
| 19 | +type PowerFilter string |
| 20 | + |
| 21 | +var powerFilter PowerFilter = "" |
| 22 | + |
| 23 | +const ( |
| 24 | + powerOn PowerFilter = "on" |
| 25 | + powerOff PowerFilter = "off" |
| 26 | + powerUndefined PowerFilter = "undefined" |
| 27 | +) |
| 28 | + |
| 29 | +func (l *PowerFilter) String() string { |
| 30 | + return string(*l) |
| 31 | +} |
| 32 | + |
| 33 | +func (l *PowerFilter) Set(value string) error { |
| 34 | + switch strings.ToLower(value) { |
| 35 | + case "on", "off", "undefined": |
| 36 | + *l = PowerFilter(strings.ToLower(value)) |
| 37 | + return nil |
| 38 | + default: |
| 39 | + return fmt.Errorf("invalid power filter: %s (must be on, off, or undefined)", value) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (l PowerFilter) Type() string { |
| 44 | + return "PowerFilter" |
| 45 | +} |
| 46 | + |
| 47 | +var ( |
| 48 | + powerFilterHelp = map[string]string{ |
| 49 | + string(powerOn): "Include components that are powered on", |
| 50 | + string(powerOff): "Include components that are powered off", |
| 51 | + string(powerUndefined): "Include components with undefined power state", |
| 52 | + } |
| 53 | +) |
| 54 | + |
| 55 | +func pcsStatusListPowerFilterCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 56 | + var helpSlice []string |
| 57 | + for k, v := range powerFilterHelp { |
| 58 | + helpSlice = append(helpSlice, fmt.Sprintf("%s\t%s", k, v)) |
| 59 | + } |
| 60 | + return helpSlice, cobra.ShellCompDirectiveNoFileComp |
| 61 | +} |
| 62 | + |
| 63 | +type MgmtFilter string |
| 64 | + |
| 65 | +var mgmtFilter MgmtFilter = "" |
| 66 | + |
| 67 | +func (l *MgmtFilter) String() string { |
| 68 | + return string(*l) |
| 69 | +} |
| 70 | + |
| 71 | +func (l *MgmtFilter) Set(value string) error { |
| 72 | + switch strings.ToLower(value) { |
| 73 | + case "available", "unavailable": |
| 74 | + *l = MgmtFilter(strings.ToLower(value)) |
| 75 | + return nil |
| 76 | + default: |
| 77 | + return fmt.Errorf("invalid management filter: %s (must be available or unavailable)", value) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func (l MgmtFilter) Type() string { |
| 82 | + return "MgmtFilter" |
| 83 | +} |
| 84 | + |
| 85 | +var ( |
| 86 | + mgmtFilterHelp = map[string]string{ |
| 87 | + "available": "Include components that are available", |
| 88 | + "unavailable": "Include components that are unavailable", |
| 89 | + } |
| 90 | +) |
| 91 | + |
| 92 | +func pcsStatusListMgmtFilterCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 93 | + var helpSlice []string |
| 94 | + for k, v := range mgmtFilterHelp { |
| 95 | + helpSlice = append(helpSlice, fmt.Sprintf("%s\t%s", k, v)) |
| 96 | + } |
| 97 | + return helpSlice, cobra.ShellCompDirectiveNoFileComp |
| 98 | +} |
| 99 | + |
| 100 | +// pcsStatusListCmd represents the "pcs status list" command |
| 101 | +var pcsStatusListCmd = &cobra.Command{ |
| 102 | + Use: "list", |
| 103 | + Args: cobra.NoArgs, |
| 104 | + Short: "List active PCS transitions", |
| 105 | + Long: `List active PCS transitions. |
| 106 | +
|
| 107 | +See ochami-pcs(1) for more details.`, |
| 108 | + Example: ` # List status |
| 109 | + ochami pcs status list`, |
| 110 | + Run: func(cmd *cobra.Command, args []string) { |
| 111 | + // Create client to use for requests |
| 112 | + pcsClient := pcsGetClient(cmd) |
| 113 | + |
| 114 | + // Handle token for this command |
| 115 | + handleToken(cmd) |
| 116 | + |
| 117 | + // Get status |
| 118 | + statusHttpEnv, err := pcsClient.GetStatus(xnames, string(powerFilter), string(mgmtFilter), token) |
| 119 | + if err != nil { |
| 120 | + if errors.Is(err, client.UnsuccessfulHTTPError) { |
| 121 | + log.Logger.Error().Err(err).Msg("PCS status request yielded unsuccessful HTTP response") |
| 122 | + } else { |
| 123 | + log.Logger.Error().Err(err).Msg("failed to list PCS transitions") |
| 124 | + } |
| 125 | + logHelpError(cmd) |
| 126 | + os.Exit(1) |
| 127 | + } |
| 128 | + |
| 129 | + var output interface{} |
| 130 | + err = json.Unmarshal(statusHttpEnv.Body, &output) |
| 131 | + if err != nil { |
| 132 | + log.Logger.Error().Err(err).Msg("failed to unmarshal status response") |
| 133 | + logHelpError(cmd) |
| 134 | + os.Exit(1) |
| 135 | + } |
| 136 | + |
| 137 | + // Print output |
| 138 | + if outBytes, err := format.MarshalData(output, formatOutput); err != nil { |
| 139 | + log.Logger.Error().Err(err).Msg("failed to format output") |
| 140 | + logHelpError(cmd) |
| 141 | + os.Exit(1) |
| 142 | + } else { |
| 143 | + fmt.Println(string(outBytes)) |
| 144 | + } |
| 145 | + }, |
| 146 | +} |
| 147 | + |
| 148 | +func init() { |
| 149 | + pcsStatusListCmd.Flags().StringSliceVarP(&xnames, "xname", "x", []string{}, "one or more xnames to get the status for") |
| 150 | + pcsStatusListCmd.Flags().VarP(&powerFilter, "powerfilter", "p", "filter results by power state (on, off, undefined)") |
| 151 | + pcsStatusListCmd.RegisterFlagCompletionFunc("powerfilter", pcsStatusListPowerFilterCompletion) |
| 152 | + pcsStatusListCmd.Flags().VarP(&mgmtFilter, "mgmtfilter", "m", "filter results by management state (available, unavailable)") |
| 153 | + pcsStatusListCmd.RegisterFlagCompletionFunc("mgmtfilter", pcsStatusListMgmtFilterCompletion) |
| 154 | + |
| 155 | + pcsStatusListCmd.Flags().VarP(&formatOutput, "format-output", "F", "format of output printed to standard output (json,json-pretty,yaml)") |
| 156 | + pcsStatusListCmd.RegisterFlagCompletionFunc("format-output", completionFormatData) |
| 157 | + |
| 158 | + pcsStatusCmd.AddCommand(pcsStatusListCmd) |
| 159 | +} |
0 commit comments