github.com/mapuri/terraform@v0.7.6-0.20161012203214-7e0408293f97/builtin/providers/azurerm/resource_arm_loadbalancer_nat_pool_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/Azure/azure-sdk-for-go/arm/network"
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAzureRMLoadBalancerNatPool_basic(t *testing.T) {
    14  	var lb network.LoadBalancer
    15  	ri := acctest.RandInt()
    16  	natPoolName := fmt.Sprintf("NatPool-%d", ri)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    27  					testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAzureRMLoadBalancerNatPool_removal(t *testing.T) {
    35  	var lb network.LoadBalancer
    36  	ri := acctest.RandInt()
    37  	natPoolName := fmt.Sprintf("NatPool-%d", ri)
    38  
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    43  		Steps: []resource.TestStep{
    44  			{
    45  				Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    48  					testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
    49  				),
    50  			},
    51  			{
    52  				Config: testAccAzureRMLoadBalancerNatPool_removal(ri),
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    55  					testCheckAzureRMLoadBalancerNatPoolNotExists(natPoolName, &lb),
    56  				),
    57  			},
    58  		},
    59  	})
    60  }
    61  
    62  func testCheckAzureRMLoadBalancerNatPoolExists(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
    63  	return func(s *terraform.State) error {
    64  		_, _, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
    65  		if !exists {
    66  			return fmt.Errorf("A NAT Pool with name %q cannot be found.", natPoolName)
    67  		}
    68  
    69  		return nil
    70  	}
    71  }
    72  
    73  func testCheckAzureRMLoadBalancerNatPoolNotExists(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		_, _, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
    76  		if exists {
    77  			return fmt.Errorf("A NAT Pool with name %q has been found.", natPoolName)
    78  		}
    79  
    80  		return nil
    81  	}
    82  }
    83  
    84  func testAccAzureRMLoadBalancerNatPool_basic(rInt int, natPoolName string) string {
    85  	return fmt.Sprintf(`
    86  resource "azurerm_resource_group" "test" {
    87      name = "acctestrg-%d"
    88      location = "West US"
    89  }
    90  
    91  resource "azurerm_public_ip" "test" {
    92      name = "test-ip-%d"
    93      location = "West US"
    94      resource_group_name = "${azurerm_resource_group.test.name}"
    95      public_ip_address_allocation = "static"
    96  }
    97  
    98  resource "azurerm_lb" "test" {
    99      name = "arm-test-loadbalancer-%d"
   100      location = "West US"
   101      resource_group_name = "${azurerm_resource_group.test.name}"
   102  
   103      frontend_ip_configuration {
   104        name = "one-%d"
   105        public_ip_address_id = "${azurerm_public_ip.test.id}"
   106      }
   107  }
   108  
   109  resource "azurerm_lb_nat_pool" "test" {
   110    location = "West US"
   111    resource_group_name = "${azurerm_resource_group.test.name}"
   112    loadbalancer_id = "${azurerm_lb.test.id}"
   113    name = "%s"
   114    protocol = "Tcp"
   115    frontend_port_start = 80
   116    frontend_port_end = 81
   117    backend_port = 3389
   118    frontend_ip_configuration_name = "one-%d"
   119  }
   120  
   121  `, rInt, rInt, rInt, rInt, natPoolName, rInt)
   122  }
   123  
   124  func testAccAzureRMLoadBalancerNatPool_removal(rInt int) string {
   125  	return fmt.Sprintf(`
   126  resource "azurerm_resource_group" "test" {
   127      name = "acctestrg-%d"
   128      location = "West US"
   129  }
   130  
   131  resource "azurerm_public_ip" "test" {
   132      name = "test-ip-%d"
   133      location = "West US"
   134      resource_group_name = "${azurerm_resource_group.test.name}"
   135      public_ip_address_allocation = "static"
   136  }
   137  
   138  resource "azurerm_lb" "test" {
   139      name = "arm-test-loadbalancer-%d"
   140      location = "West US"
   141      resource_group_name = "${azurerm_resource_group.test.name}"
   142  
   143      frontend_ip_configuration {
   144        name = "one-%d"
   145        public_ip_address_id = "${azurerm_public_ip.test.id}"
   146      }
   147  }
   148  `, rInt, rInt, rInt, rInt)
   149  }