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

@ -102,6 +102,7 @@ resource "pagerduty_escalation_policy" "foo" {
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
@ -127,6 +128,7 @@ resource "pagerduty_escalation_policy" "foo" {
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
@ -135,6 +137,7 @@ resource "pagerduty_escalation_policy" "foo" {
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"),
),
},
},
@ -79,21 +127,102 @@ const testAccCheckPagerDutyScheduleConfig = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
resource "pagerduty_schedule" "foo" {
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

@ -104,6 +104,7 @@ resource "pagerduty_escalation_policy" "foo" {
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
@ -143,6 +144,7 @@ resource "pagerduty_escalation_policy" "foo" {
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"

View File

@ -106,6 +106,7 @@ resource "pagerduty_escalation_policy" "foo" {
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
@ -139,6 +140,7 @@ resource "pagerduty_escalation_policy" "foo" {
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.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

@ -37,7 +37,6 @@ 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
@ -54,7 +53,6 @@ func flattenRules(list []pagerduty.EscalationRule) []map[string]interface{} {
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,10 +112,11 @@ 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["start"] = i.Start
r["rotation_virtual_start"] = i.RotationVirtualStart
r["rotation_turn_length_seconds"] = i.RotationTurnLengthSeconds
if len(i.Users) > 0 {
@ -138,9 +141,15 @@ func flattenLayers(list []pagerduty.ScheduleLayer) []map[string]interface{} {
result = append(result, r)
}
// 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 result
return resultReversed
}
// Takes the result of flatmap.Expand for an array of strings