github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/pagerduty/resource_pagerduty_service_integration_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 TestAccPagerDutyServiceIntegration_Basic(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 CheckDestroy: testAccCheckPagerDutyServiceIntegrationDestroy, 17 Steps: []resource.TestStep{ 18 resource.TestStep{ 19 Config: testAccCheckPagerDutyServiceIntegrationConfig, 20 Check: resource.ComposeTestCheckFunc( 21 testAccCheckPagerDutyServiceIntegrationExists("pagerduty_service_integration.foo"), 22 resource.TestCheckResourceAttr( 23 "pagerduty_service_integration.foo", "name", "foo"), 24 resource.TestCheckResourceAttr( 25 "pagerduty_service_integration.foo", "type", "generic_events_api_inbound_integration"), 26 resource.TestCheckResourceAttr( 27 "pagerduty_service_integration.foo", "vendor", "PAM4FGS"), 28 ), 29 }, 30 resource.TestStep{ 31 Config: testAccCheckPagerDutyServiceIntegrationConfigUpdated, 32 Check: resource.ComposeTestCheckFunc( 33 testAccCheckPagerDutyServiceIntegrationExists("pagerduty_service_integration.foo"), 34 resource.TestCheckResourceAttr( 35 "pagerduty_service_integration.foo", "name", "bar"), 36 resource.TestCheckResourceAttr( 37 "pagerduty_service_integration.foo", "type", "generic_events_api_inbound_integration"), 38 resource.TestCheckResourceAttr( 39 "pagerduty_service_integration.foo", "vendor", "PAM4FGS"), 40 ), 41 }, 42 }, 43 }) 44 } 45 46 func TestAccPagerDutyServiceIntegrationGeneric_Basic(t *testing.T) { 47 resource.Test(t, resource.TestCase{ 48 PreCheck: func() { testAccPreCheck(t) }, 49 Providers: testAccProviders, 50 CheckDestroy: testAccCheckPagerDutyServiceIntegrationDestroy, 51 Steps: []resource.TestStep{ 52 resource.TestStep{ 53 Config: testAccCheckPagerDutyServiceIntegrationGenericConfig, 54 Check: resource.ComposeTestCheckFunc( 55 testAccCheckPagerDutyServiceIntegrationExists("pagerduty_service_integration.foo"), 56 resource.TestCheckResourceAttr( 57 "pagerduty_service_integration.foo", "name", "foo"), 58 resource.TestCheckResourceAttr( 59 "pagerduty_service_integration.foo", "type", "generic_events_api_inbound_integration"), 60 ), 61 }, 62 resource.TestStep{ 63 Config: testAccCheckPagerDutyServiceIntegrationGenericConfigUpdated, 64 Check: resource.ComposeTestCheckFunc( 65 testAccCheckPagerDutyServiceIntegrationExists("pagerduty_service_integration.foo"), 66 resource.TestCheckResourceAttr( 67 "pagerduty_service_integration.foo", "name", "bar"), 68 resource.TestCheckResourceAttr( 69 "pagerduty_service_integration.foo", "type", "generic_events_api_inbound_integration"), 70 ), 71 }, 72 }, 73 }) 74 } 75 76 func testAccCheckPagerDutyServiceIntegrationDestroy(s *terraform.State) error { 77 client := testAccProvider.Meta().(*pagerduty.Client) 78 for _, r := range s.RootModule().Resources { 79 if r.Type != "pagerduty_service_integration" { 80 continue 81 } 82 83 service, _ := s.RootModule().Resources["pagerduty_service.foo"] 84 85 _, err := client.GetIntegration(service.Primary.ID, r.Primary.ID, pagerduty.GetIntegrationOptions{}) 86 87 if err == nil { 88 return fmt.Errorf("Service Integration still exists") 89 } 90 91 } 92 return nil 93 } 94 95 func testAccCheckPagerDutyServiceIntegrationExists(n string) resource.TestCheckFunc { 96 return func(s *terraform.State) error { 97 rs, ok := s.RootModule().Resources[n] 98 99 if !ok { 100 return fmt.Errorf("Not found: %s", n) 101 } 102 if rs.Primary.ID == "" { 103 return fmt.Errorf("No Service Integration ID is set") 104 } 105 106 service, _ := s.RootModule().Resources["pagerduty_service.foo"] 107 108 client := testAccProvider.Meta().(*pagerduty.Client) 109 110 found, err := client.GetIntegration(service.Primary.ID, rs.Primary.ID, pagerduty.GetIntegrationOptions{}) 111 if err != nil { 112 return fmt.Errorf("Service integration not found: %v", rs.Primary.ID) 113 } 114 115 if found.ID != rs.Primary.ID { 116 return fmt.Errorf("Service Integration not found: %v - %v", rs.Primary.ID, found) 117 } 118 119 return nil 120 } 121 } 122 123 const testAccCheckPagerDutyServiceIntegrationConfig = ` 124 resource "pagerduty_user" "foo" { 125 name = "foo" 126 email = "foo@bar.com" 127 } 128 129 resource "pagerduty_escalation_policy" "foo" { 130 name = "foo" 131 description = "foo" 132 num_loops = 1 133 134 rule { 135 escalation_delay_in_minutes = 10 136 137 target { 138 type = "user_reference" 139 id = "${pagerduty_user.foo.id}" 140 } 141 } 142 } 143 144 resource "pagerduty_service" "foo" { 145 name = "foo" 146 description = "foo" 147 auto_resolve_timeout = 1800 148 acknowledgement_timeout = 1800 149 escalation_policy = "${pagerduty_escalation_policy.foo.id}" 150 151 incident_urgency_rule { 152 type = "constant" 153 urgency = "high" 154 } 155 } 156 157 data "pagerduty_vendor" "datadog" { 158 name = "datadog" 159 } 160 161 resource "pagerduty_service_integration" "foo" { 162 name = "foo" 163 service = "${pagerduty_service.foo.id}" 164 vendor = "${data.pagerduty_vendor.datadog.id}" 165 } 166 ` 167 168 const testAccCheckPagerDutyServiceIntegrationConfigUpdated = ` 169 resource "pagerduty_user" "foo" { 170 name = "foo" 171 email = "foo@bar.com" 172 color = "green" 173 role = "user" 174 job_title = "foo" 175 description = "foo" 176 } 177 178 resource "pagerduty_escalation_policy" "foo" { 179 name = "bar" 180 description = "bar" 181 num_loops = 2 182 183 rule { 184 escalation_delay_in_minutes = 10 185 186 target { 187 type = "user_reference" 188 id = "${pagerduty_user.foo.id}" 189 } 190 } 191 } 192 193 resource "pagerduty_service" "foo" { 194 name = "bar" 195 description = "bar" 196 auto_resolve_timeout = 3600 197 acknowledgement_timeout = 3600 198 escalation_policy = "${pagerduty_escalation_policy.foo.id}" 199 200 incident_urgency_rule { 201 type = "constant" 202 urgency = "high" 203 } 204 } 205 206 data "pagerduty_vendor" "datadog" { 207 name = "datadog" 208 } 209 210 resource "pagerduty_service_integration" "foo" { 211 name = "bar" 212 service = "${pagerduty_service.foo.id}" 213 vendor = "${data.pagerduty_vendor.datadog.id}" 214 } 215 ` 216 217 const testAccCheckPagerDutyServiceIntegrationGenericConfig = ` 218 resource "pagerduty_user" "foo" { 219 name = "foo" 220 email = "foo@bar.com" 221 } 222 223 resource "pagerduty_escalation_policy" "foo" { 224 name = "foo" 225 description = "foo" 226 num_loops = 1 227 228 rule { 229 escalation_delay_in_minutes = 10 230 231 target { 232 type = "user_reference" 233 id = "${pagerduty_user.foo.id}" 234 } 235 } 236 } 237 238 resource "pagerduty_service" "foo" { 239 name = "foo" 240 description = "foo" 241 auto_resolve_timeout = 1800 242 acknowledgement_timeout = 1800 243 escalation_policy = "${pagerduty_escalation_policy.foo.id}" 244 245 incident_urgency_rule { 246 type = "constant" 247 urgency = "high" 248 } 249 } 250 251 resource "pagerduty_service_integration" "foo" { 252 name = "foo" 253 service = "${pagerduty_service.foo.id}" 254 type = "generic_events_api_inbound_integration" 255 } 256 ` 257 258 const testAccCheckPagerDutyServiceIntegrationGenericConfigUpdated = ` 259 resource "pagerduty_user" "foo" { 260 name = "foo" 261 email = "foo@bar.com" 262 color = "green" 263 role = "user" 264 job_title = "foo" 265 description = "foo" 266 } 267 268 resource "pagerduty_escalation_policy" "foo" { 269 name = "bar" 270 description = "bar" 271 num_loops = 2 272 273 rule { 274 escalation_delay_in_minutes = 10 275 276 target { 277 type = "user_reference" 278 id = "${pagerduty_user.foo.id}" 279 } 280 } 281 } 282 283 resource "pagerduty_service" "foo" { 284 name = "bar" 285 description = "bar" 286 auto_resolve_timeout = 3600 287 acknowledgement_timeout = 3600 288 escalation_policy = "${pagerduty_escalation_policy.foo.id}" 289 290 incident_urgency_rule { 291 type = "constant" 292 urgency = "high" 293 } 294 } 295 296 resource "pagerduty_service_integration" "foo" { 297 name = "bar" 298 service = "${pagerduty_service.foo.id}" 299 type = "generic_events_api_inbound_integration" 300 } 301 `