Skip to content

Commit

Permalink
Add support for teams in escalation policies & vendor support
Browse files Browse the repository at this point in the history
  • Loading branch information
heimweh committed Oct 24, 2016
1 parent 562e3b9 commit c41457b
Show file tree
Hide file tree
Showing 23 changed files with 661 additions and 469 deletions.
172 changes: 0 additions & 172 deletions builtin/providers/pagerduty/data_source_on_call.go

This file was deleted.

58 changes: 0 additions & 58 deletions builtin/providers/pagerduty/data_source_on_call_test.go

This file was deleted.

78 changes: 78 additions & 0 deletions builtin/providers/pagerduty/data_source_pagerduty_vendor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package pagerduty

import (
"fmt"
"log"
"regexp"

pagerduty "github.com/PagerDuty/go-pagerduty"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourcePagerDutyVendor() *schema.Resource {
return &schema.Resource{
Read: dataSourcePagerDutyVendorRead,

Schema: map[string]*schema.Schema{
"name_regex": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourcePagerDutyVendorRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

log.Printf("[INFO] Reading PagerDuty vendors")

resp, err := getVendors(client)

if err != nil {
return err
}

r := regexp.MustCompile("(?i)" + d.Get("name_regex").(string))

var vendors []pagerduty.Vendor
var vendorNames []string

for _, v := range resp {
if r.MatchString(v.Name) {
vendors = append(vendors, v)
vendorNames = append(vendorNames, v.Name)
}
}

if len(vendors) == 0 {
return fmt.Errorf("Unable to locate any vendor using the regex string: %s", r.String())
} else if len(vendors) > 1 {
return fmt.Errorf("Your query returned more than one result using the regex string: %#v. Found vendors: %#v", r.String(), vendorNames)
}

vendor := vendors[0]

genericServiceType := vendor.GenericServiceType

switch {
case genericServiceType == "email":
genericServiceType = "generic_email_inbound_integration"
case genericServiceType == "api":
genericServiceType = "generic_events_api_inbound_integration"
}

d.SetId(vendor.ID)
d.Set("name", vendor.Name)
d.Set("type", genericServiceType)

return nil
}
49 changes: 49 additions & 0 deletions builtin/providers/pagerduty/data_source_pagerduty_vendor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package pagerduty

import (
"fmt"
"testing"

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

func TestAccPagerDutyVendor_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyScheduleDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccPagerDutyVendorsConfig,
Check: resource.ComposeTestCheckFunc(
testAccPagerDutyVendors("data.pagerduty_vendor.datadog"),
),
},
},
})
}

func testAccPagerDutyVendors(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {

r := s.RootModule().Resources[n]
a := r.Primary.Attributes

if a["id"] == "" {
return fmt.Errorf("Expected to get a vendor ID from PagerDuty")
}

if a["id"] != "PAM4FGS" {
return fmt.Errorf("Expected the Datadog Vendor ID to be: PAM4FGS, but got: %s", a["id"])
}

return nil
}
}

const testAccPagerDutyVendorsConfig = `
data "pagerduty_vendor" "datadog" {
name_regex = "Datadog"
}
`
1 change: 1 addition & 0 deletions builtin/providers/pagerduty/import_pagerduty_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

func TestAccPagerDutyTeam_import(t *testing.T) {
resourceName := "pagerduty_team.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand Down
7 changes: 2 additions & 5 deletions builtin/providers/pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"pagerduty_on_call": dataSourcePagerDutyOnCall(),
"pagerduty_vendor": dataSourcePagerDutyVendor(),
},

ResourcesMap: map[string]*schema.Resource{
Expand All @@ -36,10 +36,7 @@ func Provider() terraform.ResourceProvider {
}

func providerConfigure(data *schema.ResourceData) (interface{}, error) {
config := Config{
Token: data.Get("token").(string),
}

config := Config{Token: data.Get("token").(string)}
log.Println("[INFO] Initializing PagerDuty client")
return config.Client()
}
Loading

0 comments on commit c41457b

Please sign in to comment.