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

     1  package opc
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/go-oracle-terraform/compute"
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccOPCVNICSet_Basic(t *testing.T) {
    14  	rInt := acctest.RandInt()
    15  	rName := fmt.Sprintf("testing-acc-%d", rInt)
    16  	rDesc := fmt.Sprintf("acctesting vnic set %d", rInt)
    17  	resourceName := "opc_compute_vnic_set.test"
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccOPCCheckVNICSetDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: testAccVnicSetBasic(rName, rDesc, rInt),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccOPCCheckVNICSetExists,
    27  					resource.TestCheckResourceAttr(
    28  						resourceName, "name", rName),
    29  					resource.TestCheckResourceAttr(
    30  						resourceName, "description", rDesc),
    31  					resource.TestCheckResourceAttr(
    32  						resourceName, "tags.#", "2"),
    33  					resource.TestCheckResourceAttr(
    34  						resourceName, "virtual_nics.#", "2"),
    35  				),
    36  			},
    37  			{
    38  				Config: testAccVnicSetBasic_Update(rName, rDesc, rInt),
    39  				Check: resource.ComposeTestCheckFunc(
    40  					testAccOPCCheckVNICSetExists,
    41  					resource.TestCheckResourceAttr(
    42  						resourceName, "name", rName),
    43  					resource.TestCheckResourceAttr(
    44  						resourceName, "description", fmt.Sprintf("%s-updated", rDesc)),
    45  					resource.TestCheckResourceAttr(
    46  						resourceName, "tags.#", "1"),
    47  					resource.TestCheckResourceAttr(
    48  						resourceName, "virtual_nics.#", "2"),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func testAccOPCCheckVNICSetExists(s *terraform.State) error {
    56  	client := testAccProvider.Meta().(*compute.Client).VirtNICSets()
    57  
    58  	for _, rs := range s.RootModule().Resources {
    59  		if rs.Type != "opc_compute_vnic_set" {
    60  			continue
    61  		}
    62  
    63  		input := compute.GetVirtualNICSetInput{
    64  			Name: rs.Primary.Attributes["name"],
    65  		}
    66  		if _, err := client.GetVirtualNICSet(&input); err != nil {
    67  			return fmt.Errorf("Error retrieving state of VNIC Set %s: %s", input.Name, err)
    68  		}
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func testAccOPCCheckVNICSetDestroy(s *terraform.State) error {
    75  	client := testAccProvider.Meta().(*compute.Client).VirtNICSets()
    76  
    77  	for _, rs := range s.RootModule().Resources {
    78  		if rs.Type != "opc_compute_vnic_set" {
    79  			continue
    80  		}
    81  
    82  		input := compute.GetVirtualNICSetInput{
    83  			Name: rs.Primary.Attributes["name"],
    84  		}
    85  		if info, err := client.GetVirtualNICSet(&input); err == nil {
    86  			return fmt.Errorf("VNIC Set %s still exists: %#v", input.Name, info)
    87  		}
    88  	}
    89  
    90  	return nil
    91  }
    92  
    93  func testAccVnicSetBasic(rName, rDesc string, rInt int) string {
    94  	return fmt.Sprintf(`
    95  resource "opc_compute_ip_network" "foo" {
    96    name = "testing-vnic-set-%d"
    97    description = "testing-vnic-set"
    98    ip_address_prefix = "10.1.14.0/24"
    99  }
   100  
   101  resource "opc_compute_ip_network" "bar" {
   102    name = "testing-vnic-set2-%d"
   103    description = "testing-vnic-set2"
   104    ip_address_prefix = "10.1.15.0/24"
   105  }
   106  
   107  resource "opc_compute_instance" "foo" {
   108    name = "test-vnic-set-%d"
   109    label = "testing"
   110    shape = "oc3"
   111    image_list = "/oracle/public/oel_6.7_apaas_16.4.5_1610211300"
   112    networking_info {
   113      index = 0
   114      ip_network = "${opc_compute_ip_network.foo.id}"
   115      vnic = "test-vnic-set-%d"
   116      shared_network = false
   117    }
   118    networking_info {
   119      index = 1
   120      ip_network = "${opc_compute_ip_network.bar.id}"
   121      vnic = "test-vnic-set2-%d"
   122      shared_network = false
   123    }
   124  }
   125  
   126  data "opc_compute_network_interface" "foo" {
   127    instance_name = "${opc_compute_instance.foo.name}"
   128    instance_id = "${opc_compute_instance.foo.id}"
   129    interface = "eth0"
   130  }
   131  
   132  data "opc_compute_network_interface" "bar" {
   133    instance_name = "${opc_compute_instance.foo.name}"
   134    instance_id = "${opc_compute_instance.foo.id}"
   135    interface = "eth1"
   136  }
   137  
   138  resource "opc_compute_vnic_set" "test" {
   139    name = "%s"
   140    description = "%s"
   141    tags = ["tag1", "tag2"]
   142    virtual_nics = [
   143      "${data.opc_compute_network_interface.foo.vnic}",
   144      "${data.opc_compute_network_interface.bar.vnic}",
   145    ]
   146  }`, rInt, rInt, rInt, rInt, rInt, rName, rDesc)
   147  }
   148  
   149  func testAccVnicSetBasic_Update(rName, rDesc string, rInt int) string {
   150  	return fmt.Sprintf(`
   151  resource "opc_compute_ip_network" "foo" {
   152    name = "testing-vnic-set-%d"
   153    description = "testing-vnic-set"
   154    ip_address_prefix = "10.1.14.0/24"
   155  }
   156  
   157  resource "opc_compute_ip_network" "bar" {
   158    name = "testing-vnic-set2-%d"
   159    description = "testing-vnic-set2"
   160    ip_address_prefix = "10.1.15.0/24"
   161  }
   162  
   163  resource "opc_compute_instance" "foo" {
   164    name = "test-vnic-set-%d"
   165    label = "testing"
   166    shape = "oc3"
   167    image_list = "/oracle/public/oel_6.7_apaas_16.4.5_1610211300"
   168    networking_info {
   169      index = 0
   170      ip_network = "${opc_compute_ip_network.foo.id}"
   171      vnic = "test-vnic-set-%d"
   172      shared_network = false
   173    }
   174    networking_info {
   175      index = 1
   176      ip_network = "${opc_compute_ip_network.bar.id}"
   177      vnic = "test-vnic-set2-%d"
   178      shared_network = false
   179    }
   180  }
   181  
   182  data "opc_compute_network_interface" "foo" {
   183    instance_name = "${opc_compute_instance.foo.name}"
   184    instance_id = "${opc_compute_instance.foo.id}"
   185    interface = "eth0"
   186  }
   187  
   188  data "opc_compute_network_interface" "bar" {
   189    instance_name = "${opc_compute_instance.foo.name}"
   190    instance_id = "${opc_compute_instance.foo.id}"
   191    interface = "eth1"
   192  }
   193  
   194  resource "opc_compute_vnic_set" "test" {
   195    name = "%s"
   196    description = "%s-updated"
   197    tags = ["tag1"]
   198    virtual_nics = [
   199      "${data.opc_compute_network_interface.foo.vnic}",
   200      "${data.opc_compute_network_interface.bar.vnic}",
   201    ]
   202  }`, rInt, rInt, rInt, rInt, rInt, rName, rDesc)
   203  }