github.com/newrelic/newrelic-client-go@v1.1.0/pkg/alerts/nrql_conditions.go (about) 1 package alerts 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/newrelic/newrelic-client-go/pkg/common" 8 9 "github.com/newrelic/newrelic-client-go/pkg/errors" 10 ) 11 12 // AlertsNrqlConditionExpiration 13 // Settings for how violations are opened or closed when a signal expires. 14 // nolint:revive 15 type AlertsNrqlConditionExpiration struct { 16 ExpirationDuration *int `json:"expirationDuration"` 17 CloseViolationsOnExpiration bool `json:"closeViolationsOnExpiration"` 18 OpenViolationOnExpiration bool `json:"openViolationOnExpiration"` 19 } 20 21 // AlertsNrqlConditionSignal - Configuration that defines the signal that the NRQL condition will use to evaluate. 22 // nolint:revive 23 type AlertsNrqlConditionSignal struct { 24 AggregationWindow *int `json:"aggregationWindow,omitempty"` 25 EvaluationOffset *int `json:"evaluationOffset,omitempty"` 26 FillOption *AlertsFillOption `json:"fillOption"` 27 FillValue *float64 `json:"fillValue"` 28 AggregationMethod *NrqlConditionAggregationMethod `json:"aggregationMethod,omitempty"` 29 AggregationDelay *int `json:"aggregationDelay,omitempty"` 30 AggregationTimer *int `json:"aggregationTimer,omitempty"` 31 SlideBy *int `json:"slideBy,omitempty"` 32 } 33 34 // AlertsNrqlConditionCreateSignal - Configuration that defines the signal that the NRQL condition will use to evaluate for Create. 35 // nolint:revive 36 type AlertsNrqlConditionCreateSignal struct { 37 AggregationWindow *int `json:"aggregationWindow,omitempty"` 38 EvaluationOffset *int `json:"evaluationOffset,omitempty"` 39 FillOption *AlertsFillOption `json:"fillOption"` 40 FillValue *float64 `json:"fillValue"` 41 AggregationMethod *NrqlConditionAggregationMethod `json:"aggregationMethod,omitempty"` 42 AggregationDelay *int `json:"aggregationDelay,omitempty"` 43 AggregationTimer *int `json:"aggregationTimer,omitempty"` 44 SlideBy *int `json:"slideBy,omitempty"` 45 } 46 47 // AlertsNrqlConditionUpdateSignal - Configuration that defines the signal that the NRQL condition will use to evaluate for Update. 48 // nolint:revive 49 type AlertsNrqlConditionUpdateSignal struct { 50 AggregationWindow *int `json:"aggregationWindow,omitempty"` 51 EvaluationOffset *int `json:"evaluationOffset,omitempty"` 52 FillOption *AlertsFillOption `json:"fillOption"` 53 FillValue *float64 `json:"fillValue"` 54 AggregationMethod *NrqlConditionAggregationMethod `json:"aggregationMethod"` 55 AggregationDelay *int `json:"aggregationDelay"` 56 AggregationTimer *int `json:"aggregationTimer"` 57 SlideBy *int `json:"slideBy"` 58 } 59 60 // NrqlConditionAggregationMethod - The available aggregation methods. 61 type NrqlConditionAggregationMethod string 62 63 var NrqlConditionAggregationMethodTypes = struct { 64 // Streams data points as the clocks at New Relic advance past the end of their window. This ensures a rigorous evaluation cadence, 65 // but does not take into account extraneous data latency. 66 Cadence NrqlConditionAggregationMethod 67 // Streams data points for evaluation as data for newer time windows arrive. Whenever data is received, 68 // any data points older than the specified delay will be evaluated. 69 EventFlow NrqlConditionAggregationMethod 70 // Streams data points after the specified timer elapses since data last arrived for that window. Special measures are 71 // taken to make sure data points flow in order. 72 EventTimer NrqlConditionAggregationMethod 73 }{ 74 // Streams data points as the clocks at New Relic advance past the end of their window. This ensures a rigorous evaluation cadence, 75 // but does not take into account extraneous data latency. 76 Cadence: "CADENCE", 77 // Streams data points for evaluation as data for newer time windows arrive. Whenever data is received, 78 // any data points older than the specified delay will be evaluated. 79 EventFlow: "EVENT_FLOW", 80 // Streams data points after the specified timer elapses since data last arrived for that window. Special measures are 81 // taken to make sure data points flow in order. 82 EventTimer: "EVENT_TIMER", 83 } 84 85 // AlertsFillOption - The available fill options. 86 // nolint:revive 87 type AlertsFillOption string // nolint:golint 88 89 // nolint:revive 90 var AlertsFillOptionTypes = struct { 91 // Fill using the last known value. 92 LAST_VALUE AlertsFillOption // nolint:golint 93 // Do not fill data. 94 NONE AlertsFillOption 95 // Fill using a static value. 96 STATIC AlertsFillOption 97 }{ 98 // Fill using the last known value. 99 LAST_VALUE: "LAST_VALUE", 100 // Do not fill data. 101 NONE: "NONE", 102 // Fill using a static value. 103 STATIC: "STATIC", 104 } 105 106 // ThresholdOccurrence specifies the threshold occurrence for NRQL alert condition terms. 107 type ThresholdOccurrence string 108 109 var ( 110 // ThresholdOccurrences enumerates the possible threshold occurrence values for NRQL alert condition terms. 111 ThresholdOccurrences = struct { 112 All ThresholdOccurrence 113 AtLeastOnce ThresholdOccurrence 114 }{ 115 All: "ALL", 116 AtLeastOnce: "AT_LEAST_ONCE", 117 } 118 ) 119 120 // NrqlConditionType specifies the type of NRQL alert condition. 121 type NrqlConditionType string 122 123 var ( 124 // NrqlConditionTypes enumerates the possible NRQL condition type values for NRQL alert conditions. 125 NrqlConditionTypes = struct { 126 Baseline NrqlConditionType 127 Static NrqlConditionType 128 }{ 129 Baseline: "BASELINE", 130 Static: "STATIC", 131 } 132 ) 133 134 // NrqlConditionValueFunction specifies the value function of NRQL alert condition. 135 type NrqlConditionValueFunction string 136 137 var ( 138 // NrqlConditionValueFunctions enumerates the possible NRQL condition value function values for NRQL alert conditions. 139 NrqlConditionValueFunctions = struct { 140 SingleValue NrqlConditionValueFunction 141 Sum NrqlConditionValueFunction 142 }{ 143 SingleValue: "SINGLE_VALUE", 144 Sum: "SUM", 145 } 146 ) 147 148 // NrqlConditionViolationTimeLimit specifies the value function of NRQL alert condition. 149 type NrqlConditionViolationTimeLimit string 150 151 var ( 152 // NrqlConditionViolationTimeLimits enumerates the possible NRQL condition violation time limit values for NRQL alert conditions. 153 NrqlConditionViolationTimeLimits = struct { 154 OneHour NrqlConditionViolationTimeLimit 155 TwoHours NrqlConditionViolationTimeLimit 156 FourHours NrqlConditionViolationTimeLimit 157 EightHours NrqlConditionViolationTimeLimit 158 TwelveHours NrqlConditionViolationTimeLimit 159 TwentyFourHours NrqlConditionViolationTimeLimit 160 }{ 161 OneHour: "ONE_HOUR", 162 TwoHours: "TWO_HOURS", 163 FourHours: "FOUR_HOURS", 164 EightHours: "EIGHT_HOURS", 165 TwelveHours: "TWELVE_HOURS", 166 TwentyFourHours: "TWENTY_FOUR_HOURS", 167 } 168 ) 169 170 // NrqlConditionOperator specifies the operator for alert condition terms. 171 type NrqlConditionOperator string 172 173 var ( 174 // NrqlConditionOperators enumerates the possible operator values for alert condition terms. 175 NrqlConditionOperators = struct { 176 Above NrqlConditionOperator 177 Below NrqlConditionOperator 178 Equal NrqlConditionOperator 179 }{ 180 Above: "ABOVE", 181 Below: "BELOW", 182 Equal: "EQUAL", 183 } 184 ) 185 186 // NrqlConditionPriority specifies the priority for alert condition terms. 187 type NrqlConditionPriority string 188 189 var ( 190 // NrqlConditionPriorities enumerates the possible priority values for alert condition terms. 191 NrqlConditionPriorities = struct { 192 Critical NrqlConditionPriority 193 Warning NrqlConditionPriority 194 }{ 195 Critical: "CRITICAL", 196 Warning: "WARNING", 197 } 198 ) 199 200 // NrqlBaselineDirection 201 type NrqlBaselineDirection string 202 203 var ( 204 // NrqlBaselineDirections enumerates the possible baseline direction values for a baseline NRQL alert condition. 205 NrqlBaselineDirections = struct { 206 LowerOnly NrqlBaselineDirection 207 UpperAndLower NrqlBaselineDirection 208 UpperOnly NrqlBaselineDirection 209 }{ 210 LowerOnly: "LOWER_ONLY", 211 UpperAndLower: "UPPER_AND_LOWER", 212 UpperOnly: "UPPER_ONLY", 213 } 214 ) 215 216 // NrqlConditionTerm represents the a single term of a New Relic alert condition. 217 type NrqlConditionTerm struct { 218 Operator AlertsNRQLConditionTermsOperator `json:"operator,omitempty"` 219 Priority NrqlConditionPriority `json:"priority,omitempty"` 220 Threshold *float64 `json:"threshold"` 221 ThresholdDuration int `json:"thresholdDuration,omitempty"` 222 ThresholdOccurrences ThresholdOccurrence `json:"thresholdOccurrences,omitempty"` 223 } 224 225 // NrqlConditionQuery represents the NRQL query object returned in a NerdGraph response object. 226 type NrqlConditionQuery struct { 227 Query string `json:"query,omitempty"` 228 EvaluationOffset *int `json:"evaluationOffset,omitempty"` 229 } 230 231 // NrqlConditionCreateQuery represents the NRQL query object for create. 232 type NrqlConditionCreateQuery struct { 233 Query string `json:"query,omitempty"` 234 EvaluationOffset *int `json:"evaluationOffset,omitempty"` 235 } 236 237 // NrqlConditionUpdateQuery represents the NRQL query object for update. 238 type NrqlConditionUpdateQuery struct { 239 Query string `json:"query"` 240 EvaluationOffset *int `json:"evaluationOffset"` 241 } 242 243 // NrqlConditionBase represents the base fields for a New Relic NRQL Alert condition. 244 type NrqlConditionBase struct { 245 Description string `json:"description,omitempty"` 246 Enabled bool `json:"enabled"` 247 Name string `json:"name,omitempty"` 248 Nrql NrqlConditionQuery `json:"nrql,omitempty"` 249 RunbookURL string `json:"runbookUrl,omitempty"` 250 Terms []NrqlConditionTerm `json:"terms,omitempty"` 251 Type NrqlConditionType `json:"type,omitempty"` 252 ViolationTimeLimit NrqlConditionViolationTimeLimit `json:"violationTimeLimit,omitempty"` 253 ViolationTimeLimitSeconds int `json:"violationTimeLimitSeconds,omitempty"` 254 Expiration *AlertsNrqlConditionExpiration `json:"expiration,omitempty"` 255 Signal *AlertsNrqlConditionSignal `json:"signal,omitempty"` 256 EntityGUID common.EntityGUID `json:"entityGuid,omitempty"` 257 } 258 259 // NrqlConditionCreateBase represents the base fields for creating a New Relic NRQL Alert condition. 260 type NrqlConditionCreateBase struct { 261 Description string `json:"description,omitempty"` 262 Enabled bool `json:"enabled"` 263 Name string `json:"name,omitempty"` 264 Nrql NrqlConditionCreateQuery `json:"nrql,omitempty"` 265 RunbookURL string `json:"runbookUrl,omitempty"` 266 Terms []NrqlConditionTerm `json:"terms,omitempty"` 267 Type NrqlConditionType `json:"type,omitempty"` 268 ViolationTimeLimit NrqlConditionViolationTimeLimit `json:"violationTimeLimit,omitempty"` 269 ViolationTimeLimitSeconds int `json:"violationTimeLimitSeconds,omitempty"` 270 Expiration *AlertsNrqlConditionExpiration `json:"expiration,omitempty"` 271 Signal *AlertsNrqlConditionCreateSignal `json:"signal,omitempty"` 272 } 273 274 // NrqlConditionUpdateBase represents the base fields for updating a New Relic NRQL Alert condition. 275 type NrqlConditionUpdateBase struct { 276 Description string `json:"description,omitempty"` 277 Enabled bool `json:"enabled"` 278 Name string `json:"name,omitempty"` 279 Nrql NrqlConditionUpdateQuery `json:"nrql"` 280 RunbookURL string `json:"runbookUrl,omitempty"` 281 Terms []NrqlConditionTerm `json:"terms,omitempty"` 282 Type NrqlConditionType `json:"type,omitempty"` 283 ViolationTimeLimit NrqlConditionViolationTimeLimit `json:"violationTimeLimit,omitempty"` 284 ViolationTimeLimitSeconds int `json:"violationTimeLimitSeconds,omitempty"` 285 Expiration *AlertsNrqlConditionExpiration `json:"expiration,omitempty"` 286 Signal *AlertsNrqlConditionUpdateSignal `json:"signal"` 287 } 288 289 // NrqlConditionCreateInput represents the input options for creating a Nrql Condition. 290 type NrqlConditionCreateInput struct { 291 NrqlConditionCreateBase 292 293 // BaselineDirection ONLY applies to NRQL conditions of type BASELINE. 294 BaselineDirection *NrqlBaselineDirection `json:"baselineDirection,omitempty"` 295 296 // ValueFunction ONLY applies to NRQL conditions of type STATIC. 297 ValueFunction *NrqlConditionValueFunction `json:"valueFunction,omitempty"` 298 } 299 300 // NrqlConditionUpdateInput represents the input options for updating a Nrql Condition. 301 type NrqlConditionUpdateInput struct { 302 NrqlConditionUpdateBase 303 304 // BaselineDirection ONLY applies to NRQL conditions of type BASELINE. 305 BaselineDirection *NrqlBaselineDirection `json:"baselineDirection,omitempty"` 306 307 // ValueFunction ONLY applies to NRQL conditions of type STATIC. 308 ValueFunction *NrqlConditionValueFunction `json:"valueFunction,omitempty"` 309 } 310 311 type NrqlConditionsSearchCriteria struct { 312 Name string `json:"name,omitempty"` 313 NameLike string `json:"nameLike,omitempty"` 314 PolicyID string `json:"policyId,omitempty"` 315 Query string `json:"query,omitempty"` 316 QueryLike string `json:"queryLike,omitempty"` 317 } 318 319 // NrqlAlertCondition represents a NerdGraph NRQL alert condition, which is type AlertsNrqlCondition in NerdGraph. 320 // NrqlAlertCondition could be a baseline condition or static condition. 321 type NrqlAlertCondition struct { 322 NrqlConditionBase 323 324 ID string `json:"id,omitempty"` 325 PolicyID string `json:"policyId,omitempty"` 326 327 // BaselineDirection exists ONLY for NRQL conditions of type BASELINE. 328 BaselineDirection *NrqlBaselineDirection `json:"baselineDirection,omitempty"` 329 330 // ValueFunction is returned ONLY for NRQL conditions of type STATIC. 331 ValueFunction *NrqlConditionValueFunction `json:"valueFunction,omitempty"` 332 } 333 334 // NrqlCondition represents a New Relic NRQL Alert condition. 335 type NrqlCondition struct { 336 Enabled bool `json:"enabled"` 337 ID int `json:"id,omitempty"` 338 ViolationCloseTimer int `json:"violation_time_limit_seconds,omitempty"` 339 Name string `json:"name,omitempty"` 340 Nrql NrqlQuery `json:"nrql,omitempty"` 341 RunbookURL string `json:"runbook_url,omitempty"` 342 Terms []ConditionTerm `json:"terms,omitempty"` 343 Type string `json:"type,omitempty"` 344 ValueFunction ValueFunctionType `json:"value_function,omitempty"` 345 EntityGUID *common.EntityGUID `json:"entity_guid,omitempty"` 346 } 347 348 // NrqlQuery represents a NRQL query to use with a NRQL alert condition 349 type NrqlQuery struct { 350 Query string `json:"query,omitempty"` 351 SinceValue string `json:"since_value,omitempty"` 352 } 353 354 // ListNrqlConditions returns NRQL alert conditions for a specified policy. 355 func (a *Alerts) ListNrqlConditions(policyID int) ([]*NrqlCondition, error) { 356 return a.ListNrqlConditionsWithContext(context.Background(), policyID) 357 } 358 359 // ListNrqlConditionsWithContext returns NRQL alert conditions for a specified policy. 360 func (a *Alerts) ListNrqlConditionsWithContext(ctx context.Context, policyID int) ([]*NrqlCondition, error) { 361 conditions := []*NrqlCondition{} 362 queryParams := listNrqlConditionsParams{ 363 PolicyID: policyID, 364 } 365 366 nextURL := a.config.Region().RestURL("/alerts_nrql_conditions.json") 367 368 for nextURL != "" { 369 response := nrqlConditionsResponse{} 370 resp, err := a.client.GetWithContext(ctx, nextURL, &queryParams, &response) 371 372 if err != nil { 373 return nil, err 374 } 375 376 conditions = append(conditions, response.NrqlConditions...) 377 378 paging := a.pager.Parse(resp) 379 nextURL = paging.Next 380 } 381 382 return conditions, nil 383 } 384 385 // GetNrqlCondition gets information about a NRQL alert condition 386 // for a specified policy ID and condition ID. 387 func (a *Alerts) GetNrqlCondition(policyID int, id int) (*NrqlCondition, error) { 388 return a.GetNrqlConditionWithContext(context.Background(), policyID, id) 389 } 390 391 // GetNrqlConditionWithContext gets information about a NRQL alert condition 392 // for a specified policy ID and condition ID. 393 func (a *Alerts) GetNrqlConditionWithContext(ctx context.Context, policyID int, id int) (*NrqlCondition, error) { 394 conditions, err := a.ListNrqlConditionsWithContext(ctx, policyID) 395 if err != nil { 396 return nil, err 397 } 398 399 for _, condition := range conditions { 400 if condition.ID == id { 401 return condition, nil 402 } 403 } 404 405 return nil, errors.NewNotFoundf("no condition found for policy %d and condition ID %d", policyID, id) 406 } 407 408 // CreateNrqlCondition creates a NRQL alert condition. 409 func (a *Alerts) CreateNrqlCondition(policyID int, condition NrqlCondition) (*NrqlCondition, error) { 410 return a.CreateNrqlConditionWithContext(context.Background(), policyID, condition) 411 } 412 413 // CreateNrqlConditionWithContext creates a NRQL alert condition. 414 func (a *Alerts) CreateNrqlConditionWithContext(ctx context.Context, policyID int, condition NrqlCondition) (*NrqlCondition, error) { 415 reqBody := nrqlConditionRequestBody{ 416 NrqlCondition: condition, 417 } 418 resp := nrqlConditionResponse{} 419 420 url := fmt.Sprintf("/alerts_nrql_conditions/policies/%d.json", policyID) 421 _, err := a.client.PostWithContext(ctx, a.config.Region().RestURL(url), nil, &reqBody, &resp) 422 423 if err != nil { 424 return nil, err 425 } 426 427 return &resp.NrqlCondition, nil 428 } 429 430 // UpdateNrqlCondition updates a NRQL alert condition. 431 func (a *Alerts) UpdateNrqlCondition(condition NrqlCondition) (*NrqlCondition, error) { 432 return a.UpdateNrqlConditionWithContext(context.Background(), condition) 433 } 434 435 // UpdateNrqlConditionWithContext updates a NRQL alert condition. 436 func (a *Alerts) UpdateNrqlConditionWithContext(ctx context.Context, condition NrqlCondition) (*NrqlCondition, error) { 437 reqBody := nrqlConditionRequestBody{ 438 NrqlCondition: condition, 439 } 440 resp := nrqlConditionResponse{} 441 442 url := fmt.Sprintf("/alerts_nrql_conditions/%d.json", condition.ID) 443 _, err := a.client.PutWithContext(ctx, a.config.Region().RestURL(url), nil, &reqBody, &resp) 444 445 if err != nil { 446 return nil, err 447 } 448 449 return &resp.NrqlCondition, nil 450 } 451 452 // DeleteNrqlCondition deletes a NRQL alert condition. 453 func (a *Alerts) DeleteNrqlCondition(id int) (*NrqlCondition, error) { 454 return a.DeleteNrqlConditionWithContext(context.Background(), id) 455 } 456 457 // DeleteNrqlConditionWithContext deletes a NRQL alert condition. 458 func (a *Alerts) DeleteNrqlConditionWithContext(ctx context.Context, id int) (*NrqlCondition, error) { 459 resp := nrqlConditionResponse{} 460 url := fmt.Sprintf("/alerts_nrql_conditions/%d.json", id) 461 462 _, err := a.client.DeleteWithContext(ctx, a.config.Region().RestURL(url), nil, &resp) 463 464 if err != nil { 465 return nil, err 466 } 467 468 return &resp.NrqlCondition, nil 469 } 470 471 // GetNrqlConditionQuery fetches a NRQL alert condition via New Relic's NerdGraph API. 472 func (a *Alerts) GetNrqlConditionQuery( 473 accountID int, 474 conditionID string, 475 ) (*NrqlAlertCondition, error) { 476 return a.GetNrqlConditionQueryWithContext(context.Background(), accountID, conditionID) 477 } 478 479 // GetNrqlConditionQueryWithContext fetches a NRQL alert condition via New Relic's NerdGraph API. 480 func (a *Alerts) GetNrqlConditionQueryWithContext( 481 ctx context.Context, 482 accountID int, 483 conditionID string, 484 ) (*NrqlAlertCondition, error) { 485 resp := getNrqlConditionQueryResponse{} 486 vars := map[string]interface{}{ 487 "accountId": accountID, 488 "id": conditionID, 489 } 490 491 if err := a.NerdGraphQueryWithContext(ctx, getNrqlConditionQuery, vars, &resp); err != nil { 492 return nil, err 493 } 494 495 return &resp.Actor.Account.Alerts.NrqlCondition, nil 496 } 497 498 // SearchNrqlConditionsQuery fetches multiple NRQL alert conditions based on the provided search criteria via New Relic's NerdGraph API. 499 func (a *Alerts) SearchNrqlConditionsQuery( 500 accountID int, 501 searchCriteria NrqlConditionsSearchCriteria, 502 ) ([]*NrqlAlertCondition, error) { 503 return a.SearchNrqlConditionsQueryWithContext(context.Background(), accountID, searchCriteria) 504 } 505 506 // SearchNrqlConditionsQueryWithContext fetches multiple NRQL alert conditions based on the provided search criteria via New Relic's NerdGraph API. 507 func (a *Alerts) SearchNrqlConditionsQueryWithContext( 508 ctx context.Context, 509 accountID int, 510 searchCriteria NrqlConditionsSearchCriteria, 511 ) ([]*NrqlAlertCondition, error) { 512 conditions := []*NrqlAlertCondition{} 513 var nextCursor *string 514 515 for ok := true; ok; ok = nextCursor != nil { 516 resp := searchNrqlConditionsResponse{} 517 vars := map[string]interface{}{ 518 "accountId": accountID, 519 "searchCriteria": searchCriteria, 520 "cursor": nextCursor, 521 } 522 523 if err := a.NerdGraphQueryWithContext(ctx, searchNrqlConditionsQuery, vars, &resp); err != nil { 524 return nil, err 525 } 526 527 conditions = append(conditions, resp.Actor.Account.Alerts.NrqlConditionsSearch.NrqlConditions...) 528 nextCursor = resp.Actor.Account.Alerts.NrqlConditionsSearch.NextCursor 529 } 530 531 return conditions, nil 532 } 533 534 // CreateNrqlConditionBaselineMutation creates a baseline NRQL alert condition via New Relic's NerdGraph API. 535 func (a *Alerts) CreateNrqlConditionBaselineMutation( 536 accountID int, 537 policyID string, 538 nrqlCondition NrqlConditionCreateInput, 539 ) (*NrqlAlertCondition, error) { 540 return a.CreateNrqlConditionBaselineMutationWithContext(context.Background(), accountID, policyID, nrqlCondition) 541 } 542 543 // CreateNrqlConditionBaselineMutationWithContext creates a baseline NRQL alert condition via New Relic's NerdGraph API. 544 func (a *Alerts) CreateNrqlConditionBaselineMutationWithContext( 545 ctx context.Context, 546 accountID int, 547 policyID string, 548 nrqlCondition NrqlConditionCreateInput, 549 ) (*NrqlAlertCondition, error) { 550 resp := nrqlConditionBaselineCreateResponse{} 551 vars := map[string]interface{}{ 552 "accountId": accountID, 553 "policyId": policyID, 554 "condition": nrqlCondition, 555 } 556 557 if err := a.NerdGraphQueryWithContext(ctx, createNrqlConditionBaselineMutation, vars, &resp); err != nil { 558 return nil, err 559 } 560 561 return &resp.AlertsNrqlConditionBaselineCreate, nil 562 } 563 564 // UpdateNrqlConditionBaselineMutation updates a baseline NRQL alert condition via New Relic's NerdGraph API. 565 func (a *Alerts) UpdateNrqlConditionBaselineMutation( 566 accountID int, 567 conditionID string, 568 nrqlCondition NrqlConditionUpdateInput, 569 ) (*NrqlAlertCondition, error) { 570 return a.UpdateNrqlConditionBaselineMutationWithContext(context.Background(), accountID, conditionID, nrqlCondition) 571 } 572 573 // UpdateNrqlConditionBaselineMutationWithContext updates a baseline NRQL alert condition via New Relic's NerdGraph API. 574 func (a *Alerts) UpdateNrqlConditionBaselineMutationWithContext( 575 ctx context.Context, 576 accountID int, 577 conditionID string, 578 nrqlCondition NrqlConditionUpdateInput, 579 ) (*NrqlAlertCondition, error) { 580 resp := nrqlConditionBaselineUpdateResponse{} 581 vars := map[string]interface{}{ 582 "accountId": accountID, 583 "id": conditionID, 584 "condition": nrqlCondition, 585 } 586 587 if err := a.NerdGraphQueryWithContext(ctx, updateNrqlConditionBaselineMutation, vars, &resp); err != nil { 588 return nil, err 589 } 590 591 return &resp.AlertsNrqlConditionBaselineUpdate, nil 592 } 593 594 // CreateNrqlConditionStaticMutation creates a static NRQL alert condition via New Relic's NerdGraph API. 595 func (a *Alerts) CreateNrqlConditionStaticMutation( 596 accountID int, 597 policyID string, 598 nrqlCondition NrqlConditionCreateInput, 599 ) (*NrqlAlertCondition, error) { 600 return a.CreateNrqlConditionStaticMutationWithContext(context.Background(), accountID, policyID, nrqlCondition) 601 } 602 603 // CreateNrqlConditionStaticMutationWithContext creates a static NRQL alert condition via New Relic's NerdGraph API. 604 func (a *Alerts) CreateNrqlConditionStaticMutationWithContext( 605 ctx context.Context, 606 accountID int, 607 policyID string, 608 nrqlCondition NrqlConditionCreateInput, 609 ) (*NrqlAlertCondition, error) { 610 resp := nrqlConditionStaticCreateResponse{} 611 vars := map[string]interface{}{ 612 "accountId": accountID, 613 "policyId": policyID, 614 "condition": nrqlCondition, 615 } 616 617 if err := a.NerdGraphQueryWithContext(ctx, createNrqlConditionStaticMutation, vars, &resp); err != nil { 618 return nil, err 619 } 620 return &resp.AlertsNrqlConditionStaticCreate, nil 621 } 622 623 // UpdateNrqlConditionStaticMutation updates a static NRQL alert condition via New Relic's NerdGraph API. 624 func (a *Alerts) UpdateNrqlConditionStaticMutation( 625 accountID int, 626 conditionID string, 627 nrqlCondition NrqlConditionUpdateInput, 628 ) (*NrqlAlertCondition, error) { 629 return a.UpdateNrqlConditionStaticMutationWithContext(context.Background(), accountID, conditionID, nrqlCondition) 630 } 631 632 // UpdateNrqlConditionStaticMutationWithContext updates a static NRQL alert condition via New Relic's NerdGraph API. 633 func (a *Alerts) UpdateNrqlConditionStaticMutationWithContext( 634 ctx context.Context, 635 accountID int, 636 conditionID string, 637 nrqlCondition NrqlConditionUpdateInput, 638 ) (*NrqlAlertCondition, error) { 639 resp := nrqlConditionStaticUpdateResponse{} 640 vars := map[string]interface{}{ 641 "accountId": accountID, 642 "id": conditionID, 643 "condition": nrqlCondition, 644 } 645 646 if err := a.NerdGraphQueryWithContext(ctx, updateNrqlConditionStaticMutation, vars, &resp); err != nil { 647 return nil, err 648 } 649 650 return &resp.AlertsNrqlConditionStaticUpdate, nil 651 } 652 653 func (a *Alerts) DeleteNrqlConditionMutation( 654 accountID int, 655 conditionID string, 656 ) (string, error) { 657 return a.DeleteNrqlConditionMutationWithContext(context.Background(), accountID, conditionID) 658 } 659 660 func (a *Alerts) DeleteNrqlConditionMutationWithContext( 661 ctx context.Context, 662 accountID int, 663 conditionID string, 664 ) (string, error) { 665 result, err := a.DeleteConditionMutationWithContext(ctx, accountID, conditionID) 666 if err != nil { 667 return "", err 668 } 669 670 return result, nil 671 } 672 673 type listNrqlConditionsParams struct { 674 PolicyID int `url:"policy_id,omitempty"` 675 } 676 677 type nrqlConditionsResponse struct { 678 NrqlConditions []*NrqlCondition `json:"nrql_conditions,omitempty"` 679 } 680 681 type nrqlConditionResponse struct { 682 NrqlCondition NrqlCondition `json:"nrql_condition,omitempty"` 683 } 684 685 type nrqlConditionRequestBody struct { 686 NrqlCondition NrqlCondition `json:"nrql_condition,omitempty"` 687 } 688 689 type nrqlConditionBaselineCreateResponse struct { 690 AlertsNrqlConditionBaselineCreate NrqlAlertCondition `json:"alertsNrqlConditionBaselineCreate"` 691 } 692 693 type nrqlConditionBaselineUpdateResponse struct { 694 AlertsNrqlConditionBaselineUpdate NrqlAlertCondition `json:"alertsNrqlConditionBaselineUpdate"` 695 } 696 697 type nrqlConditionStaticCreateResponse struct { 698 AlertsNrqlConditionStaticCreate NrqlAlertCondition `json:"alertsNrqlConditionStaticCreate"` 699 } 700 701 type nrqlConditionStaticUpdateResponse struct { 702 AlertsNrqlConditionStaticUpdate NrqlAlertCondition `json:"alertsNrqlConditionStaticUpdate"` 703 } 704 705 type searchNrqlConditionsResponse struct { 706 Actor struct { 707 Account struct { 708 Alerts struct { 709 NrqlConditionsSearch struct { 710 NextCursor *string 711 NrqlConditions []*NrqlAlertCondition `json:"nrqlConditions"` 712 } `json:"nrqlConditionsSearch"` 713 } `json:"alerts"` 714 } `json:"account"` 715 } `json:"actor"` 716 } 717 718 type getNrqlConditionQueryResponse struct { 719 Actor struct { 720 Account struct { 721 Alerts struct { 722 NrqlCondition NrqlAlertCondition `json:"nrqlCondition"` 723 } `json:"alerts"` 724 } `json:"account"` 725 } `json:"actor"` 726 } 727 728 const ( 729 graphqlNrqlConditionStructFields = ` 730 id 731 name 732 nrql { 733 evaluationOffset 734 query 735 } 736 enabled 737 entityGuid 738 description 739 policyId 740 runbookUrl 741 terms { 742 operator 743 priority 744 threshold 745 thresholdDuration 746 thresholdOccurrences 747 } 748 type 749 violationTimeLimit 750 violationTimeLimitSeconds 751 expiration { 752 closeViolationsOnExpiration 753 expirationDuration 754 openViolationOnExpiration 755 } 756 signal { 757 aggregationWindow 758 evaluationOffset 759 fillOption 760 fillValue 761 aggregationMethod 762 aggregationDelay 763 aggregationTimer 764 slideBy 765 } 766 ` 767 768 graphqlFragmentNrqlBaselineConditionFields = ` 769 ... on AlertsNrqlBaselineCondition { 770 baselineDirection 771 } 772 ` 773 774 graphqlFragmentNrqlStaticConditionFields = ` 775 ... on AlertsNrqlStaticCondition { 776 valueFunction 777 } 778 ` 779 780 searchNrqlConditionsQuery = ` 781 query($accountId: Int!, $searchCriteria: AlertsNrqlConditionsSearchCriteriaInput, $cursor: String) { 782 actor { 783 account(id: $accountId) { 784 alerts { 785 nrqlConditionsSearch(searchCriteria: $searchCriteria, cursor: $cursor) { 786 nextCursor 787 totalCount 788 nrqlConditions {` + 789 graphqlNrqlConditionStructFields + 790 graphqlFragmentNrqlBaselineConditionFields + 791 graphqlFragmentNrqlStaticConditionFields + 792 `} } } } } }` 793 794 getNrqlConditionQuery = ` 795 query ($accountId: Int!, $id: ID!) { 796 actor { 797 account(id: $accountId) { 798 alerts { 799 nrqlCondition(id: $id) {` + 800 graphqlNrqlConditionStructFields + 801 graphqlFragmentNrqlBaselineConditionFields + 802 graphqlFragmentNrqlStaticConditionFields + 803 `} } } } }` 804 805 // Baseline 806 createNrqlConditionBaselineMutation = ` 807 mutation($accountId: Int!, $policyId: ID!, $condition: AlertsNrqlConditionBaselineInput!) { 808 alertsNrqlConditionBaselineCreate(accountId: $accountId, policyId: $policyId, condition: $condition) { 809 baselineDirection` + 810 graphqlNrqlConditionStructFields + 811 ` } }` 812 813 // Baseline 814 updateNrqlConditionBaselineMutation = ` 815 mutation($accountId: Int!, $id: ID!, $condition: AlertsNrqlConditionUpdateBaselineInput!) { 816 alertsNrqlConditionBaselineUpdate(accountId: $accountId, id: $id, condition: $condition) { 817 baselineDirection` + 818 graphqlNrqlConditionStructFields + 819 ` } }` 820 821 // Static 822 createNrqlConditionStaticMutation = ` 823 mutation($accountId: Int!, $policyId: ID!, $condition: AlertsNrqlConditionStaticInput!) { 824 alertsNrqlConditionStaticCreate(accountId: $accountId, policyId: $policyId, condition: $condition) { 825 valueFunction` + 826 graphqlNrqlConditionStructFields + 827 ` } }` 828 829 // Static 830 updateNrqlConditionStaticMutation = ` 831 mutation($accountId: Int!, $id: ID!, $condition: AlertsNrqlConditionUpdateStaticInput!) { 832 alertsNrqlConditionStaticUpdate(accountId: $accountId, id: $id, condition: $condition) { 833 valueFunction` + 834 graphqlNrqlConditionStructFields + 835 ` } }` 836 )