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

Commit

Permalink
Merge pull request #20 from leeif/custom_http_headers
Browse files Browse the repository at this point in the history
Add custom_http_headers feature
  • Loading branch information
louy authored Apr 15, 2019
2 parents 0ebe6da + 67c05a0 commit f375e3f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
33 changes: 33 additions & 0 deletions uptimerobot/api/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ type Monitor struct {

HTTPUsername string `json:"http_username"`
HTTPPassword string `json:"http_password"`

CustomHTTPHeaders map[string]string
}

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

body, err := client.MakeCall(
"getMonitors",
Expand Down Expand Up @@ -115,6 +119,12 @@ func (client UptimeRobotApiClient) GetMonitor(id int) (m Monitor, err error) {
break
}

customHTTPHeaders := make(map[string]string)
for k, v := range monitor["custom_http_headers"].(map[string]interface{}) {
customHTTPHeaders[k] = v.(string)
}
m.CustomHTTPHeaders = customHTTPHeaders

return
}

Expand All @@ -139,6 +149,8 @@ type MonitorCreateRequest struct {
HTTPPassword string

AlertContacts []MonitorRequestAlertContact

CustomHTTPHeaders map[string]string
}

func (client UptimeRobotApiClient) CreateMonitor(req MonitorCreateRequest) (m Monitor, err error) {
Expand Down Expand Up @@ -170,6 +182,14 @@ func (client UptimeRobotApiClient) CreateMonitor(req MonitorCreateRequest) (m Mo
}
data.Add("alert_contacts", strings.Join(acStrings, "-"))

// custom http headers
if len(req.CustomHTTPHeaders) > 0 {
jsonData, err := json.Marshal(req.CustomHTTPHeaders)
if err == nil {
data.Add("custom_http_headers", string(jsonData))
}
}

body, err := client.MakeCall(
"newMonitor",
data.Encode(),
Expand Down Expand Up @@ -201,6 +221,8 @@ type MonitorUpdateRequest struct {
HTTPPassword string

AlertContacts []MonitorRequestAlertContact

CustomHTTPHeaders map[string]string
}

func (client UptimeRobotApiClient) UpdateMonitor(req MonitorUpdateRequest) (m Monitor, err error) {
Expand Down Expand Up @@ -233,6 +255,17 @@ func (client UptimeRobotApiClient) UpdateMonitor(req MonitorUpdateRequest) (m Mo
}
data.Add("alert_contacts", strings.Join(acStrings, "-"))

// custom http headers
if len(req.CustomHTTPHeaders) > 0 {
jsonData, err := json.Marshal(req.CustomHTTPHeaders)
if err == nil {
data.Add("custom_http_headers", string(jsonData))
}
} else {
//delete custom http headers when it is empty
data.Add("custom_http_headers", "{}")
}

_, err = client.MakeCall(
"editMonitor",
data.Encode(),
Expand Down
21 changes: 20 additions & 1 deletion uptimerobot/resource_uptimerobot_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ func resourceMonitor() *schema.Resource {
},
},
},
"custom_http_headers": {
Type: schema.TypeMap,
Optional: true,
},
// TODO - mwindows
// TODO - custom_http_headers
// TODO - ignore_ssl_errors
},
}
Expand Down Expand Up @@ -139,6 +142,13 @@ func resourceMonitorCreate(d *schema.ResourceData, m interface{}) error {
}
}

// custom_http_headers
httpHeaderMap := d.Get("custom_http_headers").(map[string]interface{})
req.CustomHTTPHeaders = make(map[string]string, len(httpHeaderMap))
for k, v := range httpHeaderMap {
req.CustomHTTPHeaders[k] = v.(string)
}

monitor, err := m.(uptimerobotapi.UptimeRobotApiClient).CreateMonitor(req)
if err != nil {
return err
Expand Down Expand Up @@ -205,6 +215,13 @@ func resourceMonitorUpdate(d *schema.ResourceData, m interface{}) error {
}
}

// custom_http_headers
httpHeaderMap := d.Get("custom_http_headers").(map[string]interface{})
req.CustomHTTPHeaders = make(map[string]string, len(httpHeaderMap))
for k, v := range httpHeaderMap {
req.CustomHTTPHeaders[k] = v.(string)
}

monitor, err := m.(uptimerobotapi.UptimeRobotApiClient).UpdateMonitor(req)
if err != nil {
return err
Expand Down Expand Up @@ -242,4 +259,6 @@ func updateMonitorResource(d *schema.ResourceData, m uptimerobotapi.Monitor) {

d.Set("http_username", m.HTTPUsername)
d.Set("http_password", m.HTTPPassword)

d.Set("custom_http_headers", m.CustomHTTPHeaders)
}
36 changes: 36 additions & 0 deletions uptimerobot/resource_uptimerobot_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,42 @@ func TestUptimeRobotDataResourceMonitor_custom_alert_contact_threshold_and_recur
})
}

func TestUptimeRobotDataResourceMonitor_custom_http_headers(t *testing.T) {
var FriendlyName = "TF Test: custom http headers"
var Type = "http"
var URL = "https://google.com"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMonitorDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(`
resource "uptimerobot_monitor" "test" {
friendly_name = "%s"
type = "%s"
url = "%s"
custom_http_headers {
// Accept-Language = "en"
}
}
`, FriendlyName, Type, URL),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("uptimerobot_monitor.test", "friendly_name", FriendlyName),
resource.TestCheckResourceAttr("uptimerobot_monitor.test", "type", Type),
resource.TestCheckResourceAttr("uptimerobot_monitor.test", "url", URL),
resource.TestCheckResourceAttr("uptimerobot_monitor.test", "custom_http_headers.%", "0"),
),
},
resource.TestStep{
ResourceName: "uptimerobot_monitor.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestUptimeRobotDataResourceMonitor_change_url(t *testing.T) {
var FriendlyName = "TF Test: http monitor"
var Type = "http"
Expand Down

0 comments on commit f375e3f

Please sign in to comment.