-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdashboard_edit.go
More file actions
132 lines (112 loc) · 4.09 KB
/
dashboard_edit.go
File metadata and controls
132 lines (112 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package dashboard
import (
"encoding/json"
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/generated/stackstate_api"
stscobra "github.com/stackvista/stackstate-cli/internal/cobra"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/di"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
const LongDescription = `Edit a dashboard.
The edit command allows you to directly edit any StackState Dashboard. It will open
the editor defined by your EDITOR environment variables.
The dashboard will be presented as JSON format for editing.
`
type EditArgs struct {
ID int64
Identifier string
}
func DashboardEditCommand(cli *di.Deps) *cobra.Command {
args := &EditArgs{}
cmd := &cobra.Command{
Use: "edit",
Short: "Edit a dashboard",
Long: LongDescription,
RunE: cli.CmdRunEWithApi(RunDashboardEditCommand(args)),
}
common.AddIDFlagVar(cmd, &args.ID, "ID of the dashboard")
common.AddIdentifierFlagVar(cmd, &args.Identifier, "Identifier (URN) of the dashboard")
stscobra.MarkMutexFlags(cmd, []string{common.IDFlag, common.IdentifierFlag}, "identifier", true)
return cmd
}
func RunDashboardEditCommand(args *EditArgs) di.CmdWithApiFn {
return func(
cmd *cobra.Command,
cli *di.Deps,
api *stackstate_api.APIClient,
serverInfo *stackstate_api.ServerInfo,
) common.CLIError {
dashboardIdOrUrn, err := ResolveDashboardIdOrUrn(args.ID, args.Identifier)
if err != nil {
return common.NewCLIArgParseError(err)
}
// Get the current dashboard
dashboard, resp, err := api.DashboardsApi.GetDashboard(cli.Context, dashboardIdOrUrn).LoadFullDashboard(true).Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
// Convert dashboard to pretty JSON for editing
originalYAML, err := yaml.Marshal(dashboard.DashboardReadFullSchema)
if err != nil {
return common.NewExecutionError(fmt.Errorf("failed to marshal dashboard to YAML: %v", err))
}
// Open editor with the dashboard JSON
editedContent, err := cli.Editor.Edit("dashboard-", ".yaml", strings.NewReader(string(originalYAML)))
if err != nil {
return common.NewExecutionError(fmt.Errorf("failed to open editor: %v", err))
}
// Check if any changes were made
if strings.Compare(string(originalYAML), string(editedContent)) == 0 {
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{"message": "No changes made"})
} else {
cli.Printer.PrintWarn("No changes made")
}
return nil
}
// Parse the edited YAML
var editedDashboard map[string]interface{}
if err := yaml.Unmarshal(editedContent, &editedDashboard); err != nil {
return common.NewExecutionError(fmt.Errorf("failed to parse edited YAML: %v", err))
}
// Create patch schema from the edited YAML
patchSchema := stackstate_api.NewDashboardPatchSchema()
if name, ok := editedDashboard["name"].(string); ok && name != "" {
patchSchema.SetName(name)
}
if description, ok := editedDashboard["description"].(string); ok {
patchSchema.SetDescription(description)
}
if scopeStr, ok := editedDashboard["scope"].(string); ok {
if scope, err := stackstate_api.NewDashboardScopeFromValue(scopeStr); err == nil {
patchSchema.SetScope(*scope)
}
}
if dashboardContent, ok := editedDashboard["dashboard"]; ok {
// Convert dashboard content to PersesDashboard
dashboardBytes, err := json.Marshal(dashboardContent)
if err == nil {
var persesDashboard stackstate_api.PersesDashboard
if err := json.Unmarshal(dashboardBytes, &persesDashboard); err == nil {
patchSchema.SetDashboard(persesDashboard)
}
}
}
// Apply the changes
updatedDashboard, resp, err := api.DashboardsApi.PatchDashboard(cli.Context, dashboardIdOrUrn).DashboardPatchSchema(*patchSchema).Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"dashboard": updatedDashboard,
})
} else {
cli.Printer.Success(fmt.Sprintf("Dashboard updated successfully! ID: %d, Name: %s", updatedDashboard.GetId(), updatedDashboard.GetName()))
}
return nil
}
}