github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/triton/resource_vlan_test.go (about)

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