github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_sec_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 TestAccOPCSecRule_Basic(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccOPCSecRuleBasic, ri, ri, ri, ri)
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckSecRuleDestroy,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: config,
    24  				Check:  testAccCheckSecRuleExists,
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func TestAccOPCSecRule_Complete(t *testing.T) {
    31  	ri := acctest.RandInt()
    32  	config := fmt.Sprintf(testAccOPCSecRuleComplete, ri, ri, ri, ri)
    33  
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckSecRuleDestroy,
    38  		Steps: []resource.TestStep{
    39  			{
    40  				Config: config,
    41  				Check:  testAccCheckSecRuleExists,
    42  			},
    43  		},
    44  	})
    45  }
    46  
    47  func testAccCheckSecRuleExists(s *terraform.State) error {
    48  	client := testAccProvider.Meta().(*compute.Client).SecRules()
    49  	for _, rs := range s.RootModule().Resources {
    50  		if rs.Type != "opc_compute_sec_rule" {
    51  			continue
    52  		}
    53  
    54  		input := compute.GetSecRuleInput{
    55  			Name: rs.Primary.Attributes["name"],
    56  		}
    57  		if _, err := client.GetSecRule(&input); err != nil {
    58  			return fmt.Errorf("Error retrieving state of Sec Rule %s: %s", input.Name, err)
    59  		}
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  func testAccCheckSecRuleDestroy(s *terraform.State) error {
    66  	client := testAccProvider.Meta().(*compute.Client).SecRules()
    67  
    68  	for _, rs := range s.RootModule().Resources {
    69  		if rs.Type != "opc_compute_sec_rule" {
    70  			continue
    71  		}
    72  
    73  		input := compute.GetSecRuleInput{
    74  			Name: rs.Primary.Attributes["name"],
    75  		}
    76  		if info, err := client.GetSecRule(&input); err == nil {
    77  			return fmt.Errorf("Sec Rule %s still exists: %#v", input.Name, info)
    78  		}
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  var testAccOPCSecRuleBasic = `
    85  resource "opc_compute_security_list" "test" {
    86  	name                 = "acc-test-sec-rule-list-%d"
    87          policy               = "PERMIT"
    88          outbound_cidr_policy = "DENY"
    89  }
    90  
    91  resource "opc_compute_security_application" "test" {
    92  	name     = "acc-test-sec-rule-app-%d"
    93  	protocol = "tcp"
    94  	dport    = "8080"
    95  }
    96  
    97  resource "opc_compute_security_ip_list" "test" {
    98  	name       = "acc-test-sec-rule-ip-list-%d"
    99  	ip_entries = ["217.138.34.4"]
   100  }
   101  
   102  resource "opc_compute_sec_rule" "test" {
   103  	name             = "acc-test-sec-rule-%d"
   104  	source_list      = "seclist:${opc_compute_security_list.test.name}"
   105  	destination_list = "seciplist:${opc_compute_security_ip_list.test.name}"
   106  	action           = "PERMIT"
   107  	application      = "${opc_compute_security_application.test.name}"
   108  }
   109  `
   110  
   111  var testAccOPCSecRuleComplete = `
   112  resource "opc_compute_security_list" "test" {
   113  	name                 = "acc-test-sec-rule-list-%d"
   114          policy               = "PERMIT"
   115          outbound_cidr_policy = "DENY"
   116  }
   117  
   118  resource "opc_compute_security_application" "test" {
   119  	name     = "acc-test-sec-rule-app-%d"
   120  	protocol = "tcp"
   121  	dport    = "8080"
   122  }
   123  
   124  resource "opc_compute_security_ip_list" "test" {
   125  	name       = "acc-test-sec-rule-ip-list-%d"
   126  	ip_entries = ["217.138.34.4"]
   127  }
   128  
   129  resource "opc_compute_sec_rule" "test" {
   130  	name             = "acc-test-sec-rule-%d"
   131  	source_list      = "seclist:${opc_compute_security_list.test.name}"
   132  	destination_list = "seciplist:${opc_compute_security_ip_list.test.name}"
   133  	action           = "PERMIT"
   134  	application      = "${opc_compute_security_application.test.name}"
   135  	disabled         = false
   136  	description      = "This is a test description"
   137  }
   138  `