github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_network_interface_card_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 TestAccAzureRMNetworkInterface_basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy,
    17  		Steps: []resource.TestStep{
    18  			{
    19  				Config: testAccAzureRMNetworkInterface_basic,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"),
    22  				),
    23  			},
    24  		},
    25  	})
    26  }
    27  
    28  func TestAccAzureRMNetworkInterface_disappears(t *testing.T) {
    29  	resource.Test(t, resource.TestCase{
    30  		PreCheck:     func() { testAccPreCheck(t) },
    31  		Providers:    testAccProviders,
    32  		CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy,
    33  		Steps: []resource.TestStep{
    34  			{
    35  				Config: testAccAzureRMNetworkInterface_basic,
    36  				Check: resource.ComposeTestCheckFunc(
    37  					testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"),
    38  					testCheckAzureRMNetworkInterfaceDisappears("azurerm_network_interface.test"),
    39  				),
    40  				ExpectNonEmptyPlan: true,
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func TestAccAzureRMNetworkInterface_enableIPForwarding(t *testing.T) {
    47  	resource.Test(t, resource.TestCase{
    48  		PreCheck:     func() { testAccPreCheck(t) },
    49  		Providers:    testAccProviders,
    50  		CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy,
    51  		Steps: []resource.TestStep{
    52  			{
    53  				Config: testAccAzureRMNetworkInterface_ipForwarding,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"),
    56  					resource.TestCheckResourceAttr(
    57  						"azurerm_network_interface.test", "enable_ip_forwarding", "true"),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func TestAccAzureRMNetworkInterface_withTags(t *testing.T) {
    65  	resource.Test(t, resource.TestCase{
    66  		PreCheck:     func() { testAccPreCheck(t) },
    67  		Providers:    testAccProviders,
    68  		CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy,
    69  		Steps: []resource.TestStep{
    70  			{
    71  				Config: testAccAzureRMNetworkInterface_withTags,
    72  				Check: resource.ComposeTestCheckFunc(
    73  					testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"),
    74  					resource.TestCheckResourceAttr(
    75  						"azurerm_network_interface.test", "tags.%", "2"),
    76  					resource.TestCheckResourceAttr(
    77  						"azurerm_network_interface.test", "tags.environment", "Production"),
    78  					resource.TestCheckResourceAttr(
    79  						"azurerm_network_interface.test", "tags.cost_center", "MSFT"),
    80  				),
    81  			},
    82  			{
    83  				Config: testAccAzureRMNetworkInterface_withTagsUpdate,
    84  				Check: resource.ComposeTestCheckFunc(
    85  					testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"),
    86  					resource.TestCheckResourceAttr(
    87  						"azurerm_network_interface.test", "tags.%", "1"),
    88  					resource.TestCheckResourceAttr(
    89  						"azurerm_network_interface.test", "tags.environment", "staging"),
    90  				),
    91  			},
    92  		},
    93  	})
    94  }
    95  
    96  ///TODO: Re-enable this test when https://github.com/Azure/azure-sdk-for-go/issues/259 is fixed
    97  //func TestAccAzureRMNetworkInterface_addingIpConfigurations(t *testing.T) {
    98  //
    99  //	resource.Test(t, resource.TestCase{
   100  //		PreCheck:     func() { testAccPreCheck(t) },
   101  //		Providers:    testAccProviders,
   102  //		CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy,
   103  //		Steps: []resource.TestStep{
   104  //			resource.TestStep{
   105  //				Config: testAccAzureRMNetworkInterface_basic,
   106  //				Check: resource.ComposeTestCheckFunc(
   107  //					testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"),
   108  //					resource.TestCheckResourceAttr(
   109  //						"azurerm_network_interface.test", "ip_configuration.#", "1"),
   110  //				),
   111  //			},
   112  //
   113  //			resource.TestStep{
   114  //				Config: testAccAzureRMNetworkInterface_extraIpConfiguration,
   115  //				Check: resource.ComposeTestCheckFunc(
   116  //					testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"),
   117  //					resource.TestCheckResourceAttr(
   118  //						"azurerm_network_interface.test", "ip_configuration.#", "2"),
   119  //				),
   120  //			},
   121  //		},
   122  //	})
   123  //}
   124  
   125  func testCheckAzureRMNetworkInterfaceExists(name string) resource.TestCheckFunc {
   126  	return func(s *terraform.State) error {
   127  		// Ensure we have enough information in state to look up in API
   128  		rs, ok := s.RootModule().Resources[name]
   129  		if !ok {
   130  			return fmt.Errorf("Not found: %s", name)
   131  		}
   132  
   133  		name := rs.Primary.Attributes["name"]
   134  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   135  		if !hasResourceGroup {
   136  			return fmt.Errorf("Bad: no resource group found in state for availability set: %s", name)
   137  		}
   138  
   139  		conn := testAccProvider.Meta().(*ArmClient).ifaceClient
   140  
   141  		resp, err := conn.Get(resourceGroup, name, "")
   142  		if err != nil {
   143  			return fmt.Errorf("Bad: Get on ifaceClient: %s", err)
   144  		}
   145  
   146  		if resp.StatusCode == http.StatusNotFound {
   147  			return fmt.Errorf("Bad: Network Interface %q (resource group: %q) does not exist", name, resourceGroup)
   148  		}
   149  
   150  		return nil
   151  	}
   152  }
   153  
   154  func testCheckAzureRMNetworkInterfaceDisappears(name string) resource.TestCheckFunc {
   155  	return func(s *terraform.State) error {
   156  		// Ensure we have enough information in state to look up in API
   157  		rs, ok := s.RootModule().Resources[name]
   158  		if !ok {
   159  			return fmt.Errorf("Not found: %s", name)
   160  		}
   161  
   162  		name := rs.Primary.Attributes["name"]
   163  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   164  		if !hasResourceGroup {
   165  			return fmt.Errorf("Bad: no resource group found in state for availability set: %s", name)
   166  		}
   167  
   168  		conn := testAccProvider.Meta().(*ArmClient).ifaceClient
   169  
   170  		_, err := conn.Delete(resourceGroup, name, make(chan struct{}))
   171  		if err != nil {
   172  			return fmt.Errorf("Bad: Delete on ifaceClient: %s", err)
   173  		}
   174  
   175  		return nil
   176  	}
   177  }
   178  
   179  func testCheckAzureRMNetworkInterfaceDestroy(s *terraform.State) error {
   180  	conn := testAccProvider.Meta().(*ArmClient).ifaceClient
   181  
   182  	for _, rs := range s.RootModule().Resources {
   183  		if rs.Type != "azurerm_network_interface" {
   184  			continue
   185  		}
   186  
   187  		name := rs.Primary.Attributes["name"]
   188  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   189  
   190  		resp, err := conn.Get(resourceGroup, name, "")
   191  
   192  		if err != nil {
   193  			return nil
   194  		}
   195  
   196  		if resp.StatusCode != http.StatusNotFound {
   197  			return fmt.Errorf("Network Interface still exists:\n%#v", resp.Properties)
   198  		}
   199  	}
   200  
   201  	return nil
   202  }
   203  
   204  var testAccAzureRMNetworkInterface_basic = `
   205  resource "azurerm_resource_group" "test" {
   206      name = "acceptanceTestResourceGroup1"
   207      location = "West US"
   208  }
   209  
   210  resource "azurerm_virtual_network" "test" {
   211      name = "acceptanceTestVirtualNetwork1"
   212      address_space = ["10.0.0.0/16"]
   213      location = "West US"
   214      resource_group_name = "${azurerm_resource_group.test.name}"
   215  }
   216  
   217  resource "azurerm_subnet" "test" {
   218      name = "testsubnet"
   219      resource_group_name = "${azurerm_resource_group.test.name}"
   220      virtual_network_name = "${azurerm_virtual_network.test.name}"
   221      address_prefix = "10.0.2.0/24"
   222  }
   223  
   224  resource "azurerm_network_interface" "test" {
   225      name = "acceptanceTestNetworkInterface1"
   226      location = "West US"
   227      resource_group_name = "${azurerm_resource_group.test.name}"
   228  
   229      ip_configuration {
   230      	name = "testconfiguration1"
   231      	subnet_id = "${azurerm_subnet.test.id}"
   232      	private_ip_address_allocation = "dynamic"
   233      }
   234  }
   235  `
   236  
   237  var testAccAzureRMNetworkInterface_ipForwarding = `
   238  resource "azurerm_resource_group" "test" {
   239      name = "acceptanceTestResourceGroup1"
   240      location = "West US"
   241  }
   242  
   243  resource "azurerm_virtual_network" "test" {
   244      name = "acceptanceTestVirtualNetwork1"
   245      address_space = ["10.0.0.0/16"]
   246      location = "West US"
   247      resource_group_name = "${azurerm_resource_group.test.name}"
   248  }
   249  
   250  resource "azurerm_subnet" "test" {
   251      name = "testsubnet"
   252      resource_group_name = "${azurerm_resource_group.test.name}"
   253      virtual_network_name = "${azurerm_virtual_network.test.name}"
   254      address_prefix = "10.0.2.0/24"
   255  }
   256  
   257  resource "azurerm_network_interface" "test" {
   258      name = "acceptanceTestNetworkInterface1"
   259      location = "West US"
   260      resource_group_name = "${azurerm_resource_group.test.name}"
   261      enable_ip_forwarding = true
   262  
   263      ip_configuration {
   264      	name = "testconfiguration1"
   265      	subnet_id = "${azurerm_subnet.test.id}"
   266      	private_ip_address_allocation = "dynamic"
   267      }
   268  }
   269  `
   270  
   271  var testAccAzureRMNetworkInterface_withTags = `
   272  resource "azurerm_resource_group" "test" {
   273      name = "acceptanceTestResourceGroup1"
   274      location = "West US"
   275  }
   276  
   277  resource "azurerm_virtual_network" "test" {
   278      name = "acceptanceTestVirtualNetwork1"
   279      address_space = ["10.0.0.0/16"]
   280      location = "West US"
   281      resource_group_name = "${azurerm_resource_group.test.name}"
   282  }
   283  
   284  resource "azurerm_subnet" "test" {
   285      name = "testsubnet"
   286      resource_group_name = "${azurerm_resource_group.test.name}"
   287      virtual_network_name = "${azurerm_virtual_network.test.name}"
   288      address_prefix = "10.0.2.0/24"
   289  }
   290  
   291  resource "azurerm_network_interface" "test" {
   292      name = "acceptanceTestNetworkInterface1"
   293      location = "West US"
   294      resource_group_name = "${azurerm_resource_group.test.name}"
   295  
   296      ip_configuration {
   297      	name = "testconfiguration1"
   298      	subnet_id = "${azurerm_subnet.test.id}"
   299      	private_ip_address_allocation = "dynamic"
   300      }
   301  
   302      tags {
   303  	environment = "Production"
   304  	cost_center = "MSFT"
   305      }
   306  }
   307  `
   308  
   309  var testAccAzureRMNetworkInterface_withTagsUpdate = `
   310  resource "azurerm_resource_group" "test" {
   311      name = "acceptanceTestResourceGroup1"
   312      location = "West US"
   313  }
   314  
   315  resource "azurerm_virtual_network" "test" {
   316      name = "acceptanceTestVirtualNetwork1"
   317      address_space = ["10.0.0.0/16"]
   318      location = "West US"
   319      resource_group_name = "${azurerm_resource_group.test.name}"
   320  }
   321  
   322  resource "azurerm_subnet" "test" {
   323      name = "testsubnet"
   324      resource_group_name = "${azurerm_resource_group.test.name}"
   325      virtual_network_name = "${azurerm_virtual_network.test.name}"
   326      address_prefix = "10.0.2.0/24"
   327  }
   328  
   329  resource "azurerm_network_interface" "test" {
   330      name = "acceptanceTestNetworkInterface1"
   331      location = "West US"
   332      resource_group_name = "${azurerm_resource_group.test.name}"
   333  
   334      ip_configuration {
   335      	name = "testconfiguration1"
   336      	subnet_id = "${azurerm_subnet.test.id}"
   337      	private_ip_address_allocation = "dynamic"
   338      }
   339  
   340      tags {
   341  	environment = "staging"
   342      }
   343  }
   344  `
   345  
   346  //TODO: Re-enable this test when https://github.com/Azure/azure-sdk-for-go/issues/259 is fixed
   347  //var testAccAzureRMNetworkInterface_extraIpConfiguration = `
   348  //resource "azurerm_resource_group" "test" {
   349  //    name = "acceptanceTestResourceGroup1"
   350  //    location = "West US"
   351  //}
   352  //
   353  //resource "azurerm_virtual_network" "test" {
   354  //    name = "acceptanceTestVirtualNetwork1"
   355  //    address_space = ["10.0.0.0/16"]
   356  //    location = "West US"
   357  //    resource_group_name = "${azurerm_resource_group.test.name}"
   358  //}
   359  //
   360  //resource "azurerm_subnet" "test" {
   361  //    name = "testsubnet"
   362  //    resource_group_name = "${azurerm_resource_group.test.name}"
   363  //    virtual_network_name = "${azurerm_virtual_network.test.name}"
   364  //    address_prefix = "10.0.2.0/24"
   365  //}
   366  //
   367  //resource "azurerm_subnet" "test1" {
   368  //    name = "testsubnet1"
   369  //    resource_group_name = "${azurerm_resource_group.test.name}"
   370  //    virtual_network_name = "${azurerm_virtual_network.test.name}"
   371  //    address_prefix = "10.0.1.0/24"
   372  //}
   373  //
   374  //resource "azurerm_network_interface" "test" {
   375  //    name = "acceptanceTestNetworkInterface1"
   376  //    location = "West US"
   377  //    resource_group_name = "${azurerm_resource_group.test.name}"
   378  //
   379  //    ip_configuration {
   380  //    	name = "testconfiguration1"
   381  //    	subnet_id = "${azurerm_subnet.test.id}"
   382  //    	private_ip_address_allocation = "dynamic"
   383  //    }
   384  //
   385  //    ip_configuration {
   386  //    	name = "testconfiguration2"
   387  //    	subnet_id = "${azurerm_subnet.test1.id}"
   388  //    	private_ip_address_allocation = "dynamic"
   389  //    	primary = true
   390  //    }
   391  //}
   392  //`