github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/builtin/providers/triton/resource_fabric_test.go (about)

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