Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

add custom_http_statuses option #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions uptimerobot/api/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ type MonitorAlertContact struct {
Threshold int `json:"threshold"`
}

type MonitorRequestCustomHTTPStatuses struct {
Up []int
Down []int
}

type Monitor struct {
ID int `json:"id"`
FriendlyName string `json:"friendly_name"`
Expand All @@ -73,13 +78,16 @@ type Monitor struct {

CustomHTTPHeaders map[string]string

CustomHTTPStatuses MonitorRequestCustomHTTPStatuses

AlertContacts []MonitorAlertContact
}

func (client UptimeRobotApiClient) GetMonitor(id int) (m Monitor, err error) {
data := url.Values{}
data.Add("monitors", fmt.Sprintf("%d", id))
data.Add("custom_http_headers", fmt.Sprintf("%d", 1))
data.Add("custom_http_statuses", fmt.Sprintf("%d", 1))
data.Add("alert_contacts", fmt.Sprintf("%d", 1))

body, err := client.MakeCall(
Expand Down Expand Up @@ -148,6 +156,21 @@ func (client UptimeRobotApiClient) GetMonitor(id int) (m Monitor, err error) {
}
m.CustomHTTPHeaders = customHTTPHeaders

if httpStatuses, ok := monitor["custom_http_statuses"].(map[string]interface{}); ok {
for _, up := range httpStatuses["up"].([]interface{}) {
v := int(up.(float64))
if v >= 400 {
m.CustomHTTPStatuses.Up = append(m.CustomHTTPStatuses.Up, v)
}
}
for _, down := range httpStatuses["down"].([]interface{}) {
v := int(down.(float64))
if v < 400 {
m.CustomHTTPStatuses.Down = append(m.CustomHTTPStatuses.Down, v)
}
}
}

if contacts := monitor["alert_contacts"].([]interface{}); contacts != nil {
m.AlertContacts = make([]MonitorAlertContact, len(contacts))
for k, v := range contacts {
Expand Down Expand Up @@ -187,6 +210,8 @@ type MonitorCreateRequest struct {
AlertContacts []MonitorRequestAlertContact

CustomHTTPHeaders map[string]string

CustomHTTPStatuses MonitorRequestCustomHTTPStatuses
}

func (client UptimeRobotApiClient) CreateMonitor(req MonitorCreateRequest) (m Monitor, err error) {
Expand Down Expand Up @@ -228,6 +253,20 @@ func (client UptimeRobotApiClient) CreateMonitor(req MonitorCreateRequest) (m Mo
}
}

// custom http statuses
if len(req.CustomHTTPStatuses.Up) != 0 || len(req.CustomHTTPStatuses.Down) != 0 {
statusStrings := make([]string, 0)
for _, v := range req.CustomHTTPStatuses.Down {
s := fmt.Sprintf("%d:0", v)
statusStrings = append(statusStrings, s)
}
for _, v := range req.CustomHTTPStatuses.Up {
s := fmt.Sprintf("%d:1", v)
statusStrings = append(statusStrings, s)
}
data.Add("custom_http_statuses", strings.Join(statusStrings, "_"))
}

body, err := client.MakeCall(
"newMonitor",
data.Encode(),
Expand Down Expand Up @@ -262,6 +301,8 @@ type MonitorUpdateRequest struct {
AlertContacts []MonitorRequestAlertContact

CustomHTTPHeaders map[string]string

CustomHTTPStatuses MonitorRequestCustomHTTPStatuses
}

func (client UptimeRobotApiClient) UpdateMonitor(req MonitorUpdateRequest) (m Monitor, err error) {
Expand Down Expand Up @@ -307,6 +348,22 @@ func (client UptimeRobotApiClient) UpdateMonitor(req MonitorUpdateRequest) (m Mo
data.Add("custom_http_headers", "{}")
}

// custom http statuses
if len(req.CustomHTTPStatuses.Up) != 0 || len(req.CustomHTTPStatuses.Down) != 0 {
statusStrings := make([]string, 0)
for _, v := range req.CustomHTTPStatuses.Down {
s := fmt.Sprintf("%d:0", v)
statusStrings = append(statusStrings, s)
}
for _, v := range req.CustomHTTPStatuses.Up {
s := fmt.Sprintf("%d:1", v)
statusStrings = append(statusStrings, s)
}
data.Add("custom_http_statuses", strings.Join(statusStrings, "_"))
} else {
data.Add("custom_http_statuses", "200:1")
}

_, err = client.MakeCall(
"editMonitor",
data.Encode(),
Expand Down
55 changes: 55 additions & 0 deletions uptimerobot/resource_uptimerobot_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ func resourceMonitor() *schema.Resource {
Type: schema.TypeMap,
Optional: true,
},
"custom_http_statuses": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"up": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"down": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
},
},
},
// TODO - mwindows
// TODO - ignore_ssl_errors
},
Expand Down Expand Up @@ -156,6 +178,17 @@ func resourceMonitorCreate(d *schema.ResourceData, m interface{}) error {
req.CustomHTTPHeaders[k] = v.(string)
}

// custom_http_statuses
for _, httpStatuses := range d.Get("custom_http_statuses").(*schema.Set).List() {
httpStatusesMap := httpStatuses.(map[string]interface{})
for _, up := range httpStatusesMap["up"].([]interface{}) {
req.CustomHTTPStatuses.Up = append(req.CustomHTTPStatuses.Up, up.(int))
}
for _, down := range httpStatusesMap["down"].([]interface{}) {
req.CustomHTTPStatuses.Down = append(req.CustomHTTPStatuses.Down, down.(int))
}
}

monitor, err := m.(uptimerobotapi.UptimeRobotApiClient).CreateMonitor(req)
if err != nil {
return err
Expand Down Expand Up @@ -235,6 +268,17 @@ func resourceMonitorUpdate(d *schema.ResourceData, m interface{}) error {
req.CustomHTTPHeaders[k] = v.(string)
}

// custom_http_statuses
for _, httpStatuses := range d.Get("custom_http_statuses").(*schema.Set).List() {
httpStatusesMap := httpStatuses.(map[string]interface{})
for _, up := range httpStatusesMap["up"].([]interface{}) {
req.CustomHTTPStatuses.Up = append(req.CustomHTTPStatuses.Up, up.(int))
}
for _, down := range httpStatusesMap["down"].([]interface{}) {
req.CustomHTTPStatuses.Down = append(req.CustomHTTPStatuses.Down, down.(int))
}
}

monitor, err := m.(uptimerobotapi.UptimeRobotApiClient).UpdateMonitor(req)
if err != nil {
return err
Expand Down Expand Up @@ -280,6 +324,17 @@ func updateMonitorResource(d *schema.ResourceData, m uptimerobotapi.Monitor) err
return fmt.Errorf("error setting custom_http_headers for resource %s: %s", d.Id(), err)
}

if len(m.CustomHTTPStatuses.Up) > 0 || len(m.CustomHTTPStatuses.Down) > 0 {
rawHTTPStatuses := make([]map[string]interface{}, 1)
rawHTTPStatuses[0] = map[string]interface{}{
"up": m.CustomHTTPStatuses.Up,
"down": m.CustomHTTPStatuses.Down,
}
if err := d.Set("custom_http_statuses", rawHTTPStatuses); err != nil {
return fmt.Errorf("error setting custom_http_statuses for resource %s: %s", d.Id(), err)
}
}

rawContacts := make([]map[string]interface{}, len(m.AlertContacts))
for k, v := range m.AlertContacts {
rawContacts[k] = map[string]interface{}{
Expand Down