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