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

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Bloemberg committed Mar 6, 2019
1 parent 8281146 commit d981d8d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
39 changes: 39 additions & 0 deletions uptimerobot/api/alert_contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/url"
"strconv"
)

var alertContactType = map[string]int{
Expand Down Expand Up @@ -37,6 +38,44 @@ type AlertContact struct {
Status string
}

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, e := strconv.Atoi(alertcontact["id"].(string))
if e != nil {
err = e
return
}
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 int) (ac AlertContact, err error) {
ac.ID = id
data := url.Values{}
Expand Down
48 changes: 48 additions & 0 deletions uptimerobot/data_source_uptimerobot_alert_contact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package uptimerobot

import (
"fmt"

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

func dataSourceAlertContact() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlertContactRead,
Schema: map[string]*schema.Schema{
"friendly_name": {Required: true, Type: schema.TypeString},
"type": {Computed: true, Type: schema.TypeString},
"status": {Computed: true, Type: schema.TypeString},
"value": {Computed: 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 a.FriendlyName == friendlyName {
alertContact = a
break
}
}
if alertContact == (uptimerobotapi.AlertContact{}) {
return fmt.Errorf("Failed to find alert contact with name %s", friendlyName)
}

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

0 comments on commit d981d8d

Please sign in to comment.