github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/pagerduty/resource_pagerduty_service_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 TestAccPagerDutyService_Basic(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 CheckDestroy: testAccCheckPagerDutyServiceDestroy, 17 Steps: []resource.TestStep{ 18 resource.TestStep{ 19 Config: testAccCheckPagerDutyServiceConfig, 20 Check: resource.ComposeTestCheckFunc( 21 testAccCheckPagerDutyServiceExists("pagerduty_service.foo"), 22 resource.TestCheckResourceAttr( 23 "pagerduty_service.foo", "name", "foo"), 24 resource.TestCheckResourceAttr( 25 "pagerduty_service.foo", "description", "foo"), 26 resource.TestCheckResourceAttr( 27 "pagerduty_service.foo", "auto_resolve_timeout", "1800"), 28 resource.TestCheckResourceAttr( 29 "pagerduty_service.foo", "acknowledgement_timeout", "1800"), 30 resource.TestCheckResourceAttr( 31 "pagerduty_service.foo", "incident_urgency_rule.#", "1"), 32 resource.TestCheckResourceAttr( 33 "pagerduty_service.foo", "incident_urgency_rule.0.urgency", "high"), 34 resource.TestCheckResourceAttr( 35 "pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"), 36 ), 37 }, 38 resource.TestStep{ 39 Config: testAccCheckPagerDutyServiceConfigUpdated, 40 Check: resource.ComposeTestCheckFunc( 41 testAccCheckPagerDutyServiceExists("pagerduty_service.foo"), 42 resource.TestCheckResourceAttr( 43 "pagerduty_service.foo", "name", "bar"), 44 resource.TestCheckResourceAttr( 45 "pagerduty_service.foo", "description", "bar"), 46 resource.TestCheckResourceAttr( 47 "pagerduty_service.foo", "auto_resolve_timeout", "3600"), 48 resource.TestCheckResourceAttr( 49 "pagerduty_service.foo", "acknowledgement_timeout", "3600"), 50 resource.TestCheckResourceAttr( 51 "pagerduty_service.foo", "incident_urgency_rule.#", "1"), 52 resource.TestCheckResourceAttr( 53 "pagerduty_service.foo", "incident_urgency_rule.0.urgency", "high"), 54 resource.TestCheckResourceAttr( 55 "pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"), 56 ), 57 }, 58 }, 59 }) 60 } 61 62 func TestAccPagerDutyService_BasicWithIncidentUrgencyRules(t *testing.T) { 63 resource.Test(t, resource.TestCase{ 64 PreCheck: func() { testAccPreCheck(t) }, 65 Providers: testAccProviders, 66 CheckDestroy: testAccCheckPagerDutyServiceDestroy, 67 Steps: []resource.TestStep{ 68 resource.TestStep{ 69 Config: testAccCheckPagerDutyServiceWithIncidentUrgencyRulesConfig, 70 Check: resource.ComposeTestCheckFunc( 71 testAccCheckPagerDutyServiceExists("pagerduty_service.foo"), 72 resource.TestCheckResourceAttr( 73 "pagerduty_service.foo", "name", "foo"), 74 resource.TestCheckResourceAttr( 75 "pagerduty_service.foo", "description", "foo"), 76 resource.TestCheckResourceAttr( 77 "pagerduty_service.foo", "auto_resolve_timeout", "1800"), 78 resource.TestCheckResourceAttr( 79 "pagerduty_service.foo", "acknowledgement_timeout", "1800"), 80 resource.TestCheckResourceAttr( 81 "pagerduty_service.foo", "incident_urgency_rule.#", "1"), 82 resource.TestCheckResourceAttr( 83 "pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.#", "1"), 84 resource.TestCheckResourceAttr( 85 "pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.0.type", "constant"), 86 resource.TestCheckResourceAttr( 87 "pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.0.urgency", "high"), 88 resource.TestCheckResourceAttr( 89 "pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.#", "1"), 90 resource.TestCheckResourceAttr( 91 "pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.0.type", "constant"), 92 resource.TestCheckResourceAttr( 93 "pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.0.urgency", "low"), 94 resource.TestCheckResourceAttr( 95 "pagerduty_service.foo", "incident_urgency_rule.0.type", "use_support_hours"), 96 resource.TestCheckResourceAttr( 97 "pagerduty_service.foo", "scheduled_actions.#", "1"), 98 resource.TestCheckResourceAttr( 99 "pagerduty_service.foo", "scheduled_actions.0.at.#", "1"), 100 resource.TestCheckResourceAttr( 101 "pagerduty_service.foo", "scheduled_actions.0.at.0.name", "support_hours_start"), 102 resource.TestCheckResourceAttr( 103 "pagerduty_service.foo", "scheduled_actions.0.to_urgency", "high"), 104 resource.TestCheckResourceAttr( 105 "pagerduty_service.foo", "scheduled_actions.0.type", "urgency_change"), 106 resource.TestCheckResourceAttr( 107 "pagerduty_service.foo", "support_hours.#", "1"), 108 resource.TestCheckResourceAttr( 109 "pagerduty_service.foo", "support_hours.0.days_of_week.#", "5"), 110 resource.TestCheckResourceAttr( 111 "pagerduty_service.foo", "support_hours.0.days_of_week.0", "1"), 112 resource.TestCheckResourceAttr( 113 "pagerduty_service.foo", "support_hours.0.days_of_week.1", "2"), 114 resource.TestCheckResourceAttr( 115 "pagerduty_service.foo", "support_hours.0.days_of_week.2", "3"), 116 resource.TestCheckResourceAttr( 117 "pagerduty_service.foo", "support_hours.0.days_of_week.3", "4"), 118 resource.TestCheckResourceAttr( 119 "pagerduty_service.foo", "support_hours.0.days_of_week.4", "5"), 120 resource.TestCheckResourceAttr( 121 "pagerduty_service.foo", "support_hours.0.end_time", "17:00:00"), 122 resource.TestCheckResourceAttr( 123 "pagerduty_service.foo", "support_hours.0.start_time", "09:00:00"), 124 resource.TestCheckResourceAttr( 125 "pagerduty_service.foo", "support_hours.0.time_zone", "America/Lima"), 126 resource.TestCheckResourceAttr( 127 "pagerduty_service.foo", "support_hours.0.type", "fixed_time_per_day"), 128 ), 129 }, 130 resource.TestStep{ 131 Config: testAccCheckPagerDutyServiceWithIncidentUrgencyRulesConfigUpdated, 132 Check: resource.ComposeTestCheckFunc( 133 testAccCheckPagerDutyServiceExists("pagerduty_service.foo"), 134 resource.TestCheckResourceAttr( 135 "pagerduty_service.foo", "name", "bar"), 136 resource.TestCheckResourceAttr( 137 "pagerduty_service.foo", "description", "bar bar bar"), 138 resource.TestCheckResourceAttr( 139 "pagerduty_service.foo", "auto_resolve_timeout", "3600"), 140 resource.TestCheckResourceAttr( 141 "pagerduty_service.foo", "acknowledgement_timeout", "3600"), 142 resource.TestCheckResourceAttr( 143 "pagerduty_service.foo", "incident_urgency_rule.#", "1"), 144 resource.TestCheckResourceAttr( 145 "pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.#", "1"), 146 resource.TestCheckResourceAttr( 147 "pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.0.type", "constant"), 148 resource.TestCheckResourceAttr( 149 "pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.0.urgency", "high"), 150 resource.TestCheckResourceAttr( 151 "pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.#", "1"), 152 resource.TestCheckResourceAttr( 153 "pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.0.type", "constant"), 154 resource.TestCheckResourceAttr( 155 "pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.0.urgency", "low"), 156 resource.TestCheckResourceAttr( 157 "pagerduty_service.foo", "incident_urgency_rule.0.type", "use_support_hours"), 158 resource.TestCheckResourceAttr( 159 "pagerduty_service.foo", "scheduled_actions.#", "1"), 160 resource.TestCheckResourceAttr( 161 "pagerduty_service.foo", "scheduled_actions.0.at.#", "1"), 162 resource.TestCheckResourceAttr( 163 "pagerduty_service.foo", "scheduled_actions.0.at.0.name", "support_hours_start"), 164 resource.TestCheckResourceAttr( 165 "pagerduty_service.foo", "scheduled_actions.0.to_urgency", "high"), 166 resource.TestCheckResourceAttr( 167 "pagerduty_service.foo", "scheduled_actions.0.type", "urgency_change"), 168 resource.TestCheckResourceAttr( 169 "pagerduty_service.foo", "support_hours.#", "1"), 170 resource.TestCheckResourceAttr( 171 "pagerduty_service.foo", "support_hours.0.days_of_week.#", "5"), 172 resource.TestCheckResourceAttr( 173 "pagerduty_service.foo", "support_hours.0.days_of_week.0", "1"), 174 resource.TestCheckResourceAttr( 175 "pagerduty_service.foo", "support_hours.0.days_of_week.1", "2"), 176 resource.TestCheckResourceAttr( 177 "pagerduty_service.foo", "support_hours.0.days_of_week.2", "3"), 178 resource.TestCheckResourceAttr( 179 "pagerduty_service.foo", "support_hours.0.days_of_week.3", "4"), 180 resource.TestCheckResourceAttr( 181 "pagerduty_service.foo", "support_hours.0.days_of_week.4", "5"), 182 resource.TestCheckResourceAttr( 183 "pagerduty_service.foo", "support_hours.0.end_time", "17:00:00"), 184 resource.TestCheckResourceAttr( 185 "pagerduty_service.foo", "support_hours.0.start_time", "09:00:00"), 186 resource.TestCheckResourceAttr( 187 "pagerduty_service.foo", "support_hours.0.time_zone", "America/Lima"), 188 resource.TestCheckResourceAttr( 189 "pagerduty_service.foo", "support_hours.0.type", "fixed_time_per_day"), 190 ), 191 }, 192 }, 193 }) 194 } 195 196 func TestAccPagerDutyService_FromBasicToCustomIncidentUrgencyRules(t *testing.T) { 197 resource.Test(t, resource.TestCase{ 198 PreCheck: func() { testAccPreCheck(t) }, 199 Providers: testAccProviders, 200 CheckDestroy: testAccCheckPagerDutyServiceDestroy, 201 Steps: []resource.TestStep{ 202 resource.TestStep{ 203 Config: testAccCheckPagerDutyServiceConfig, 204 Check: resource.ComposeTestCheckFunc( 205 testAccCheckPagerDutyServiceExists("pagerduty_service.foo"), 206 resource.TestCheckResourceAttr( 207 "pagerduty_service.foo", "name", "foo"), 208 resource.TestCheckResourceAttr( 209 "pagerduty_service.foo", "description", "foo"), 210 resource.TestCheckResourceAttr( 211 "pagerduty_service.foo", "auto_resolve_timeout", "1800"), 212 resource.TestCheckResourceAttr( 213 "pagerduty_service.foo", "acknowledgement_timeout", "1800"), 214 resource.TestCheckResourceAttr( 215 "pagerduty_service.foo", "incident_urgency_rule.#", "1"), 216 resource.TestCheckResourceAttr( 217 "pagerduty_service.foo", "incident_urgency_rule.0.urgency", "high"), 218 resource.TestCheckResourceAttr( 219 "pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"), 220 ), 221 }, 222 resource.TestStep{ 223 Config: testAccCheckPagerDutyServiceWithIncidentUrgencyRulesConfigUpdated, 224 Check: resource.ComposeTestCheckFunc( 225 testAccCheckPagerDutyServiceExists("pagerduty_service.foo"), 226 resource.TestCheckResourceAttr( 227 "pagerduty_service.foo", "name", "bar"), 228 resource.TestCheckResourceAttr( 229 "pagerduty_service.foo", "description", "bar bar bar"), 230 resource.TestCheckResourceAttr( 231 "pagerduty_service.foo", "auto_resolve_timeout", "3600"), 232 resource.TestCheckResourceAttr( 233 "pagerduty_service.foo", "acknowledgement_timeout", "3600"), 234 resource.TestCheckResourceAttr( 235 "pagerduty_service.foo", "incident_urgency_rule.#", "1"), 236 resource.TestCheckResourceAttr( 237 "pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.#", "1"), 238 resource.TestCheckResourceAttr( 239 "pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.0.type", "constant"), 240 resource.TestCheckResourceAttr( 241 "pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.0.urgency", "high"), 242 resource.TestCheckResourceAttr( 243 "pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.#", "1"), 244 resource.TestCheckResourceAttr( 245 "pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.0.type", "constant"), 246 resource.TestCheckResourceAttr( 247 "pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.0.urgency", "low"), 248 resource.TestCheckResourceAttr( 249 "pagerduty_service.foo", "incident_urgency_rule.0.type", "use_support_hours"), 250 resource.TestCheckResourceAttr( 251 "pagerduty_service.foo", "scheduled_actions.#", "1"), 252 resource.TestCheckResourceAttr( 253 "pagerduty_service.foo", "scheduled_actions.0.at.#", "1"), 254 resource.TestCheckResourceAttr( 255 "pagerduty_service.foo", "scheduled_actions.0.at.0.name", "support_hours_start"), 256 resource.TestCheckResourceAttr( 257 "pagerduty_service.foo", "scheduled_actions.0.to_urgency", "high"), 258 resource.TestCheckResourceAttr( 259 "pagerduty_service.foo", "scheduled_actions.0.type", "urgency_change"), 260 resource.TestCheckResourceAttr( 261 "pagerduty_service.foo", "support_hours.#", "1"), 262 resource.TestCheckResourceAttr( 263 "pagerduty_service.foo", "support_hours.0.days_of_week.#", "5"), 264 resource.TestCheckResourceAttr( 265 "pagerduty_service.foo", "support_hours.0.days_of_week.0", "1"), 266 resource.TestCheckResourceAttr( 267 "pagerduty_service.foo", "support_hours.0.days_of_week.1", "2"), 268 resource.TestCheckResourceAttr( 269 "pagerduty_service.foo", "support_hours.0.days_of_week.2", "3"), 270 resource.TestCheckResourceAttr( 271 "pagerduty_service.foo", "support_hours.0.days_of_week.3", "4"), 272 resource.TestCheckResourceAttr( 273 "pagerduty_service.foo", "support_hours.0.days_of_week.4", "5"), 274 resource.TestCheckResourceAttr( 275 "pagerduty_service.foo", "support_hours.0.end_time", "17:00:00"), 276 resource.TestCheckResourceAttr( 277 "pagerduty_service.foo", "support_hours.0.start_time", "09:00:00"), 278 resource.TestCheckResourceAttr( 279 "pagerduty_service.foo", "support_hours.0.time_zone", "America/Lima"), 280 resource.TestCheckResourceAttr( 281 "pagerduty_service.foo", "support_hours.0.type", "fixed_time_per_day"), 282 ), 283 }, 284 }, 285 }) 286 } 287 288 func testAccCheckPagerDutyServiceDestroy(s *terraform.State) error { 289 client := testAccProvider.Meta().(*pagerduty.Client) 290 for _, r := range s.RootModule().Resources { 291 if r.Type != "pagerduty_service" { 292 continue 293 } 294 295 _, err := client.GetService(r.Primary.ID, &pagerduty.GetServiceOptions{}) 296 297 if err == nil { 298 return fmt.Errorf("Service still exists") 299 } 300 301 } 302 return nil 303 } 304 305 func testAccCheckPagerDutyServiceExists(n string) resource.TestCheckFunc { 306 return func(s *terraform.State) error { 307 rs, ok := s.RootModule().Resources[n] 308 if !ok { 309 return fmt.Errorf("Not found: %s", n) 310 } 311 312 if rs.Primary.ID == "" { 313 return fmt.Errorf("No Service ID is set") 314 } 315 316 client := testAccProvider.Meta().(*pagerduty.Client) 317 318 found, err := client.GetService(rs.Primary.ID, &pagerduty.GetServiceOptions{}) 319 if err != nil { 320 return err 321 } 322 323 if found.ID != rs.Primary.ID { 324 return fmt.Errorf("Service not found: %v - %v", rs.Primary.ID, found) 325 } 326 327 return nil 328 } 329 } 330 331 const testAccCheckPagerDutyServiceConfig = ` 332 resource "pagerduty_user" "foo" { 333 name = "foo" 334 email = "foo@example.com" 335 color = "green" 336 role = "user" 337 job_title = "foo" 338 description = "foo" 339 } 340 341 resource "pagerduty_escalation_policy" "foo" { 342 name = "bar" 343 description = "bar" 344 num_loops = 2 345 rule { 346 escalation_delay_in_minutes = 10 347 target { 348 type = "user_reference" 349 id = "${pagerduty_user.foo.id}" 350 } 351 } 352 } 353 354 resource "pagerduty_service" "foo" { 355 name = "foo" 356 description = "foo" 357 auto_resolve_timeout = 1800 358 acknowledgement_timeout = 1800 359 escalation_policy = "${pagerduty_escalation_policy.foo.id}" 360 incident_urgency_rule { 361 type = "constant" 362 urgency = "high" 363 } 364 } 365 ` 366 367 const testAccCheckPagerDutyServiceConfigUpdated = ` 368 resource "pagerduty_user" "foo" { 369 name = "foo" 370 email = "foo@example.com" 371 color = "green" 372 role = "user" 373 job_title = "foo" 374 description = "foo" 375 } 376 377 resource "pagerduty_escalation_policy" "foo" { 378 name = "bar" 379 description = "bar" 380 num_loops = 2 381 382 rule { 383 escalation_delay_in_minutes = 10 384 target { 385 type = "user_reference" 386 id = "${pagerduty_user.foo.id}" 387 } 388 } 389 } 390 391 resource "pagerduty_service" "foo" { 392 name = "bar" 393 description = "bar" 394 auto_resolve_timeout = 3600 395 acknowledgement_timeout = 3600 396 397 escalation_policy = "${pagerduty_escalation_policy.foo.id}" 398 incident_urgency_rule { 399 type = "constant" 400 urgency = "high" 401 } 402 } 403 ` 404 405 const testAccCheckPagerDutyServiceWithIncidentUrgencyRulesConfig = ` 406 resource "pagerduty_user" "foo" { 407 name = "foo" 408 email = "foo@example.com" 409 color = "green" 410 role = "user" 411 job_title = "foo" 412 description = "foo" 413 } 414 415 resource "pagerduty_escalation_policy" "foo" { 416 name = "bar" 417 description = "bar" 418 num_loops = 2 419 420 rule { 421 escalation_delay_in_minutes = 10 422 target { 423 type = "user_reference" 424 id = "${pagerduty_user.foo.id}" 425 } 426 } 427 } 428 429 resource "pagerduty_service" "foo" { 430 name = "foo" 431 description = "foo" 432 auto_resolve_timeout = 1800 433 acknowledgement_timeout = 1800 434 escalation_policy = "${pagerduty_escalation_policy.foo.id}" 435 436 incident_urgency_rule { 437 type = "use_support_hours" 438 439 during_support_hours { 440 type = "constant" 441 urgency = "high" 442 } 443 outside_support_hours { 444 type = "constant" 445 urgency = "low" 446 } 447 } 448 449 support_hours = [{ 450 type = "fixed_time_per_day" 451 time_zone = "America/Lima" 452 start_time = "09:00:00" 453 end_time = "17:00:00" 454 days_of_week = [ 1, 2, 3, 4, 5 ] 455 }] 456 457 scheduled_actions { 458 type = "urgency_change" 459 to_urgency = "high" 460 at { 461 type = "named_time", 462 name = "support_hours_start" 463 } 464 } 465 } 466 ` 467 468 const testAccCheckPagerDutyServiceWithIncidentUrgencyRulesConfigUpdated = ` 469 resource "pagerduty_user" "foo" { 470 name = "foo" 471 email = "foo@example.com" 472 color = "green" 473 role = "user" 474 job_title = "foo" 475 description = "foo" 476 } 477 478 resource "pagerduty_escalation_policy" "foo" { 479 name = "bar" 480 description = "bar" 481 num_loops = 2 482 483 rule { 484 escalation_delay_in_minutes = 10 485 target { 486 type = "user_reference" 487 id = "${pagerduty_user.foo.id}" 488 } 489 } 490 } 491 492 resource "pagerduty_service" "foo" { 493 name = "bar" 494 description = "bar bar bar" 495 auto_resolve_timeout = 3600 496 acknowledgement_timeout = 3600 497 escalation_policy = "${pagerduty_escalation_policy.foo.id}" 498 499 incident_urgency_rule { 500 type = "use_support_hours" 501 during_support_hours { 502 type = "constant" 503 urgency = "high" 504 } 505 outside_support_hours { 506 type = "constant" 507 urgency = "low" 508 } 509 } 510 511 support_hours = [{ 512 type = "fixed_time_per_day" 513 time_zone = "America/Lima" 514 start_time = "09:00:00" 515 end_time = "17:00:00" 516 days_of_week = [ 1, 2, 3, 4, 5 ] 517 }] 518 519 scheduled_actions { 520 type = "urgency_change" 521 to_urgency = "high" 522 at { 523 type = "named_time", 524 name = "support_hours_start" 525 } 526 } 527 } 528 `