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

     1  package alerts
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/newrelic/newrelic-client-go/pkg/errors"
     8  )
     9  
    10  // SyntheticsCondition represents a New Relic Synthetics alert condition.
    11  type SyntheticsCondition struct {
    12  	ID         int    `json:"id,omitempty"`
    13  	Name       string `json:"name,omitempty"`
    14  	Enabled    bool   `json:"enabled"`
    15  	RunbookURL string `json:"runbook_url,omitempty"`
    16  	MonitorID  string `json:"monitor_id,omitempty"`
    17  }
    18  
    19  // ListSyntheticsConditions returns a list of Synthetics alert conditions for a given policy.
    20  func (a *Alerts) ListSyntheticsConditions(policyID int) ([]*SyntheticsCondition, error) {
    21  	return a.ListSyntheticsConditionsWithContext(context.Background(), policyID)
    22  }
    23  
    24  // ListSyntheticsConditionsWithContext returns a list of Synthetics alert conditions for a given policy.
    25  func (a *Alerts) ListSyntheticsConditionsWithContext(ctx context.Context, policyID int) ([]*SyntheticsCondition, error) {
    26  	conditions := []*SyntheticsCondition{}
    27  	nextURL := a.config.Region().RestURL("/alerts_synthetics_conditions.json")
    28  	queryParams := listSyntheticsConditionsParams{
    29  		PolicyID: policyID,
    30  	}
    31  
    32  	for nextURL != "" {
    33  		response := syntheticsConditionsResponse{}
    34  		resp, err := a.client.GetWithContext(ctx, nextURL, &queryParams, &response)
    35  
    36  		if err != nil {
    37  			return nil, err
    38  		}
    39  
    40  		conditions = append(conditions, response.Conditions...)
    41  
    42  		paging := a.pager.Parse(resp)
    43  		nextURL = paging.Next
    44  	}
    45  
    46  	return conditions, nil
    47  }
    48  
    49  // GetSyntheticsCondition retrieves a specific Synthetics alert condition.
    50  func (a *Alerts) GetSyntheticsCondition(policyID int, conditionID int) (*SyntheticsCondition, error) {
    51  	return a.GetSyntheticsConditionWithContext(context.Background(), policyID, conditionID)
    52  }
    53  
    54  // GetSyntheticsConditionWithContext retrieves a specific Synthetics alert condition.
    55  func (a *Alerts) GetSyntheticsConditionWithContext(ctx context.Context, policyID int, conditionID int) (*SyntheticsCondition, error) {
    56  	conditions, err := a.ListSyntheticsConditionsWithContext(ctx, policyID)
    57  
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	for _, c := range conditions {
    63  		if c.ID == conditionID {
    64  			return c, nil
    65  		}
    66  	}
    67  
    68  	return nil, errors.NewNotFoundf("no condition found for policy %d and condition ID %d", policyID, conditionID)
    69  }
    70  
    71  // CreateSyntheticsCondition creates a new Synthetics alert condition.
    72  func (a *Alerts) CreateSyntheticsCondition(policyID int, condition SyntheticsCondition) (*SyntheticsCondition, error) {
    73  	return a.CreateSyntheticsConditionWithContext(context.Background(), policyID, condition)
    74  }
    75  
    76  // CreateSyntheticsConditionWithContext creates a new Synthetics alert condition.
    77  func (a *Alerts) CreateSyntheticsConditionWithContext(ctx context.Context, policyID int, condition SyntheticsCondition) (*SyntheticsCondition, error) {
    78  	resp := syntheticsConditionResponse{}
    79  	reqBody := syntheticsConditionRequest{condition}
    80  	url := fmt.Sprintf("/alerts_synthetics_conditions/policies/%d.json", policyID)
    81  	_, err := a.client.PostWithContext(ctx, a.config.Region().RestURL(url), nil, &reqBody, &resp)
    82  
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	return &resp.Condition, nil
    88  }
    89  
    90  // UpdateSyntheticsCondition updates an existing Synthetics alert condition.
    91  func (a *Alerts) UpdateSyntheticsCondition(condition SyntheticsCondition) (*SyntheticsCondition, error) {
    92  	return a.UpdateSyntheticsConditionWithContext(context.Background(), condition)
    93  }
    94  
    95  // UpdateSyntheticsConditionWithContext updates an existing Synthetics alert condition.
    96  func (a *Alerts) UpdateSyntheticsConditionWithContext(ctx context.Context, condition SyntheticsCondition) (*SyntheticsCondition, error) {
    97  	resp := syntheticsConditionResponse{}
    98  	reqBody := syntheticsConditionRequest{condition}
    99  	url := fmt.Sprintf("/alerts_synthetics_conditions/%d.json", condition.ID)
   100  	_, err := a.client.PutWithContext(ctx, a.config.Region().RestURL(url), nil, &reqBody, &resp)
   101  
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	return &resp.Condition, nil
   107  }
   108  
   109  // DeleteSyntheticsCondition deletes a Synthetics alert condition.
   110  func (a *Alerts) DeleteSyntheticsCondition(conditionID int) (*SyntheticsCondition, error) {
   111  	return a.DeleteSyntheticsConditionWithContext(context.Background(), conditionID)
   112  }
   113  
   114  // DeleteSyntheticsConditionWithContext deletes a Synthetics alert condition.
   115  func (a *Alerts) DeleteSyntheticsConditionWithContext(ctx context.Context, conditionID int) (*SyntheticsCondition, error) {
   116  	resp := syntheticsConditionResponse{}
   117  	url := fmt.Sprintf("/alerts_synthetics_conditions/%d.json", conditionID)
   118  	_, err := a.client.DeleteWithContext(ctx, a.config.Region().RestURL(url), nil, &resp)
   119  
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  
   124  	return &resp.Condition, nil
   125  }
   126  
   127  type listSyntheticsConditionsParams struct {
   128  	PolicyID int `url:"policy_id,omitempty"`
   129  }
   130  
   131  type syntheticsConditionsResponse struct {
   132  	Conditions []*SyntheticsCondition `json:"synthetics_conditions,omitempty"`
   133  }
   134  
   135  type syntheticsConditionResponse struct {
   136  	Condition SyntheticsCondition `json:"synthetics_condition,omitempty"`
   137  }
   138  
   139  type syntheticsConditionRequest struct {
   140  	Condition SyntheticsCondition `json:"synthetics_condition,omitempty"`
   141  }