github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_security_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 TestAccOPCSecurityAssociation_Basic(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccSecurityAssociationBasic, ri, ri)
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccOPCCheckSecurityAssociationDestroy,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: config,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccOPCCheckSecurityAssociationExists,
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccOPCSecurityAssociation_Complete(t *testing.T) {
    33  	ri := acctest.RandInt()
    34  	config := fmt.Sprintf(testAccSecurityAssociationComplete, ri, ri, ri)
    35  
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccOPCCheckSecurityAssociationDestroy,
    40  		Steps: []resource.TestStep{
    41  			{
    42  				Config: config,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccOPCCheckSecurityAssociationExists,
    45  				),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func testAccOPCCheckSecurityAssociationExists(s *terraform.State) error {
    52  	client := testAccProvider.Meta().(*compute.Client).SecurityAssociations()
    53  
    54  	for _, rs := range s.RootModule().Resources {
    55  		if rs.Type != "opc_compute_security_association" {
    56  			continue
    57  		}
    58  
    59  		input := compute.GetSecurityAssociationInput{
    60  			Name: rs.Primary.Attributes["name"],
    61  		}
    62  		if _, err := client.GetSecurityAssociation(&input); err != nil {
    63  			return fmt.Errorf("Error retrieving state of Security Association %s: %s", input.Name, err)
    64  		}
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  func testAccOPCCheckSecurityAssociationDestroy(s *terraform.State) error {
    71  	client := testAccProvider.Meta().(*compute.Client).SecurityAssociations()
    72  
    73  	for _, rs := range s.RootModule().Resources {
    74  		if rs.Type != "opc_compute_security_association" {
    75  			continue
    76  		}
    77  
    78  		input := compute.GetSecurityAssociationInput{
    79  			Name: rs.Primary.Attributes["name"],
    80  		}
    81  		if info, err := client.GetSecurityAssociation(&input); err == nil {
    82  			return fmt.Errorf("Security Association %s still exists: %#v", input.Name, info)
    83  		}
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  var testAccSecurityAssociationBasic = `
    90  resource "opc_compute_security_list" "test" {
    91    name                 = "acc-test-sec-ass-sec-list-%d"
    92    policy               = "PERMIT"
    93    outbound_cidr_policy = "DENY"
    94  }
    95  
    96  resource "opc_compute_instance" "test" {
    97    name        = "acc-test-sec-ass-instance-%d"
    98    label       = "Security Associations Test Instance"
    99    shape       = "oc3"
   100    image_list   = "/oracle/public/oel_6.7_apaas_16.4.5_1610211300"
   101  }
   102  
   103  resource "opc_compute_security_association" "test" {
   104    vcable  = "${opc_compute_instance.test.vcable}"
   105    seclist = "${opc_compute_security_list.test.name}"
   106  }
   107  `
   108  
   109  var testAccSecurityAssociationComplete = `
   110  resource "opc_compute_security_list" "test" {
   111    name                 = "acc-test-sec-ass-sec-list-%d"
   112    policy               = "PERMIT"
   113    outbound_cidr_policy = "DENY"
   114  }
   115  
   116  resource "opc_compute_instance" "test" {
   117    name        = "acc-test-sec-ass-instance-%d"
   118    label       = "Security Associations Test Instance"
   119    shape       = "oc3"
   120    image_list   = "/oracle/public/oel_6.7_apaas_16.4.5_1610211300"
   121  }
   122  
   123  resource "opc_compute_security_association" "test" {
   124    name    = "acc-test-sec-ass-%d"
   125    vcable  = "${opc_compute_instance.test.vcable}"
   126    seclist = "${opc_compute_security_list.test.name}"
   127  }
   128  `