-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdashboard_apply.go
More file actions
147 lines (125 loc) · 4.99 KB
/
dashboard_apply.go
File metadata and controls
147 lines (125 loc) · 4.99 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package dashboard
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/generated/stackstate_api"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/di"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
// ApplyArgs contains arguments for dashboard apply command
type ApplyArgs struct {
File string
}
func DashboardApplyCommand(cli *di.Deps) *cobra.Command {
args := &ApplyArgs{}
cmd := &cobra.Command{
Use: "apply --file FILE",
Short: "Create or update a dashboard from a YAML file",
Long: "Create or update a dashboard from a YAML file. If the YAML contains an 'id' field, the existing dashboard is updated; otherwise a new dashboard is created.",
Example: `# create a new dashboard from file
sts dashboard apply --file new-dashboard.yaml
# update an existing dashboard (file must contain 'id' field)
sts dashboard apply --file updated-dashboard.yaml`,
RunE: cli.CmdRunEWithApi(RunDashboardApplyCommand(args)),
}
common.AddRequiredFileFlagVar(cmd, &args.File, "Path to a .yaml file with the dashboard definition")
return cmd
}
func RunDashboardApplyCommand(args *ApplyArgs) di.CmdWithApiFn {
return func(cmd *cobra.Command, cli *di.Deps, api *stackstate_api.APIClient, serverInfo *stackstate_api.ServerInfo) common.CLIError {
fileBytes, err := os.ReadFile(args.File)
if err != nil {
return common.NewReadFileError(err, args.File)
}
// Determine file type by extension
ext := strings.ToLower(filepath.Ext(args.File))
if ext != ".yaml" {
return common.NewCLIArgParseError(fmt.Errorf("unsupported file type: %s. Only .yaml files are supported", ext))
}
return applyYAMLDashboard(cli, api, fileBytes)
}
}
// applyYAMLDashboard processes JSON dashboard file and determines create vs update operation
func applyYAMLDashboard(cli *di.Deps, api *stackstate_api.APIClient, fileBytes []byte) common.CLIError {
// Parse the JSON to determine if it's a create or update operation
var dashboardData map[string]interface{}
if err := yaml.Unmarshal(fileBytes, &dashboardData); err != nil {
return common.NewCLIArgParseError(fmt.Errorf("failed to parse YAML: %v", err))
}
// Check if it has an ID field (indicates update operation)
if idField, hasId := dashboardData["id"]; hasId {
// Update existing dashboard
dashboardId := fmt.Sprintf("%.0d", idField.(int))
return updateDashboard(cli, api, dashboardId, dashboardData)
} else {
// Create new dashboard
return createDashboard(cli, api, fileBytes)
}
}
// createDashboard creates a new dashboard from JSON schema
func createDashboard(cli *di.Deps, api *stackstate_api.APIClient, fileBytes []byte) common.CLIError {
var writeSchema stackstate_api.DashboardWriteSchema
if err := yaml.Unmarshal(fileBytes, &writeSchema); err != nil {
return common.NewCLIArgParseError(fmt.Errorf("failed to parse YAML as DashboardWriteSchema: %v", err))
}
// Validate required fields
if writeSchema.Name == "" {
return common.NewCLIArgParseError(fmt.Errorf("dashboard name is required"))
}
// Create new dashboard
dashboard, resp, err := api.DashboardsApi.CreateDashboard(cli.Context).DashboardWriteSchema(writeSchema).Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"dashboard": dashboard,
})
} else {
cli.Printer.Success(fmt.Sprintf("Dashboard created successfully! ID: %d, Name: %s", dashboard.GetId(), dashboard.GetName()))
}
return nil
}
// updateDashboard patches an existing dashboard with new data
func updateDashboard(cli *di.Deps, api *stackstate_api.APIClient, dashboardId string, dashboardData map[string]interface{}) common.CLIError {
// Create patch schema from the JSON data
patchSchema := stackstate_api.NewDashboardPatchSchema()
if name, ok := dashboardData["name"].(string); ok && name != "" {
patchSchema.SetName(name)
}
if description, ok := dashboardData["description"].(string); ok {
patchSchema.SetDescription(description)
}
if scopeStr, ok := dashboardData["scope"].(string); ok {
if scope, err := stackstate_api.NewDashboardScopeFromValue(scopeStr); err == nil {
patchSchema.SetScope(*scope)
}
}
if dashboardContent, ok := dashboardData["dashboard"]; ok {
// Convert dashboard content to PersesDashboard
dashboardBytes, err := yaml.Marshal(dashboardContent)
if err == nil {
var persesDashboard stackstate_api.PersesDashboard
if err := yaml.Unmarshal(dashboardBytes, &persesDashboard); err == nil {
patchSchema.SetDashboard(persesDashboard)
}
}
}
// Update existing dashboard
dashboard, resp, err := api.DashboardsApi.PatchDashboard(cli.Context, dashboardId).DashboardPatchSchema(*patchSchema).Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"dashboard": dashboard,
})
} else {
cli.Printer.Success(fmt.Sprintf("Dashboard updated successfully! ID: %d, Name: %s", dashboard.GetId(), dashboard.GetName()))
}
return nil
}