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

     1  package triton
     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/joyent/triton-go"
    12  )
    13  
    14  func TestAccTritonVLAN_basic(t *testing.T) {
    15  	config := testAccTritonVLAN_basic(acctest.RandIntRange(3, 2048))
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testCheckTritonVLANDestroy,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: config,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testCheckTritonVLANExists("triton_vlan.test"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccTritonVLAN_update(t *testing.T) {
    33  	vlanNumber := acctest.RandIntRange(3, 2048)
    34  	preConfig := testAccTritonVLAN_basic(vlanNumber)
    35  	postConfig := testAccTritonVLAN_update(vlanNumber)
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testCheckTritonVLANDestroy,
    41  		Steps: []resource.TestStep{
    42  			{
    43  				Config: preConfig,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testCheckTritonVLANExists("triton_vlan.test"),
    46  					resource.TestCheckResourceAttr("triton_vlan.test", "vlan_id", strconv.Itoa(vlanNumber)),
    47  					resource.TestCheckResourceAttr("triton_vlan.test", "name", "test-vlan"),
    48  					resource.TestCheckResourceAttr("triton_vlan.test", "description", "test vlan"),
    49  				),
    50  			},
    51  
    52  			{
    53  				Config: postConfig,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testCheckTritonVLANExists("triton_vlan.test"),
    56  					resource.TestCheckResourceAttr("triton_vlan.test", "vlan_id", strconv.Itoa(vlanNumber)),
    57  					resource.TestCheckResourceAttr("triton_vlan.test", "name", "test-vlan-2"),
    58  					resource.TestCheckResourceAttr("triton_vlan.test", "description", "test vlan 2"),
    59  				),
    60  			},
    61  		},
    62  	})
    63  }
    64  
    65  func testCheckTritonVLANExists(name string) resource.TestCheckFunc {
    66  	return func(s *terraform.State) error {
    67  		// Ensure we have enough information in state to look up in API
    68  		rs, ok := s.RootModule().Resources[name]
    69  		if !ok {
    70  			return fmt.Errorf("Not found: %s", name)
    71  		}
    72  		conn := testAccProvider.Meta().(*triton.Client)
    73  
    74  		id, err := resourceVLANIDInt(rs.Primary.ID)
    75  		if err != nil {
    76  			return err
    77  		}
    78  
    79  		resp, err := conn.Fabrics().GetFabricVLAN(&triton.GetFabricVLANInput{
    80  			ID: id,
    81  		})
    82  		if err != nil && triton.IsResourceNotFound(err) {
    83  			return fmt.Errorf("Bad: Check VLAN Exists: %s", err)
    84  		} else if err != nil {
    85  			return err
    86  		}
    87  
    88  		if resp == nil {
    89  			return fmt.Errorf("Bad: VLAN %q does not exist", rs.Primary.ID)
    90  		}
    91  
    92  		return nil
    93  	}
    94  }
    95  
    96  func testCheckTritonVLANDestroy(s *terraform.State) error {
    97  	conn := testAccProvider.Meta().(*triton.Client)
    98  
    99  	for _, rs := range s.RootModule().Resources {
   100  		if rs.Type != "triton_vlan" {
   101  			continue
   102  		}
   103  
   104  		id, err := resourceVLANIDInt(rs.Primary.ID)
   105  		if err != nil {
   106  			return err
   107  		}
   108  
   109  		resp, err := conn.Fabrics().GetFabricVLAN(&triton.GetFabricVLANInput{
   110  			ID: id,
   111  		})
   112  		if triton.IsResourceNotFound(err) {
   113  			return nil
   114  		} else if err != nil {
   115  			return err
   116  		}
   117  
   118  		if resp != nil {
   119  			return fmt.Errorf("Bad: VLAN %q still exists", rs.Primary.ID)
   120  		}
   121  	}
   122  
   123  	return nil
   124  }
   125  
   126  var testAccTritonVLAN_basic = func(vlanID int) string {
   127  	return fmt.Sprintf(`resource "triton_vlan" "test" {
   128  	  vlan_id = %d
   129  	  name = "test-vlan"
   130  	  description = "test vlan"
   131  	}`, vlanID)
   132  }
   133  
   134  var testAccTritonVLAN_update = func(vlanID int) string {
   135  	return fmt.Sprintf(`resource "triton_vlan" "test" {
   136  	  vlan_id = %d
   137  	  name = "test-vlan-2"
   138  	  description = "test vlan 2"
   139  	}`, vlanID)
   140  }