github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_security_ip_list_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 TestAccOPCSecurityIPList_Basic(t *testing.T) {
    14  	listResourceName := "opc_compute_security_ip_list.test"
    15  	ri := acctest.RandInt()
    16  	config := fmt.Sprintf(testAccOPCSecurityIPListBasic, ri)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckSecurityIPListDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: config,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckSecurityIPListExists,
    27  					resource.TestCheckResourceAttr(listResourceName, "ip_entries.0", "192.168.0.1"),
    28  					resource.TestCheckResourceAttr(listResourceName, "ip_entries.1", "192.168.0.2"),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func TestAccOPCSecurityIPList_Updated(t *testing.T) {
    36  	listResourceName := "opc_compute_security_ip_list.test"
    37  	ri := acctest.RandInt()
    38  	config := fmt.Sprintf(testAccOPCSecurityIPListBasic, ri)
    39  	config2 := fmt.Sprintf(testAccOPCSecurityIPListUpdated, ri)
    40  
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testAccCheckSecurityIPListDestroy,
    45  		Steps: []resource.TestStep{
    46  			{
    47  				Config: config,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckSecurityIPListExists,
    50  					resource.TestCheckResourceAttr(listResourceName, "description", "Terraform Acceptance Test"),
    51  				),
    52  			},
    53  			{
    54  				Config: config2,
    55  				Check: resource.ComposeTestCheckFunc(
    56  					testAccCheckSecurityIPListExists,
    57  					resource.TestCheckResourceAttr(listResourceName, "description", ""),
    58  					resource.TestCheckResourceAttr(listResourceName, "ip_entries.0", "192.168.0.1"),
    59  					resource.TestCheckResourceAttr(listResourceName, "ip_entries.1", "192.168.0.3"),
    60  				),
    61  			},
    62  		},
    63  	})
    64  }
    65  
    66  func testAccCheckSecurityIPListExists(s *terraform.State) error {
    67  	client := testAccProvider.Meta().(*compute.Client).SecurityIPLists()
    68  
    69  	for _, rs := range s.RootModule().Resources {
    70  		if rs.Type != "opc_compute_security_ip_list" {
    71  			continue
    72  		}
    73  
    74  		input := compute.GetSecurityIPListInput{
    75  			Name: rs.Primary.Attributes["name"],
    76  		}
    77  		if _, err := client.GetSecurityIPList(&input); err != nil {
    78  			return fmt.Errorf("Error retrieving state of Security IP List %s: %s", input.Name, err)
    79  		}
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  func testAccCheckSecurityIPListDestroy(s *terraform.State) error {
    86  	client := testAccProvider.Meta().(*compute.Client).SecurityIPLists()
    87  
    88  	for _, rs := range s.RootModule().Resources {
    89  		if rs.Type != "opc_compute_security_ip_list" {
    90  			continue
    91  		}
    92  
    93  		input := compute.GetSecurityIPListInput{
    94  			Name: rs.Primary.Attributes["name"],
    95  		}
    96  		if info, err := client.GetSecurityIPList(&input); err == nil {
    97  			return fmt.Errorf("Security IP List %s still exists: %#v", input.Name, info)
    98  		}
    99  	}
   100  
   101  	return nil
   102  }
   103  
   104  const testAccOPCSecurityIPListBasic = `
   105  resource "opc_compute_security_ip_list" "test" {
   106  	name        = "acc-security-application-tcp-%d"
   107    ip_entries = ["192.168.0.1", "192.168.0.2"]
   108  	description = "Terraform Acceptance Test"
   109  }
   110  `
   111  
   112  const testAccOPCSecurityIPListUpdated = `
   113  resource "opc_compute_security_ip_list" "test" {
   114  	name        = "acc-security-application-tcp-%d"
   115    ip_entries = ["192.168.0.1", "192.168.0.3"]
   116  }
   117  `