github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/profitbricks/resource_profitbricks_lan_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 TestAccProfitBricksLan_Basic(t *testing.T) {
    13  	var lan profitbricks.Lan
    14  	lanName := "lanName"
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck: func() {
    18  			testAccPreCheck(t)
    19  		},
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckDProfitBricksLanDestroyCheck,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: fmt.Sprintf(testAccCheckProfitbricksLanConfig_basic, lanName),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckProfitBricksLanExists("profitbricks_lan.webserver_lan", &lan),
    27  					testAccCheckProfitBricksLanAttributes("profitbricks_lan.webserver_lan", lanName),
    28  					resource.TestCheckResourceAttr("profitbricks_lan.webserver_lan", "name", lanName),
    29  				),
    30  			},
    31  			resource.TestStep{
    32  				Config: testAccCheckProfitbricksLanConfig_update,
    33  				Check: resource.ComposeTestCheckFunc(
    34  					testAccCheckProfitBricksLanAttributes("profitbricks_lan.webserver_lan", "updated"),
    35  					resource.TestCheckResourceAttr("profitbricks_lan.webserver_lan", "name", "updated"),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func testAccCheckDProfitBricksLanDestroyCheck(s *terraform.State) error {
    43  	for _, rs := range s.RootModule().Resources {
    44  		if rs.Type != "profitbricks_datacenter" {
    45  			continue
    46  		}
    47  
    48  		resp := profitbricks.GetLan(rs.Primary.Attributes["datacenter_id"], rs.Primary.ID)
    49  
    50  		if resp.StatusCode < 299 {
    51  			return fmt.Errorf("LAN still exists %s %s", rs.Primary.ID, resp.Response)
    52  		}
    53  	}
    54  
    55  	return nil
    56  }
    57  
    58  func testAccCheckProfitBricksLanAttributes(n string, name string) resource.TestCheckFunc {
    59  	return func(s *terraform.State) error {
    60  		rs, ok := s.RootModule().Resources[n]
    61  		if !ok {
    62  			return fmt.Errorf("testAccCheckProfitBricksLanAttributes: Not found: %s", n)
    63  		}
    64  		if rs.Primary.Attributes["name"] != name {
    65  			return fmt.Errorf("Bad name: %s", rs.Primary.Attributes["name"])
    66  		}
    67  
    68  		return nil
    69  	}
    70  }
    71  
    72  func testAccCheckProfitBricksLanExists(n string, lan *profitbricks.Lan) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  		rs, ok := s.RootModule().Resources[n]
    75  
    76  		if !ok {
    77  			return fmt.Errorf("testAccCheckProfitBricksLanExists: Not found: %s", n)
    78  		}
    79  
    80  		if rs.Primary.ID == "" {
    81  			return fmt.Errorf("No Record ID is set")
    82  		}
    83  
    84  		foundLan := profitbricks.GetLan(rs.Primary.Attributes["datacenter_id"], rs.Primary.ID)
    85  
    86  		if foundLan.StatusCode != 200 {
    87  			return fmt.Errorf("Error occured while fetching Server: %s", rs.Primary.ID)
    88  		}
    89  		if foundLan.Id != rs.Primary.ID {
    90  			return fmt.Errorf("Record not found")
    91  		}
    92  
    93  		lan = &foundLan
    94  
    95  		return nil
    96  	}
    97  }
    98  
    99  const testAccCheckProfitbricksLanConfig_basic = `
   100  resource "profitbricks_datacenter" "foobar" {
   101  	name       = "lan-test"
   102  	location = "us/las"
   103  }
   104  
   105  resource "profitbricks_lan" "webserver_lan" {
   106    datacenter_id = "${profitbricks_datacenter.foobar.id}"
   107    public = true
   108    name = "%s"
   109  }`
   110  
   111  const testAccCheckProfitbricksLanConfig_update = `
   112  resource "profitbricks_datacenter" "foobar" {
   113  	name       = "lan-test"
   114  	location = "us/las"
   115  }
   116  resource "profitbricks_lan" "webserver_lan" {
   117    datacenter_id = "${profitbricks_datacenter.foobar.id}"
   118    public = true
   119    name = "updated"
   120  }`