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