github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/opc/resource_ip_reservation_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 TestAccOPCIPReservation_Basic(t *testing.T) {
    14  
    15  	ri := acctest.RandInt()
    16  	config := fmt.Sprintf(testAccIPReservationBasic, ri)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckIPReservationDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: config,
    25  				Check:  testAccCheckIPReservationExists,
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCheckIPReservationExists(s *terraform.State) error {
    32  	client := testAccProvider.Meta().(*compute.Client).IPReservations()
    33  
    34  	for _, rs := range s.RootModule().Resources {
    35  		if rs.Type != "opc_compute_ip_reservation" {
    36  			continue
    37  		}
    38  
    39  		input := compute.GetIPReservationInput{
    40  			Name: rs.Primary.Attributes["name"],
    41  		}
    42  		if _, err := client.GetIPReservation(&input); err != nil {
    43  			return fmt.Errorf("Error retrieving state of IP Reservation %s: %s", input.Name, err)
    44  		}
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  func testAccCheckIPReservationDestroy(s *terraform.State) error {
    51  	client := testAccProvider.Meta().(*compute.Client).IPReservations()
    52  
    53  	for _, rs := range s.RootModule().Resources {
    54  		if rs.Type != "opc_compute_ip_reservation" {
    55  			continue
    56  		}
    57  
    58  		input := compute.GetIPReservationInput{
    59  			Name: rs.Primary.Attributes["name"],
    60  		}
    61  		if info, err := client.GetIPReservation(&input); err == nil {
    62  			return fmt.Errorf("IP Reservation %s still exists: %#v", input.Name, info)
    63  		}
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  var testAccIPReservationBasic = `
    70  resource "opc_compute_ip_reservation" "test" {
    71    name        = "acc-test-ip-reservation-%d"
    72    parent_pool = "/oracle/public/ippool"
    73    permanent   = true
    74  }
    75  `