github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_network_security_group_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 TestAccAzureRMNetworkSecurityGroup_basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testCheckAzureRMNetworkSecurityGroupDestroy,
    17  		Steps: []resource.TestStep{
    18  			{
    19  				Config: testAccAzureRMNetworkSecurityGroup_basic,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testCheckAzureRMNetworkSecurityGroupExists("azurerm_network_security_group.test"),
    22  				),
    23  			},
    24  		},
    25  	})
    26  }
    27  
    28  func TestAccAzureRMNetworkSecurityGroup_disappears(t *testing.T) {
    29  	resource.Test(t, resource.TestCase{
    30  		PreCheck:     func() { testAccPreCheck(t) },
    31  		Providers:    testAccProviders,
    32  		CheckDestroy: testCheckAzureRMNetworkSecurityGroupDestroy,
    33  		Steps: []resource.TestStep{
    34  			{
    35  				Config: testAccAzureRMNetworkSecurityGroup_basic,
    36  				Check: resource.ComposeTestCheckFunc(
    37  					testCheckAzureRMNetworkSecurityGroupExists("azurerm_network_security_group.test"),
    38  					testCheckAzureRMNetworkSecurityGroupDisappears("azurerm_network_security_group.test"),
    39  				),
    40  				ExpectNonEmptyPlan: true,
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func TestAccAzureRMNetworkSecurityGroup_withTags(t *testing.T) {
    47  	resource.Test(t, resource.TestCase{
    48  		PreCheck:     func() { testAccPreCheck(t) },
    49  		Providers:    testAccProviders,
    50  		CheckDestroy: testCheckAzureRMNetworkSecurityGroupDestroy,
    51  		Steps: []resource.TestStep{
    52  			{
    53  				Config: testAccAzureRMNetworkSecurityGroup_withTags,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testCheckAzureRMNetworkSecurityGroupExists("azurerm_network_security_group.test"),
    56  					resource.TestCheckResourceAttr(
    57  						"azurerm_network_security_group.test", "tags.%", "2"),
    58  					resource.TestCheckResourceAttr(
    59  						"azurerm_network_security_group.test", "tags.environment", "Production"),
    60  					resource.TestCheckResourceAttr(
    61  						"azurerm_network_security_group.test", "tags.cost_center", "MSFT"),
    62  				),
    63  			},
    64  
    65  			{
    66  				Config: testAccAzureRMNetworkSecurityGroup_withTagsUpdate,
    67  				Check: resource.ComposeTestCheckFunc(
    68  					testCheckAzureRMNetworkSecurityGroupExists("azurerm_network_security_group.test"),
    69  					resource.TestCheckResourceAttr(
    70  						"azurerm_network_security_group.test", "tags.%", "1"),
    71  					resource.TestCheckResourceAttr(
    72  						"azurerm_network_security_group.test", "tags.environment", "staging"),
    73  				),
    74  			},
    75  		},
    76  	})
    77  }
    78  
    79  func TestAccAzureRMNetworkSecurityGroup_addingExtraRules(t *testing.T) {
    80  	resource.Test(t, resource.TestCase{
    81  		PreCheck:     func() { testAccPreCheck(t) },
    82  		Providers:    testAccProviders,
    83  		CheckDestroy: testCheckAzureRMNetworkSecurityGroupDestroy,
    84  		Steps: []resource.TestStep{
    85  			{
    86  				Config: testAccAzureRMNetworkSecurityGroup_basic,
    87  				Check: resource.ComposeTestCheckFunc(
    88  					testCheckAzureRMNetworkSecurityGroupExists("azurerm_network_security_group.test"),
    89  					resource.TestCheckResourceAttr(
    90  						"azurerm_network_security_group.test", "security_rule.#", "1"),
    91  				),
    92  			},
    93  
    94  			{
    95  				Config: testAccAzureRMNetworkSecurityGroup_anotherRule,
    96  				Check: resource.ComposeTestCheckFunc(
    97  					testCheckAzureRMNetworkSecurityGroupExists("azurerm_network_security_group.test"),
    98  					resource.TestCheckResourceAttr(
    99  						"azurerm_network_security_group.test", "security_rule.#", "2"),
   100  				),
   101  			},
   102  		},
   103  	})
   104  }
   105  
   106  func testCheckAzureRMNetworkSecurityGroupExists(name string) resource.TestCheckFunc {
   107  	return func(s *terraform.State) error {
   108  
   109  		rs, ok := s.RootModule().Resources[name]
   110  		if !ok {
   111  			return fmt.Errorf("Not found: %s", name)
   112  		}
   113  
   114  		sgName := rs.Primary.Attributes["name"]
   115  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   116  		if !hasResourceGroup {
   117  			return fmt.Errorf("Bad: no resource group found in state for network security group: %s", sgName)
   118  		}
   119  
   120  		conn := testAccProvider.Meta().(*ArmClient).secGroupClient
   121  
   122  		resp, err := conn.Get(resourceGroup, sgName, "")
   123  		if err != nil {
   124  			return fmt.Errorf("Bad: Get on secGroupClient: %s", err)
   125  		}
   126  
   127  		if resp.StatusCode == http.StatusNotFound {
   128  			return fmt.Errorf("Bad: Network Security Group %q (resource group: %q) does not exist", name, resourceGroup)
   129  		}
   130  
   131  		return nil
   132  	}
   133  }
   134  
   135  func testCheckAzureRMNetworkSecurityGroupDisappears(name string) resource.TestCheckFunc {
   136  	return func(s *terraform.State) error {
   137  
   138  		rs, ok := s.RootModule().Resources[name]
   139  		if !ok {
   140  			return fmt.Errorf("Not found: %s", name)
   141  		}
   142  
   143  		sgName := rs.Primary.Attributes["name"]
   144  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   145  		if !hasResourceGroup {
   146  			return fmt.Errorf("Bad: no resource group found in state for network security group: %s", sgName)
   147  		}
   148  
   149  		conn := testAccProvider.Meta().(*ArmClient).secGroupClient
   150  
   151  		_, err := conn.Delete(resourceGroup, sgName, make(chan struct{}))
   152  		if err != nil {
   153  			return fmt.Errorf("Bad: Delete on secGroupClient: %s", err)
   154  		}
   155  
   156  		return nil
   157  	}
   158  }
   159  
   160  func testCheckAzureRMNetworkSecurityGroupDestroy(s *terraform.State) error {
   161  	conn := testAccProvider.Meta().(*ArmClient).secGroupClient
   162  
   163  	for _, rs := range s.RootModule().Resources {
   164  		if rs.Type != "azurerm_network_security_group" {
   165  			continue
   166  		}
   167  
   168  		name := rs.Primary.Attributes["name"]
   169  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   170  
   171  		resp, err := conn.Get(resourceGroup, name, "")
   172  
   173  		if err != nil {
   174  			return nil
   175  		}
   176  
   177  		if resp.StatusCode != http.StatusNotFound {
   178  			return fmt.Errorf("Network Security Group still exists:\n%#v", resp.Properties)
   179  		}
   180  	}
   181  
   182  	return nil
   183  }
   184  
   185  var testAccAzureRMNetworkSecurityGroup_basic = `
   186  resource "azurerm_resource_group" "test" {
   187      name = "acceptanceTestResourceGroup1"
   188      location = "West US"
   189  }
   190  
   191  resource "azurerm_network_security_group" "test" {
   192      name = "acceptanceTestSecurityGroup1"
   193      location = "West US"
   194      resource_group_name = "${azurerm_resource_group.test.name}"
   195  
   196      security_rule {
   197      	name = "test123"
   198      	priority = 100
   199      	direction = "Inbound"
   200      	access = "Allow"
   201      	protocol = "Tcp"
   202      	source_port_range = "*"
   203      	destination_port_range = "*"
   204      	source_address_prefix = "*"
   205      	destination_address_prefix = "*"
   206      }
   207  }
   208  `
   209  
   210  var testAccAzureRMNetworkSecurityGroup_anotherRule = `
   211  resource "azurerm_resource_group" "test" {
   212      name = "acceptanceTestResourceGroup1"
   213      location = "West US"
   214  }
   215  
   216  resource "azurerm_network_security_group" "test" {
   217      name = "acceptanceTestSecurityGroup1"
   218      location = "West US"
   219      resource_group_name = "${azurerm_resource_group.test.name}"
   220  
   221      security_rule {
   222      	name = "test123"
   223      	priority = 100
   224      	direction = "Inbound"
   225      	access = "Allow"
   226      	protocol = "Tcp"
   227      	source_port_range = "*"
   228      	destination_port_range = "*"
   229      	source_address_prefix = "*"
   230      	destination_address_prefix = "*"
   231      }
   232  
   233      security_rule {
   234      	name = "testDeny"
   235      	priority = 101
   236      	direction = "Inbound"
   237      	access = "Deny"
   238      	protocol = "Udp"
   239      	source_port_range = "*"
   240      	destination_port_range = "*"
   241      	source_address_prefix = "*"
   242      	destination_address_prefix = "*"
   243      }
   244  }
   245  `
   246  
   247  var testAccAzureRMNetworkSecurityGroup_withTags = `
   248  resource "azurerm_resource_group" "test" {
   249      name = "acceptanceTestResourceGroup1"
   250      location = "West US"
   251  }
   252  
   253  resource "azurerm_network_security_group" "test" {
   254      name = "acceptanceTestSecurityGroup1"
   255      location = "West US"
   256      resource_group_name = "${azurerm_resource_group.test.name}"
   257  
   258      security_rule {
   259      	name = "test123"
   260      	priority = 100
   261      	direction = "Inbound"
   262      	access = "Allow"
   263      	protocol = "Tcp"
   264      	source_port_range = "*"
   265      	destination_port_range = "*"
   266      	source_address_prefix = "*"
   267      	destination_address_prefix = "*"
   268      }
   269  
   270  
   271      tags {
   272  	environment = "Production"
   273  	cost_center = "MSFT"
   274      }
   275  }
   276  `
   277  
   278  var testAccAzureRMNetworkSecurityGroup_withTagsUpdate = `
   279  resource "azurerm_resource_group" "test" {
   280      name = "acceptanceTestResourceGroup1"
   281      location = "West US"
   282  }
   283  
   284  resource "azurerm_network_security_group" "test" {
   285      name = "acceptanceTestSecurityGroup1"
   286      location = "West US"
   287      resource_group_name = "${azurerm_resource_group.test.name}"
   288  
   289      security_rule {
   290      	name = "test123"
   291      	priority = 100
   292      	direction = "Inbound"
   293      	access = "Allow"
   294      	protocol = "Tcp"
   295      	source_port_range = "*"
   296      	destination_port_range = "*"
   297      	source_address_prefix = "*"
   298      	destination_address_prefix = "*"
   299      }
   300  
   301      tags {
   302  	environment = "staging"
   303      }
   304  }
   305  `