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