mirror of https://github.com/hashicorp/terraform
parent
b40ba1042f
commit
4c45c790c3
@ -1,172 +0,0 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
pagerduty "github.com/PagerDuty/go-pagerduty"
|
||||
"github.com/hashicorp/terraform/helper/hashcode"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
||||
func dataSourcePagerDutyOnCall() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Read: dataSourcePagerDutyOnCallRead,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"time_zone": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
"include": &schema.Schema{
|
||||
Type: schema.TypeList,
|
||||
Optional: true,
|
||||
Elem: &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
},
|
||||
},
|
||||
"user_ids": &schema.Schema{
|
||||
Type: schema.TypeList,
|
||||
Optional: true,
|
||||
Elem: &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
},
|
||||
},
|
||||
"escalation_policy_ids": &schema.Schema{
|
||||
Type: schema.TypeList,
|
||||
Optional: true,
|
||||
Elem: &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
},
|
||||
},
|
||||
"schedule_ids": &schema.Schema{
|
||||
Type: schema.TypeList,
|
||||
Optional: true,
|
||||
Elem: &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
},
|
||||
},
|
||||
"since": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
"until": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
"earliest": &schema.Schema{
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
},
|
||||
"oncalls": &schema.Schema{
|
||||
Type: schema.TypeList,
|
||||
Computed: true,
|
||||
Elem: &schema.Resource{
|
||||
Schema: map[string]*schema.Schema{
|
||||
"escalation_level": &schema.Schema{
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
},
|
||||
"start": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"end": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"user": &schema.Schema{
|
||||
Type: schema.TypeMap,
|
||||
Computed: true,
|
||||
},
|
||||
"schedule": &schema.Schema{
|
||||
Type: schema.TypeMap,
|
||||
Computed: true,
|
||||
},
|
||||
"escalation_policy": &schema.Schema{
|
||||
Type: schema.TypeMap,
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func dataSourcePagerDutyOnCallRead(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*pagerduty.Client)
|
||||
|
||||
o := &pagerduty.ListOnCallOptions{}
|
||||
|
||||
if attr, ok := d.GetOk("time_zone"); ok {
|
||||
o.TimeZone = attr.(string)
|
||||
}
|
||||
|
||||
if attr, ok := d.GetOk("include"); ok {
|
||||
includes := make([]string, 0, len(attr.([]interface{})))
|
||||
|
||||
for _, include := range attr.([]interface{}) {
|
||||
includes = append(includes, include.(string))
|
||||
}
|
||||
|
||||
o.Includes = includes
|
||||
}
|
||||
|
||||
if attr, ok := d.GetOk("user_ids"); ok {
|
||||
userIDs := make([]string, 0, len(attr.([]interface{})))
|
||||
|
||||
for _, user := range attr.([]interface{}) {
|
||||
userIDs = append(userIDs, user.(string))
|
||||
}
|
||||
|
||||
o.UserIDs = userIDs
|
||||
}
|
||||
|
||||
if attr, ok := d.GetOk("escalation_policy_ids"); ok {
|
||||
escalationPolicyIDs := make([]string, 0, len(attr.([]interface{})))
|
||||
|
||||
for _, escalationPolicy := range attr.([]interface{}) {
|
||||
escalationPolicyIDs = append(escalationPolicyIDs, escalationPolicy.(string))
|
||||
}
|
||||
|
||||
o.EscalationPolicyIDs = escalationPolicyIDs
|
||||
}
|
||||
|
||||
if attr, ok := d.GetOk("since"); ok {
|
||||
o.Since = attr.(string)
|
||||
}
|
||||
|
||||
if attr, ok := d.GetOk("until"); ok {
|
||||
o.Until = attr.(string)
|
||||
}
|
||||
|
||||
if attr, ok := d.GetOk("earliest"); ok {
|
||||
o.Earliest = attr.(bool)
|
||||
}
|
||||
|
||||
log.Printf("[INFO] Reading On Calls with options: %v", *o)
|
||||
|
||||
resp, err := client.ListOnCalls(*o)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data := flattenOnCalls(resp.OnCalls)
|
||||
id, err := json.Marshal(data)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.SetId(strconv.Itoa(hashcode.String(string(id))))
|
||||
|
||||
if err := d.Set("oncalls", data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
@ -1,58 +0,0 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
func TestAccPagerDutyOnCall_Basic(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckPagerDutyScheduleDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccPagerDutyOnCallsConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccPagerDutyOnCalls("data.pagerduty_on_call.foo"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccPagerDutyOnCalls(n string) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
|
||||
r := s.RootModule().Resources[n]
|
||||
a := r.Primary.Attributes
|
||||
|
||||
var size int
|
||||
var err error
|
||||
|
||||
if size, err = strconv.Atoi(a["oncalls.#"]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == 0 {
|
||||
return fmt.Errorf("Expected at least one on call in the list. Found: %d", size)
|
||||
}
|
||||
|
||||
for i := range make([]string, size) {
|
||||
escalationLevel := a[fmt.Sprintf("oncalls.%d.escalation_level", i)]
|
||||
if escalationLevel == "" {
|
||||
return fmt.Errorf("Expected the on call to have an escalation_level set")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
const testAccPagerDutyOnCallsConfig = `
|
||||
data "pagerduty_on_call" "foo" {}
|
||||
`
|
||||
@ -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
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
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"])
|
||||
}
|
||||
|
||||
if a["name"] != "Datadog" {
|
||||
return fmt.Errorf("Expected the Datadog Vendor Name to be: Datadog, but got: %s", a["name"])
|
||||
}
|
||||
|
||||
if a["type"] != "generic_events_api_inbound_integration" {
|
||||
return fmt.Errorf("Expected the Datadog Vendor Type to be: generic_events_api_inbound_integration, but got: %s", a["type"])
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
const testAccPagerDutyVendorsConfig = `
|
||||
data "pagerduty_vendor" "datadog" {
|
||||
name_regex = "Datadog"
|
||||
}
|
||||
`
|
||||
@ -0,0 +1,73 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
// Vendor represents a specific type of integration. AWS Cloudwatch, Splunk, Datadog, etc are all examples of vendors that can be integrated in PagerDuty by making an integration.
|
||||
type Vendor struct {
|
||||
APIObject
|
||||
Name string `json:"name,omitempty"`
|
||||
LogoURL string `json:"logo_url,omitempty"`
|
||||
LongName string `json:"long_name,omitempty"`
|
||||
WebsiteURL string `json:"website_url,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Connectable bool `json:"connectable,omitempty"`
|
||||
ThumbnailURL string `json:"thumbnail_url,omitempty"`
|
||||
GenericServiceType string `json:"generic_service_type,omitempty"`
|
||||
IntegrationGuideURL string `json:"integration_guide_url,omitempty"`
|
||||
}
|
||||
|
||||
// ListVendorResponse is the data structure returned from calling the ListVendors API endpoint.
|
||||
type ListVendorResponse struct {
|
||||
APIListObject
|
||||
Vendors []Vendor
|
||||
}
|
||||
|
||||
// ListVendorOptions is the data structure used when calling the ListVendors API endpoint.
|
||||
type ListVendorOptions struct {
|
||||
APIListObject
|
||||
}
|
||||
|
||||
// ListVendors lists existing vendors.
|
||||
func (c *Client) ListVendors(o ListVendorOptions) (*ListVendorResponse, error) {
|
||||
v, err := query.Values(o)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := c.get("/vendors?" + v.Encode())
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result ListVendorResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
||||
|
||||
// GetVendor gets details about an existing vendor.
|
||||
func (c *Client) GetVendor(id string) (*Vendor, error) {
|
||||
resp, err := c.get("/vendors/" + id)
|
||||
return getVendorFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
func getVendorFromResponse(c *Client, resp *http.Response, err error) (*Vendor, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var target map[string]Vendor
|
||||
if dErr := c.decodeJSON(resp, &target); dErr != nil {
|
||||
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
|
||||
}
|
||||
rootNode := "vendor"
|
||||
t, nodeOK := target[rootNode]
|
||||
if !nodeOK {
|
||||
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
|
||||
}
|
||||
return &t, nil
|
||||
}
|
||||
@ -1,59 +0,0 @@
|
||||
---
|
||||
layout: "pagerduty"
|
||||
page_title: "PagerDuty: pagerduty_on_call"
|
||||
sidebar_current: "docs-pagerduty-datasource-on_call"
|
||||
description: |-
|
||||
Get information about who's on call.
|
||||
---
|
||||
|
||||
# pagerduty\_on_call
|
||||
|
||||
Use this data source to get all of the users [on call][1] in a given schedule.
|
||||
|
||||
## Example Usage
|
||||
|
||||
```
|
||||
resource "pagerduty_schedule" "foo" {
|
||||
name = "Daily Engineering Rotation"
|
||||
time_zone = "America/New_York"
|
||||
|
||||
layer {
|
||||
name = "Night Shift"
|
||||
start = "2015-11-06T20:00:00-05:00"
|
||||
rotation_virtual_start = "2015-11-06T20:00:00-05:00"
|
||||
rotation_turn_length_seconds = 86400
|
||||
users = ["${pagerduty_user.foo.id}"]
|
||||
|
||||
restriction {
|
||||
type = "daily_restriction"
|
||||
start_time_of_day = "08:00:00"
|
||||
duration_seconds = 32400
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data "pagerduty_on_call" "on_call" {}
|
||||
|
||||
resource "pagerduty_team" "on_call" {
|
||||
name = "On call"
|
||||
description = "Primarily used by ${data.pagerduty_on_call.oncalls.0.id}"
|
||||
}
|
||||
```
|
||||
|
||||
## Argument Reference
|
||||
|
||||
The following arguments are supported:
|
||||
|
||||
* `time_zone` - (Optional) Time zone in which dates in the result will be rendered.
|
||||
* `include` - (Optional) List of of additional details to include. Can be `escalation_policies`, `users`, `schedules`.
|
||||
* `user_ids` - (Optional) Filters the results, showing only on-calls for the specified user IDs.
|
||||
* `escalation_policy_ids` - (Optional) Filters the results, showing only on-calls for the specified escalation policy IDs.
|
||||
* `user_ids` - (Optional) Filters the results, showing only on-calls for the specified schedule IDs.
|
||||
* `since` - (Optional) The start of the time range over which you want to search. If an on-call period overlaps with the range, it will be included in the result. Defaults to current time. The search range cannot exceed 3 months.
|
||||
* `until` - (Optional) The end of the time range over which you want to search. If an on-call period overlaps with the range, it will be included in the result. Defaults to current time. The search range cannot exceed 3 months, and the until time cannot be before the since time.
|
||||
* `earliest` - (Optional) This will filter on-calls such that only the earliest on-call for each combination of escalation policy, escalation level, and user is returned. This is useful for determining when the "next" on-calls are for a given set of filters.
|
||||
|
||||
## Attributes Reference
|
||||
* `oncalls` - A list of on-call entries during a given time range.
|
||||
|
||||
[1]: https://v2.developer.pagerduty.com/v2/page/api-reference#!/On-Calls/get_oncalls
|
||||
@ -0,0 +1,65 @@
|
||||
---
|
||||
layout: "pagerduty"
|
||||
page_title: "PagerDuty: pagerduty_vendor"
|
||||
sidebar_current: "docs-pagerduty-datasource-vendor"
|
||||
description: |-
|
||||
Get information about a vendor that you can use for a service integration (e.g Amazon Cloudwatch, Splunk, Datadog).
|
||||
---
|
||||
|
||||
# pagerduty\_vendor
|
||||
|
||||
Use this data source to get information about a specific [vendor][1] that you can use for a service integration (e.g Amazon Cloudwatch, Splunk, Datadog).
|
||||
|
||||
## Example Usage
|
||||
|
||||
```
|
||||
data "pagerduty_vendor" "datadog" {
|
||||
name_regex = "^Datadog$"
|
||||
}
|
||||
|
||||
resource "pagerduty_user" "example" {
|
||||
name = "Earline Greenholt"
|
||||
email = "125.greenholt.earline@graham.name"
|
||||
teams = ["${pagerduty_team.example.id}"]
|
||||
}
|
||||
|
||||
resource "pagerduty_escalation_policy" "foo" {
|
||||
name = "Engineering Escalation Policy"
|
||||
num_loops = 2
|
||||
|
||||
rule {
|
||||
escalation_delay_in_minutes = 10
|
||||
|
||||
target {
|
||||
type = "user"
|
||||
id = "${pagerduty_user.example.id}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "pagerduty_service" "example" {
|
||||
name = "My Web App"
|
||||
auto_resolve_timeout = 14400
|
||||
acknowledgement_timeout = 600
|
||||
escalation_policy = "${pagerduty_escalation_policy.example.id}"
|
||||
}
|
||||
|
||||
resource "pagerduty_service_integration" "example" {
|
||||
name = "Datadog Integration"
|
||||
vendor = "${data.pagerduty_vendor.datadog.id}"
|
||||
service = "${pagerduty_service.example.id}"
|
||||
type = "generic_events_api_inbound_integration"
|
||||
}
|
||||
```
|
||||
|
||||
## Argument Reference
|
||||
|
||||
The following arguments are supported:
|
||||
|
||||
* `name_regex` - (Required) A regex string to apply to the vendor list returned by the PagerDuty API. This regex should be very specific. If your regex matches several vendors a list of found vendors will be returned so you can tweak your regex further. The final regex string is made case insensitive.
|
||||
|
||||
## Attributes Reference
|
||||
* `name` - The short name of the found vendor.
|
||||
* `type` - The generic service type for this vendor.
|
||||
|
||||
[1]: https://v2.developer.pagerduty.com/v2/page/api-reference#!/Vendors/get_vendors
|
||||
Loading…
Reference in new issue