forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/pagerduty: pagerduty_schedule data source (hashicorp#11614)
* Add data source * Add tests * Add documentation * Remove unnecessary id from schema
- Loading branch information
1 parent
7826081
commit fd92df3
Showing
5 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
builtin/providers/pagerduty/data_source_pagerduty_schedule.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package pagerduty | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
pagerduty "github.com/PagerDuty/go-pagerduty" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourcePagerDutySchedule() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourcePagerDutyScheduleRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourcePagerDutyScheduleRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*pagerduty.Client) | ||
|
||
log.Printf("[INFO] Reading PagerDuty schedule") | ||
|
||
searchName := d.Get("name").(string) | ||
|
||
o := &pagerduty.ListSchedulesOptions{ | ||
Query: searchName, | ||
} | ||
|
||
resp, err := client.ListSchedules(*o) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var found *pagerduty.Schedule | ||
|
||
for _, schedule := range resp.Schedules { | ||
if schedule.Name == searchName { | ||
found = &schedule | ||
break | ||
} | ||
} | ||
|
||
if found == nil { | ||
return fmt.Errorf("Unable to locate any schedule with the name: %s", searchName) | ||
} | ||
|
||
d.SetId(found.ID) | ||
d.Set("name", found.Name) | ||
|
||
return nil | ||
} |
85 changes: 85 additions & 0 deletions
85
builtin/providers/pagerduty/data_source_pagerduty_schedule_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package pagerduty | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccDataSourcePagerDutySchedule_Basic(t *testing.T) { | ||
rName := acctest.RandString(5) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccDataSourcePagerDutyScheduleConfig(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccDataSourcePagerDutySchedule("pagerduty_schedule.test", "data.pagerduty_schedule.by_name"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourcePagerDutySchedule(src, n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
|
||
srcR := s.RootModule().Resources[src] | ||
srcA := srcR.Primary.Attributes | ||
|
||
r := s.RootModule().Resources[n] | ||
a := r.Primary.Attributes | ||
|
||
if a["id"] == "" { | ||
return fmt.Errorf("Expected to get a schedule ID from PagerDuty") | ||
} | ||
|
||
testAtts := []string{"id", "name"} | ||
|
||
for _, att := range testAtts { | ||
if a[att] != srcA[att] { | ||
return fmt.Errorf("Expected the schedule %s to be: %s, but got: %s", att, srcA[att], a[att]) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccDataSourcePagerDutyScheduleConfig(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "pagerduty_user" "test" { | ||
name = "TF User %[1]s" | ||
email = "tf.%[1]s@example.com" | ||
} | ||
resource "pagerduty_schedule" "test" { | ||
name = "TF Schedule %[1]s" | ||
time_zone = "America/New_York" | ||
layer { | ||
name = "foo" | ||
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.test.id}"] | ||
restriction { | ||
type = "weekly_restriction" | ||
start_time_of_day = "08:00:00" | ||
start_day_of_week = 5 | ||
duration_seconds = 32101 | ||
} | ||
} | ||
} | ||
data "pagerduty_schedule" "by_name" { | ||
name = "${pagerduty_schedule.test.name}" | ||
} | ||
`, rName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
website/source/docs/providers/pagerduty/d/schedule.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
layout: "pagerduty" | ||
page_title: "PagerDuty: pagerduty_schedule" | ||
sidebar_current: "docs-pagerduty-datasource-schedule" | ||
description: |- | ||
Provides information about a Schedule. | ||
This data source can be helpful when a schedule is handled outside Terraform but still want to reference it in other resources. | ||
--- | ||
|
||
# pagerduty\_schedule | ||
|
||
Use this data source to get information about a specific [schedule][1] that you can use for other PagerDuty resources. | ||
|
||
## Example Usage | ||
|
||
``` | ||
data "pagerduty_schedule" "test" { | ||
name = "Daily Engineering Rotation" | ||
} | ||
resource "pagerduty_escalation_policy" "foo" { | ||
name = "Engineering Escalation Policy" | ||
num_loops = 2 | ||
rule { | ||
escalation_delay_in_minutes = 10 | ||
target { | ||
type = "schedule" | ||
id = "${data.pagerduty_schedule.test.id}" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required) The name to use to find a schedule in the PagerDuty API. | ||
|
||
## Attributes Reference | ||
* `name` - The short name of the found schedule. | ||
|
||
[1]: https://v2.developer.pagerduty.com/v2/page/api-reference#!/Schedules/get_schedules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters