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

     1  package triton
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  	"github.com/joyent/triton-go"
    13  )
    14  
    15  func TestAccTritonFabric_basic(t *testing.T) {
    16  	fabricName := fmt.Sprintf("acctest-%d", acctest.RandInt())
    17  	config := fmt.Sprintf(testAccTritonFabric_basic, acctest.RandIntRange(3, 2049), fabricName, fabricName)
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testCheckTritonFabricDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: config,
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testCheckTritonFabricExists("triton_fabric.test"),
    28  					func(*terraform.State) error {
    29  						time.Sleep(10 * time.Second)
    30  						return nil
    31  					},
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testCheckTritonFabricExists(name string) resource.TestCheckFunc {
    39  	return func(s *terraform.State) error {
    40  		rs, ok := s.RootModule().Resources[name]
    41  		if !ok {
    42  			return fmt.Errorf("Not found: %s", name)
    43  		}
    44  		conn := testAccProvider.Meta().(*triton.Client)
    45  
    46  		vlanID, err := strconv.Atoi(rs.Primary.Attributes["vlan_id"])
    47  		if err != nil {
    48  			return err
    49  		}
    50  
    51  		exists, err := resourceExists(conn.Fabrics().GetFabricNetwork(&triton.GetFabricNetworkInput{
    52  			FabricVLANID: vlanID,
    53  			NetworkID:    rs.Primary.ID,
    54  		}))
    55  		if err != nil {
    56  			return fmt.Errorf("Error: Check Fabric Exists: %s", err)
    57  		}
    58  
    59  		if exists {
    60  			return nil
    61  		}
    62  
    63  		return fmt.Errorf("Error: Fabric %q (VLAN %d) Does Not Exist", rs.Primary.ID, vlanID)
    64  	}
    65  }
    66  
    67  func testCheckTritonFabricDestroy(s *terraform.State) error {
    68  	conn := testAccProvider.Meta().(*triton.Client)
    69  
    70  	for _, rs := range s.RootModule().Resources {
    71  		if rs.Type != "triton_fabric" {
    72  			continue
    73  		}
    74  
    75  		vlanID, err := strconv.Atoi(rs.Primary.Attributes["vlan_id"])
    76  		if err != nil {
    77  			return err
    78  		}
    79  
    80  		exists, err := resourceExists(conn.Fabrics().GetFabricNetwork(&triton.GetFabricNetworkInput{
    81  			FabricVLANID: vlanID,
    82  			NetworkID:    rs.Primary.ID,
    83  		}))
    84  		if err != nil {
    85  			return nil
    86  		}
    87  
    88  		if exists {
    89  			return fmt.Errorf("Error: Fabric %q (VLAN %d) Still Exists", rs.Primary.ID, vlanID)
    90  		}
    91  
    92  		return nil
    93  	}
    94  
    95  	return nil
    96  }
    97  
    98  var testAccTritonFabric_basic = `
    99  resource "triton_vlan" "test" {
   100    vlan_id = "%d"
   101    name = "%s"
   102    description = "testAccTritonFabric_basic"
   103  }
   104  
   105  resource "triton_fabric" "test" {
   106    name = "%s"
   107    description = "test network"
   108    vlan_id = "${triton_vlan.test.id}"
   109  
   110    subnet = "10.0.0.0/22"
   111    gateway = "10.0.0.1"
   112    provision_start_ip = "10.0.0.5"
   113    provision_end_ip = "10.0.3.250"
   114  
   115    resolvers = ["8.8.8.8", "8.8.4.4"]
   116  }
   117  `