github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/pagerduty/resource_pagerduty_user_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 TestAccPagerDutyUser_Basic(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 CheckDestroy: testAccCheckPagerDutyUserDestroy, 17 Steps: []resource.TestStep{ 18 resource.TestStep{ 19 Config: testAccCheckPagerDutyUserConfig, 20 Check: resource.ComposeTestCheckFunc( 21 testAccCheckPagerDutyUserExists("pagerduty_user.foo"), 22 resource.TestCheckResourceAttr( 23 "pagerduty_user.foo", "name", "foo"), 24 resource.TestCheckResourceAttr( 25 "pagerduty_user.foo", "email", "foo@bar.com"), 26 resource.TestCheckResourceAttr( 27 "pagerduty_user.foo", "color", "green"), 28 resource.TestCheckResourceAttr( 29 "pagerduty_user.foo", "role", "user"), 30 resource.TestCheckResourceAttr( 31 "pagerduty_user.foo", "job_title", "foo"), 32 resource.TestCheckResourceAttr( 33 "pagerduty_user.foo", "description", "foo"), 34 ), 35 }, 36 resource.TestStep{ 37 Config: testAccCheckPagerDutyUserConfigUpdated, 38 Check: resource.ComposeTestCheckFunc( 39 testAccCheckPagerDutyUserExists("pagerduty_user.foo"), 40 resource.TestCheckResourceAttr( 41 "pagerduty_user.foo", "name", "bar"), 42 resource.TestCheckResourceAttr( 43 "pagerduty_user.foo", "email", "bar@foo.com"), 44 resource.TestCheckResourceAttr( 45 "pagerduty_user.foo", "color", "red"), 46 resource.TestCheckResourceAttr( 47 "pagerduty_user.foo", "role", "team_responder"), 48 resource.TestCheckResourceAttr( 49 "pagerduty_user.foo", "job_title", "bar"), 50 resource.TestCheckResourceAttr( 51 "pagerduty_user.foo", "description", "bar"), 52 ), 53 }, 54 }, 55 }) 56 } 57 58 func TestAccPagerDutyUserWithTeams_Basic(t *testing.T) { 59 resource.Test(t, resource.TestCase{ 60 PreCheck: func() { testAccPreCheck(t) }, 61 Providers: testAccProviders, 62 CheckDestroy: testAccCheckPagerDutyUserDestroy, 63 Steps: []resource.TestStep{ 64 resource.TestStep{ 65 Config: testAccCheckPagerDutyUserWithTeamsConfig, 66 Check: resource.ComposeTestCheckFunc( 67 testAccCheckPagerDutyUserExists("pagerduty_user.foo"), 68 resource.TestCheckResourceAttr( 69 "pagerduty_user.foo", "name", "foo"), 70 resource.TestCheckResourceAttr( 71 "pagerduty_user.foo", "email", "foo@bar.com"), 72 resource.TestCheckResourceAttr( 73 "pagerduty_user.foo", "teams.#", "1"), 74 ), 75 }, 76 resource.TestStep{ 77 Config: testAccCheckPagerDutyUserWithTeamsConfigUpdated, 78 Check: resource.ComposeTestCheckFunc( 79 testAccCheckPagerDutyUserExists("pagerduty_user.foo"), 80 resource.TestCheckResourceAttr( 81 "pagerduty_user.foo", "name", "foo"), 82 resource.TestCheckResourceAttr( 83 "pagerduty_user.foo", "email", "foo@bar.com"), 84 resource.TestCheckResourceAttr( 85 "pagerduty_user.foo", "teams.#", "2"), 86 ), 87 }, 88 resource.TestStep{ 89 Config: testAccCheckPagerDutyUserWithNoTeamsConfig, 90 Check: resource.ComposeTestCheckFunc( 91 testAccCheckPagerDutyUserExists("pagerduty_user.foo"), 92 resource.TestCheckResourceAttr( 93 "pagerduty_user.foo", "name", "foo"), 94 resource.TestCheckResourceAttr( 95 "pagerduty_user.foo", "email", "foo@bar.com"), 96 resource.TestCheckResourceAttr( 97 "pagerduty_user.foo", "teams.#", "0"), 98 ), 99 }, 100 }, 101 }) 102 } 103 104 func testAccCheckPagerDutyUserDestroy(s *terraform.State) error { 105 client := testAccProvider.Meta().(*pagerduty.Client) 106 for _, r := range s.RootModule().Resources { 107 if r.Type != "pagerduty_user" { 108 continue 109 } 110 111 opts := pagerduty.GetUserOptions{} 112 113 _, err := client.GetUser(r.Primary.ID, opts) 114 115 if err == nil { 116 return fmt.Errorf("User still exists") 117 } 118 119 } 120 return nil 121 } 122 123 func testAccCheckPagerDutyUserExists(n string) resource.TestCheckFunc { 124 return func(s *terraform.State) error { 125 rs, ok := s.RootModule().Resources[n] 126 if !ok { 127 return fmt.Errorf("Not found: %s", n) 128 } 129 if rs.Primary.ID == "" { 130 return fmt.Errorf("No user ID is set") 131 } 132 133 client := testAccProvider.Meta().(*pagerduty.Client) 134 135 found, err := client.GetUser(rs.Primary.ID, pagerduty.GetUserOptions{}) 136 if err != nil { 137 return err 138 } 139 140 if found.ID != rs.Primary.ID { 141 return fmt.Errorf("User not found: %v - %v", rs.Primary.ID, found) 142 } 143 144 return nil 145 } 146 } 147 148 const testAccCheckPagerDutyUserConfig = ` 149 resource "pagerduty_user" "foo" { 150 name = "foo" 151 email = "foo@bar.com" 152 color = "green" 153 role = "user" 154 job_title = "foo" 155 description = "foo" 156 } 157 ` 158 159 const testAccCheckPagerDutyUserConfigUpdated = ` 160 resource "pagerduty_user" "foo" { 161 name = "bar" 162 email = "bar@foo.com" 163 color = "red" 164 role = "team_responder" 165 job_title = "bar" 166 description = "bar" 167 } 168 ` 169 170 const testAccCheckPagerDutyUserWithTeamsConfig = ` 171 resource "pagerduty_team" "foo" { 172 name = "Foo team" 173 } 174 175 resource "pagerduty_user" "foo" { 176 name = "foo" 177 email = "foo@bar.com" 178 teams = ["${pagerduty_team.foo.id}"] 179 } 180 ` 181 const testAccCheckPagerDutyUserWithTeamsConfigUpdated = ` 182 resource "pagerduty_team" "foo" { 183 name = "Foo team" 184 } 185 186 resource "pagerduty_team" "bar" { 187 name = "Bar team" 188 } 189 190 resource "pagerduty_user" "foo" { 191 name = "foo" 192 email = "foo@bar.com" 193 teams = ["${pagerduty_team.foo.id}", "${pagerduty_team.bar.id}"] 194 } 195 ` 196 197 const testAccCheckPagerDutyUserWithNoTeamsConfig = ` 198 resource "pagerduty_team" "foo" { 199 name = "Foo team" 200 } 201 202 resource "pagerduty_team" "bar" { 203 name = "Bar team" 204 } 205 206 resource "pagerduty_user" "foo" { 207 name = "foo" 208 email = "foo@bar.com" 209 } 210 `