github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/azure/resource_azure_virtual_network_test.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/Azure/azure-sdk-for-go/management/virtualnetwork"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccAzureVirtualNetwork_basic(t *testing.T) {
    13  	var network virtualnetwork.VirtualNetworkSite
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAzureVirtualNetworkDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAzureVirtualNetwork_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAzureVirtualNetworkExists(
    24  						"azure_virtual_network.foo", &network),
    25  					testAccCheckAzureVirtualNetworkAttributes(&network),
    26  					resource.TestCheckResourceAttr(
    27  						"azure_virtual_network.foo", "name", "terraform-vnet"),
    28  					resource.TestCheckResourceAttr(
    29  						"azure_virtual_network.foo", "location", "West US"),
    30  					resource.TestCheckResourceAttr(
    31  						"azure_virtual_network.foo", "address_space.0", "10.1.2.0/24"),
    32  					resource.TestCheckResourceAttr(
    33  						"azure_virtual_network.foo", "subnet.1787288781.name", "subnet1"),
    34  					resource.TestCheckResourceAttr(
    35  						"azure_virtual_network.foo", "subnet.1787288781.address_prefix", "10.1.2.0/25"),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func TestAccAzureVirtualNetwork_advanced(t *testing.T) {
    43  	var network virtualnetwork.VirtualNetworkSite
    44  
    45  	resource.Test(t, resource.TestCase{
    46  		PreCheck:     func() { testAccPreCheck(t) },
    47  		Providers:    testAccProviders,
    48  		CheckDestroy: testAccCheckAzureVirtualNetworkDestroy,
    49  		Steps: []resource.TestStep{
    50  			resource.TestStep{
    51  				Config: testAccAzureVirtualNetwork_advanced,
    52  				Check: resource.ComposeTestCheckFunc(
    53  					testAccCheckAzureVirtualNetworkExists(
    54  						"azure_virtual_network.foo", &network),
    55  					testAccCheckAzureVirtualNetworkAttributes(&network),
    56  					resource.TestCheckResourceAttr(
    57  						"azure_virtual_network.foo", "name", "terraform-vnet"),
    58  					resource.TestCheckResourceAttr(
    59  						"azure_virtual_network.foo", "location", "West US"),
    60  					resource.TestCheckResourceAttr(
    61  						"azure_virtual_network.foo", "address_space.0", "10.1.2.0/24"),
    62  					resource.TestCheckResourceAttr(
    63  						"azure_virtual_network.foo", "subnet.33778499.name", "subnet1"),
    64  					resource.TestCheckResourceAttr(
    65  						"azure_virtual_network.foo", "subnet.33778499.address_prefix", "10.1.2.0/25"),
    66  					resource.TestCheckResourceAttr(
    67  						"azure_virtual_network.foo", "subnet.33778499.security_group", "terraform-security-group1"),
    68  				),
    69  			},
    70  		},
    71  	})
    72  }
    73  
    74  func TestAccAzureVirtualNetwork_update(t *testing.T) {
    75  	var network virtualnetwork.VirtualNetworkSite
    76  
    77  	resource.Test(t, resource.TestCase{
    78  		PreCheck:     func() { testAccPreCheck(t) },
    79  		Providers:    testAccProviders,
    80  		CheckDestroy: testAccCheckAzureVirtualNetworkDestroy,
    81  		Steps: []resource.TestStep{
    82  			resource.TestStep{
    83  				Config: testAccAzureVirtualNetwork_advanced,
    84  				Check: resource.ComposeTestCheckFunc(
    85  					testAccCheckAzureVirtualNetworkExists(
    86  						"azure_virtual_network.foo", &network),
    87  					testAccCheckAzureVirtualNetworkAttributes(&network),
    88  					resource.TestCheckResourceAttr(
    89  						"azure_virtual_network.foo", "name", "terraform-vnet"),
    90  					resource.TestCheckResourceAttr(
    91  						"azure_virtual_network.foo", "location", "West US"),
    92  					resource.TestCheckResourceAttr(
    93  						"azure_virtual_network.foo", "address_space.0", "10.1.2.0/24"),
    94  					resource.TestCheckResourceAttr(
    95  						"azure_virtual_network.foo", "subnet.33778499.name", "subnet1"),
    96  					resource.TestCheckResourceAttr(
    97  						"azure_virtual_network.foo", "subnet.33778499.address_prefix", "10.1.2.0/25"),
    98  					resource.TestCheckResourceAttr(
    99  						"azure_virtual_network.foo", "subnet.33778499.security_group", "terraform-security-group1"),
   100  				),
   101  			},
   102  
   103  			resource.TestStep{
   104  				Config: testAccAzureVirtualNetwork_update,
   105  				Check: resource.ComposeTestCheckFunc(
   106  					testAccCheckAzureVirtualNetworkExists(
   107  						"azure_virtual_network.foo", &network),
   108  					testAccCheckAzureVirtualNetworkAttributes(&network),
   109  					resource.TestCheckResourceAttr(
   110  						"azure_virtual_network.foo", "name", "terraform-vnet"),
   111  					resource.TestCheckResourceAttr(
   112  						"azure_virtual_network.foo", "location", "West US"),
   113  					resource.TestCheckResourceAttr(
   114  						"azure_virtual_network.foo", "address_space.0", "10.1.3.0/24"),
   115  					resource.TestCheckResourceAttr(
   116  						"azure_virtual_network.foo", "subnet.514595123.name", "subnet1"),
   117  					resource.TestCheckResourceAttr(
   118  						"azure_virtual_network.foo", "subnet.514595123.address_prefix", "10.1.3.128/25"),
   119  					resource.TestCheckResourceAttr(
   120  						"azure_virtual_network.foo", "subnet.514595123.security_group", "terraform-security-group2"),
   121  				),
   122  			},
   123  		},
   124  	})
   125  }
   126  
   127  func testAccCheckAzureVirtualNetworkExists(
   128  	n string,
   129  	network *virtualnetwork.VirtualNetworkSite) resource.TestCheckFunc {
   130  	return func(s *terraform.State) error {
   131  		rs, ok := s.RootModule().Resources[n]
   132  		if !ok {
   133  			return fmt.Errorf("Not found: %s", n)
   134  		}
   135  
   136  		if rs.Primary.ID == "" {
   137  			return fmt.Errorf("No Virtual Network ID is set")
   138  		}
   139  
   140  		vnetClient := testAccProvider.Meta().(*Client).vnetClient
   141  		nc, err := vnetClient.GetVirtualNetworkConfiguration()
   142  		if err != nil {
   143  			return err
   144  		}
   145  
   146  		for _, n := range nc.Configuration.VirtualNetworkSites {
   147  			if n.Name == rs.Primary.ID {
   148  				*network = n
   149  
   150  				return nil
   151  			}
   152  		}
   153  
   154  		return fmt.Errorf("Virtual Network not found")
   155  	}
   156  }
   157  
   158  func testAccCheckAzureVirtualNetworkAttributes(
   159  	network *virtualnetwork.VirtualNetworkSite) resource.TestCheckFunc {
   160  	return func(s *terraform.State) error {
   161  
   162  		if network.Name != "terraform-vnet" {
   163  			return fmt.Errorf("Bad name: %s", network.Name)
   164  		}
   165  
   166  		if network.Location != "West US" {
   167  			return fmt.Errorf("Bad location: %s", network.Location)
   168  		}
   169  
   170  		return nil
   171  	}
   172  }
   173  
   174  func testAccCheckAzureVirtualNetworkDestroy(s *terraform.State) error {
   175  	vnetClient := testAccProvider.Meta().(*Client).vnetClient
   176  
   177  	for _, rs := range s.RootModule().Resources {
   178  		if rs.Type != "azure_virtual_network" {
   179  			continue
   180  		}
   181  
   182  		if rs.Primary.ID == "" {
   183  			return fmt.Errorf("No Virtual Network ID is set")
   184  		}
   185  
   186  		nc, err := vnetClient.GetVirtualNetworkConfiguration()
   187  		if err != nil {
   188  			return fmt.Errorf("Error retrieving Virtual Network Configuration: %s", err)
   189  		}
   190  
   191  		for _, n := range nc.Configuration.VirtualNetworkSites {
   192  			if n.Name == rs.Primary.ID {
   193  				return fmt.Errorf("Virtual Network %s still exists", rs.Primary.ID)
   194  			}
   195  		}
   196  	}
   197  
   198  	return nil
   199  }
   200  
   201  const testAccAzureVirtualNetwork_basic = `
   202  resource "azure_virtual_network" "foo" {
   203      name = "terraform-vnet"
   204      address_space = ["10.1.2.0/24"]
   205      location = "West US"
   206  
   207      subnet {
   208          name = "subnet1"
   209          address_prefix = "10.1.2.0/25"
   210      }
   211  }`
   212  
   213  const testAccAzureVirtualNetwork_advanced = `
   214  resource "azure_security_group" "foo" {
   215      name = "terraform-security-group1"
   216      location = "West US"
   217  }
   218  
   219  resource "azure_security_group_rule" "foo" {
   220  	name = "terraform-secgroup-rule"
   221  	security_group_names = ["${azure_security_group.foo.name}"]
   222  	type = "Inbound"
   223  	action = "Deny"
   224  	priority = 200
   225  	source_address_prefix = "100.0.0.0/32"
   226  	source_port_range = "1000"
   227  	destination_address_prefix = "10.0.0.0/32"
   228  	destination_port_range = "1000"
   229  	protocol = "TCP"
   230  }
   231  
   232  resource "azure_virtual_network" "foo" {
   233      name = "terraform-vnet"
   234      address_space = ["10.1.2.0/24"]
   235      location = "West US"
   236  
   237      subnet {
   238          name = "subnet1"
   239          address_prefix = "10.1.2.0/25"
   240          security_group = "${azure_security_group.foo.name}"
   241      }
   242  }`
   243  
   244  const testAccAzureVirtualNetwork_update = `
   245  resource "azure_security_group" "foo" {
   246      name = "terraform-security-group1"
   247      location = "West US"
   248  }
   249  
   250  resource "azure_security_group_rule" "foo" {
   251  	name = "terraform-secgroup-rule"
   252  	security_group_names = ["${azure_security_group.foo.name}"]
   253  	type = "Inbound"
   254  	action = "Deny"
   255  	priority = 200
   256  	source_address_prefix = "100.0.0.0/32"
   257  	source_port_range = "1000"
   258  	destination_address_prefix = "10.0.0.0/32"
   259  	destination_port_range = "1000"
   260  	protocol = "TCP"
   261  }
   262  
   263  resource "azure_security_group" "bar" {
   264      name = "terraform-security-group2"
   265      location = "West US"
   266  }
   267  
   268  resource "azure_virtual_network" "foo" {
   269      name = "terraform-vnet"
   270      address_space = ["10.1.3.0/24"]
   271      location = "West US"
   272  
   273      subnet {
   274          name = "subnet1"
   275          address_prefix = "10.1.3.128/25"
   276          security_group = "${azure_security_group.bar.name}"
   277      }
   278  }`