github.com/richardmarshall/terraform@v0.9.5-0.20170429023105-15704cc6ee35/builtin/providers/datadog/resource_datadog_user_test.go (about) 1 package datadog 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 "gopkg.in/zorkian/go-datadog-api.v2" 11 ) 12 13 func TestAccDatadogUser_Updated(t *testing.T) { 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 Providers: testAccProviders, 17 CheckDestroy: testAccCheckDatadogUserDestroy, 18 Steps: []resource.TestStep{ 19 resource.TestStep{ 20 Config: testAccCheckDatadogUserConfigRequired, 21 Check: resource.ComposeTestCheckFunc( 22 testAccCheckDatadogUserExists("datadog_user.foo"), 23 resource.TestCheckResourceAttr( 24 "datadog_user.foo", "email", "test@example.com"), 25 resource.TestCheckResourceAttr( 26 "datadog_user.foo", "handle", "test@example.com"), 27 resource.TestCheckResourceAttr( 28 "datadog_user.foo", "name", "Test User"), 29 ), 30 }, 31 resource.TestStep{ 32 Config: testAccCheckDatadogUserConfigUpdated, 33 Check: resource.ComposeTestCheckFunc( 34 testAccCheckDatadogUserExists("datadog_user.foo"), 35 resource.TestCheckResourceAttr( 36 "datadog_user.foo", "disabled", "true"), 37 resource.TestCheckResourceAttr( 38 "datadog_user.foo", "email", "updated@example.com"), 39 resource.TestCheckResourceAttr( 40 "datadog_user.foo", "handle", "test@example.com"), 41 resource.TestCheckResourceAttr( 42 "datadog_user.foo", "is_admin", "true"), 43 resource.TestCheckResourceAttr( 44 "datadog_user.foo", "name", "Updated User"), 45 ), 46 }, 47 }, 48 }) 49 } 50 51 func testAccCheckDatadogUserDestroy(s *terraform.State) error { 52 client := testAccProvider.Meta().(*datadog.Client) 53 54 if err := datadogUserDestroyHelper(s, client); err != nil { 55 return err 56 } 57 return nil 58 } 59 60 func testAccCheckDatadogUserExists(n string) resource.TestCheckFunc { 61 return func(s *terraform.State) error { 62 client := testAccProvider.Meta().(*datadog.Client) 63 if err := datadogUserExistsHelper(s, client); err != nil { 64 return err 65 } 66 return nil 67 } 68 } 69 70 const testAccCheckDatadogUserConfigRequired = ` 71 resource "datadog_user" "foo" { 72 email = "test@example.com" 73 handle = "test@example.com" 74 name = "Test User" 75 } 76 ` 77 78 const testAccCheckDatadogUserConfigUpdated = ` 79 resource "datadog_user" "foo" { 80 disabled = true 81 email = "updated@example.com" 82 handle = "test@example.com" 83 is_admin = true 84 name = "Updated User" 85 } 86 ` 87 88 func datadogUserDestroyHelper(s *terraform.State, client *datadog.Client) error { 89 for _, r := range s.RootModule().Resources { 90 id := r.Primary.ID 91 u, err := client.GetUser(id) 92 93 if err != nil { 94 if strings.Contains(err.Error(), "404 Not Found") { 95 continue 96 } 97 return fmt.Errorf("Received an error retrieving user %s", err) 98 } 99 100 // Datadog only disables user on DELETE 101 if u.GetDisabled() { 102 continue 103 } 104 return fmt.Errorf("User still exists") 105 } 106 return nil 107 } 108 109 func datadogUserExistsHelper(s *terraform.State, client *datadog.Client) error { 110 for _, r := range s.RootModule().Resources { 111 id := r.Primary.ID 112 if _, err := client.GetUser(id); err != nil { 113 return fmt.Errorf("Received an error retrieving user %s", err) 114 } 115 } 116 return nil 117 }