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