github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/librato/resource_librato_space_test.go (about)

     1  package librato
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	"github.com/henrikhodne/go-librato/librato"
    12  )
    13  
    14  func TestAccLibratoSpace_Basic(t *testing.T) {
    15  	var space librato.Space
    16  	name := acctest.RandString(10)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckLibratoSpaceDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: testAccCheckLibratoSpaceConfig_basic(name),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckLibratoSpaceExists("librato_space.foobar", &space),
    27  					testAccCheckLibratoSpaceAttributes(&space, name),
    28  					resource.TestCheckResourceAttr(
    29  						"librato_space.foobar", "name", name),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func testAccCheckLibratoSpaceDestroy(s *terraform.State) error {
    37  	client := testAccProvider.Meta().(*librato.Client)
    38  
    39  	for _, rs := range s.RootModule().Resources {
    40  		if rs.Type != "librato_space" {
    41  			continue
    42  		}
    43  
    44  		id, err := strconv.ParseUint(rs.Primary.ID, 10, 0)
    45  		if err != nil {
    46  			return fmt.Errorf("ID not a number")
    47  		}
    48  
    49  		_, _, err = client.Spaces.Get(uint(id))
    50  
    51  		if err == nil {
    52  			return fmt.Errorf("Space still exists")
    53  		}
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  func testAccCheckLibratoSpaceAttributes(space *librato.Space, name string) resource.TestCheckFunc {
    60  	return func(s *terraform.State) error {
    61  
    62  		if space.Name == nil || *space.Name != name {
    63  			return fmt.Errorf("Bad name: %s", *space.Name)
    64  		}
    65  
    66  		return nil
    67  	}
    68  }
    69  
    70  func testAccCheckLibratoSpaceExists(n string, space *librato.Space) resource.TestCheckFunc {
    71  	return func(s *terraform.State) error {
    72  		rs, ok := s.RootModule().Resources[n]
    73  
    74  		if !ok {
    75  			return fmt.Errorf("Not found: %s", n)
    76  		}
    77  
    78  		if rs.Primary.ID == "" {
    79  			return fmt.Errorf("No Space ID is set")
    80  		}
    81  
    82  		client := testAccProvider.Meta().(*librato.Client)
    83  
    84  		id, err := strconv.ParseUint(rs.Primary.ID, 10, 0)
    85  		if err != nil {
    86  			return fmt.Errorf("ID not a number")
    87  		}
    88  
    89  		foundSpace, _, err := client.Spaces.Get(uint(id))
    90  
    91  		if err != nil {
    92  			return err
    93  		}
    94  
    95  		if foundSpace.ID == nil || *foundSpace.ID != uint(id) {
    96  			return fmt.Errorf("Space not found")
    97  		}
    98  
    99  		*space = *foundSpace
   100  
   101  		return nil
   102  	}
   103  }
   104  
   105  func testAccCheckLibratoSpaceConfig_basic(name string) string {
   106  	return fmt.Sprintf(`
   107  resource "librato_space" "foobar" {
   108      name = "%s"
   109  }`, name)
   110  }