From 6e0900d782f4b24b3c42e4e4479b6181011848bd Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Tue, 20 Oct 2020 14:54:07 +0900 Subject: [PATCH] add custom_http_statuses option --- uptimerobot/api/monitor.go | 57 +++++++++++++++++++++ uptimerobot/resource_uptimerobot_monitor.go | 55 ++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/uptimerobot/api/monitor.go b/uptimerobot/api/monitor.go index e70c5f2..16c2d39 100644 --- a/uptimerobot/api/monitor.go +++ b/uptimerobot/api/monitor.go @@ -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"` @@ -73,6 +78,8 @@ type Monitor struct { CustomHTTPHeaders map[string]string + CustomHTTPStatuses MonitorRequestCustomHTTPStatuses + AlertContacts []MonitorAlertContact } @@ -80,6 +87,7 @@ 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( @@ -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 { @@ -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) { @@ -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(), @@ -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) { @@ -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(), diff --git a/uptimerobot/resource_uptimerobot_monitor.go b/uptimerobot/resource_uptimerobot_monitor.go index 883aaae..e41a4cf 100644 --- a/uptimerobot/resource_uptimerobot_monitor.go +++ b/uptimerobot/resource_uptimerobot_monitor.go @@ -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 }, @@ -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 @@ -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 @@ -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{}{