Skip to content

Commit

Permalink
Merge pull request #33 from iLert/feature/support-hours-resource
Browse files Browse the repository at this point in the history
Feature/support hours resource
  • Loading branch information
STLVRTX authored Jan 3, 2024
2 parents b42b0ae + 09499b7 commit 0b1fd01
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog

# 13.12.2023, Version 3.4.0
# 03.01.2024, Version 3.4.0

- deprecate uptime monitors in [#32](https://github.com/iLert/ilert-go/pull/32)
- add new resource support hours in [#33](https://github.com/iLert/ilert-go/pull/33)

# 12.12.2023, Version 3.3.0

Expand Down
26 changes: 18 additions & 8 deletions alert_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type AlertSource struct {
ResolveFilterOperator string `json:"resolveFilterOperator,omitempty"`
AlertPriorityRule string `json:"alertPriorityRule,omitempty"`
IncidentPriorityRule string `json:"incidentPriorityRule,omitempty"` // @deprecated
SupportHours *SupportHours `json:"supportHours,omitempty"`
SupportHours interface{} `json:"supportHours,omitempty"`
EscalationPolicy *EscalationPolicy `json:"escalationPolicy,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"` // @deprecated
AutotaskMetadata *AutotaskMetadata `json:"autotaskMetadata,omitempty"` // @deprecated
Expand All @@ -53,14 +53,26 @@ type EmailPredicate struct {
Value string `json:"value"`
}

// SupportHours definition
// @deprecated SupportHours definition
type SupportHours struct {
Timezone string `json:"timezone"`
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"`
}

func (s *SupportHours) RemoveLegacyFields() {
if s.AutoRaiseIncidents {
s.AutoRaiseAlerts = true
s.AutoRaiseIncidents = false
}
}

// SupportHoursReference definition
type SupportHoursReference struct {
ID int64 `json:"id"`
}

// SupportDays definition
type SupportDays struct {
MONDAY *SupportDay `json:"MONDAY"`
Expand Down Expand Up @@ -485,9 +497,8 @@ func (c *Client) CreateAlertSource(input *CreateAlertSourceInput) (*CreateAlertS
input.AlertSource.IncidentPriorityRule = ""
}

if input.AlertSource.SupportHours != nil && input.AlertSource.SupportHours.AutoRaiseIncidents {
input.AlertSource.SupportHours.AutoRaiseAlerts = true
input.AlertSource.SupportHours.AutoRaiseIncidents = false
if v, ok := input.AlertSource.SupportHours.(SupportHours); ok {
v.RemoveLegacyFields()
}

resp, err := c.httpClient.R().SetBody(input.AlertSource).Post(apiRoutes.alertSources)
Expand Down Expand Up @@ -679,9 +690,8 @@ func (c *Client) UpdateAlertSource(input *UpdateAlertSourceInput) (*UpdateAlertS
input.AlertSource.IncidentPriorityRule = ""
}

if input.AlertSource.SupportHours != nil && input.AlertSource.SupportHours.AutoRaiseIncidents {
input.AlertSource.SupportHours.AutoRaiseAlerts = true
input.AlertSource.SupportHours.AutoRaiseIncidents = false
if v, ok := input.AlertSource.SupportHours.(SupportHours); ok {
v.RemoveLegacyFields()
}

resp, err := c.httpClient.R().SetBody(input.AlertSource).Put(fmt.Sprintf("%s/%d", apiRoutes.alertSources, *input.AlertSourceID))
Expand Down
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ var apiRoutes = struct {
series string
services string
statusPages string
supportHours string
uptimeMonitors string
users string
teams string
Expand All @@ -262,6 +263,7 @@ var apiRoutes = struct {
series: "/api/series",
services: "/api/services",
statusPages: "/api/status-pages",
supportHours: "/api/support-hours",
uptimeMonitors: "/api/uptime-monitors",
users: "/api/users",
teams: "/api/teams",
Expand Down
40 changes: 40 additions & 0 deletions examples/support_hour/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"log"

"github.com/iLert/ilert-go/v3"
)

func main() {
var apiToken = "your API token"
client := ilert.NewClient(ilert.WithAPIToken(apiToken))

createSupportHourInput := &ilert.CreateSupportHourInput{
SupportHour: &ilert.SupportHour{
Name: "example",
Timezone: "Europe/Berlin",
SupportDays: &ilert.SupportDays{
MONDAY: &ilert.SupportDay{
Start: "09:00",
End: "18:00",
},
WEDNESDAY: &ilert.SupportDay{
Start: "09:00",
End: "18:00",
},
FRIDAY: &ilert.SupportDay{
Start: "09:00",
End: "18:00",
},
},
},
}

result, err := client.CreateSupportHour(createSupportHourInput)
if err != nil {
log.Println(result)
log.Fatalln("ERROR:", err)
}
log.Printf("Support hour:\n\n %+v\n", result.SupportHour)
}
249 changes: 249 additions & 0 deletions support_hour.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
package ilert

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

// SupportHour definition https://api.ilert.com/api-docs/#tag/SupportHours
type SupportHour struct {
ID int64 `json:"id"`
Name string `json:"name"`
Teams []TeamShort `json:"teams,omitempty"`
Timezone string `json:"timezone,omitempty"`
SupportDays *SupportDays `json:"supportDays"`
}

// CreateSupportHourInput represents the input of a CreateSupportHour operation.
type CreateSupportHourInput struct {
_ struct{}
SupportHour *SupportHour
}

// CreateSupportHourOutput represents the output of a CreateSupportHour operation.
type CreateSupportHourOutput struct {
_ struct{}
SupportHour *SupportHour
}

// CreateSupportHour creates a new support hours resource. https://api.ilert.com/api-docs/#tag/Support-Hours/paths/~1support-hours/post
func (c *Client) CreateSupportHour(input *CreateSupportHourInput) (*CreateSupportHourOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.SupportHour == nil {
return nil, errors.New("support hour input is required")
}
resp, err := c.httpClient.R().SetBody(input.SupportHour).Post(apiRoutes.supportHours)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 201); apiErr != nil {
return nil, apiErr
}

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

return &CreateSupportHourOutput{SupportHour: supportHour}, nil
}

// GetSupportHourInput represents the input of a GetSupportHour operation.
type GetSupportHourInput struct {
_ struct{}
SupportHourID *int64
}

// GetSupportHourOutput represents the output of a GetSupportHour operation.
type GetSupportHourOutput struct {
_ struct{}
SupportHour *SupportHour
}

// GetSupportHour gets the support hours resource with specified id. https://api.ilert.com/api-docs/#tag/Support-Hours/paths/~1support-hours~1{id}/get
func (c *Client) GetSupportHour(input *GetSupportHourInput) (*GetSupportHourOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.SupportHourID == nil {
return nil, errors.New("support hour id is required")
}

resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/%d", apiRoutes.supportHours, *input.SupportHourID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}

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

return &GetSupportHourOutput{SupportHour: supportHour}, nil
}

// GetSupportHoursInput represents the input of a GetSupportHours operation.
type GetSupportHoursInput 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.
// Maximum: 100
MaxResults *int
}

// GetSupportHoursOutput represents the output of a GetSupportHours operation.
type GetSupportHoursOutput struct {
_ struct{}
SupportHours []*SupportHour
}

// GetSupportHours lists existing support hours resources. https://api.ilert.com/api-docs/#tag/Support-Hours/paths/~1support-hours/get
func (c *Client) GetSupportHours(input *GetSupportHoursInput) (*GetSupportHoursOutput, error) {
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))
}

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

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

return &GetSupportHoursOutput{SupportHours: supportHours}, nil
}

// SearchSupportHourInput represents the input of a SearchSupportHour operation.
type SearchSupportHourInput struct {
_ struct{}
SupportHourName *string
}

// SearchSupportHourOutput represents the output of a SearchSupportHour operation.
type SearchSupportHourOutput struct {
_ struct{}
SupportHour *SupportHour
}

// SearchSupportHour gets the support hours resource with specified name.
func (c *Client) SearchSupportHour(input *SearchSupportHourInput) (*SearchSupportHourOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.SupportHourName == nil {
return nil, errors.New("support hour name is required")
}

resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/name/%s", apiRoutes.supportHours, *input.SupportHourName))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}

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

return &SearchSupportHourOutput{SupportHour: supportHour}, nil
}

// UpdateSupportHourInput represents the input of a UpdateSupportHour operation.
type UpdateSupportHourInput struct {
_ struct{}
SupportHourID *int64
SupportHour *SupportHour
}

// UpdateSupportHourOutput represents the output of a UpdateSupportHour operation.
type UpdateSupportHourOutput struct {
_ struct{}
SupportHour *SupportHour
}

// UpdateSupportHour updates an existing support hours resource. https://api.ilert.com/api-docs/#tag/Support-Hours/paths/~1support-hours~1{id}/put
func (c *Client) UpdateSupportHour(input *UpdateSupportHourInput) (*UpdateSupportHourOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.SupportHour == nil {
return nil, errors.New("support hour input is required")
}
if input.SupportHourID == nil {
return nil, errors.New("support hour id is required")
}

resp, err := c.httpClient.R().SetBody(input.SupportHour).Put(fmt.Sprintf("%s/%d", apiRoutes.supportHours, *input.SupportHourID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}

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

return &UpdateSupportHourOutput{SupportHour: supportHour}, nil
}

// DeleteSupportHourInput represents the input of a DeleteSupportHour operation.
type DeleteSupportHourInput struct {
_ struct{}
SupportHourID *int64
}

// DeleteSupportHourOutput represents the output of a DeleteSupportHour operation.
type DeleteSupportHourOutput struct {
_ struct{}
}

// DeleteSupportHour deletes the specified support hours resource. https://api.ilert.com/api-docs/#tag/Support-Hours/paths/~1support-hours~1{id}/delete
func (c *Client) DeleteSupportHour(input *DeleteSupportHourInput) (*DeleteSupportHourOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.SupportHourID == nil {
return nil, errors.New("support hour id is required")
}

resp, err := c.httpClient.R().Delete(fmt.Sprintf("%s/%d", apiRoutes.supportHours, *input.SupportHourID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 204); apiErr != nil {
return nil, apiErr
}

return &DeleteSupportHourOutput{}, nil
}
2 changes: 1 addition & 1 deletion team.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type CreateTeamOutput struct {
Team *Team
}

// CreateTeam creates a new team. https://api.ilert.com/api-docs/#tag/Teams/paths/~1teams/posts
// CreateTeam creates a new team. https://api.ilert.com/api-docs/#tag/Teams/paths/~1teams/post
func (c *Client) CreateTeam(input *CreateTeamInput) (*CreateTeamOutput, error) {
if input == nil {
return nil, errors.New("input is required")
Expand Down

0 comments on commit 0b1fd01

Please sign in to comment.