github.com/newrelic/newrelic-client-go@v1.1.0/pkg/alerts/incidents.go (about)

     1  package alerts
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/newrelic/newrelic-client-go/internal/serialization"
     8  )
     9  
    10  // Incident represents a New Relic alert incident.
    11  type Incident struct {
    12  	ID                 int                      `json:"id,omitempty"`
    13  	OpenedAt           *serialization.EpochTime `json:"opened_at,omitempty"`
    14  	ClosedAt           *serialization.EpochTime `json:"closed_at,omitempty"`
    15  	IncidentPreference string                   `json:"incident_preference,omitempty"`
    16  	Links              IncidentLink             `json:"links"`
    17  }
    18  
    19  // IncidentLink represents a link between a New Relic alert incident and its violations
    20  type IncidentLink struct {
    21  	Violations []int `json:"violations,omitempty"`
    22  	PolicyID   int   `json:"policy_id"`
    23  }
    24  
    25  // ListIncidents returns all alert incidents.
    26  func (a *Alerts) ListIncidents(onlyOpen bool, excludeViolations bool) ([]*Incident, error) {
    27  	return a.ListIncidentsWithContext(context.Background(), onlyOpen, excludeViolations)
    28  }
    29  
    30  // ListIncidentsWithContext returns all alert incidents.
    31  func (a *Alerts) ListIncidentsWithContext(ctx context.Context, onlyOpen bool, excludeViolations bool) ([]*Incident, error) {
    32  	incidents := []*Incident{}
    33  	queryParams := listIncidentsParams{
    34  		OnlyOpen:          onlyOpen,
    35  		ExcludeViolations: excludeViolations,
    36  	}
    37  
    38  	nextURL := a.config.Region().RestURL("/alerts_incidents.json")
    39  
    40  	for nextURL != "" {
    41  		incidentsResponse := alertIncidentsResponse{}
    42  		resp, err := a.client.GetWithContext(ctx, nextURL, queryParams, &incidentsResponse)
    43  
    44  		if err != nil {
    45  			return nil, err
    46  		}
    47  
    48  		incidents = append(incidents, incidentsResponse.Incidents...)
    49  
    50  		paging := a.pager.Parse(resp)
    51  		nextURL = paging.Next
    52  	}
    53  
    54  	return incidents, nil
    55  }
    56  
    57  // AcknowledgeIncident acknowledges an existing incident.
    58  func (a *Alerts) AcknowledgeIncident(id int) (*Incident, error) {
    59  	return a.AcknowledgeIncidentWithContext(context.Background(), id)
    60  }
    61  
    62  // AcknowledgeIncidentWithContext acknowledges an existing incident.
    63  func (a *Alerts) AcknowledgeIncidentWithContext(ctx context.Context, id int) (*Incident, error) {
    64  	return a.updateIncident(ctx, id, "acknowledge")
    65  }
    66  
    67  // CloseIncident closes an existing open incident.
    68  func (a *Alerts) CloseIncident(id int) (*Incident, error) {
    69  	return a.CloseIncidentWithContext(context.Background(), id)
    70  }
    71  
    72  // CloseIncidentWithContext closes an existing open incident.
    73  func (a *Alerts) CloseIncidentWithContext(ctx context.Context, id int) (*Incident, error) {
    74  	return a.updateIncident(ctx, id, "close")
    75  }
    76  
    77  func (a *Alerts) updateIncident(ctx context.Context, id int, verb string) (*Incident, error) {
    78  	response := alertIncidentResponse{}
    79  	url := fmt.Sprintf("/alerts_incidents/%d/%s.json", id, verb)
    80  	_, err := a.client.PutWithContext(ctx, a.config.Region().RestURL(url), nil, nil, &response)
    81  
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	return &response.Incident, nil
    87  }
    88  
    89  type listIncidentsParams struct {
    90  	OnlyOpen          bool `url:"only_open,omitempty"`
    91  	ExcludeViolations bool `url:"exclude_violations,omitempty"`
    92  }
    93  
    94  type alertIncidentsResponse struct {
    95  	Incidents []*Incident `json:"incidents,omitempty"`
    96  }
    97  
    98  type alertIncidentResponse struct {
    99  	Incident Incident `json:"incident,omitempty"`
   100  }