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