diff --git a/builtin/providers/ns1/resource_user.go b/builtin/providers/ns1/resource_user.go index 0add2b429..012d021f9 100644 --- a/builtin/providers/ns1/resource_user.go +++ b/builtin/providers/ns1/resource_user.go @@ -28,14 +28,7 @@ func userResource() *schema.Resource { "notify": &schema.Schema{ Type: schema.TypeMap, Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "billing": &schema.Schema{ - Type: schema.TypeBool, - Required: true, - }, - }, - }, + Elem: schema.TypeBool, }, "teams": &schema.Schema{ Type: schema.TypeList, diff --git a/builtin/providers/ns1/resource_user_test.go b/builtin/providers/ns1/resource_user_test.go new file mode 100644 index 000000000..b32d7e453 --- /dev/null +++ b/builtin/providers/ns1/resource_user_test.go @@ -0,0 +1,102 @@ +package ns1 + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + + ns1 "gopkg.in/ns1/ns1-go.v2/rest" + "gopkg.in/ns1/ns1-go.v2/rest/model/account" +) + +func TestAccUser_basic(t *testing.T) { + var user account.User + rString := acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum) + name := fmt.Sprintf("terraform acc test user %s", rString) + username := fmt.Sprintf("tf_acc_test_user_%s", rString) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckUserDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccUserBasic(rString), + Check: resource.ComposeTestCheckFunc( + testAccCheckUserExists("ns1_user.u", &user), + resource.TestCheckResourceAttr("ns1_user.u", "email", "tf_acc_test_ns1@hashicorp.com"), + resource.TestCheckResourceAttr("ns1_user.u", "name", name), + resource.TestCheckResourceAttr("ns1_user.u", "teams.#", "1"), + resource.TestCheckResourceAttr("ns1_user.u", "notify.%", "1"), + resource.TestCheckResourceAttr("ns1_user.u", "notify.billing", "true"), + resource.TestCheckResourceAttr("ns1_user.u", "username", username), + ), + }, + }, + }) +} + +func testAccCheckUserDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*ns1.Client) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "ns1_user" { + continue + } + + user, _, err := client.Users.Get(rs.Primary.Attributes["id"]) + if err == nil { + return fmt.Errorf("User still exists: %#v: %#v", err, user.Name) + } + } + + return nil +} + +func testAccCheckUserExists(n string, user *account.User) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + client := testAccProvider.Meta().(*ns1.Client) + + foundUser, _, err := client.Users.Get(rs.Primary.ID) + if err != nil { + return err + } + + if foundUser.Username != rs.Primary.ID { + return fmt.Errorf("User not found (%#v != %s)", foundUser, rs.Primary.ID) + } + + *user = *foundUser + + return nil + } +} + +func testAccUserBasic(rString string) string { + return fmt.Sprintf(`resource "ns1_team" "t" { + name = "terraform acc test team %s" +} + +resource "ns1_user" "u" { + name = "terraform acc test user %s" + username = "tf_acc_test_user_%s" + email = "tf_acc_test_ns1@hashicorp.com" + teams = ["${ns1_team.t.id}"] + notify { + billing = true + } +} +`, rString, rString, rString) +}