github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/cloudstack/resource_cloudstack_nic_test.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/xanzy/go-cloudstack/cloudstack"
    10  )
    11  
    12  func TestAccCloudStackNIC_basic(t *testing.T) {
    13  	var nic cloudstack.Nic
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckCloudStackNICDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCloudStackNIC_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCloudStackNICExists(
    24  						"cloudstack_instance.foobar", "cloudstack_nic.foo", &nic),
    25  					testAccCheckCloudStackNICAttributes(&nic),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccCloudStackNIC_update(t *testing.T) {
    33  	var nic cloudstack.Nic
    34  
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testAccCheckCloudStackNICDestroy,
    39  		Steps: []resource.TestStep{
    40  			resource.TestStep{
    41  				Config: testAccCloudStackNIC_basic,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccCheckCloudStackNICExists(
    44  						"cloudstack_instance.foobar", "cloudstack_nic.foo", &nic),
    45  					testAccCheckCloudStackNICAttributes(&nic),
    46  				),
    47  			},
    48  
    49  			resource.TestStep{
    50  				Config: testAccCloudStackNIC_ipaddress,
    51  				Check: resource.ComposeTestCheckFunc(
    52  					testAccCheckCloudStackNICExists(
    53  						"cloudstack_instance.foobar", "cloudstack_nic.foo", &nic),
    54  					testAccCheckCloudStackNICIPAddress(&nic),
    55  					resource.TestCheckResourceAttr(
    56  						"cloudstack_nic.foo", "ipaddress", CLOUDSTACK_2ND_NIC_IPADDRESS),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func testAccCheckCloudStackNICExists(
    64  	v, n string, nic *cloudstack.Nic) resource.TestCheckFunc {
    65  	return func(s *terraform.State) error {
    66  		rsv, ok := s.RootModule().Resources[v]
    67  		if !ok {
    68  			return fmt.Errorf("Not found: %s", v)
    69  		}
    70  
    71  		if rsv.Primary.ID == "" {
    72  			return fmt.Errorf("No instance ID is set")
    73  		}
    74  
    75  		rsn, ok := s.RootModule().Resources[n]
    76  		if !ok {
    77  			return fmt.Errorf("Not found: %s", n)
    78  		}
    79  
    80  		if rsn.Primary.ID == "" {
    81  			return fmt.Errorf("No NIC ID is set")
    82  		}
    83  
    84  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    85  		vm, _, err := cs.VirtualMachine.GetVirtualMachineByID(rsv.Primary.ID)
    86  
    87  		if err != nil {
    88  			return err
    89  		}
    90  
    91  		for _, n := range vm.Nic {
    92  			if n.Id == rsn.Primary.ID {
    93  				*nic = n
    94  				return nil
    95  			}
    96  		}
    97  
    98  		return fmt.Errorf("NIC not found")
    99  	}
   100  }
   101  
   102  func testAccCheckCloudStackNICAttributes(
   103  	nic *cloudstack.Nic) resource.TestCheckFunc {
   104  	return func(s *terraform.State) error {
   105  
   106  		if nic.Networkname != CLOUDSTACK_2ND_NIC_NETWORK {
   107  			return fmt.Errorf("Bad network: %s", nic.Networkname)
   108  		}
   109  
   110  		return nil
   111  	}
   112  }
   113  
   114  func testAccCheckCloudStackNICIPAddress(
   115  	nic *cloudstack.Nic) resource.TestCheckFunc {
   116  	return func(s *terraform.State) error {
   117  
   118  		if nic.Networkname != CLOUDSTACK_2ND_NIC_NETWORK {
   119  			return fmt.Errorf("Bad network: %s", nic.Networkname)
   120  		}
   121  
   122  		if nic.Ipaddress != CLOUDSTACK_2ND_NIC_IPADDRESS {
   123  			return fmt.Errorf("Bad IP address: %s", nic.Ipaddress)
   124  		}
   125  
   126  		return nil
   127  	}
   128  }
   129  
   130  func testAccCheckCloudStackNICDestroy(s *terraform.State) error {
   131  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   132  
   133  	// Deleting the instance automatically deletes any additional NICs
   134  	for _, rs := range s.RootModule().Resources {
   135  		if rs.Type != "cloudstack_instance" {
   136  			continue
   137  		}
   138  
   139  		if rs.Primary.ID == "" {
   140  			return fmt.Errorf("No instance ID is set")
   141  		}
   142  
   143  		_, _, err := cs.VirtualMachine.GetVirtualMachineByID(rs.Primary.ID)
   144  		if err == nil {
   145  			return fmt.Errorf("Virtual Machine %s still exists", rs.Primary.ID)
   146  		}
   147  	}
   148  
   149  	return nil
   150  }
   151  
   152  var testAccCloudStackNIC_basic = fmt.Sprintf(`
   153  resource "cloudstack_instance" "foobar" {
   154    name = "terraform-test"
   155    display_name = "terraform"
   156    service_offering= "%s"
   157    network = "%s"
   158    template = "%s"
   159    zone = "%s"
   160    expunge = true
   161  }
   162  
   163  resource "cloudstack_nic" "foo" {
   164    network = "%s"
   165    virtual_machine = "${cloudstack_instance.foobar.name}"
   166  }`,
   167  	CLOUDSTACK_SERVICE_OFFERING_1,
   168  	CLOUDSTACK_NETWORK_1,
   169  	CLOUDSTACK_TEMPLATE,
   170  	CLOUDSTACK_ZONE,
   171  	CLOUDSTACK_2ND_NIC_NETWORK)
   172  
   173  var testAccCloudStackNIC_ipaddress = fmt.Sprintf(`
   174  resource "cloudstack_instance" "foobar" {
   175    name = "terraform-test"
   176    display_name = "terraform"
   177    service_offering= "%s"
   178    network = "%s"
   179    template = "%s"
   180    zone = "%s"
   181    expunge = true
   182  }
   183  
   184  resource "cloudstack_nic" "foo" {
   185    network = "%s"
   186    ipaddress = "%s"
   187    virtual_machine = "${cloudstack_instance.foobar.name}"
   188  }`,
   189  	CLOUDSTACK_SERVICE_OFFERING_1,
   190  	CLOUDSTACK_NETWORK_1,
   191  	CLOUDSTACK_TEMPLATE,
   192  	CLOUDSTACK_ZONE,
   193  	CLOUDSTACK_2ND_NIC_NETWORK,
   194  	CLOUDSTACK_2ND_NIC_IPADDRESS)