github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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_addingRules(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_updateBasic, 38 Check: resource.ComposeTestCheckFunc( 39 testCheckAzureRMNetworkSecurityRuleExists("azurerm_network_security_rule.test1"), 40 ), 41 }, 42 43 { 44 Config: testAccAzureRMNetworkSecurityRule_updateExtraRule, 45 Check: resource.ComposeTestCheckFunc( 46 testCheckAzureRMNetworkSecurityRuleExists("azurerm_network_security_rule.test2"), 47 ), 48 }, 49 }, 50 }) 51 } 52 53 func testCheckAzureRMNetworkSecurityRuleExists(name string) resource.TestCheckFunc { 54 return func(s *terraform.State) error { 55 56 rs, ok := s.RootModule().Resources[name] 57 if !ok { 58 return fmt.Errorf("Not found: %s", name) 59 } 60 61 sgName := rs.Primary.Attributes["network_security_group_name"] 62 sgrName := rs.Primary.Attributes["name"] 63 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 64 if !hasResourceGroup { 65 return fmt.Errorf("Bad: no resource group found in state for network security rule: %s", sgName) 66 } 67 68 conn := testAccProvider.Meta().(*ArmClient).secRuleClient 69 70 resp, err := conn.Get(resourceGroup, sgName, sgrName) 71 if err != nil { 72 return fmt.Errorf("Bad: Get on secRuleClient: %s", err) 73 } 74 75 if resp.StatusCode == http.StatusNotFound { 76 return fmt.Errorf("Bad: Network Security Rule %q (resource group: %q) (network security group: %q) does not exist", sgrName, sgName, resourceGroup) 77 } 78 79 return nil 80 } 81 } 82 83 func testCheckAzureRMNetworkSecurityRuleDestroy(s *terraform.State) error { 84 conn := testAccProvider.Meta().(*ArmClient).secRuleClient 85 86 for _, rs := range s.RootModule().Resources { 87 88 if rs.Type != "azurerm_network_security_rule" { 89 continue 90 } 91 92 sgName := rs.Primary.Attributes["network_security_group_name"] 93 sgrName := rs.Primary.Attributes["name"] 94 resourceGroup := rs.Primary.Attributes["resource_group_name"] 95 96 resp, err := conn.Get(resourceGroup, sgName, sgrName) 97 98 if err != nil { 99 return nil 100 } 101 102 if resp.StatusCode != http.StatusNotFound { 103 return fmt.Errorf("Network Security Rule still exists:\n%#v", resp.Properties) 104 } 105 } 106 107 return nil 108 } 109 110 var testAccAzureRMNetworkSecurityRule_basic = ` 111 resource "azurerm_resource_group" "test" { 112 name = "acceptanceTestResourceGroup1" 113 location = "West US" 114 } 115 116 resource "azurerm_network_security_group" "test" { 117 name = "acceptanceTestSecurityGroup1" 118 location = "West US" 119 resource_group_name = "${azurerm_resource_group.test.name}" 120 } 121 122 resource "azurerm_network_security_rule" "test" { 123 name = "test123" 124 priority = 100 125 direction = "Outbound" 126 access = "Allow" 127 protocol = "Tcp" 128 source_port_range = "*" 129 destination_port_range = "*" 130 source_address_prefix = "*" 131 destination_address_prefix = "*" 132 resource_group_name = "${azurerm_resource_group.test.name}" 133 network_security_group_name = "${azurerm_network_security_group.test.name}" 134 } 135 ` 136 137 var testAccAzureRMNetworkSecurityRule_updateBasic = ` 138 resource "azurerm_resource_group" "test1" { 139 name = "acceptanceTestResourceGroup2" 140 location = "West US" 141 } 142 143 resource "azurerm_network_security_group" "test1" { 144 name = "acceptanceTestSecurityGroup2" 145 location = "West US" 146 resource_group_name = "${azurerm_resource_group.test1.name}" 147 } 148 149 resource "azurerm_network_security_rule" "test1" { 150 name = "test123" 151 priority = 100 152 direction = "Outbound" 153 access = "Allow" 154 protocol = "Tcp" 155 source_port_range = "*" 156 destination_port_range = "*" 157 source_address_prefix = "*" 158 destination_address_prefix = "*" 159 resource_group_name = "${azurerm_resource_group.test1.name}" 160 network_security_group_name = "${azurerm_network_security_group.test1.name}" 161 } 162 ` 163 164 var testAccAzureRMNetworkSecurityRule_updateExtraRule = ` 165 resource "azurerm_resource_group" "test1" { 166 name = "acceptanceTestResourceGroup2" 167 location = "West US" 168 } 169 170 resource "azurerm_network_security_group" "test1" { 171 name = "acceptanceTestSecurityGroup2" 172 location = "West US" 173 resource_group_name = "${azurerm_resource_group.test1.name}" 174 } 175 176 resource "azurerm_network_security_rule" "test1" { 177 name = "test123" 178 priority = 100 179 direction = "Outbound" 180 access = "Allow" 181 protocol = "Tcp" 182 source_port_range = "*" 183 destination_port_range = "*" 184 source_address_prefix = "*" 185 destination_address_prefix = "*" 186 resource_group_name = "${azurerm_resource_group.test1.name}" 187 network_security_group_name = "${azurerm_network_security_group.test1.name}" 188 } 189 190 resource "azurerm_network_security_rule" "test2" { 191 name = "testing456" 192 priority = 101 193 direction = "Inbound" 194 access = "Deny" 195 protocol = "Tcp" 196 source_port_range = "*" 197 destination_port_range = "*" 198 source_address_prefix = "*" 199 destination_address_prefix = "*" 200 resource_group_name = "${azurerm_resource_group.test1.name}" 201 network_security_group_name = "${azurerm_network_security_group.test1.name}" 202 } 203 `