Skip to content

Commit cbde46d

Browse files
feat(dashboard): support multiple account id's when creating dashboards
1 parent a36a75a commit cbde46d

File tree

1 file changed

+31
-60
lines changed

1 file changed

+31
-60
lines changed

internal/utils/terraform/dashboard.go

Lines changed: 31 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,18 @@ func GenerateDashboardHCL(resourceLabel string, shiftWidth int, input []byte) (s
172172

173173
for _, q := range config.NRQLQueries {
174174
h.WriteBlock("nrql_query", []string{}, func() {
175-
if len(q.AccountIDs) > 1 {
176-
h.WriteIntArrayAttribute("account_ids", q.AccountIDs)
177-
} else {
178-
h.WriteIntAttributeIfNotZero("account_id", q.AccountID)
175+
parts := strings.Split(q.Query, "||SPECIAL_ACCOUNT_ID:")
176+
if len(parts) > 1 {
177+
specialAccountID := parts[1]
178+
q.Query = parts[0]
179+
h.WriteStringAttribute("account_id", specialAccountID)
180+
} else if len(q.AccountIDs) == 1 {
181+
if q.AccountIDs[0] != -1 {
182+
h.WriteIntAttribute("account_id", q.AccountIDs[0])
183+
}
184+
} else if len(q.AccountIDs) > 1 {
185+
accountIDsStr := fmt.Sprintf("[%s]", strings.Join(strings.Fields(fmt.Sprint(q.AccountIDs)), ","))
186+
h.WriteStringAttribute("account_id", accountIDsStr)
179187
}
180188
h.WriteMultilineStringAttribute("query", q.Query)
181189
})
@@ -288,72 +296,36 @@ func unmarshalDashboardWidgetRawConfiguration(title string, widgetType string, b
288296
}
289297

290298
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"`
299+
var raw struct {
300+
Query string `json:"query"`
301+
AccountID int `json:"accountId"`
302+
AccountIDs json.RawMessage `json:"accountIds"` // Use RawMessage to check for presence
294303
}
295304

296-
var temp NRQLQueryTemp
297-
if err := json.Unmarshal(data, &temp); err != nil {
298-
return err
305+
if err := json.Unmarshal(data, &raw); err != nil {
306+
return fmt.Errorf("failed to unmarshal base NRQL query: %w", err)
299307
}
308+
d.Query = raw.Query
300309

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-
}
310+
if len(raw.AccountIDs) > 0 && string(raw.AccountIDs) != "null" {
311+
var multipleIDs []int
312+
if err := json.Unmarshal(raw.AccountIDs, &multipleIDs); err == nil && len(multipleIDs) > 0 {
326313

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-
}
314+
jsonBytes, _ := json.Marshal(multipleIDs)
332315

333-
return fmt.Errorf("failed to unmarshal NRQL query: %s", string(data))
334-
}
335-
336-
type AccountIDs struct {
337-
IDs []int
338-
}
316+
d.Query = d.Query + "||SPECIAL_ACCOUNT_ID:" + string(jsonBytes)
339317

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
318+
d.AccountIDs = []int{-1}
319+
return nil
320+
}
348321
}
349322

350-
// Try unmarshalling as a list of IDs
351-
if err := json.Unmarshal(data, &multipleIDs); err == nil {
352-
a.IDs = multipleIDs
323+
if raw.AccountID != 0 {
324+
d.AccountIDs = []int{raw.AccountID}
353325
return nil
354326
}
355327

356-
return fmt.Errorf("failed to unmarshal account IDs: %s", string(data))
328+
return nil
357329
}
358330

359331
func requireValidVisualizationID(id string) {
@@ -382,7 +354,7 @@ func writeLineWidgetAttributes(h *HCLGen, config *DashboardWidgetRawConfiguratio
382354
}
383355

384356
func writeBillboardWidgetAttributes(h *HCLGen, config *DashboardWidgetRawConfiguration) {
385-
log.Info("Inside billboardThreshold:", config.Threshold)
357+
386358
if len(config.Threshold) == 0 {
387359
log.Warnf("Threshold is empty for BillBoard widget")
388360
return
@@ -391,7 +363,6 @@ func writeBillboardWidgetAttributes(h *HCLGen, config *DashboardWidgetRawConfigu
391363
if err := json.Unmarshal(config.Threshold, &billboardThreshold); err != nil {
392364
log.Fatal("Error unmarshalling billboardThreshold:", err)
393365
}
394-
log.Info("billboardThreshold:", billboardThreshold)
395366
for _, q := range billboardThreshold {
396367
h.WriteFloatAttribute(ThresholdSeverityValues[q.AlertSeverity], q.Value)
397368
}

0 commit comments

Comments
 (0)