|
| 1 | +package dashboard |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/stackvista/stackstate-cli/generated/stackstate_api" |
| 12 | + "github.com/stackvista/stackstate-cli/internal/common" |
| 13 | + "github.com/stackvista/stackstate-cli/internal/di" |
| 14 | +) |
| 15 | + |
| 16 | +type ApplyArgs struct { |
| 17 | + File string |
| 18 | +} |
| 19 | + |
| 20 | +func DashboardApplyCommand(cli *di.Deps) *cobra.Command { |
| 21 | + args := &ApplyArgs{} |
| 22 | + cmd := &cobra.Command{ |
| 23 | + Use: "apply", |
| 24 | + Short: "Create or edit a dashboard from JSON", |
| 25 | + Long: "Create or edit a dashboard from JSON file.", |
| 26 | + RunE: cli.CmdRunEWithApi(RunDashboardApplyCommand(args)), |
| 27 | + } |
| 28 | + |
| 29 | + common.AddRequiredFileFlagVar(cmd, &args.File, "Path to a .json file with the dashboard definition") |
| 30 | + |
| 31 | + return cmd |
| 32 | +} |
| 33 | + |
| 34 | +func RunDashboardApplyCommand(args *ApplyArgs) di.CmdWithApiFn { |
| 35 | + return func(cmd *cobra.Command, cli *di.Deps, api *stackstate_api.APIClient, serverInfo *stackstate_api.ServerInfo) common.CLIError { |
| 36 | + fileBytes, err := os.ReadFile(args.File) |
| 37 | + if err != nil { |
| 38 | + return common.NewReadFileError(err, args.File) |
| 39 | + } |
| 40 | + |
| 41 | + // Determine file type by extension |
| 42 | + ext := strings.ToLower(filepath.Ext(args.File)) |
| 43 | + if ext != ".json" { |
| 44 | + return common.NewCLIArgParseError(fmt.Errorf("unsupported file type: %s. Only .json files are supported", ext)) |
| 45 | + } |
| 46 | + |
| 47 | + return applyJSONDashboard(cli, api, fileBytes) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Apply JSON format dashboard using the Dashboard API directly |
| 52 | +func applyJSONDashboard(cli *di.Deps, api *stackstate_api.APIClient, fileBytes []byte) common.CLIError { |
| 53 | + // Parse the JSON to determine if it's a create or update operation |
| 54 | + var dashboardData map[string]interface{} |
| 55 | + if err := json.Unmarshal(fileBytes, &dashboardData); err != nil { |
| 56 | + return common.NewCLIArgParseError(fmt.Errorf("failed to parse JSON: %v", err)) |
| 57 | + } |
| 58 | + |
| 59 | + // Check if it has an ID field (indicates update operation) |
| 60 | + if idField, hasId := dashboardData["id"]; hasId { |
| 61 | + // Update existing dashboard |
| 62 | + dashboardId := fmt.Sprintf("%.0f", idField.(float64)) |
| 63 | + return updateDashboard(cli, api, dashboardId, dashboardData) |
| 64 | + } else { |
| 65 | + // Create new dashboard |
| 66 | + return createDashboard(cli, api, fileBytes) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func createDashboard(cli *di.Deps, api *stackstate_api.APIClient, fileBytes []byte) common.CLIError { |
| 71 | + var writeSchema stackstate_api.DashboardWriteSchema |
| 72 | + if err := json.Unmarshal(fileBytes, &writeSchema); err != nil { |
| 73 | + return common.NewCLIArgParseError(fmt.Errorf("failed to parse JSON as DashboardWriteSchema: %v", err)) |
| 74 | + } |
| 75 | + |
| 76 | + // Validate required fields |
| 77 | + if writeSchema.Name == "" { |
| 78 | + return common.NewCLIArgParseError(fmt.Errorf("dashboard name is required")) |
| 79 | + } |
| 80 | + |
| 81 | + // Create new dashboard |
| 82 | + dashboard, resp, err := api.DashboardsApi.CreateDashboard(cli.Context).DashboardWriteSchema(writeSchema).Execute() |
| 83 | + if err != nil { |
| 84 | + return common.NewResponseError(err, resp) |
| 85 | + } |
| 86 | + |
| 87 | + if cli.IsJson() { |
| 88 | + cli.Printer.PrintJson(map[string]interface{}{ |
| 89 | + "dashboard": dashboard, |
| 90 | + }) |
| 91 | + } else { |
| 92 | + cli.Printer.Success(fmt.Sprintf("Dashboard created successfully! ID: %d, Name: %s", dashboard.GetId(), dashboard.GetName())) |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func updateDashboard(cli *di.Deps, api *stackstate_api.APIClient, dashboardId string, dashboardData map[string]interface{}) common.CLIError { |
| 99 | + // Create patch schema from the JSON data |
| 100 | + patchSchema := stackstate_api.NewDashboardPatchSchema() |
| 101 | + |
| 102 | + if name, ok := dashboardData["name"].(string); ok && name != "" { |
| 103 | + patchSchema.SetName(name) |
| 104 | + } |
| 105 | + if description, ok := dashboardData["description"].(string); ok { |
| 106 | + patchSchema.SetDescription(description) |
| 107 | + } |
| 108 | + if scopeStr, ok := dashboardData["scope"].(string); ok { |
| 109 | + if scope, err := stackstate_api.NewDashboardScopeFromValue(scopeStr); err == nil { |
| 110 | + patchSchema.SetScope(*scope) |
| 111 | + } |
| 112 | + } |
| 113 | + if dashboardContent, ok := dashboardData["dashboard"]; ok { |
| 114 | + // Convert dashboard content to PersesDashboard |
| 115 | + dashboardBytes, err := json.Marshal(dashboardContent) |
| 116 | + if err == nil { |
| 117 | + var persesDashboard stackstate_api.PersesDashboard |
| 118 | + if err := json.Unmarshal(dashboardBytes, &persesDashboard); err == nil { |
| 119 | + patchSchema.SetDashboard(persesDashboard) |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Update existing dashboard |
| 125 | + dashboard, resp, err := api.DashboardsApi.PatchDashboard(cli.Context, dashboardId).DashboardPatchSchema(*patchSchema).Execute() |
| 126 | + if err != nil { |
| 127 | + return common.NewResponseError(err, resp) |
| 128 | + } |
| 129 | + |
| 130 | + if cli.IsJson() { |
| 131 | + cli.Printer.PrintJson(map[string]interface{}{ |
| 132 | + "dashboard": dashboard, |
| 133 | + }) |
| 134 | + } else { |
| 135 | + cli.Printer.Success(fmt.Sprintf("Dashboard updated successfully! ID: %d, Name: %s", dashboard.GetId(), dashboard.GetName())) |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
0 commit comments