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