Skip to content

Commit

Permalink
Add data source for monitors
Browse files Browse the repository at this point in the history
Fixes louy#91
  • Loading branch information
evansmurithi committed Oct 29, 2020
1 parent a1e7870 commit 306b3be
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
70 changes: 70 additions & 0 deletions uptimerobot/data_source_uptimerobot_monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package uptimerobot

import (
"fmt"
"reflect"

"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 {
monitors, err := m.(uptimerobotapi.UptimeRobotApiClient).GetMonitors()
if err != nil {
return err
}

friendlyName := d.Get("friendly_name").(string)

var monitor uptimerobotapi.Monitor

for _, m := range monitors {
if friendlyName != "" && m.FriendlyName == friendlyName {
monitor = m
break
}
}
if reflect.DeepEqual(monitor, uptimerobotapi.Monitor{}) {
return fmt.Errorf("Failed to find monitor by name %s", friendlyName)
}

d.SetId(fmt.Sprintf("%d", monitor.ID))
if err := updateMonitorResource(d, monitor); err != nil {
return err
}

return nil
}
3 changes: 2 additions & 1 deletion uptimerobot/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package uptimerobot
import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/louy/terraform-provider-uptimerobot/uptimerobot/api"
uptimerobotapi "github.com/louy/terraform-provider-uptimerobot/uptimerobot/api"
)

// Provider returns a terraform.ResourceProvider.
Expand All @@ -28,6 +28,7 @@ func Provider() terraform.ResourceProvider {
DataSourcesMap: map[string]*schema.Resource{
"uptimerobot_account": dataSourceAccount(),
"uptimerobot_alert_contact": dataSourceAlertContact(),
"uptimerobot_monitor": dataSourceMonitor(),
},
ResourcesMap: map[string]*schema.Resource{
"uptimerobot_alert_contact": resourceAlertContact(),
Expand Down

0 comments on commit 306b3be

Please sign in to comment.