Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.
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
53 changes: 53 additions & 0 deletions uptimerobot/api/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,59 @@ type Monitor struct {
AlertContacts []MonitorAlertContact
}

func (client UptimeRobotApiClient) GetMonitorIDs(friendlyName string) (ids []int, err error) {
data := url.Values{}
data.Add("custom_http_headers", fmt.Sprintf("%d", 1))
data.Add("alert_contacts", fmt.Sprintf("%d", 1))
data.Add("search", friendlyName)

maxMonitorRecords := 50
data.Add("limit", fmt.Sprintf("%d", maxMonitorRecords))

offset := 0
data.Add("offset", fmt.Sprintf("%d", offset))

var total int

for {
body, err := client.MakeCall(
"getMonitors",
data.Encode(),
)
if err != nil {
return nil, err
}

monitors, ok := body["monitors"].([]interface{})
if !ok {
j, _ := json.Marshal(body)
err = errors.New("Unknown response from the server: " + string(j))
return nil, err
}

for _, i := range monitors {
monitor := i.(map[string]interface{})
monitorID := int(monitor["id"].(float64))
monitorFriendlyName := monitor["friendly_name"].(string)

if monitorFriendlyName == friendlyName {
ids = append(ids, monitorID)
}
}

total = int(body["pagination"].(map[string]interface{})["total"].(float64))
offset += maxMonitorRecords
if offset < total {
// get monitors for next page
data.Set("offset", fmt.Sprintf("%d", offset))
} else {
break
}
}

return
}

func (client UptimeRobotApiClient) GetMonitor(id int) (m Monitor, err error) {
data := url.Values{}
data.Add("monitors", fmt.Sprintf("%d", id))
Expand Down
69 changes: 69 additions & 0 deletions uptimerobot/data_source_uptimerobot_monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package uptimerobot

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
uptimerobotapi "github.com/louy/terraform-provider-uptimerobot/uptimerobot/api"
)

func dataSourceMonitor() *schema.Resource {
return &schema.Resource{
Read: dataSourceMonitorRead,
Schema: map[string]*schema.Schema{
"id": {Type: schema.TypeInt, Computed: true},
"friendly_name": {Type: schema.TypeString, Required: true},
"url": {Type: schema.TypeString, Computed: true},
"type": {Type: schema.TypeString, Computed: true},
"status": {Type: schema.TypeString, Computed: true},
"interval": {Type: schema.TypeInt, Computed: true},
"sub_type": {Type: schema.TypeString, Computed: true},
"port": {Type: schema.TypeInt, Computed: true},
"keyword_type": {Type: schema.TypeString, Computed: true},
"keyword_value": {Type: schema.TypeString, Computed: true},
"http_username": {Type: schema.TypeString, Computed: true},
"http_password": {Type: schema.TypeString, Computed: true},
"http_auth_type": {Type: schema.TypeString, Computed: true},
"custom_http_headers": {Type: schema.TypeMap, Computed: true},
"alert_contact": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {Type: schema.TypeString, Computed: true},
"threshold": {Type: schema.TypeInt, Computed: true},
"recurrence": {Type: schema.TypeInt, Computed: true},
},
},
},
},
}
}

func dataSourceMonitorRead(d *schema.ResourceData, m interface{}) error {
friendlyName := d.Get("friendly_name").(string)

ids, err := m.(uptimerobotapi.UptimeRobotApiClient).GetMonitorIDs(friendlyName)
if err != nil {
return err
}

if len(ids) < 1 {
return fmt.Errorf("Failed to find monitor by name %s", friendlyName)
}

if len(ids) > 1 {
return fmt.Errorf("More than one monitor with name %s exists", friendlyName)
}

monitor, err := m.(uptimerobotapi.UptimeRobotApiClient).GetMonitor(ids[0])
if err != nil {
return err
}
d.SetId(fmt.Sprintf("%d", monitor.ID))
if err := updateMonitorResource(d, monitor); err != nil {
return err
}

return nil
}
Loading