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

     1  package alerts
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     6  )
     7  
     8  // PolicyChannels represents an association of alert channels to a specific alert policy.
     9  type PolicyChannels struct {
    10  	ID         int   `json:"id,omitempty"`
    11  	ChannelIDs []int `json:"channel_ids,omitempty"`
    12  }
    13  
    14  // UpdatePolicyChannels updates a policy by adding the specified notification channels.
    15  func (a *Alerts) UpdatePolicyChannels(policyID int, channelIDs []int) (*PolicyChannels, error) {
    16  	return a.UpdatePolicyChannelsWithContext(context.Background(), policyID, channelIDs)
    17  }
    18  
    19  // UpdatePolicyChannelsWithContext updates a policy by adding the specified notification channels.
    20  func (a *Alerts) UpdatePolicyChannelsWithContext(ctx context.Context, policyID int, channelIDs []int) (*PolicyChannels, error) {
    21  	channelIDStrings := make([]string, len(channelIDs))
    22  
    23  	for i, channelID := range channelIDs {
    24  		channelIDStrings[i] = strconv.Itoa(channelID)
    25  	}
    26  
    27  	queryParams := updatePolicyChannelsParams{
    28  		PolicyID:   policyID,
    29  		ChannelIDs: channelIDs,
    30  	}
    31  
    32  	resp := updatePolicyChannelsResponse{}
    33  
    34  	_, err := a.client.PutWithContext(ctx, a.config.Region().RestURL("/alerts_policy_channels.json"), &queryParams, nil, &resp)
    35  
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	return &resp.Policy, nil
    41  }
    42  
    43  // DeletePolicyChannel deletes a notification channel from an alert policy.
    44  // This method returns a response containing the Channel that was deleted from the policy.
    45  func (a *Alerts) DeletePolicyChannel(policyID int, channelID int) (*Channel, error) {
    46  	return a.DeletePolicyChannelWithContext(context.Background(), policyID, channelID)
    47  }
    48  
    49  // DeletePolicyChannelWithContext deletes a notification channel from an alert policy.
    50  // This method returns a response containing the Channel that was deleted from the policy.
    51  func (a *Alerts) DeletePolicyChannelWithContext(ctx context.Context, policyID int, channelID int) (*Channel, error) {
    52  	queryParams := deletePolicyChannelsParams{
    53  		PolicyID:  policyID,
    54  		ChannelID: channelID,
    55  	}
    56  
    57  	resp := deletePolicyChannelResponse{}
    58  
    59  	_, err := a.client.DeleteWithContext(ctx, a.config.Region().RestURL("/alerts_policy_channels.json"), &queryParams, &resp)
    60  
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	return &resp.Channel, nil
    66  }
    67  
    68  type updatePolicyChannelsParams struct {
    69  	PolicyID   int   `url:"policy_id,omitempty"`
    70  	ChannelIDs []int `url:"channel_ids,comma"`
    71  }
    72  
    73  type deletePolicyChannelsParams struct {
    74  	PolicyID  int `url:"policy_id,omitempty"`
    75  	ChannelID int `url:"channel_id,omitempty"`
    76  }
    77  
    78  type updatePolicyChannelsResponse struct {
    79  	Policy PolicyChannels `json:"policy,omitempty"`
    80  }
    81  
    82  type deletePolicyChannelResponse struct {
    83  	Channel Channel           `json:"channel,omitempty"`
    84  	Links   map[string]string `json:"channel.policy_ids,omitempty"`
    85  }