|
33 | 33 | validReporterAttributes = []string{reporterConfigFile, reporterCurrentBatch, reporterIntervalSeconds, reporterOutputPath} |
34 | 34 | reporterAttributeName string |
35 | 35 | reporterAttributeValue string |
| 36 | + reporterNodID int |
36 | 37 | ) |
37 | 38 |
|
38 | 39 | // getReportersCmd represents the get reporters command. |
@@ -155,6 +156,73 @@ var describeReporterCmd = &cobra.Command{ |
155 | 156 | }, |
156 | 157 | } |
157 | 158 |
|
| 159 | +// runReportCmd represents the run report command. |
| 160 | +var runReportCmd = &cobra.Command{ |
| 161 | + Use: "report report-name", |
| 162 | + Short: "run a report and return the output", |
| 163 | + Long: `The 'run report' command runs a report on a specific node and returns the report output in JSON. |
| 164 | +The report name should not include the .xml extension and will have the 'report' prefix added. E.g. |
| 165 | +'report-node' will expand to 'reports/report-node.xml'. A HTTP 400 will be returned if the report name is not valid.`, |
| 166 | + Args: func(cmd *cobra.Command, args []string) error { |
| 167 | + if len(args) != 1 { |
| 168 | + displayErrorAndExit(cmd, "you must provide a report name") |
| 169 | + } |
| 170 | + return nil |
| 171 | + }, |
| 172 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 173 | + var ( |
| 174 | + jsonData []byte |
| 175 | + err error |
| 176 | + dataFetcher fetcher.Fetcher |
| 177 | + found = false |
| 178 | + ) |
| 179 | + |
| 180 | + // retrieve the current context or the value from "-c" |
| 181 | + _, dataFetcher, err = GetConnectionAndDataFetcher() |
| 182 | + if err != nil { |
| 183 | + return err |
| 184 | + } |
| 185 | + |
| 186 | + // validate the nodeID |
| 187 | + nodeIDArray, err := GetClusterNodeIDs(dataFetcher) |
| 188 | + if err != nil { |
| 189 | + return err |
| 190 | + } |
| 191 | + for _, v := range nodeIDArray { |
| 192 | + i, _ := strconv.Atoi(v) |
| 193 | + if i == reporterNodID { |
| 194 | + found = true |
| 195 | + } |
| 196 | + } |
| 197 | + if !found { |
| 198 | + return fmt.Errorf("unable to find node id %v", reporterNodID) |
| 199 | + } |
| 200 | + |
| 201 | + jsonData, err = dataFetcher.RunReportJSON(args[0], reporterNodID) |
| 202 | + if err != nil { |
| 203 | + return err |
| 204 | + } |
| 205 | + |
| 206 | + // output format cannot be table |
| 207 | + if OutputFormat == constants.TABLE { |
| 208 | + OutputFormat = constants.JSON |
| 209 | + } |
| 210 | + |
| 211 | + if strings.Contains(OutputFormat, constants.JSONPATH) { |
| 212 | + jsonPathResult, err := utils.GetJSONPathResults(jsonData, OutputFormat) |
| 213 | + if err != nil { |
| 214 | + return err |
| 215 | + } |
| 216 | + cmd.Println(jsonPathResult) |
| 217 | + return nil |
| 218 | + } else if OutputFormat == constants.JSON { |
| 219 | + cmd.Println(string(jsonData)) |
| 220 | + } |
| 221 | + |
| 222 | + return nil |
| 223 | + }, |
| 224 | +} |
| 225 | + |
158 | 226 | // startReporterCmd represents the start reporter command. |
159 | 227 | var startReporterCmd = &cobra.Command{ |
160 | 228 | Use: reporterUse, |
@@ -309,4 +377,7 @@ func init() { |
309 | 377 | _ = setReporterCmd.MarkFlagRequired("attribute") |
310 | 378 | setReporterCmd.Flags().StringVarP(&reporterAttributeValue, "value", "v", "", "attribute value to set") |
311 | 379 | _ = setReporterCmd.MarkFlagRequired("value") |
| 380 | + |
| 381 | + runReportCmd.Flags().IntVarP(&reporterNodID, "node", "n", 0, "node to run report on") |
| 382 | + _ = runReportCmd.MarkFlagRequired("node") |
312 | 383 | } |
0 commit comments