ns1: Fix incorrect schema for 'ns1_user.notify' (#12721)

* ns1: Add acceptance test for ns1_user

* ns1: Fix incorrect schema for 'notify'
This commit is contained in:
Radek Simko 2017-03-15 14:39:26 +00:00 committed by GitHub
parent cfb8f8326a
commit fff0b694f7
2 changed files with 103 additions and 8 deletions

View File

@ -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,

View File

@ -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)
}