Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Leverage changed go-pingdom mod features for teams/contacts
Browse files Browse the repository at this point in the history
Relies on the v1.3 version of the module, and the newly revamped interfaces for teams and contacts
  • Loading branch information
joshsouza committed Oct 13, 2020
1 parent 1ff2281 commit 5464ce0
Show file tree
Hide file tree
Showing 8 changed files with 424 additions and 356 deletions.
63 changes: 35 additions & 28 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,52 @@ resource "pingdom_team" "test_one" {
}

provider "pingdom" {
user = "${var.pingdom_user}"
password = "${var.pingdom_password}"
api_key = "${var.pingdom_api_key}"
api_token = "${var.pingdom_api_token}"
}

resource "pingdom_team" "test" {
name = "Team for testing"
}
name = "Team 2 (updated by Terraform) with contacts"

resource "pingdom_user" "first_user" {
username = "johndoe"
member_ids = [pingdom_contact.first_contact.id, pingdom_contact.second_contact.id]
}

resource "pingdom_contact" "first_user_contact_email_1" {
user_id = "${pingdom_user.first_user.id}"
email = "john@doe.com"
severity_level = "HIGH"
}
resource "pingdom_contact" "first_contact" {
name = "johndoe"

resource "pingdom_contact" "first_user_contact_email_2" {
user_id = "${pingdom_user.first_user.id}"
email = "john.doe@doe.com"
severity_level = "LOW"
sms_notification {
number = "5555555555"
severity = "HIGH"
}
sms_notification {
number = "2222222222"
severity = "LOW"
}
}

resource "pingdom_contact" "first_user_contact_sms_1" {
user_id = "${pingdom_user.first_user.id}"
number = "700000000"
country_code = "33"
phone_provider = "nexmo"
severity_level = "HIGH"
resource "pingdom_contact" "second_contact" {
name = "janedoe"
paused = true

sms_notification {
number = "5555555555"
severity = "HIGH"
}
sms_notification {
number = "3333333333"
country_code = "91"
severity = "LOW"
provider = "esendex"
}
email_notification {
address = "test@test.com"
severity = "LOW"
}
}

resource "pingdom_user" "second_user" {
username = "janedoe"
data "pingdom_contact" "data_contact" {
name = "janedoe"
}

resource "pingdom_contact" "second_user_contact_email_1" {
user_id = "${pingdom_user.second_user.id}"
email = "jane@doe.com"
severity_level = "high"
output "test" {
value = data.pingdom_contact.data_contact
}
8 changes: 2 additions & 6 deletions examples/variables.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
variable "pingdom_user" {}

variable "pingdom_password" {}

variable "pingdom_api_key" {
description = "The API key to used"
variable "pingdom_api_token" {
description = "The API key to use"
}
163 changes: 163 additions & 0 deletions pingdom/data_source_pingdom_contact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package pingdom

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/russellcardullo/go-pingdom/pingdom"
)

func dataSourcePingdomContact() *schema.Resource {
return &schema.Resource{
Read: dataSourcePingdomContactRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"paused": {
Type: schema.TypeBool,
Computed: true,
},
"teams": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"sms_notification": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"number": {
Type: schema.TypeString,
Required: true,
},
"country_code": {
Type: schema.TypeString,
Optional: true,
Default: "1",
},
"severity": {
Type: schema.TypeString,
Required: true,
},
"provider": {
Type: schema.TypeString,
Optional: true,
Default: "nexmo",
},
},
},
},
"email_notification": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address": {
Type: schema.TypeString,
Required: true,
},
"severity": {
Type: schema.TypeString,
Required: true,
},
},
},
},
// If Pingdom re-adds this to their API, we can uncomment
// "apns_notification": {
// Type: schema.TypeSet,
// Optional: true,
// Elem: &schema.Resource{
// Schema: map[string]*schema.Schema{
// "device": {
// Type: schema.TypeString,
// Required: true,
// },
// "name": {
// Type: schema.TypeString,
// Required: true,
// },
// "severity": {
// Type: schema.TypeString,
// Required: true,
// },
// },
// },
// },
// "agcm_notification": {
// Type: schema.TypeSet,
// Optional: true,
// Elem: &schema.Resource{
// Schema: map[string]*schema.Schema{
// "agcmid": {
// Type: schema.TypeString,
// Required: true,
// },
// "severity": {
// Type: schema.TypeString,
// Required: true,
// },
// },
// },
// },
},
}
}

func dataSourcePingdomContactRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pingdom.Client)
name := d.Get("name").(string)
contacts, err := client.Contacts.List()
log.Printf("==== contacts : %v", contacts)
if err != nil {
return fmt.Errorf("Error retrieving contact: %s", err)
}
var found *pingdom.Contact
for _, contact := range contacts {
if contact.Name == name {
log.Printf("Contact: %v", contact)
found = &contact
}
}
if found == nil {
return fmt.Errorf("User '%s' not found", name)
}

if err = d.Set("name", found.Name); err != nil {
return fmt.Errorf("Error setting name: %s", err)
}

teams := []map[string]interface{}{}
for _, team := range found.Teams {
teams = append(teams, map[string]interface{}{
"id": team.ID,
"name": team.Name,
})
}
if err = d.Set("teams", teams); err != nil {
return err
}

if err = updateResourceFromContactResponse(d, found); err != nil {
return err
}

d.SetId(fmt.Sprintf("%d", found.ID))
return nil
}
45 changes: 0 additions & 45 deletions pingdom/data_source_pingdom_user.go

This file was deleted.

3 changes: 1 addition & 2 deletions pingdom/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ func Provider() terraform.ResourceProvider {
ResourcesMap: map[string]*schema.Resource{
"pingdom_check": resourcePingdomCheck(),
"pingdom_team": resourcePingdomTeam(),
"pingdom_user": resourcePingdomUser(),
"pingdom_contact": resourcePingdomContact(),
},
DataSourcesMap: map[string]*schema.Resource{
"pingdom_user": dataSourcePingdomUser(),
"pingdom_contact": dataSourcePingdomContact(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
Loading

0 comments on commit 5464ce0

Please sign in to comment.