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

Commit

Permalink
Add support for status pages
Browse files Browse the repository at this point in the history
  • Loading branch information
louy committed May 29, 2018
1 parent 062f73e commit 064db94
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
1 change: 1 addition & 0 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func Provider() *schema.Provider {
ResourcesMap: map[string]*schema.Resource{
"uptimerobot_alert_contact": resourceAlertContact(),
"uptimerobot_monitor": resourceMonitor(),
"uptimerobot_status_page": resourceStatusPage(),
},
ConfigureFunc: func(r *schema.ResourceData) (interface{}, error) {
config := UptimeRobotConfig{
Expand Down
173 changes: 173 additions & 0 deletions resource_uptimerobot_status_page.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

var statusPageStatus = map[string]int{
"paused": 0,
"active": 1,
}
var statusPageSort = map[string]int{
"a-z": 1,
"z-a": 2,
"up-down-paused": 3,
"down-up-paused": 4,
}

func resourceStatusPage() *schema.Resource {
return &schema.Resource{
Create: resourceStatusPageCreate,
Read: resourceStatusPageRead,
Update: resourceStatusPageUpdate,
Delete: resourceStatusPageDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"friendly_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"custom_domain": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "",
},
"password": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"sort": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(mapKeys(statusPageSort), false),
Default: mapKeys(monitorTypes)[0],
},
"status": &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(mapKeys(statusPageStatus), false),
Optional: true,
Default: "active",
},
"monitors": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
PromoteSingle: true,
},
},
}
}

func resourceStatusPageCreate(d *schema.ResourceData, m interface{}) error {
data := url.Values{}
data.Add("type", fmt.Sprintf("%d", 1))
data.Add("friendly_name", d.Get("friendly_name").(string))
data.Add("custom_domain", d.Get("custom_domain").(string))
data.Add("password", d.Get("password").(string))
if len(d.Get("monitors").([]string)) == 0 {
data.Add("monitors", "0")
} else {
data.Add("monitors", strings.Join(d.Get("monitors").([]string), "-"))
}
data.Add("sort", fmt.Sprintf("%d", statusPageSort[d.Get("sort").(string)]))
data.Add("status", fmt.Sprintf("%d", statusPageStatus[d.Get("status").(string)]))

body, err := uptimerobotAPICall(
m.(UptimeRobotConfig).apiKey,
"newPSP",
data.Encode(),
)
if err != nil {
return err
}
psp := body["psp"].(map[string]interface{})
d.SetId(fmt.Sprintf("%d", int(psp["id"].(float64))))
d.Set("status", "active")
return nil
}

func resourceStatusPageRead(d *schema.ResourceData, m interface{}) error {
data := url.Values{}
data.Add("psps", d.Id())

body, err := uptimerobotAPICall(
m.(UptimeRobotConfig).apiKey,
"getPSPs",
data.Encode(),
)
if err != nil {
return err
}

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

psp := psps[0].(map[string]interface{})

d.Set("friendly_name", psp["friendly_name"].(string))
d.Set("standard_url", psp["standard_url"].(string))
d.Set("custom_url", psp["custom_url"].(string))
d.Set("sort", intToString(statusPageSort, psp["sort"].(int)))
d.Set("status", intToString(statusPageStatus, psp["status"].(int)))

return nil
}

func resourceStatusPageUpdate(d *schema.ResourceData, m interface{}) error {
data := url.Values{}
data.Add("id", d.Id())
data.Add("type", fmt.Sprintf("%d", 1))
data.Add("friendly_name", d.Get("friendly_name").(string))
data.Add("custom_domain", d.Get("custom_domain").(string))
data.Add("password", d.Get("password").(string))
if len(d.Get("monitors").([]string)) == 0 {
data.Add("monitors", "0")
} else {
data.Add("monitors", strings.Join(d.Get("monitors").([]string), "-"))
}
data.Add("sort", fmt.Sprintf("%d", statusPageSort[d.Get("sort").(string)]))
data.Add("status", fmt.Sprintf("%d", statusPageStatus[d.Get("status").(string)]))

_, err := uptimerobotAPICall(
m.(UptimeRobotConfig).apiKey,
"editPSP",
data.Encode(),
)
if err != nil {
return err
}

return nil
}

func resourceStatusPageDelete(d *schema.ResourceData, m interface{}) error {
data := url.Values{}
data.Add("id", d.Id())

_, err := uptimerobotAPICall(
m.(UptimeRobotConfig).apiKey,
"deletePSP",
data.Encode(),
)
if err != nil {
return err
}

return nil
}

0 comments on commit 064db94

Please sign in to comment.