github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_security_rule_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 TestAccOPCSecurityRule_Basic(t *testing.T) {
    14  	rInt := acctest.RandInt()
    15  	resName := "opc_compute_security_rule.test"
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckSecurityRuleDestroy,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: testAccOPCSecurityRuleConfig_Basic(rInt),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckSecurityRuleExists,
    26  					resource.TestCheckResourceAttr(resName, "name", fmt.Sprintf("testing-security-rule-%d", rInt)),
    27  				),
    28  			},
    29  			{
    30  				Config: testAccOPCSecurityRuleConfig_BasicUpdate(rInt),
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckSecurityRuleExists,
    33  					resource.TestCheckResourceAttr(resName, "enabled", "false"),
    34  				),
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func TestAccOPCSecurityRule_Full(t *testing.T) {
    41  	rInt := acctest.RandInt()
    42  	resName := "opc_compute_security_rule.test"
    43  
    44  	resource.Test(t, resource.TestCase{
    45  		PreCheck:     func() { testAccPreCheck(t) },
    46  		Providers:    testAccProviders,
    47  		CheckDestroy: testAccCheckSecurityRuleDestroy,
    48  		Steps: []resource.TestStep{
    49  			{
    50  				Config: testAccOPCSecurityRuleConfig_Full(rInt),
    51  				Check: resource.ComposeTestCheckFunc(
    52  					testAccCheckSecurityRuleExists,
    53  					resource.TestCheckResourceAttr(resName, "name", fmt.Sprintf("testing-security-rule-%d", rInt)),
    54  					resource.TestCheckResourceAttr(resName, "acl", fmt.Sprintf("test-security-rule-%d", rInt)),
    55  					resource.TestCheckResourceAttr(resName, "src_vnic_set", fmt.Sprintf("test-security-rule-src-%d", rInt)),
    56  					resource.TestCheckResourceAttr(resName, "dst_vnic_set", fmt.Sprintf("test-security-rule-dst-%d", rInt)),
    57  					resource.TestCheckResourceAttr(resName, "dst_ip_address_prefixes.0", fmt.Sprintf("test-security-rule-dst-%d", rInt)),
    58  					resource.TestCheckResourceAttr(resName, "src_ip_address_prefixes.0", fmt.Sprintf("test-security-rule-src-%d", rInt)),
    59  					resource.TestCheckResourceAttr(resName, "security_protocols.0", fmt.Sprintf("test-security-rule-%d", rInt)),
    60  				),
    61  			},
    62  		},
    63  	})
    64  }
    65  
    66  func testAccCheckSecurityRuleExists(s *terraform.State) error {
    67  	client := testAccProvider.Meta().(*compute.Client).SecurityRules()
    68  	for _, rs := range s.RootModule().Resources {
    69  		if rs.Type != "opc_compute_sec_rule" {
    70  			continue
    71  		}
    72  
    73  		input := compute.GetSecurityRuleInput{
    74  			Name: rs.Primary.Attributes["name"],
    75  		}
    76  		if _, err := client.GetSecurityRule(&input); err != nil {
    77  			return fmt.Errorf("Error retrieving state of Security Rule %s: %s", input.Name, err)
    78  		}
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  func testAccCheckSecurityRuleDestroy(s *terraform.State) error {
    85  	client := testAccProvider.Meta().(*compute.Client).SecurityRules()
    86  
    87  	for _, rs := range s.RootModule().Resources {
    88  		if rs.Type != "opc_compute_security_rule" {
    89  			continue
    90  		}
    91  
    92  		input := compute.GetSecurityRuleInput{
    93  			Name: rs.Primary.Attributes["name"],
    94  		}
    95  		if info, err := client.GetSecurityRule(&input); err == nil {
    96  			return fmt.Errorf("Security Rule %s still exists: %#v", input.Name, info)
    97  		}
    98  	}
    99  
   100  	return nil
   101  }
   102  
   103  func testAccOPCSecurityRuleConfig_Basic(rInt int) string {
   104  	return fmt.Sprintf(`
   105  resource "opc_compute_security_rule" "test" {
   106    name = "testing-security-rule-%d"
   107    description = "testing-desc-%d"
   108    flow_direction = "ingress"
   109  }`, rInt, rInt)
   110  }
   111  
   112  func testAccOPCSecurityRuleConfig_BasicUpdate(rInt int) string {
   113  	return fmt.Sprintf(`
   114  resource "opc_compute_security_rule" "test" {
   115    name = "testing-security-rule-%d"
   116    description = "testing-desc-%d"
   117    flow_direction = "egress"
   118    enabled = false
   119  }`, rInt, rInt)
   120  }
   121  
   122  func testAccOPCSecurityRuleConfig_Full(rInt int) string {
   123  	return fmt.Sprintf(`
   124  resource "opc_compute_acl" "test" {
   125    name = "test-security-rule-%d"
   126  }
   127  
   128  resource "opc_compute_vnic_set" "src" {
   129    name = "test-security-rule-src-%d"
   130  }
   131  
   132  resource "opc_compute_vnic_set" "dst" {
   133    name = "test-security-rule-dst-%d"
   134  }
   135  
   136  resource "opc_compute_security_protocol" "test" {
   137    name = "test-security-rule-%d"
   138  }
   139  
   140  resource "opc_compute_ip_address_prefix_set" "src" {
   141    name = "test-security-rule-src-%d"
   142  }
   143  
   144  resource "opc_compute_ip_address_prefix_set" "dst" {
   145    name = "test-security-rule-dst-%d"
   146  }
   147  
   148  resource "opc_compute_security_rule" "test" {
   149    name                    = "testing-security-rule-%d"
   150    description             = "testing-desc-%d"
   151    flow_direction          = "ingress"
   152    acl                     = "${opc_compute_acl.test.name}"
   153    src_vnic_set            = "${opc_compute_vnic_set.src.name}"
   154    dst_vnic_set            = "${opc_compute_vnic_set.dst.name}"
   155    dst_ip_address_prefixes = ["${opc_compute_ip_address_prefix_set.dst.name}"]
   156    src_ip_address_prefixes = ["${opc_compute_ip_address_prefix_set.src.name}"]
   157    security_protocols     =  ["${opc_compute_security_protocol.test.name}"]
   158  }`, rInt, rInt, rInt, rInt, rInt, rInt, rInt, rInt)
   159  }