github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/profitbricks/resource_profitbricks_datacenter_test.go (about)

     1  package profitbricks
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/profitbricks/profitbricks-sdk-go"
    10  )
    11  
    12  func TestAccProfitBricksDataCenter_Basic(t *testing.T) {
    13  	var datacenter profitbricks.Datacenter
    14  	lanName := "datacenter-test"
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck: func() {
    18  			testAccPreCheck(t)
    19  		},
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckDProfitBricksDatacenterDestroyCheck,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: fmt.Sprintf(testAccCheckProfitBricksDatacenterConfig_basic, lanName),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckProfitBricksDatacenterExists("profitbricks_datacenter.foobar", &datacenter),
    27  					testAccCheckProfitBricksDatacenterAttributes("profitbricks_datacenter.foobar", lanName),
    28  					resource.TestCheckResourceAttr("profitbricks_datacenter.foobar", "name", lanName),
    29  				),
    30  			},
    31  			resource.TestStep{
    32  				Config: testAccCheckProfitBricksDatacenterConfig_update,
    33  				Check: resource.ComposeTestCheckFunc(
    34  					testAccCheckProfitBricksDatacenterExists("profitbricks_datacenter.foobar", &datacenter),
    35  					testAccCheckProfitBricksDatacenterAttributes("profitbricks_datacenter.foobar", "updated"),
    36  					resource.TestCheckResourceAttr("profitbricks_datacenter.foobar", "name", "updated"),
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  func testAccCheckDProfitBricksDatacenterDestroyCheck(s *terraform.State) error {
    44  	for _, rs := range s.RootModule().Resources {
    45  		if rs.Type != "profitbricks_datacenter" {
    46  			continue
    47  		}
    48  
    49  		resp := profitbricks.GetDatacenter(rs.Primary.ID)
    50  
    51  		if resp.StatusCode < 299 {
    52  			return fmt.Errorf("DataCenter still exists %s %s", rs.Primary.ID, resp.Response)
    53  		}
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  func testAccCheckProfitBricksDatacenterAttributes(n string, name string) resource.TestCheckFunc {
    60  	return func(s *terraform.State) error {
    61  		rs, ok := s.RootModule().Resources[n]
    62  		if !ok {
    63  			return fmt.Errorf("Not found: %s", n)
    64  		}
    65  		if rs.Primary.Attributes["name"] != name {
    66  			return fmt.Errorf("Bad name: expected %s : found %s ", name, rs.Primary.Attributes["name"])
    67  		}
    68  
    69  		return nil
    70  	}
    71  }
    72  
    73  func testAccCheckProfitBricksDatacenterExists(n string, datacenter *profitbricks.Datacenter) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		rs, ok := s.RootModule().Resources[n]
    76  
    77  		if !ok {
    78  			return fmt.Errorf("Not found: %s", n)
    79  		}
    80  
    81  		if rs.Primary.ID == "" {
    82  			return fmt.Errorf("No Record ID is set")
    83  		}
    84  
    85  		foundDC := profitbricks.GetDatacenter(rs.Primary.ID)
    86  
    87  		if foundDC.StatusCode != 200 {
    88  			return fmt.Errorf("Error occured while fetching DC: %s", rs.Primary.ID)
    89  		}
    90  		if foundDC.Id != rs.Primary.ID {
    91  			return fmt.Errorf("Record not found")
    92  		}
    93  		datacenter = &foundDC
    94  
    95  		return nil
    96  	}
    97  }
    98  
    99  const testAccCheckProfitBricksDatacenterConfig_basic = `
   100  resource "profitbricks_datacenter" "foobar" {
   101  	name       = "%s"
   102  	location = "us/las"
   103  }`
   104  
   105  const testAccCheckProfitBricksDatacenterConfig_update = `
   106  resource "profitbricks_datacenter" "foobar" {
   107  	name       =  "updated"
   108  	location = "us/las"
   109  }`