github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/ns1/resource_user_test.go (about) 1 package ns1 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/acctest" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 11 ns1 "gopkg.in/ns1/ns1-go.v2/rest" 12 "gopkg.in/ns1/ns1-go.v2/rest/model/account" 13 ) 14 15 func TestAccUser_basic(t *testing.T) { 16 var user account.User 17 rString := acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum) 18 name := fmt.Sprintf("terraform acc test user %s", rString) 19 username := fmt.Sprintf("tf_acc_test_user_%s", rString) 20 21 resource.Test(t, resource.TestCase{ 22 PreCheck: func() { testAccPreCheck(t) }, 23 Providers: testAccProviders, 24 CheckDestroy: testAccCheckUserDestroy, 25 Steps: []resource.TestStep{ 26 resource.TestStep{ 27 Config: testAccUserBasic(rString), 28 Check: resource.ComposeTestCheckFunc( 29 testAccCheckUserExists("ns1_user.u", &user), 30 resource.TestCheckResourceAttr("ns1_user.u", "email", "tf_acc_test_ns1@hashicorp.com"), 31 resource.TestCheckResourceAttr("ns1_user.u", "name", name), 32 resource.TestCheckResourceAttr("ns1_user.u", "teams.#", "1"), 33 resource.TestCheckResourceAttr("ns1_user.u", "notify.%", "1"), 34 resource.TestCheckResourceAttr("ns1_user.u", "notify.billing", "true"), 35 resource.TestCheckResourceAttr("ns1_user.u", "username", username), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func testAccCheckUserDestroy(s *terraform.State) error { 43 client := testAccProvider.Meta().(*ns1.Client) 44 45 for _, rs := range s.RootModule().Resources { 46 if rs.Type != "ns1_user" { 47 continue 48 } 49 50 user, _, err := client.Users.Get(rs.Primary.Attributes["id"]) 51 if err == nil { 52 return fmt.Errorf("User still exists: %#v: %#v", err, user.Name) 53 } 54 } 55 56 return nil 57 } 58 59 func testAccCheckUserExists(n string, user *account.User) resource.TestCheckFunc { 60 return func(s *terraform.State) error { 61 rs, ok := s.RootModule().Resources[n] 62 if !ok { 63 return fmt.Errorf("Not found: %s", n) 64 } 65 66 if rs.Primary.ID == "" { 67 return fmt.Errorf("No ID is set") 68 } 69 70 client := testAccProvider.Meta().(*ns1.Client) 71 72 foundUser, _, err := client.Users.Get(rs.Primary.ID) 73 if err != nil { 74 return err 75 } 76 77 if foundUser.Username != rs.Primary.ID { 78 return fmt.Errorf("User not found (%#v != %s)", foundUser, rs.Primary.ID) 79 } 80 81 *user = *foundUser 82 83 return nil 84 } 85 } 86 87 func testAccUserBasic(rString string) string { 88 return fmt.Sprintf(`resource "ns1_team" "t" { 89 name = "terraform acc test team %s" 90 } 91 92 resource "ns1_user" "u" { 93 name = "terraform acc test user %s" 94 username = "tf_acc_test_user_%s" 95 email = "tf_acc_test_ns1@hashicorp.com" 96 teams = ["${ns1_team.t.id}"] 97 notify { 98 billing = true 99 } 100 } 101 `, rString, rString, rString) 102 }