github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_subnet_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAzureRMSubnet_basic(t *testing.T) {
    14  
    15  	ri := acctest.RandInt()
    16  	config := fmt.Sprintf(testAccAzureRMSubnet_basic, ri, ri, ri)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testCheckAzureRMSubnetDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: config,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testCheckAzureRMSubnetExists("azurerm_subnet.test"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func testCheckAzureRMSubnetExists(name string) resource.TestCheckFunc {
    34  	return func(s *terraform.State) error {
    35  		// Ensure we have enough information in state to look up in API
    36  		rs, ok := s.RootModule().Resources[name]
    37  		if !ok {
    38  			return fmt.Errorf("Not found: %s", name)
    39  		}
    40  
    41  		name := rs.Primary.Attributes["name"]
    42  		vnetName := rs.Primary.Attributes["virtual_network_name"]
    43  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    44  		if !hasResourceGroup {
    45  			return fmt.Errorf("Bad: no resource group found in state for subnet: %s", name)
    46  		}
    47  
    48  		conn := testAccProvider.Meta().(*ArmClient).subnetClient
    49  
    50  		resp, err := conn.Get(resourceGroup, vnetName, name, "")
    51  		if err != nil {
    52  			return fmt.Errorf("Bad: Get on subnetClient: %s", err)
    53  		}
    54  
    55  		if resp.StatusCode == http.StatusNotFound {
    56  			return fmt.Errorf("Bad: Subnet %q (resource group: %q) does not exist", name, resourceGroup)
    57  		}
    58  
    59  		return nil
    60  	}
    61  }
    62  
    63  func testCheckAzureRMSubnetDestroy(s *terraform.State) error {
    64  	conn := testAccProvider.Meta().(*ArmClient).subnetClient
    65  
    66  	for _, rs := range s.RootModule().Resources {
    67  		if rs.Type != "azurerm_subnet" {
    68  			continue
    69  		}
    70  
    71  		name := rs.Primary.Attributes["name"]
    72  		vnetName := rs.Primary.Attributes["virtual_network_name"]
    73  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
    74  
    75  		resp, err := conn.Get(resourceGroup, vnetName, name, "")
    76  
    77  		if err != nil {
    78  			return nil
    79  		}
    80  
    81  		if resp.StatusCode != http.StatusNotFound {
    82  			return fmt.Errorf("Subnet still exists:\n%#v", resp.Properties)
    83  		}
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  var testAccAzureRMSubnet_basic = `
    90  resource "azurerm_resource_group" "test" {
    91      name = "acctestrg-%d"
    92      location = "West US"
    93  }
    94  
    95  resource "azurerm_virtual_network" "test" {
    96      name = "acctestvirtnet%d"
    97      address_space = ["10.0.0.0/16"]
    98      location = "West US"
    99      resource_group_name = "${azurerm_resource_group.test.name}"
   100  }
   101  
   102  resource "azurerm_subnet" "test" {
   103      name = "acctestsubnet%d"
   104      resource_group_name = "${azurerm_resource_group.test.name}"
   105      virtual_network_name = "${azurerm_virtual_network.test.name}"
   106      address_prefix = "10.0.2.0/24"
   107  }
   108  `