Skip to content

Commit 4d0b444

Browse files
feat(dashboard): support multiple account id's when creating dashboards
1 parent 5be6bfa commit 4d0b444

File tree

1 file changed

+84
-3
lines changed

1 file changed

+84
-3
lines changed

internal/utils/terraform/dashboard.go

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package terraform
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"strings"
67

78
log "github.com/sirupsen/logrus"
@@ -63,8 +64,9 @@ type DashboardWidgetFacet struct {
6364
}
6465

6566
type DashboardWidgetNRQLQuery struct {
66-
AccountID int `json:"accountId"`
67-
Query string `json:"query"`
67+
AccountID int `json:"-"` // For backward compatibility
68+
AccountIDs []int `json:"-"` // To store multiple account IDs
69+
Query string `json:"query"`
6870
}
6971

7072
type DashboardWidgetLegend struct {
@@ -170,7 +172,11 @@ func GenerateDashboardHCL(resourceLabel string, shiftWidth int, input []byte) (s
170172

171173
for _, q := range config.NRQLQueries {
172174
h.WriteBlock("nrql_query", []string{}, func() {
173-
h.WriteIntAttributeIfNotZero("account_id", q.AccountID)
175+
if len(q.AccountIDs) > 1 {
176+
h.WriteIntArrayAttribute("account_ids", q.AccountIDs)
177+
} else {
178+
h.WriteIntAttributeIfNotZero("account_id", q.AccountID)
179+
}
174180
h.WriteMultilineStringAttribute("query", q.Query)
175181
})
176182
}
@@ -281,6 +287,75 @@ func unmarshalDashboardWidgetRawConfiguration(title string, widgetType string, b
281287
return &c
282288
}
283289

290+
func (d *DashboardWidgetNRQLQuery) UnmarshalJSON(data []byte) error {
291+
// Define a temporary struct to unmarshal the common fields
292+
type NRQLQueryTemp struct {
293+
Query string `json:"query"`
294+
}
295+
296+
var temp NRQLQueryTemp
297+
if err := json.Unmarshal(data, &temp); err != nil {
298+
return err
299+
}
300+
301+
// Check for accountId (single int)
302+
type WithSingleAccount struct {
303+
AccountID int `json:"accountId"`
304+
}
305+
var singleAcc WithSingleAccount
306+
307+
if err := json.Unmarshal(data, &singleAcc); err == nil && singleAcc.AccountID != 0 {
308+
d.AccountID = singleAcc.AccountID
309+
d.AccountIDs = []int{singleAcc.AccountID} // Store as an array too
310+
d.Query = temp.Query
311+
return nil
312+
}
313+
314+
// Check for accountIds (array of ints)
315+
type WithMultipleAccounts struct {
316+
AccountIDs []int `json:"accountIds"`
317+
}
318+
var multiAcc WithMultipleAccounts
319+
320+
if err := json.Unmarshal(data, &multiAcc); err == nil && len(multiAcc.AccountIDs) > 0 {
321+
d.AccountIDs = multiAcc.AccountIDs // Store all account IDs
322+
d.AccountID = multiAcc.AccountIDs[0] // Store first one for backward compatibility
323+
d.Query = temp.Query
324+
return nil
325+
}
326+
327+
// If we get here and have a query, accept it without account ID
328+
if temp.Query != "" {
329+
d.Query = temp.Query
330+
return nil
331+
}
332+
333+
return fmt.Errorf("failed to unmarshal NRQL query: %s", string(data))
334+
}
335+
336+
type AccountIDs struct {
337+
IDs []int
338+
}
339+
340+
func (a *AccountIDs) UnmarshalJSON(data []byte) error {
341+
var singleID int
342+
var multipleIDs []int
343+
344+
// Try unmarshalling as a single ID
345+
if err := json.Unmarshal(data, &singleID); err == nil {
346+
a.IDs = []int{singleID}
347+
return nil
348+
}
349+
350+
// Try unmarshalling as a list of IDs
351+
if err := json.Unmarshal(data, &multipleIDs); err == nil {
352+
a.IDs = multipleIDs
353+
return nil
354+
}
355+
356+
return fmt.Errorf("failed to unmarshal account IDs: %s", string(data))
357+
}
358+
284359
func requireValidVisualizationID(id string) {
285360
if widgetTypes[id] == "" {
286361
log.Fatalf("unrecognized widget type \"%s\"", id)
@@ -307,10 +382,16 @@ func writeLineWidgetAttributes(h *HCLGen, config *DashboardWidgetRawConfiguratio
307382
}
308383

309384
func writeBillboardWidgetAttributes(h *HCLGen, config *DashboardWidgetRawConfiguration) {
385+
log.Info("Inside billboardThreshold:", config.Threshold)
386+
if len(config.Threshold) == 0 {
387+
log.Warnf("Threshold is empty for BillBoard widget")
388+
return
389+
}
310390
var billboardThreshold []DashboardWidgetBillBoardThreshold
311391
if err := json.Unmarshal(config.Threshold, &billboardThreshold); err != nil {
312392
log.Fatal("Error unmarshalling billboardThreshold:", err)
313393
}
394+
log.Info("billboardThreshold:", billboardThreshold)
314395
for _, q := range billboardThreshold {
315396
h.WriteFloatAttribute(ThresholdSeverityValues[q.AlertSeverity], q.Value)
316397
}

0 commit comments

Comments
 (0)