github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_network_security_rule_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccAzureRMNetworkSecurityRule_basic(t *testing.T) {
    13  
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testCheckAzureRMNetworkSecurityRuleDestroy,
    18  		Steps: []resource.TestStep{
    19  			{
    20  				Config: testAccAzureRMNetworkSecurityRule_basic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testCheckAzureRMNetworkSecurityRuleExists("azurerm_network_security_rule.test"),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func TestAccAzureRMNetworkSecurityRule_disappears(t *testing.T) {
    30  
    31  	resource.Test(t, resource.TestCase{
    32  		PreCheck:     func() { testAccPreCheck(t) },
    33  		Providers:    testAccProviders,
    34  		CheckDestroy: testCheckAzureRMNetworkSecurityRuleDestroy,
    35  		Steps: []resource.TestStep{
    36  			{
    37  				Config: testAccAzureRMNetworkSecurityRule_basic,
    38  				Check: resource.ComposeTestCheckFunc(
    39  					testCheckAzureRMNetworkSecurityRuleExists("azurerm_network_security_rule.test"),
    40  					testCheckAzureRMNetworkSecurityRuleDisappears("azurerm_network_security_rule.test"),
    41  				),
    42  				ExpectNonEmptyPlan: true,
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  func TestAccAzureRMNetworkSecurityRule_addingRules(t *testing.T) {
    49  
    50  	resource.Test(t, resource.TestCase{
    51  		PreCheck:     func() { testAccPreCheck(t) },
    52  		Providers:    testAccProviders,
    53  		CheckDestroy: testCheckAzureRMNetworkSecurityRuleDestroy,
    54  		Steps: []resource.TestStep{
    55  			{
    56  				Config: testAccAzureRMNetworkSecurityRule_updateBasic,
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testCheckAzureRMNetworkSecurityRuleExists("azurerm_network_security_rule.test1"),
    59  				),
    60  			},
    61  
    62  			{
    63  				Config: testAccAzureRMNetworkSecurityRule_updateExtraRule,
    64  				Check: resource.ComposeTestCheckFunc(
    65  					testCheckAzureRMNetworkSecurityRuleExists("azurerm_network_security_rule.test2"),
    66  				),
    67  			},
    68  		},
    69  	})
    70  }
    71  
    72  func testCheckAzureRMNetworkSecurityRuleExists(name string) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  
    75  		rs, ok := s.RootModule().Resources[name]
    76  		if !ok {
    77  			return fmt.Errorf("Not found: %s", name)
    78  		}
    79  
    80  		sgName := rs.Primary.Attributes["network_security_group_name"]
    81  		sgrName := rs.Primary.Attributes["name"]
    82  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    83  		if !hasResourceGroup {
    84  			return fmt.Errorf("Bad: no resource group found in state for network security rule: %s", sgName)
    85  		}
    86  
    87  		conn := testAccProvider.Meta().(*ArmClient).secRuleClient
    88  
    89  		resp, err := conn.Get(resourceGroup, sgName, sgrName)
    90  		if err != nil {
    91  			return fmt.Errorf("Bad: Get on secRuleClient: %s", err)
    92  		}
    93  
    94  		if resp.StatusCode == http.StatusNotFound {
    95  			return fmt.Errorf("Bad: Network Security Rule %q (resource group: %q) (network security group: %q) does not exist", sgrName, sgName, resourceGroup)
    96  		}
    97  
    98  		return nil
    99  	}
   100  }
   101  
   102  func testCheckAzureRMNetworkSecurityRuleDisappears(name string) resource.TestCheckFunc {
   103  	return func(s *terraform.State) error {
   104  
   105  		rs, ok := s.RootModule().Resources[name]
   106  		if !ok {
   107  			return fmt.Errorf("Not found: %s", name)
   108  		}
   109  
   110  		sgName := rs.Primary.Attributes["network_security_group_name"]
   111  		sgrName := rs.Primary.Attributes["name"]
   112  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   113  		if !hasResourceGroup {
   114  			return fmt.Errorf("Bad: no resource group found in state for network security rule: %s", sgName)
   115  		}
   116  
   117  		conn := testAccProvider.Meta().(*ArmClient).secRuleClient
   118  
   119  		_, err := conn.Delete(resourceGroup, sgName, sgrName, make(chan struct{}))
   120  		if err != nil {
   121  			return fmt.Errorf("Bad: Delete on secRuleClient: %s", err)
   122  		}
   123  
   124  		return nil
   125  	}
   126  }
   127  
   128  func testCheckAzureRMNetworkSecurityRuleDestroy(s *terraform.State) error {
   129  	conn := testAccProvider.Meta().(*ArmClient).secRuleClient
   130  
   131  	for _, rs := range s.RootModule().Resources {
   132  
   133  		if rs.Type != "azurerm_network_security_rule" {
   134  			continue
   135  		}
   136  
   137  		sgName := rs.Primary.Attributes["network_security_group_name"]
   138  		sgrName := rs.Primary.Attributes["name"]
   139  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   140  
   141  		resp, err := conn.Get(resourceGroup, sgName, sgrName)
   142  
   143  		if err != nil {
   144  			return nil
   145  		}
   146  
   147  		if resp.StatusCode != http.StatusNotFound {
   148  			return fmt.Errorf("Network Security Rule still exists:\n%#v", resp.Properties)
   149  		}
   150  	}
   151  
   152  	return nil
   153  }
   154  
   155  var testAccAzureRMNetworkSecurityRule_basic = `
   156  resource "azurerm_resource_group" "test" {
   157      name = "acceptanceTestResourceGroup1"
   158      location = "West US"
   159  }
   160  
   161  resource "azurerm_network_security_group" "test" {
   162      name = "acceptanceTestSecurityGroup1"
   163      location = "West US"
   164      resource_group_name = "${azurerm_resource_group.test.name}"
   165  }
   166  
   167  resource "azurerm_network_security_rule" "test" {
   168  	name = "test123"
   169      	priority = 100
   170      	direction = "Outbound"
   171      	access = "Allow"
   172      	protocol = "Tcp"
   173      	source_port_range = "*"
   174      	destination_port_range = "*"
   175      	source_address_prefix = "*"
   176      	destination_address_prefix = "*"
   177      	resource_group_name = "${azurerm_resource_group.test.name}"
   178      	network_security_group_name = "${azurerm_network_security_group.test.name}"
   179  }
   180  `
   181  
   182  var testAccAzureRMNetworkSecurityRule_updateBasic = `
   183  resource "azurerm_resource_group" "test1" {
   184      name = "acceptanceTestResourceGroup2"
   185      location = "West US"
   186  }
   187  
   188  resource "azurerm_network_security_group" "test1" {
   189      name = "acceptanceTestSecurityGroup2"
   190      location = "West US"
   191      resource_group_name = "${azurerm_resource_group.test1.name}"
   192  }
   193  
   194  resource "azurerm_network_security_rule" "test1" {
   195  	name = "test123"
   196      	priority = 100
   197      	direction = "Outbound"
   198      	access = "Allow"
   199      	protocol = "Tcp"
   200      	source_port_range = "*"
   201      	destination_port_range = "*"
   202      	source_address_prefix = "*"
   203      	destination_address_prefix = "*"
   204      	resource_group_name = "${azurerm_resource_group.test1.name}"
   205      	network_security_group_name = "${azurerm_network_security_group.test1.name}"
   206  }
   207  `
   208  
   209  var testAccAzureRMNetworkSecurityRule_updateExtraRule = `
   210  resource "azurerm_resource_group" "test1" {
   211      name = "acceptanceTestResourceGroup2"
   212      location = "West US"
   213  }
   214  
   215  resource "azurerm_network_security_group" "test1" {
   216      name = "acceptanceTestSecurityGroup2"
   217      location = "West US"
   218      resource_group_name = "${azurerm_resource_group.test1.name}"
   219  }
   220  
   221  resource "azurerm_network_security_rule" "test1" {
   222  	name = "test123"
   223      	priority = 100
   224      	direction = "Outbound"
   225      	access = "Allow"
   226      	protocol = "Tcp"
   227      	source_port_range = "*"
   228      	destination_port_range = "*"
   229      	source_address_prefix = "*"
   230      	destination_address_prefix = "*"
   231      	resource_group_name = "${azurerm_resource_group.test1.name}"
   232      	network_security_group_name = "${azurerm_network_security_group.test1.name}"
   233  }
   234  
   235  resource "azurerm_network_security_rule" "test2" {
   236  	name = "testing456"
   237      	priority = 101
   238      	direction = "Inbound"
   239      	access = "Deny"
   240      	protocol = "Tcp"
   241      	source_port_range = "*"
   242      	destination_port_range = "*"
   243      	source_address_prefix = "*"
   244      	destination_address_prefix = "*"
   245      	resource_group_name = "${azurerm_resource_group.test1.name}"
   246      	network_security_group_name = "${azurerm_network_security_group.test1.name}"
   247  }
   248  `