Make Schedule work and add tests for import and resource + cleanups

This commit is contained in:
Alexander Hellbom 2016-10-18 14:54:33 +02:00
parent ffd3ceef0d
commit ec10e031ee
11 changed files with 318 additions and 154 deletions

View File

@ -0,0 +1,28 @@
package pagerduty
import (
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccPagerDutySchedule_import(t *testing.T) {
resourceName := "pagerduty_schedule.foo"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyUserDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckPagerDutyScheduleConfig,
},
resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

View File

@ -100,13 +100,14 @@ resource "pagerduty_escalation_policy" "foo" {
description = "foo"
num_loops = 1
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
`
@ -125,20 +126,22 @@ resource "pagerduty_escalation_policy" "foo" {
description = "bar"
num_loops = 2
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
escalation_rule {
escalation_delay_in_minutes = 10
escalation_rule {
escalation_delay_in_minutes = 20
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
escalation_rule {
escalation_delay_in_minutes = 20
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
`

View File

@ -1,12 +1,9 @@
package pagerduty
import (
"bytes"
"fmt"
"log"
"github.com/PagerDuty/go-pagerduty"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)
@ -34,8 +31,7 @@ func resourcePagerDutySchedule() *schema.Resource {
Default: "Managed by Terraform",
},
"schedule_layer": &schema.Schema{
Type: schema.TypeSet,
Set: resourcePagerDutyEscalationHash,
Type: schema.TypeList,
Required: true,
ForceNew: true,
Elem: &schema.Resource{
@ -47,10 +43,18 @@ func resourcePagerDutySchedule() *schema.Resource {
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"start": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
Computed: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "" {
return false
}
return true
},
},
"end": &schema.Schema{
Type: schema.TypeString,
@ -58,7 +62,14 @@ func resourcePagerDutySchedule() *schema.Resource {
},
"rotation_virtual_start": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
Computed: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "" {
return false
}
return true
},
},
"rotation_turn_length_seconds": &schema.Schema{
Type: schema.TypeInt,
@ -72,8 +83,8 @@ func resourcePagerDutySchedule() *schema.Resource {
},
},
"restriction": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
@ -99,12 +110,12 @@ func resourcePagerDutySchedule() *schema.Resource {
}
func buildScheduleStruct(d *schema.ResourceData) (*pagerduty.Schedule, error) {
pagerdutyLayers := d.Get("schedule_layer").(*schema.Set).List()
scheduleLayers := d.Get("schedule_layer").([]interface{})
schedule := pagerduty.Schedule{
Name: d.Get("name").(string),
TimeZone: d.Get("time_zone").(string),
ScheduleLayers: expandLayers(pagerdutyLayers),
ScheduleLayers: expandLayers(scheduleLayers),
}
if attr, ok := d.GetOk("description"); ok {
@ -129,7 +140,7 @@ func resourcePagerDutyScheduleCreate(d *schema.ResourceData, meta interface{}) e
d.SetId(e.ID)
return nil
return resourcePagerDutyScheduleRead(d, meta)
}
func resourcePagerDutyScheduleRead(d *schema.ResourceData, meta interface{}) error {
@ -144,6 +155,7 @@ func resourcePagerDutyScheduleRead(d *schema.ResourceData, meta interface{}) err
}
d.Set("name", s.Name)
d.Set("time_zone", s.TimeZone)
d.Set("description", s.Description)
if err := d.Set("schedule_layer", flattenLayers(s.ScheduleLayers)); err != nil {
@ -191,30 +203,3 @@ func resourcePagerDutyScheduleImport(d *schema.ResourceData, meta interface{}) (
}
return []*schema.ResourceData{d}, nil
}
func resourcePagerDutyEscalationHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%d-", m["rotation_turn_length_seconds"].(int)))
if _, ok := m["name"]; ok {
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
}
if _, ok := m["end"]; ok {
buf.WriteString(fmt.Sprintf("%s-", m["end"].(string)))
}
for _, u := range m["users"].([]interface{}) {
buf.WriteString(fmt.Sprintf("%s-", u))
}
for _, r := range m["restriction"].([]interface{}) {
restriction := r.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", restriction["type"].(string)))
buf.WriteString(fmt.Sprintf("%s-", restriction["start_time_of_day"].(string)))
buf.WriteString(fmt.Sprintf("%d-", restriction["duration_seconds"].(int)))
}
return hashcode.String(buf.String())
}

View File

@ -22,11 +22,59 @@ func TestAccPagerDutySchedule_Basic(t *testing.T) {
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "name", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "description", "Managed by Terraform"),
"pagerduty_schedule.foo", "description", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "time_zone", "Europe/Berlin"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "schedule_layer.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "schedule_layer.0.name", "foo"),
),
},
resource.TestStep{
Config: testAccCheckPagerDutyScheduleConfigUpdated,
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyScheduleExists("pagerduty_schedule.foo"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "name", "bar"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "description", "Managed by Terraform"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "time_zone", "America/New_York"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "schedule_layer.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "schedule_layer.0.name", "foo"),
),
},
},
})
}
func TestAccPagerDutySchedule_Multi(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyScheduleDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckPagerDutyScheduleConfigMulti,
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyScheduleExists("pagerduty_schedule.foo"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "name", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "description", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "time_zone", "America/New_York"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "schedule_layer.#", "3"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "schedule_layer.0.name", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "schedule_layer.1.name", "bar"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "schedule_layer.2.name", "foobar"),
),
},
},
@ -77,23 +125,104 @@ func testAccCheckPagerDutyScheduleExists(n string) resource.TestCheckFunc {
const testAccCheckPagerDutyScheduleConfig = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
name = "foo"
email = "foo@bar.com"
}
resource "pagerduty_schedule" "foo" {
name = "foo"
time_zone = "Europe/Berlin"
name = "foo"
time_zone = "Europe/Berlin"
description = "foo"
schedule_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 = 86401
rotation_turn_length_seconds = 86400
users = ["${pagerduty_user.foo.id}"]
restriction {
type = "daily_restriction"
start_time_of_day = "08:00:00"
duration_seconds = 32101
}
}
}
`
const testAccCheckPagerDutyScheduleConfigUpdated = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
}
resource "pagerduty_schedule" "foo" {
name = "bar"
time_zone = "America/New_York"
schedule_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.foo.id}"]
restriction {
type = "daily_restriction"
start_time_of_day = "08:00:00"
duration_seconds = 32101
}
}
}
`
const testAccCheckPagerDutyScheduleConfigMulti = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
}
resource "pagerduty_schedule" "foo" {
name = "foo"
time_zone = "America/New_York"
description = "foo"
schedule_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.foo.id}"]
restriction {
type = "daily_restriction"
start_time_of_day = "08:00:00"
duration_seconds = 32101
}
}
schedule_layer {
name = "bar"
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 = 32101
}
}
schedule_layer {
name = "foobar"
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 {

View File

@ -45,6 +45,9 @@ func resourcePagerDutyService() *schema.Resource {
func buildServiceStruct(d *schema.ResourceData) *pagerduty.Service {
service := pagerduty.Service{
Name: d.Get("name").(string),
APIObject: pagerduty.APIObject{
ID: d.Id(),
},
}
if attr, ok := d.GetOk("description"); ok {
@ -115,7 +118,6 @@ func resourcePagerDutyServiceUpdate(d *schema.ResourceData, meta interface{}) er
client := meta.(*pagerduty.Client)
s := buildServiceStruct(d)
s.ID = d.Id()
log.Printf("[INFO] Updating PagerDuty service %s", d.Id())

View File

@ -60,6 +60,9 @@ func buildServiceIntegrationStruct(d *schema.ResourceData) *pagerduty.Integratio
Type: "service",
ID: d.Get("service").(string),
},
APIObject: pagerduty.APIObject{
ID: d.Id(),
},
}
return &service
@ -111,7 +114,6 @@ func resourcePagerDutyServiceIntegrationUpdate(d *schema.ResourceData, meta inte
client := meta.(*pagerduty.Client)
s := buildServiceIntegrationStruct(d)
s.ID = d.Id()
service := d.Get("service").(string)

View File

@ -102,21 +102,22 @@ resource "pagerduty_escalation_policy" "foo" {
description = "foo"
num_loops = 1
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
resource "pagerduty_service" "foo" {
name = "foo"
description = "foo"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
}
resource "pagerduty_service_integration" "foo" {
@ -141,21 +142,22 @@ resource "pagerduty_escalation_policy" "foo" {
description = "bar"
num_loops = 2
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
resource "pagerduty_service" "foo" {
name = "bar"
description = "bar"
auto_resolve_timeout = 3600
acknowledgement_timeout = 3600
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
auto_resolve_timeout = 3600
acknowledgement_timeout = 3600
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
}
resource "pagerduty_service_integration" "foo" {

View File

@ -104,21 +104,22 @@ resource "pagerduty_escalation_policy" "foo" {
description = "bar"
num_loops = 2
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
resource "pagerduty_service" "foo" {
name = "foo"
description = "foo"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
}
`
@ -137,20 +138,21 @@ resource "pagerduty_escalation_policy" "foo" {
description = "bar"
num_loops = 2
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
resource "pagerduty_service" "foo" {
name = "bar"
description = "bar"
auto_resolve_timeout = 3600
acknowledgement_timeout = 3600
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
auto_resolve_timeout = 3600
acknowledgement_timeout = 3600
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
}
`

View File

@ -83,6 +83,9 @@ func buildUserStruct(d *schema.ResourceData) *pagerduty.User {
user := pagerduty.User{
Name: d.Get("name").(string),
Email: d.Get("email").(string),
APIObject: pagerduty.APIObject{
ID: d.Id(),
},
}
if attr, ok := d.GetOk("color"); ok {
@ -150,7 +153,6 @@ func resourcePagerDutyUserUpdate(d *schema.ResourceData, meta interface{}) error
client := meta.(*pagerduty.Client)
u := buildUserStruct(d)
u.ID = d.Id()
log.Printf("[INFO] Updating PagerDuty user %s", d.Id())

View File

@ -175,7 +175,7 @@ resource "pagerduty_team" "foo" {
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
teams = ["${pagerduty_team.foo.id}"]
teams = ["${pagerduty_team.foo.id}"]
}
`
const testAccCheckPagerDutyUserWithTeamsConfigUpdated = `
@ -190,7 +190,7 @@ resource "pagerduty_team" "bar" {
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
teams = ["${pagerduty_team.foo.id}", "${pagerduty_team.bar.id}"]
teams = ["${pagerduty_team.foo.id}", "${pagerduty_team.bar.id}"]
}
`

View File

@ -37,23 +37,21 @@ func flattenRules(list []pagerduty.EscalationRule) []map[string]interface{} {
for _, i := range list {
r := make(map[string]interface{})
if i.ID != "" {
r["id"] = i.ID
r["escalation_delay_in_minutes"] = i.Delay
r["id"] = i.ID
r["escalation_delay_in_minutes"] = i.Delay
if len(i.Targets) > 0 {
targets := make([]map[string]interface{}, 0, len(i.Targets))
for _, t := range i.Targets {
targets = append(targets, map[string]interface{}{
"id": t.ID,
"type": t.Type,
})
}
r["target"] = targets
if len(i.Targets) > 0 {
targets := make([]map[string]interface{}, 0, len(i.Targets))
for _, t := range i.Targets {
targets = append(targets, map[string]interface{}{
"id": t.ID,
"type": t.Type,
})
}
result = append(result, r)
r["target"] = targets
}
result = append(result, r)
}
return result
@ -74,6 +72,10 @@ func expandLayers(list []interface{}) []pagerduty.ScheduleLayer {
RotationTurnLengthSeconds: uint(layer["rotation_turn_length_seconds"].(int)),
}
if layer["id"] != "" {
scheduleLayer.ID = layer["id"].(string)
}
for _, u := range layer["users"].([]interface{}) {
scheduleLayer.Users = append(
scheduleLayer.Users,
@ -110,37 +112,44 @@ func flattenLayers(list []pagerduty.ScheduleLayer) []map[string]interface{} {
for _, i := range list {
r := make(map[string]interface{})
if i.ID != "" {
r["id"] = i.ID
r["name"] = i.Name
r["end"] = i.End
r["rotation_turn_length_seconds"] = i.RotationTurnLengthSeconds
r["id"] = i.ID
r["name"] = i.Name
r["end"] = i.End
r["start"] = i.Start
r["rotation_virtual_start"] = i.RotationVirtualStart
r["rotation_turn_length_seconds"] = i.RotationTurnLengthSeconds
if len(i.Users) > 0 {
users := make([]string, 0, len(i.Users))
for _, u := range i.Users {
users = append(users, u.User.ID)
}
r["users"] = users
if len(i.Users) > 0 {
users := make([]string, 0, len(i.Users))
for _, u := range i.Users {
users = append(users, u.User.ID)
}
if len(i.Restrictions) > 0 {
restrictions := make([]map[string]interface{}, 0, len(i.Restrictions))
for _, r := range i.Restrictions {
restrictions = append(restrictions, map[string]interface{}{
"duration_seconds": r.DurationSeconds,
"start_time_of_day": r.StartTimeOfDay,
"type": r.Type,
})
}
r["restriction"] = restrictions
}
result = append(result, r)
r["users"] = users
}
if len(i.Restrictions) > 0 {
restrictions := make([]map[string]interface{}, 0, len(i.Restrictions))
for _, r := range i.Restrictions {
restrictions = append(restrictions, map[string]interface{}{
"duration_seconds": r.DurationSeconds,
"start_time_of_day": r.StartTimeOfDay,
"type": r.Type,
})
}
r["restriction"] = restrictions
}
result = append(result, r)
}
return result
// Reverse the final result and return it
resultReversed := make([]map[string]interface{}, 0, len(result))
for i := len(result) - 1; i >= 0; i-- {
resultReversed = append(resultReversed, result[i])
}
return resultReversed
}
// Takes the result of flatmap.Expand for an array of strings