terraform/builtin/providers/profitbricks/resource_profitbricks_datac...

110 lines
3.0 KiB
Go
Raw Normal View History

package profitbricks
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/profitbricks/profitbricks-sdk-go"
)
func TestAccProfitBricksDataCenter_Basic(t *testing.T) {
var datacenter profitbricks.Datacenter
2017-03-25 21:43:41 +01:00
dc_name := "datacenter-test"
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckDProfitBricksDatacenterDestroyCheck,
Steps: []resource.TestStep{
resource.TestStep{
2017-03-25 21:43:41 +01:00
Config: fmt.Sprintf(testAccCheckProfitBricksDatacenterConfig_basic, dc_name),
Check: resource.ComposeTestCheckFunc(
testAccCheckProfitBricksDatacenterExists("profitbricks_datacenter.foobar", &datacenter),
2017-03-25 21:43:41 +01:00
testAccCheckProfitBricksDatacenterAttributes("profitbricks_datacenter.foobar", dc_name),
resource.TestCheckResourceAttr("profitbricks_datacenter.foobar", "name", dc_name),
),
},
resource.TestStep{
Config: testAccCheckProfitBricksDatacenterConfig_update,
Check: resource.ComposeTestCheckFunc(
testAccCheckProfitBricksDatacenterExists("profitbricks_datacenter.foobar", &datacenter),
testAccCheckProfitBricksDatacenterAttributes("profitbricks_datacenter.foobar", "updated"),
resource.TestCheckResourceAttr("profitbricks_datacenter.foobar", "name", "updated"),
),
},
},
})
}
func testAccCheckDProfitBricksDatacenterDestroyCheck(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "profitbricks_datacenter" {
continue
}
resp := profitbricks.GetDatacenter(rs.Primary.ID)
if resp.StatusCode < 299 {
return fmt.Errorf("DataCenter still exists %s %s", rs.Primary.ID, resp.Response)
}
}
return nil
}
func testAccCheckProfitBricksDatacenterAttributes(n string, name string) 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.Attributes["name"] != name {
return fmt.Errorf("Bad name: expected %s : found %s ", name, rs.Primary.Attributes["name"])
}
return nil
}
}
func testAccCheckProfitBricksDatacenterExists(n string, datacenter *profitbricks.Datacenter) 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 Record ID is set")
}
foundDC := profitbricks.GetDatacenter(rs.Primary.ID)
if foundDC.StatusCode != 200 {
return fmt.Errorf("Error occured while fetching DC: %s", rs.Primary.ID)
}
if foundDC.Id != rs.Primary.ID {
return fmt.Errorf("Record not found")
}
datacenter = &foundDC
return nil
}
}
const testAccCheckProfitBricksDatacenterConfig_basic = `
resource "profitbricks_datacenter" "foobar" {
name = "%s"
location = "us/las"
}`
const testAccCheckProfitBricksDatacenterConfig_update = `
resource "profitbricks_datacenter" "foobar" {
name = "updated"
location = "us/las"
}`