github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/pagerduty/resource_pagerduty_schedule_test.go (about) 1 package pagerduty 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/PagerDuty/go-pagerduty" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccPagerDutySchedule_Basic(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 CheckDestroy: testAccCheckPagerDutyScheduleDestroy, 17 Steps: []resource.TestStep{ 18 resource.TestStep{ 19 Config: testAccCheckPagerDutyScheduleConfig, 20 Check: resource.ComposeTestCheckFunc( 21 testAccCheckPagerDutyScheduleExists("pagerduty_schedule.foo"), 22 resource.TestCheckResourceAttr( 23 "pagerduty_schedule.foo", "name", "foo"), 24 resource.TestCheckResourceAttr( 25 "pagerduty_schedule.foo", "description", "foo"), 26 resource.TestCheckResourceAttr( 27 "pagerduty_schedule.foo", "time_zone", "Europe/Berlin"), 28 resource.TestCheckResourceAttr( 29 "pagerduty_schedule.foo", "layer.#", "1"), 30 resource.TestCheckResourceAttr( 31 "pagerduty_schedule.foo", "layer.0.name", "foo"), 32 ), 33 }, 34 resource.TestStep{ 35 Config: testAccCheckPagerDutyScheduleConfigUpdated, 36 Check: resource.ComposeTestCheckFunc( 37 testAccCheckPagerDutyScheduleExists("pagerduty_schedule.foo"), 38 resource.TestCheckResourceAttr( 39 "pagerduty_schedule.foo", "name", "bar"), 40 resource.TestCheckResourceAttr( 41 "pagerduty_schedule.foo", "description", "Managed by Terraform"), 42 resource.TestCheckResourceAttr( 43 "pagerduty_schedule.foo", "time_zone", "America/New_York"), 44 resource.TestCheckResourceAttr( 45 "pagerduty_schedule.foo", "layer.#", "1"), 46 resource.TestCheckResourceAttr( 47 "pagerduty_schedule.foo", "layer.0.name", "foo"), 48 ), 49 }, 50 }, 51 }) 52 } 53 54 func TestAccPagerDutySchedule_BasicWeek(t *testing.T) { 55 resource.Test(t, resource.TestCase{ 56 PreCheck: func() { testAccPreCheck(t) }, 57 Providers: testAccProviders, 58 CheckDestroy: testAccCheckPagerDutyScheduleDestroy, 59 Steps: []resource.TestStep{ 60 resource.TestStep{ 61 Config: testAccCheckPagerDutyScheduleConfigWeek, 62 Check: resource.ComposeTestCheckFunc( 63 testAccCheckPagerDutyScheduleExists("pagerduty_schedule.foo"), 64 resource.TestCheckResourceAttr( 65 "pagerduty_schedule.foo", "name", "foo"), 66 resource.TestCheckResourceAttr( 67 "pagerduty_schedule.foo", "description", "foo"), 68 resource.TestCheckResourceAttr( 69 "pagerduty_schedule.foo", "time_zone", "Europe/Berlin"), 70 resource.TestCheckResourceAttr( 71 "pagerduty_schedule.foo", "layer.#", "1"), 72 resource.TestCheckResourceAttr( 73 "pagerduty_schedule.foo", "layer.0.name", "foo"), 74 resource.TestCheckResourceAttr( 75 "pagerduty_schedule.foo", "layer.0.restriction.0.start_day_of_week", "1"), 76 ), 77 }, 78 resource.TestStep{ 79 Config: testAccCheckPagerDutyScheduleConfigWeekUpdated, 80 Check: resource.ComposeTestCheckFunc( 81 testAccCheckPagerDutyScheduleExists("pagerduty_schedule.foo"), 82 resource.TestCheckResourceAttr( 83 "pagerduty_schedule.foo", "name", "bar"), 84 resource.TestCheckResourceAttr( 85 "pagerduty_schedule.foo", "description", "Managed by Terraform"), 86 resource.TestCheckResourceAttr( 87 "pagerduty_schedule.foo", "time_zone", "America/New_York"), 88 resource.TestCheckResourceAttr( 89 "pagerduty_schedule.foo", "layer.#", "1"), 90 resource.TestCheckResourceAttr( 91 "pagerduty_schedule.foo", "layer.0.name", "foo"), 92 resource.TestCheckResourceAttr( 93 "pagerduty_schedule.foo", "layer.0.restriction.0.start_day_of_week", "5"), 94 ), 95 }, 96 }, 97 }) 98 } 99 100 func TestAccPagerDutySchedule_Multi(t *testing.T) { 101 resource.Test(t, resource.TestCase{ 102 PreCheck: func() { testAccPreCheck(t) }, 103 Providers: testAccProviders, 104 CheckDestroy: testAccCheckPagerDutyScheduleDestroy, 105 Steps: []resource.TestStep{ 106 resource.TestStep{ 107 Config: testAccCheckPagerDutyScheduleConfigMulti, 108 Check: resource.ComposeTestCheckFunc( 109 testAccCheckPagerDutyScheduleExists("pagerduty_schedule.foo"), 110 resource.TestCheckResourceAttr( 111 "pagerduty_schedule.foo", "name", "foo"), 112 resource.TestCheckResourceAttr( 113 "pagerduty_schedule.foo", "description", "foo"), 114 resource.TestCheckResourceAttr( 115 "pagerduty_schedule.foo", "time_zone", "America/New_York"), 116 117 resource.TestCheckResourceAttr( 118 "pagerduty_schedule.foo", "layer.#", "3"), 119 resource.TestCheckResourceAttr( 120 "pagerduty_schedule.foo", "layer.0.name", "foo"), 121 resource.TestCheckResourceAttr( 122 "pagerduty_schedule.foo", "layer.0.restriction.#", "1"), 123 resource.TestCheckResourceAttr( 124 "pagerduty_schedule.foo", "layer.0.restriction.0.duration_seconds", "32101"), 125 resource.TestCheckResourceAttr( 126 "pagerduty_schedule.foo", "layer.0.restriction.0.start_time_of_day", "08:00:00"), 127 resource.TestCheckResourceAttr( 128 "pagerduty_schedule.foo", "layer.0.rotation_turn_length_seconds", "86400"), 129 // NOTE: Temporarily disabled due to API inconsistencies 130 // resource.TestCheckResourceAttr( 131 // "pagerduty_schedule.foo", "layer.0.rotation_virtual_start", "2015-11-06T20:00:00-05:00"), 132 resource.TestCheckResourceAttr( 133 "pagerduty_schedule.foo", "layer.0.users.#", "1"), 134 135 resource.TestCheckResourceAttr( 136 "pagerduty_schedule.foo", "layer.1.name", "bar"), 137 resource.TestCheckResourceAttr( 138 "pagerduty_schedule.foo", "layer.1.restriction.#", "1"), 139 resource.TestCheckResourceAttr( 140 "pagerduty_schedule.foo", "layer.1.restriction.0.duration_seconds", "32101"), 141 resource.TestCheckResourceAttr( 142 "pagerduty_schedule.foo", "layer.1.restriction.0.start_time_of_day", "08:00:00"), 143 resource.TestCheckResourceAttr( 144 "pagerduty_schedule.foo", "layer.1.restriction.0.start_day_of_week", "5"), 145 resource.TestCheckResourceAttr( 146 "pagerduty_schedule.foo", "layer.1.rotation_turn_length_seconds", "86400"), 147 // NOTE: Temporarily disabled due to API inconsistencies 148 // resource.TestCheckResourceAttr( 149 // "pagerduty_schedule.foo", "layer.1.rotation_virtual_start", "2015-11-06T20:00:00-05:00"), 150 resource.TestCheckResourceAttr( 151 "pagerduty_schedule.foo", "layer.1.users.#", "1"), 152 153 resource.TestCheckResourceAttr( 154 "pagerduty_schedule.foo", "layer.2.name", "foobar"), 155 resource.TestCheckResourceAttr( 156 "pagerduty_schedule.foo", "layer.2.restriction.#", "1"), 157 resource.TestCheckResourceAttr( 158 "pagerduty_schedule.foo", "layer.2.restriction.0.duration_seconds", "32101"), 159 resource.TestCheckResourceAttr( 160 "pagerduty_schedule.foo", "layer.2.restriction.0.start_time_of_day", "08:00:00"), 161 resource.TestCheckResourceAttr( 162 "pagerduty_schedule.foo", "layer.2.restriction.0.start_day_of_week", "1"), 163 resource.TestCheckResourceAttr( 164 "pagerduty_schedule.foo", "layer.2.rotation_turn_length_seconds", "86400"), 165 // NOTE: Temporarily disabled due to API inconsistencies 166 // resource.TestCheckResourceAttr( 167 // "pagerduty_schedule.foo", "layer.2.rotation_virtual_start", "2015-11-06T20:00:00-05:00"), 168 resource.TestCheckResourceAttr( 169 "pagerduty_schedule.foo", "layer.2.users.#", "1"), 170 ), 171 }, 172 }, 173 }) 174 } 175 176 func testAccCheckPagerDutyScheduleDestroy(s *terraform.State) error { 177 client := testAccProvider.Meta().(*pagerduty.Client) 178 for _, r := range s.RootModule().Resources { 179 if r.Type != "pagerduty_schedule" { 180 continue 181 } 182 183 _, err := client.GetSchedule(r.Primary.ID, pagerduty.GetScheduleOptions{}) 184 185 if err == nil { 186 return fmt.Errorf("Schedule still exists") 187 } 188 189 } 190 return nil 191 } 192 193 func testAccCheckPagerDutyScheduleExists(n string) resource.TestCheckFunc { 194 return func(s *terraform.State) error { 195 rs, ok := s.RootModule().Resources[n] 196 if !ok { 197 return fmt.Errorf("Not found: %s", n) 198 } 199 if rs.Primary.ID == "" { 200 return fmt.Errorf("No Schedule ID is set") 201 } 202 203 client := testAccProvider.Meta().(*pagerduty.Client) 204 205 found, err := client.GetSchedule(rs.Primary.ID, pagerduty.GetScheduleOptions{}) 206 if err != nil { 207 return err 208 } 209 210 if found.ID != rs.Primary.ID { 211 return fmt.Errorf("Schedule not found: %v - %v", rs.Primary.ID, found) 212 } 213 214 return nil 215 } 216 } 217 218 const testAccCheckPagerDutyScheduleConfig = ` 219 resource "pagerduty_user" "foo" { 220 name = "foo" 221 email = "foo@bar.com" 222 } 223 224 resource "pagerduty_schedule" "foo" { 225 name = "foo" 226 227 time_zone = "Europe/Berlin" 228 description = "foo" 229 230 layer { 231 name = "foo" 232 start = "2015-11-06T20:00:00-05:00" 233 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 234 rotation_turn_length_seconds = 86400 235 users = ["${pagerduty_user.foo.id}"] 236 237 restriction { 238 type = "daily_restriction" 239 start_time_of_day = "08:00:00" 240 duration_seconds = 32101 241 } 242 } 243 } 244 ` 245 246 const testAccCheckPagerDutyScheduleConfigUpdated = ` 247 resource "pagerduty_user" "foo" { 248 name = "foo" 249 email = "foo@bar.com" 250 } 251 252 resource "pagerduty_schedule" "foo" { 253 name = "bar" 254 255 time_zone = "America/New_York" 256 257 layer { 258 name = "foo" 259 start = "2015-11-06T20:00:00-05:00" 260 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 261 rotation_turn_length_seconds = 86400 262 users = ["${pagerduty_user.foo.id}"] 263 264 restriction { 265 type = "daily_restriction" 266 start_time_of_day = "08:00:00" 267 duration_seconds = 32101 268 } 269 } 270 } 271 ` 272 273 const testAccCheckPagerDutyScheduleConfigWeek = ` 274 resource "pagerduty_user" "foo" { 275 name = "foo" 276 email = "foo@bar.com" 277 } 278 279 resource "pagerduty_schedule" "foo" { 280 name = "foo" 281 282 time_zone = "Europe/Berlin" 283 description = "foo" 284 285 layer { 286 name = "foo" 287 start = "2015-11-06T20:00:00-05:00" 288 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 289 rotation_turn_length_seconds = 86400 290 users = ["${pagerduty_user.foo.id}"] 291 292 restriction { 293 type = "weekly_restriction" 294 start_time_of_day = "08:00:00" 295 start_day_of_week = 1 296 duration_seconds = 32101 297 } 298 } 299 } 300 ` 301 302 const testAccCheckPagerDutyScheduleConfigWeekUpdated = ` 303 resource "pagerduty_user" "foo" { 304 name = "foo" 305 email = "foo@bar.com" 306 } 307 308 resource "pagerduty_schedule" "foo" { 309 name = "bar" 310 311 time_zone = "America/New_York" 312 313 layer { 314 name = "foo" 315 start = "2015-11-06T20:00:00-05:00" 316 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 317 rotation_turn_length_seconds = 86400 318 users = ["${pagerduty_user.foo.id}"] 319 320 restriction { 321 type = "weekly_restriction" 322 start_time_of_day = "08:00:00" 323 start_day_of_week = 5 324 duration_seconds = 32101 325 } 326 } 327 } 328 ` 329 330 const testAccCheckPagerDutyScheduleConfigMulti = ` 331 resource "pagerduty_user" "foo" { 332 name = "foo" 333 email = "foo@bar.com" 334 } 335 336 resource "pagerduty_schedule" "foo" { 337 name = "foo" 338 339 time_zone = "America/New_York" 340 description = "foo" 341 342 layer { 343 name = "foo" 344 start = "2015-11-06T20:00:00-05:00" 345 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 346 rotation_turn_length_seconds = 86400 347 users = ["${pagerduty_user.foo.id}"] 348 349 restriction { 350 type = "daily_restriction" 351 start_time_of_day = "08:00:00" 352 duration_seconds = 32101 353 } 354 } 355 356 layer { 357 name = "bar" 358 start = "2015-11-06T20:00:00-05:00" 359 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 360 rotation_turn_length_seconds = 86400 361 users = ["${pagerduty_user.foo.id}"] 362 363 restriction { 364 type = "weekly_restriction" 365 start_time_of_day = "08:00:00" 366 start_day_of_week = 5 367 duration_seconds = 32101 368 } 369 } 370 371 layer { 372 name = "foobar" 373 start = "2015-11-06T20:00:00-05:00" 374 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 375 rotation_turn_length_seconds = 86400 376 users = ["${pagerduty_user.foo.id}"] 377 378 restriction { 379 type = "weekly_restriction" 380 start_time_of_day = "08:00:00" 381 start_day_of_week = 1 382 duration_seconds = 32101 383 } 384 } 385 } 386 `