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

Commit

Permalink
Allow default alert contact to be configured
Browse files Browse the repository at this point in the history
- Convert alert contact ID to string (it can be 0 padded number)
- Add alert contact datasource
- Extend example configuration with default alert contact
  • Loading branch information
Johan Bloemberg committed Apr 23, 2019
1 parent b7229be commit 5831159
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 38 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ provider "uptimerobot" {
api_key = "[YOUR MAIN API KEY]"
}
data "uptimerobot_account" "account" {}
data "uptimerobot_alert_contact" "default_alert_contact" {
friendly_name = "${data.uptimerobot_account.account.email}"
}
resource "uptimerobot_alert_contact" "slack" {
friendly_name = "Slack Alert"
type = "slack"
Expand All @@ -27,6 +33,10 @@ resource "uptimerobot_monitor" "main" {
# threshold = 0 # pro only
# recurrence = 0 # pro only
}
alert_contact {
id = "${data.uptimerobot_alert_contact.default_alert_contact.id}"
}
}
resource "uptimerobot_status_page" "main" {
Expand Down
50 changes: 42 additions & 8 deletions uptimerobot/api/alert_contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,51 @@ var alertContactStatus = map[string]int{
var AlertContactStatus = mapKeys(alertContactStatus)

type AlertContact struct {
ID int `json:"id"`
ID string `json:"id"`
FriendlyName string `json:"friendly_name"`
Value string `json:"value"`
Type string
Status string
}

func (client UptimeRobotApiClient) GetAlertContact(id int) (ac AlertContact, err error) {
func (client UptimeRobotApiClient) GetAlertContacts() (acs []AlertContact, err error) {
data := url.Values{}

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

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

for _, i := range alertcontacts {
alertcontact := i.(map[string]interface{})
id := alertcontact["id"].(string)
ac := AlertContact{
id,
alertcontact["friendly_name"].(string),
alertcontact["value"].(string),
intToString(alertContactType, int(alertcontact["type"].(float64))),
intToString(alertContactStatus, int(alertcontact["status"].(float64))),
}
acs = append(acs, ac)
}

return
}

func (client UptimeRobotApiClient) GetAlertContact(id string) (ac AlertContact, err error) {
ac.ID = id
data := url.Values{}
data.Add("alert_contacts", fmt.Sprintf("%d", id))
data.Add("alert_contacts", id)

body, err := client.MakeCall(
"getAlertContacts",
Expand Down Expand Up @@ -94,12 +128,12 @@ func (client UptimeRobotApiClient) CreateAlertContact(req AlertContactCreateRequ
return
}

return client.GetAlertContact(int(alertcontact["id"].(float64)))
return client.GetAlertContact(alertcontact["id"].(string))
}

func (client UptimeRobotApiClient) DeleteAlertContact(id int) (err error) {
func (client UptimeRobotApiClient) DeleteAlertContact(id string) (err error) {
data := url.Values{}
data.Add("id", fmt.Sprintf("%d", id))
data.Add("id", id)

_, err = client.MakeCall(
"deleteAlertContact",
Expand All @@ -112,14 +146,14 @@ func (client UptimeRobotApiClient) DeleteAlertContact(id int) (err error) {
}

type AlertContactUpdateRequest struct {
ID int
ID string
FriendlyName string
Value string
}

func (client UptimeRobotApiClient) UpdateAlertContact(req AlertContactUpdateRequest) (err error) {
data := url.Values{}
data.Add("id", fmt.Sprintf("%d", req.ID))
data.Add("id", req.ID)
data.Add("friendly_name", req.FriendlyName)
data.Add("value", req.Value)

Expand Down
51 changes: 51 additions & 0 deletions uptimerobot/data_source_uptimerobot_alert_contact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package uptimerobot

import (
"fmt"

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

func dataSourceAlertContact() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlertContactRead,
Schema: map[string]*schema.Schema{
"friendly_name": {Optional: true, Type: schema.TypeString},
"id": {Computed: true, Type: schema.TypeString},
"type": {Computed: true, Type: schema.TypeString},
"status": {Computed: true, Type: schema.TypeString},
"value": {Optional: true, Type: schema.TypeString},
},
}
}

func dataSourceAlertContactRead(d *schema.ResourceData, m interface{}) error {
alertContacts, err := m.(uptimerobotapi.UptimeRobotApiClient).GetAlertContacts()
if err != nil {
return err
}

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

var alertContact uptimerobotapi.AlertContact

for _, a := range alertContacts {
if friendlyName != "" && a.FriendlyName == friendlyName {
alertContact = a
break
}
}
if alertContact == (uptimerobotapi.AlertContact{}) {
return fmt.Errorf("Failed to find alert contact by name %s", friendlyName)
}

d.SetId(alertContact.ID)

d.Set("friendly_name", alertContact.FriendlyName)
d.Set("type", alertContact.Type)
d.Set("status", alertContact.Status)
d.Set("value", alertContact.Value)

return nil
}
3 changes: 2 additions & 1 deletion uptimerobot/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func Provider() terraform.ResourceProvider {
},
},
DataSourcesMap: map[string]*schema.Resource{
"uptimerobot_account": dataSourceAccount(),
"uptimerobot_account": dataSourceAccount(),
"uptimerobot_alert_contact": dataSourceAlertContact(),
},
ResourcesMap: map[string]*schema.Resource{
"uptimerobot_alert_contact": resourceAlertContact(),
Expand Down
26 changes: 7 additions & 19 deletions uptimerobot/resource_uptimerobot_alert_contact.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package uptimerobot

import (
"fmt"
"strconv"

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

func resourceAlertContact() *schema.Resource {
Expand Down Expand Up @@ -54,17 +51,14 @@ func resourceAlertContactCreate(d *schema.ResourceData, m interface{}) error {
return err
}

d.SetId(fmt.Sprintf("%d", ac.ID))
d.SetId(ac.ID)
updateAlertContactResource(d, ac)

return nil
}

func resourceAlertContactRead(d *schema.ResourceData, m interface{}) error {
id, err := strconv.Atoi(d.Id())
if err != nil {
return err
}
id := d.Id()

ac, err := m.(uptimerobotapi.UptimeRobotApiClient).GetAlertContact(id)
if err != nil {
Expand All @@ -77,12 +71,9 @@ func resourceAlertContactRead(d *schema.ResourceData, m interface{}) error {
}

func resourceAlertContactUpdate(d *schema.ResourceData, m interface{}) error {
id, err := strconv.Atoi(d.Id())
if err != nil {
return err
}
id := d.Id()

err = m.(uptimerobotapi.UptimeRobotApiClient).UpdateAlertContact(
err := m.(uptimerobotapi.UptimeRobotApiClient).UpdateAlertContact(
uptimerobotapi.AlertContactUpdateRequest{
ID: id,
FriendlyName: d.Get("friendly_name").(string),
Expand All @@ -96,12 +87,9 @@ func resourceAlertContactUpdate(d *schema.ResourceData, m interface{}) error {
}

func resourceAlertContactDelete(d *schema.ResourceData, m interface{}) error {
id, err := strconv.Atoi(d.Id())
if err != nil {
return err
}
id := d.Id()

err = m.(uptimerobotapi.UptimeRobotApiClient).DeleteAlertContact(id)
err := m.(uptimerobotapi.UptimeRobotApiClient).DeleteAlertContact(id)
if err != nil {
return err
}
Expand Down
10 changes: 3 additions & 7 deletions uptimerobot/resource_uptimerobot_alert_contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package uptimerobot

import (
"fmt"
"strconv"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/louy/terraform-provider-uptimerobot/uptimerobot/api"
uptimerobotapi "github.com/louy/terraform-provider-uptimerobot/uptimerobot/api"
)

func TestUptimeRobotDataResourceAlertContact_email(t *testing.T) {
Expand Down Expand Up @@ -127,12 +126,9 @@ func testAccCheckAlertContactDestroy(s *terraform.State) error {
continue
}

id, err := strconv.Atoi(rs.Primary.ID)
if err != nil {
return err
}
id := rs.Primary.ID

_, err = client.GetAlertContact(id)
_, err := client.GetAlertContact(id)

if err == nil {
return fmt.Errorf("Alert contact still exists")
Expand Down
6 changes: 3 additions & 3 deletions uptimerobot/resource_uptimerobot_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

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

func resourceMonitor() *schema.Resource {
Expand Down Expand Up @@ -81,7 +81,7 @@ func resourceMonitor() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Type: schema.TypeString,
Required: true,
},
"threshold": {
Expand Down Expand Up @@ -211,7 +211,7 @@ func resourceMonitorUpdate(d *schema.ResourceData, m interface{}) error {
req.AlertContacts = make([]uptimerobotapi.MonitorRequestAlertContact, len(d.Get("alert_contact").([]interface{})))
for k, v := range d.Get("alert_contact").([]interface{}) {
req.AlertContacts[k] = uptimerobotapi.MonitorRequestAlertContact{
ID: v.(map[string]interface{})["id"].(int),
ID: v.(map[string]interface{})["id"].(string),
}
}

Expand Down

0 comments on commit 5831159

Please sign in to comment.