github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/opc/resource_ip_address_association_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 TestAccOPCIPAddressAssociation_Basic(t *testing.T) {
    14  	rInt := acctest.RandInt()
    15  	resourceName := "opc_compute_ip_address_association.test"
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckIPAddressAssociationDestroy,
    20  		Steps: []resource.TestStep{
    21  			{
    22  				Config: testAccIPAddressAssociationBasic(rInt),
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckIPAddressAssociationExists,
    25  					resource.TestCheckResourceAttr(
    26  						resourceName, "tags.#", "2"),
    27  				),
    28  			},
    29  			{
    30  				Config: testAccIPAddressAssociationBasic_Update(rInt),
    31  				Check: resource.ComposeTestCheckFunc(
    32  					resource.TestCheckResourceAttr(
    33  						resourceName, "tags.#", "1"),
    34  				),
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func TestAccOPCIPAddressAssociation_Full(t *testing.T) {
    41  	rInt := acctest.RandInt()
    42  	resourceName := "opc_compute_ip_address_association.test"
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testAccCheckIPAddressAssociationDestroy,
    47  		Steps: []resource.TestStep{
    48  			{
    49  				Config: testAccIPAddressAssociationFull(rInt),
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckIPAddressAssociationExists,
    52  					resource.TestCheckResourceAttr(
    53  						resourceName, "vnic", fmt.Sprintf("test-vnic-data-%d", rInt)),
    54  					resource.TestCheckResourceAttr(
    55  						resourceName, "ip_address_reservation", fmt.Sprintf("testing-ip-address-association-%d", rInt)),
    56  				),
    57  			},
    58  		},
    59  	})
    60  }
    61  
    62  func testAccCheckIPAddressAssociationExists(s *terraform.State) error {
    63  	client := testAccProvider.Meta().(*compute.Client).IPAddressAssociations()
    64  
    65  	for _, rs := range s.RootModule().Resources {
    66  		if rs.Type != "opc_compute_ip_address_association" {
    67  			continue
    68  		}
    69  
    70  		input := compute.GetIPAddressAssociationInput{
    71  			Name: rs.Primary.Attributes["name"],
    72  		}
    73  		if _, err := client.GetIPAddressAssociation(&input); err != nil {
    74  			return fmt.Errorf("Error retrieving state of IP Address Association %s: %s", input.Name, err)
    75  		}
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  func testAccCheckIPAddressAssociationDestroy(s *terraform.State) error {
    82  	client := testAccProvider.Meta().(*compute.Client).IPAddressAssociations()
    83  
    84  	for _, rs := range s.RootModule().Resources {
    85  		if rs.Type != "opc_compute_ip_address_association" {
    86  			continue
    87  		}
    88  
    89  		input := compute.GetIPAddressAssociationInput{
    90  			Name: rs.Primary.Attributes["name"],
    91  		}
    92  		if info, err := client.GetIPAddressAssociation(&input); err == nil {
    93  			return fmt.Errorf("IP Address Association %s still exists: %#v", input.Name, info)
    94  		}
    95  	}
    96  
    97  	return nil
    98  }
    99  
   100  func testAccIPAddressAssociationBasic(rInt int) string {
   101  	return fmt.Sprintf(`
   102  resource "opc_compute_ip_address_association" "test" {
   103    name = "testing-acc-%d"
   104    description = "acctesting ip address association test %d"
   105    tags = ["tag1", "tag2"]
   106  }`, rInt, rInt)
   107  }
   108  
   109  func testAccIPAddressAssociationBasic_Update(rInt int) string {
   110  	return fmt.Sprintf(`
   111  resource "opc_compute_ip_address_association" "test" {
   112    name = "testing-acc-%d"
   113    description = "acctesting ip address association test updated %d"
   114    tags = ["tag1"]
   115  }`, rInt, rInt)
   116  }
   117  
   118  func testAccIPAddressAssociationFull(rInt int) string {
   119  	return fmt.Sprintf(`
   120  resource "opc_compute_ip_network" "foo" {
   121    name = "testing-vnic-data-%d"
   122    description = "testing-ip-address-association"
   123    ip_address_prefix = "10.1.13.0/24"
   124  }
   125  resource "opc_compute_instance" "test" {
   126    name = "test-%d"
   127    label = "test"
   128    shape = "oc3"
   129    image_list = "/oracle/public/oel_6.7_apaas_16.4.5_1610211300"
   130    networking_info {
   131      index = 0
   132      ip_network = "${opc_compute_ip_network.foo.id}"
   133      vnic = "test-vnic-data-%d"
   134      shared_network = false
   135      mac_address = "02:5a:cd:ec:2e:4c"
   136    }
   137  }
   138  data "opc_compute_network_interface" "eth0" {
   139    instance_name = "${opc_compute_instance.test.name}"
   140    instance_id = "${opc_compute_instance.test.id}"
   141    interface = "eth0"
   142  }
   143  resource "opc_compute_ip_address_reservation" "test" {
   144    name = "testing-ip-address-association-%d"
   145    description = "testing-desc-%d"
   146    ip_address_pool = "public-ippool"
   147  }
   148  resource "opc_compute_ip_address_association" "test" {
   149    name = "testing-acc-%d"
   150    ip_address_reservation = "${opc_compute_ip_address_reservation.test.name}"
   151    vnic = "${data.opc_compute_network_interface.eth0.vnic}"
   152    description = "acctesting ip address association test %d"
   153    tags = ["tag1", "tag2"]
   154  }`, rInt, rInt, rInt, rInt, rInt, rInt, rInt)
   155  }