github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/builtin/providers/profitbricks/resource_profitbricks_ipblock_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 TestAccProfitBricksIPBlock_Basic(t *testing.T) { 13 var ipblock profitbricks.IpBlock 14 location := "us/las" 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { 18 testAccPreCheck(t) 19 }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckDProfitBricksIPBlockDestroyCheck, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: fmt.Sprintf(testAccCheckProfitbricksIPBlockConfig_basic, location), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckProfitBricksIPBlockExists("profitbricks_ipblock.webserver_ip", &ipblock), 27 testAccCheckProfitBricksIPBlockAttributes("profitbricks_ipblock.webserver_ip", location), 28 resource.TestCheckResourceAttr("profitbricks_ipblock.webserver_ip", "location", location), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func testAccCheckDProfitBricksIPBlockDestroyCheck(s *terraform.State) error { 36 for _, rs := range s.RootModule().Resources { 37 if rs.Type != "profitbricks_ipblock" { 38 continue 39 } 40 41 resp := profitbricks.GetIpBlock(rs.Primary.ID) 42 43 if resp.StatusCode < 299 { 44 return fmt.Errorf("IPBlock still exists %s %s", rs.Primary.ID, resp.Response) 45 } 46 } 47 48 return nil 49 } 50 51 func testAccCheckProfitBricksIPBlockAttributes(n string, location string) resource.TestCheckFunc { 52 return func(s *terraform.State) error { 53 rs, ok := s.RootModule().Resources[n] 54 if !ok { 55 return fmt.Errorf("testAccCheckProfitBricksLanAttributes: Not found: %s", n) 56 } 57 if rs.Primary.Attributes["location"] != location { 58 return fmt.Errorf("Bad name: %s", rs.Primary.Attributes["location"]) 59 } 60 61 return nil 62 } 63 } 64 65 func testAccCheckProfitBricksIPBlockExists(n string, ipblock *profitbricks.IpBlock) resource.TestCheckFunc { 66 return func(s *terraform.State) error { 67 rs, ok := s.RootModule().Resources[n] 68 69 if !ok { 70 return fmt.Errorf("testAccCheckProfitBricksIPBlockExists: Not found: %s", n) 71 } 72 73 if rs.Primary.ID == "" { 74 return fmt.Errorf("No Record ID is set") 75 } 76 77 foundIP := profitbricks.GetIpBlock(rs.Primary.ID) 78 79 if foundIP.StatusCode != 200 { 80 return fmt.Errorf("Error occured while fetching IP Block: %s", rs.Primary.ID) 81 } 82 if foundIP.Id != rs.Primary.ID { 83 return fmt.Errorf("Record not found") 84 } 85 86 ipblock = &foundIP 87 88 return nil 89 } 90 } 91 92 const testAccCheckProfitbricksIPBlockConfig_basic = ` 93 resource "profitbricks_ipblock" "webserver_ip" { 94 location = "%s" 95 size = 1 96 }`