Skip to content

Commit

Permalink
Merge pull request #4 from STLVRTX/master
Browse files Browse the repository at this point in the history
Feature: new API resources, migrated v1 to versionless
  • Loading branch information
yacut authored Jul 4, 2022
2 parents 1649600 + 7c90d5d commit 6c340c5
Show file tree
Hide file tree
Showing 33 changed files with 2,928 additions and 550 deletions.
701 changes: 701 additions & 0 deletions alert.go

Large diffs are not rendered by default.

408 changes: 408 additions & 0 deletions alertaction.go

Large diffs are not rendered by default.

76 changes: 61 additions & 15 deletions alertsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type AlertSource struct {
IntegrationType string `json:"integrationType"`
IntegrationKey string `json:"integrationKey,omitempty"`
IntegrationURL string `json:"integrationUrl,omitempty"`
IncidentCreation string `json:"incidentCreation,omitempty"`
AlertCreation string `json:"alertCreation,omitempty"`
IncidentCreation string `json:"incidentCreation,omitempty"` // @deprecated
EmailFiltered bool `json:"emailFiltered,omitempty"`
EmailResolveFiltered bool `json:"emailResolveFiltered,omitempty"`
Active bool `json:"active,omitempty"`
Expand All @@ -27,7 +28,8 @@ type AlertSource struct {
ResolveKeyExtractor *EmailPredicate `json:"resolveKeyExtractor,omitempty"`
FilterOperator string `json:"filterOperator,omitempty"`
ResolveFilterOperator string `json:"resolveFilterOperator,omitempty"`
IncidentPriorityRule string `json:"incidentPriorityRule,omitempty"`
AlertPriorityRule string `json:"alertPriorityRule,omitempty"`
IncidentPriorityRule string `json:"incidentPriorityRule,omitempty"` // @deprecated
SupportHours *SupportHours `json:"supportHours,omitempty"`
EscalationPolicy *EscalationPolicy `json:"escalationPolicy,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Expand All @@ -46,7 +48,8 @@ type EmailPredicate struct {
// SupportHours definition
type SupportHours struct {
Timezone string `json:"timezone"`
AutoRaiseIncidents bool `json:"autoRaiseIncidents,omitempty"` // Raise priority of all pending incidents for this alert source to 'high' when support hours begin
AutoRaiseAlerts bool `json:"autoRaiseAlerts,omitempty"` // Raise priority of all pending alerts for this alert source to 'high' when support hours begin
AutoRaiseIncidents bool `json:"autoRaiseIncidents,omitempty"` // @deprecated
SupportDays SupportDays `json:"supportDays"`
}

Expand Down Expand Up @@ -96,19 +99,19 @@ var AlertSourceStatuses = struct {
Disabled: "DISABLED",
}

// AlertSourceIncidentCreations defines alert source incident creations
var AlertSourceIncidentCreations = struct {
OneIncidentPerEmail string
OneIncidentPerEmailSubject string
OnePendingIncidentAllowed string
OneOpenIncidentAllowed string
OpenResolveOnExtraction string
// AlertSourceAlertCreations defines alert source alert creations
var AlertSourceAlertCreations = struct {
OneAlertPerEmail string
OneAlertPerEmailSubject string
OnePendingAlertAllowed string
OneOpenAlertAllowed string
OpenResolveOnExtraction string
}{
OneIncidentPerEmail: "ONE_INCIDENT_PER_EMAIL",
OneIncidentPerEmailSubject: "ONE_INCIDENT_PER_EMAIL_SUBJECT",
OnePendingIncidentAllowed: "ONE_PENDING_INCIDENT_ALLOWED",
OneOpenIncidentAllowed: "ONE_OPEN_INCIDENT_ALLOWED",
OpenResolveOnExtraction: "OPEN_RESOLVE_ON_EXTRACTION",
OneAlertPerEmail: "ONE_INCIDENT_PER_EMAIL",
OneAlertPerEmailSubject: "ONE_INCIDENT_PER_EMAIL_SUBJECT",
OnePendingAlertAllowed: "ONE_PENDING_INCIDENT_ALLOWED",
OneOpenAlertAllowed: "ONE_OPEN_INCIDENT_ALLOWED",
OpenResolveOnExtraction: "OPEN_RESOLVE_ON_EXTRACTION",
}

// AlertSourceIntegrationTypes defines alert source integration types
Expand Down Expand Up @@ -353,6 +356,28 @@ func (c *Client) CreateAlertSource(input *CreateAlertSourceInput) (*CreateAlertS
if input.AlertSource == nil {
return nil, errors.New("alert source input is required")
}

if input.AlertSource.AlertCreation != "" && input.AlertSource.IncidentCreation != "" {
input.AlertSource.IncidentCreation = ""
}
if input.AlertSource.AlertCreation == "" {
input.AlertSource.AlertCreation = input.AlertSource.IncidentCreation
input.AlertSource.IncidentCreation = ""
}

if input.AlertSource.AlertPriorityRule != "" && input.AlertSource.IncidentPriorityRule != "" {
input.AlertSource.IncidentPriorityRule = ""
}
if input.AlertSource.AlertPriorityRule == "" {
input.AlertSource.AlertPriorityRule = input.AlertSource.IncidentPriorityRule
input.AlertSource.IncidentPriorityRule = ""
}

if input.AlertSource.SupportHours.AutoRaiseIncidents {
input.AlertSource.SupportHours.AutoRaiseAlerts = true
input.AlertSource.SupportHours.AutoRaiseIncidents = false
}

resp, err := c.httpClient.R().SetBody(input.AlertSource).Post(apiRoutes.alertSources)
if err != nil {
return nil, err
Expand Down Expand Up @@ -463,6 +488,27 @@ func (c *Client) UpdateAlertSource(input *UpdateAlertSourceInput) (*UpdateAlertS
return nil, errors.New("alert source id is required")
}

if input.AlertSource.AlertCreation != "" && input.AlertSource.IncidentCreation != "" {
input.AlertSource.IncidentCreation = ""
}
if input.AlertSource.AlertCreation == "" {
input.AlertSource.AlertCreation = input.AlertSource.IncidentCreation
input.AlertSource.IncidentCreation = ""
}

if input.AlertSource.AlertPriorityRule != "" && input.AlertSource.IncidentPriorityRule != "" {
input.AlertSource.IncidentPriorityRule = ""
}
if input.AlertSource.AlertPriorityRule == "" {
input.AlertSource.AlertPriorityRule = input.AlertSource.IncidentPriorityRule
input.AlertSource.IncidentPriorityRule = ""
}

if input.AlertSource.SupportHours.AutoRaiseIncidents {
input.AlertSource.SupportHours.AutoRaiseAlerts = true
input.AlertSource.SupportHours.AutoRaiseIncidents = false
}

resp, err := c.httpClient.R().SetBody(input.AlertSource).Put(fmt.Sprintf("%s/%d", apiRoutes.alertSources, *input.AlertSourceID))
if err != nil {
return nil, err
Expand Down
244 changes: 244 additions & 0 deletions automationrule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
package ilert

import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
)

// Automation Rules definition https://api.ilert.com/api-docs/#tag/Automation-Rules
type AutomationRule struct {
ID string `json:"id"`
AlertType string `json:"alertType"`
ResolveIncident bool `json:"resolveIncident"`
ResolveService bool `json:"resolveService"`
ServiceStatus string `json:"serviceStatus"`
Template *IncidentTemplate `json:"template"`
Service *Service `json:"service"`
AlertSource *AlertSource `json:"alertSource"`
SendNotification bool `json:"sendNotification"`
}

var AlertType = struct {
Created string
Accepted string
}{
Created: "CREATED",
Accepted: "ACCEPTED",
}

// CreateAutomationRuleInput represents the input of a CreateAutomationRule operation.
type CreateAutomationRuleInput struct {
_ struct{}
AutomationRule *AutomationRule
}

// CreateAutomationRuleOutput represents the output of a CreateAutomationRule operation.
type CreateAutomationRuleOutput struct {
_ struct{}
AutomationRule *AutomationRule
}

// CreateAutomationRule creates a new automationRule. https://api.ilert.com/api-docs/#tag/Automation-Rules/paths/~1automation-rules/post
func (c *Client) CreateAutomationRule(input *CreateAutomationRuleInput) (*CreateAutomationRuleOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.AutomationRule == nil {
return nil, errors.New("automationRule input is required")
}
resp, err := c.httpClient.R().SetBody(input.AutomationRule).Post(apiRoutes.automationRules)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}

automationRule := &AutomationRule{}
err = json.Unmarshal(resp.Body(), automationRule)
if err != nil {
return nil, err
}

return &CreateAutomationRuleOutput{AutomationRule: automationRule}, nil
}

// GetAutomationRulesInput represents the input of a GetAutomationRules operation.
type GetAutomationRulesInput struct {
_ struct{}
// an integer specifying the starting point (beginning with 0) when paging through a list of entities
StartIndex *int

// the maximum number of results when paging through a list of entities.
// Default: 50, Maximum: 100
MaxResults *int

// The service id for which automation rules are filtered for, this param is required
Service *int
}

// GetAutomationRulesOutput represents the output of a GetAutomationRules operation.
type GetAutomationRulesOutput struct {
_ struct{}
AutomationRules []*AutomationRule
}

// GetAutomationRules lists automationRule sources. https://api.ilert.com/api-docs/#tag/Automation-Rules/paths/~1automation-rules/get
func (c *Client) GetAutomationRules(input *GetAutomationRulesInput) (*GetAutomationRulesOutput, error) {
if input == nil {
input = &GetAutomationRulesInput{}
}
if input.Service == nil {
return nil, errors.New("service id is required")
}

q := url.Values{}
if input.StartIndex != nil {
q.Add("start-index", strconv.Itoa(*input.StartIndex))
}
if input.MaxResults != nil {
q.Add("max-results", strconv.Itoa(*input.MaxResults))
}
if input.Service != nil {
q.Add("service", strconv.Itoa(*input.Service))
}

resp, err := c.httpClient.R().Get(fmt.Sprintf("%s?%s", apiRoutes.automationRules, q.Encode()))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}

automationRules := make([]*AutomationRule, 0)
err = json.Unmarshal(resp.Body(), &automationRules)
if err != nil {
return nil, err
}

return &GetAutomationRulesOutput{AutomationRules: automationRules}, nil
}

// GetAutomationRuleInput represents the input of a GetAutomationRule operation.
type GetAutomationRuleInput struct {
_ struct{}
AutomationRuleID *string
}

// GetAutomationRuleOutput represents the output of a GetAutomationRule operation.
type GetAutomationRuleOutput struct {
_ struct{}
AutomationRule *AutomationRule
}

// GetAutomationRule gets a automationRule by ID. https://api.ilert.com/api-docs/#tag/Automation-Rules/paths/~1automation-rules~1{id}/get
func (c *Client) GetAutomationRule(input *GetAutomationRuleInput) (*GetAutomationRuleOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.AutomationRuleID == nil {
return nil, errors.New("automationRule id is required")
}

q := url.Values{}

var url = fmt.Sprintf("%s/%s?%s", apiRoutes.automationRules, *input.AutomationRuleID, q.Encode())

resp, err := c.httpClient.R().Get(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}

automationRule := &AutomationRule{}
err = json.Unmarshal(resp.Body(), automationRule)
if err != nil {
return nil, err
}

return &GetAutomationRuleOutput{AutomationRule: automationRule}, nil
}

// UpdateAutomationRuleInput represents the input of a UpdateAutomationRule operation.
type UpdateAutomationRuleInput struct {
_ struct{}
AutomationRuleID *string
AutomationRule *AutomationRule
}

// UpdateAutomationRuleOutput represents the output of a UpdateAutomationRule operation.
type UpdateAutomationRuleOutput struct {
_ struct{}
AutomationRule *AutomationRule
}

// UpdateAutomationRule updates the specific automationRule. https://api.ilert.com/api-docs/#tag/Automation-Rules/paths/~1automation-rules~1{id}/put
func (c *Client) UpdateAutomationRule(input *UpdateAutomationRuleInput) (*UpdateAutomationRuleOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.AutomationRuleID == nil {
return nil, errors.New("automationRule id is required")
}
if input.AutomationRule == nil {
return nil, errors.New("automationRule input is required")
}

url := fmt.Sprintf("%s/%s", apiRoutes.automationRules, *input.AutomationRuleID)

resp, err := c.httpClient.R().SetBody(input.AutomationRule).Put(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}

automationRule := &AutomationRule{}
err = json.Unmarshal(resp.Body(), automationRule)
if err != nil {
return nil, err
}

return &UpdateAutomationRuleOutput{AutomationRule: automationRule}, nil
}

// DeleteAutomationRuleInput represents the input of a DeleteAutomationRule operation.
type DeleteAutomationRuleInput struct {
_ struct{}
AutomationRuleID *string
}

// DeleteAutomationRuleOutput represents the output of a DeleteAutomationRule operation.
type DeleteAutomationRuleOutput struct {
_ struct{}
}

// DeleteAutomationRule deletes the specified automationRule. https://api.ilert.com/api-docs/#tag/Automation-Rules/paths/~1automation-rules~1{id}/delete
func (c *Client) DeleteAutomationRule(input *DeleteAutomationRuleInput) (*DeleteAutomationRuleOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.AutomationRuleID == nil {
return nil, errors.New("automationRule id is required")
}

url := fmt.Sprintf("%s/%s", apiRoutes.automationRules, *input.AutomationRuleID)

resp, err := c.httpClient.R().Delete(url)

if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 204); apiErr != nil {
return nil, apiErr
}

return &DeleteAutomationRuleOutput{}, nil
}
Loading

0 comments on commit 6c340c5

Please sign in to comment.