github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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  					resource.TestCheckResourceAttr(
    30  						"datadog_user.foo", "verified", "false"),
    31  				),
    32  			},
    33  			resource.TestStep{
    34  				Config: testAccCheckDatadogUserConfigUpdated,
    35  				Check: resource.ComposeTestCheckFunc(
    36  					testAccCheckDatadogUserExists("datadog_user.foo"),
    37  					resource.TestCheckResourceAttr(
    38  						"datadog_user.foo", "disabled", "true"),
    39  					resource.TestCheckResourceAttr(
    40  						"datadog_user.foo", "email", "updated@example.com"),
    41  					resource.TestCheckResourceAttr(
    42  						"datadog_user.foo", "handle", "test@example.com"),
    43  					resource.TestCheckResourceAttr(
    44  						"datadog_user.foo", "is_admin", "true"),
    45  					resource.TestCheckResourceAttr(
    46  						"datadog_user.foo", "name", "Updated User"),
    47  					resource.TestCheckResourceAttr(
    48  						"datadog_user.foo", "verified", "false"),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func testAccCheckDatadogUserDestroy(s *terraform.State) error {
    56  	client := testAccProvider.Meta().(*datadog.Client)
    57  
    58  	if err := datadogUserDestroyHelper(s, client); err != nil {
    59  		return err
    60  	}
    61  	return nil
    62  }
    63  
    64  func testAccCheckDatadogUserExists(n string) resource.TestCheckFunc {
    65  	return func(s *terraform.State) error {
    66  		client := testAccProvider.Meta().(*datadog.Client)
    67  		if err := datadogUserExistsHelper(s, client); err != nil {
    68  			return err
    69  		}
    70  		return nil
    71  	}
    72  }
    73  
    74  const testAccCheckDatadogUserConfigRequired = `
    75  resource "datadog_user" "foo" {
    76    email  = "test@example.com"
    77    handle = "test@example.com"
    78    name   = "Test User"
    79  }
    80  `
    81  
    82  const testAccCheckDatadogUserConfigUpdated = `
    83  resource "datadog_user" "foo" {
    84    disabled = true
    85    email    = "updated@example.com"
    86    handle   = "test@example.com"
    87    is_admin = true
    88    name     = "Updated User"
    89  }
    90  `
    91  
    92  func datadogUserDestroyHelper(s *terraform.State, client *datadog.Client) error {
    93  	for _, r := range s.RootModule().Resources {
    94  		id := r.Primary.ID
    95  		u, err := client.GetUser(id)
    96  
    97  		if err != nil {
    98  			if strings.Contains(err.Error(), "404 Not Found") {
    99  				continue
   100  			}
   101  			return fmt.Errorf("Received an error retrieving user %s", err)
   102  		}
   103  
   104  		// Datadog only disables user on DELETE
   105  		if u.GetDisabled() {
   106  			continue
   107  		}
   108  		return fmt.Errorf("User still exists")
   109  	}
   110  	return nil
   111  }
   112  
   113  func datadogUserExistsHelper(s *terraform.State, client *datadog.Client) error {
   114  	for _, r := range s.RootModule().Resources {
   115  		id := r.Primary.ID
   116  		if _, err := client.GetUser(id); err != nil {
   117  			return fmt.Errorf("Received an error retrieving user %s", err)
   118  		}
   119  	}
   120  	return nil
   121  }