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

     1  package alerts
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/newrelic/newrelic-client-go/pkg/errors"
     8  
     9  	"github.com/newrelic/newrelic-client-go/internal/serialization"
    10  )
    11  
    12  // ChannelType specifies the channel type used when creating the alert channel.
    13  type ChannelType string
    14  
    15  var (
    16  	// ChannelTypes enumerates the possible channel types for an alert channel.
    17  	ChannelTypes = struct {
    18  		Email     ChannelType
    19  		OpsGenie  ChannelType
    20  		PagerDuty ChannelType
    21  		Slack     ChannelType
    22  		User      ChannelType
    23  		VictorOps ChannelType
    24  		Webhook   ChannelType
    25  	}{
    26  		Email:     "email",
    27  		OpsGenie:  "opsgenie",
    28  		PagerDuty: "pagerduty",
    29  		Slack:     "slack",
    30  		User:      "user",
    31  		VictorOps: "victorops",
    32  		Webhook:   "webhook",
    33  	}
    34  )
    35  
    36  // Channel represents a New Relic alert notification channel
    37  type Channel struct {
    38  	ID            int                  `json:"id,omitempty"`
    39  	Name          string               `json:"name,omitempty"`
    40  	Type          ChannelType          `json:"type,omitempty"`
    41  	Configuration ChannelConfiguration `json:"configuration,omitempty"`
    42  	Links         ChannelLinks         `json:"links,omitempty"`
    43  }
    44  
    45  // ChannelLinks represent the links between policies and alert channels
    46  type ChannelLinks struct {
    47  	PolicyIDs []int `json:"policy_ids,omitempty"`
    48  }
    49  
    50  // ChannelConfiguration represents a Configuration type within Channels
    51  type ChannelConfiguration struct {
    52  	Recipients            string `json:"recipients,omitempty"`
    53  	IncludeJSONAttachment string `json:"include_json_attachment,omitempty"`
    54  	AuthToken             string `json:"auth_token,omitempty"`
    55  	APIKey                string `json:"api_key,omitempty"`
    56  	Teams                 string `json:"teams,omitempty"`
    57  	Tags                  string `json:"tags,omitempty"`
    58  	URL                   string `json:"url,omitempty"`
    59  	Channel               string `json:"channel,omitempty"`
    60  	Key                   string `json:"key,omitempty"`
    61  	RouteKey              string `json:"route_key,omitempty"`
    62  	ServiceKey            string `json:"service_key,omitempty"`
    63  	BaseURL               string `json:"base_url,omitempty"`
    64  	AuthUsername          string `json:"auth_username,omitempty"`
    65  	AuthPassword          string `json:"auth_password,omitempty"`
    66  	PayloadType           string `json:"payload_type,omitempty"`
    67  	Region                string `json:"region,omitempty"`
    68  	UserID                string `json:"user_id,omitempty"`
    69  
    70  	// Payload is unmarshaled to type map[string]string
    71  	Payload serialization.MapStringInterface `json:"payload,omitempty"`
    72  
    73  	// Headers is unmarshaled to type map[string]string
    74  	Headers serialization.MapStringInterface `json:"headers,omitempty"`
    75  }
    76  
    77  // ListChannels returns all alert channels for a given account.
    78  func (a *Alerts) ListChannels() ([]*Channel, error) {
    79  	return a.ListChannelsWithContext(context.Background())
    80  }
    81  
    82  // ListChannelsWithContext returns all alert channels for a given account.
    83  func (a *Alerts) ListChannelsWithContext(ctx context.Context) ([]*Channel, error) {
    84  	alertChannels := []*Channel{}
    85  	nextURL := a.config.Region().RestURL("/alerts_channels.json")
    86  
    87  	for nextURL != "" {
    88  		response := alertChannelsResponse{}
    89  		resp, err := a.client.GetWithContext(ctx, nextURL, nil, &response)
    90  
    91  		if err != nil {
    92  			return nil, err
    93  		}
    94  
    95  		alertChannels = append(alertChannels, response.Channels...)
    96  
    97  		paging := a.pager.Parse(resp)
    98  		nextURL = paging.Next
    99  	}
   100  
   101  	return alertChannels, nil
   102  }
   103  
   104  // GetChannel returns a specific alert channel by ID for a given account.
   105  func (a *Alerts) GetChannel(id int) (*Channel, error) {
   106  	return a.GetChannelWithContext(context.Background(), id)
   107  }
   108  
   109  // GetChannelWithContext returns a specific alert channel by ID for a given account.
   110  func (a *Alerts) GetChannelWithContext(ctx context.Context, id int) (*Channel, error) {
   111  	channels, err := a.ListChannelsWithContext(ctx)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	for _, channel := range channels {
   117  		if channel.ID == id {
   118  			return channel, nil
   119  		}
   120  	}
   121  	return nil, errors.NewNotFoundf("no channel found for id %d", id)
   122  }
   123  
   124  // CreateChannel creates an alert channel within a given account.
   125  // The configuration options different based on channel type.
   126  // For more information on the different configurations, please
   127  // view the New Relic API documentation for this endpoint.
   128  // Docs: https://docs.newrelic.com/docs/alerts/rest-api-alerts/new-relic-alerts-rest-api/rest-api-calls-new-relic-alerts#channels
   129  func (a *Alerts) CreateChannel(channel Channel) (*Channel, error) {
   130  	return a.CreateChannelWithContext(context.Background(), channel)
   131  }
   132  
   133  // CreateChannelWithContext creates an alert channel within a given account.
   134  // The configuration options different based on channel type.
   135  // For more information on the different configurations, please
   136  // view the New Relic API documentation for this endpoint.
   137  // Docs: https://docs.newrelic.com/docs/alerts/rest-api-alerts/new-relic-alerts-rest-api/rest-api-calls-new-relic-alerts#channels
   138  func (a *Alerts) CreateChannelWithContext(ctx context.Context, channel Channel) (*Channel, error) {
   139  	reqBody := alertChannelRequestBody{
   140  		Channel: channel,
   141  	}
   142  	resp := alertChannelsResponse{}
   143  
   144  	_, err := a.client.PostWithContext(ctx, a.config.Region().RestURL("alerts_channels.json"), nil, &reqBody, &resp)
   145  
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  
   150  	return resp.Channels[0], nil
   151  }
   152  
   153  // DeleteChannel deletes the alert channel with the specified ID.
   154  func (a *Alerts) DeleteChannel(id int) (*Channel, error) {
   155  	return a.DeleteChannelWithContext(context.Background(), id)
   156  }
   157  
   158  // DeleteChannelWithContext deletes the alert channel with the specified ID.
   159  func (a *Alerts) DeleteChannelWithContext(ctx context.Context, id int) (*Channel, error) {
   160  	resp := alertChannelResponse{}
   161  	url := fmt.Sprintf("/alerts_channels/%d.json", id)
   162  	_, err := a.client.DeleteWithContext(ctx, a.config.Region().RestURL(url), nil, &resp)
   163  
   164  	if err != nil {
   165  		return nil, err
   166  	}
   167  
   168  	return &resp.Channel, nil
   169  }
   170  
   171  type alertChannelsResponse struct {
   172  	Channels []*Channel `json:"channels,omitempty"`
   173  }
   174  
   175  type alertChannelResponse struct {
   176  	Channel Channel `json:"channel,omitempty"`
   177  }
   178  
   179  type alertChannelRequestBody struct {
   180  	Channel Channel `json:"channel"`
   181  }