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

Commit

Permalink
First working resource method
Browse files Browse the repository at this point in the history
  • Loading branch information
louy committed May 29, 2018
1 parent 6534e8d commit 31156aa
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/vendor/*/
!/vendor/vendor.json

/terraform-provider-uptime-robot
/terraform-provider-uptimerobot

*.tfvars
32 changes: 22 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,42 @@

```tf
provider "uptime-robot" {
provider "uptimerobot" {
api_key = "[YOUR MAIN API KEY]"
}
resource "uptime_robot_monitor" "main" {
title = "My Monitor"
type = "http"
endpoint = "http://example.com"
frequency = 300
resource "uptimerobot_alert_contact" "slack" {
friendly_name = "Slack Alert"
type = "slack"
webhook_url = "https://hooks.slack.com/services/XXXXXXX"
}
resource "uptime_robot_status_page" "main" {
title = "My Status Page"
resource "uptimerobot_monitor" "main" {
friendly_name = "My Monitor"
type = "http"
url = "http://example.com"
interval = 300
alert_contact {
id = "${resource.uptimerobot_alert_contact.slack.id}"
# threshold = 0
# recurrence = 0
}
}
resource "uptimerobot_status_page" "main" {
friendly_name = "My Status Page"
custom_domain = "status.example.com"
password = "WeAreAwsome"
sort_monitors = "down-up-paused"
monitors = ["${resource.uptime_robot_monitor.main.id}"]
monitors = ["${resource.uptimerobot_monitor.main.id}"]
hide_logo = false # pro only
}
resource "aws_route53_record" {
zone_id = "[MY ZONE ID]"
type = "CNAME"
records = ["${resource.uptime_robot_status_page.main.id}"]
records = ["${resource.uptimerobot_status_page.main.id}"]
}
```
51 changes: 51 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
)

func uptimerobotAPICall(
apiKey string,
endpoint string,
params string,
) (map[string]interface{}, error) {
url := "https://api.uptimerobot.com/v2/" + endpoint

payload := strings.NewReader(
fmt.Sprintf("api_key=%s&format=json&%s", apiKey, params),
)

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("cache-control", "no-cache")
req.Header.Add("content-type", "application/x-www-form-urlencoded")

res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}

fmt.Println(res)
fmt.Println(string(body))

var result map[string]interface{}
json.Unmarshal([]byte(body), &result)

if result["stat"] != "ok" {
message, _ := json.Marshal(result["error"])
return nil, errors.New("Got error from UptimeRobot: " + string(message))
}

return result, nil
}
21 changes: 20 additions & 1 deletion provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,27 @@ import (
"github.com/hashicorp/terraform/helper/schema"
)

type UptimeRobotConfig struct {
apiKey string
}

func Provider() *schema.Provider {
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{},
Schema: map[string]*schema.Schema{
"api_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
ResourcesMap: map[string]*schema.Resource{
"uptimerobot_alert_contact": resourceAlertContact(),
"uptimerobot_monitor": resourceMonitor(),
},
ConfigureFunc: func(r *schema.ResourceData) (interface{}, error) {
config := UptimeRobotConfig{
apiKey: r.Get("api_key").(string),
}
return config, nil
},
}
}
91 changes: 91 additions & 0 deletions resource_alert_contact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"fmt"
"net/url"

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

var alertContactTypes = map[string]int{
"sms": 1,
"email": 2,
"twitter-dm": 3,
"boxcar": 4,
"webhook": 5,
"pushbullet": 6,
"zapier": 7,
"pushover": 8,
"hipchat": 10,
"slack": 11,
}

func resourceAlertContact() *schema.Resource {
return &schema.Resource{
Create: resourceAlertContactCreate,
Read: resourceAlertContactRead,
Update: resourceAlertContactUpdate,
Delete: resourceAlertContactDelete,

Schema: map[string]*schema.Schema{
"friendly_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}

if _, ok := alertContactTypes[v]; !ok {
es = append(es, fmt.Errorf("unknown type for %s", v))
return
}

return
},
},
"value": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}

func resourceAlertContactCreate(d *schema.ResourceData, m interface{}) error {
data := url.Values{}
data.Add("friendly_name", d.Get("friendly_name").(string))
data.Add("type", fmt.Sprintf("%d", alertContactTypes[d.Get("type").(string)]))
data.Add("value", d.Get("value").(string))

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

func resourceAlertContactRead(d *schema.ResourceData, m interface{}) error {
return nil
}

func resourceAlertContactUpdate(d *schema.ResourceData, m interface{}) error {
return nil
}

func resourceAlertContactDelete(d *schema.ResourceData, m interface{}) error {
return nil
}
85 changes: 85 additions & 0 deletions resource_monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package main

import (
"github.com/hashicorp/terraform/helper/schema"
)

func resourceMonitor() *schema.Resource {
return &schema.Resource{
Create: resourceMonitorCreate,
Read: resourceMonitorRead,
Update: resourceMonitorUpdate,
Delete: resourceMonitorDelete,

Schema: map[string]*schema.Schema{
"friendly_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"url": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"sub_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
// required for port monitoring
},
"port": &schema.Schema{
Type: schema.TypeString,
Optional: true,
// required for port monitoring
},
"keyword_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
// required for keyword monitoring
},
"keyword_value": &schema.Schema{
Type: schema.TypeString,
Optional: true,
// required for keyword monitoring
},
"interval": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"http_username": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"http_password": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"alert_contacts": &schema.Schema{
// FIXME - array of id,threshold,recurrence
Type: schema.TypeString,
Optional: true,
},
// TODO - mwindows
// TODO - custom_http_headers
// TODO - ignore_ssl_errors
},
}
}

func resourceMonitorCreate(d *schema.ResourceData, m interface{}) error {
return nil
}

func resourceMonitorRead(d *schema.ResourceData, m interface{}) error {
return nil
}

func resourceMonitorUpdate(d *schema.ResourceData, m interface{}) error {
return nil
}

func resourceMonitorDelete(d *schema.ResourceData, m interface{}) error {
return nil
}
2 changes: 1 addition & 1 deletion vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -1107,5 +1107,5 @@
"revisionTime": "2018-05-29T16:06:35Z"
}
],
"rootPath": "github.com/louy/terraform-provider-uptime-robot"
"rootPath": "github.com/louy/terraform-provider-uptimerobot"
}

0 comments on commit 31156aa

Please sign in to comment.