github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_loadbalancer_nat_pool_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/arm/network"
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAzureRMLoadBalancerNatPool_basic(t *testing.T) {
    15  	var lb network.LoadBalancer
    16  	ri := acctest.RandInt()
    17  	natPoolName := fmt.Sprintf("NatPool-%d", ri)
    18  
    19  	subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
    20  	natPool_id := fmt.Sprintf(
    21  		"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/inboundNatPools/%s",
    22  		subscriptionID, ri, ri, natPoolName)
    23  
    24  	resource.Test(t, resource.TestCase{
    25  		PreCheck:     func() { testAccPreCheck(t) },
    26  		Providers:    testAccProviders,
    27  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    28  		Steps: []resource.TestStep{
    29  			{
    30  				Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    33  					testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
    34  					resource.TestCheckResourceAttr(
    35  						"azurerm_lb_nat_pool.test", "id", natPool_id),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func TestAccAzureRMLoadBalancerNatPool_removal(t *testing.T) {
    43  	var lb network.LoadBalancer
    44  	ri := acctest.RandInt()
    45  	natPoolName := fmt.Sprintf("NatPool-%d", ri)
    46  
    47  	resource.Test(t, resource.TestCase{
    48  		PreCheck:     func() { testAccPreCheck(t) },
    49  		Providers:    testAccProviders,
    50  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    51  		Steps: []resource.TestStep{
    52  			{
    53  				Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    56  					testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
    57  				),
    58  			},
    59  			{
    60  				Config: testAccAzureRMLoadBalancerNatPool_removal(ri),
    61  				Check: resource.ComposeTestCheckFunc(
    62  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    63  					testCheckAzureRMLoadBalancerNatPoolNotExists(natPoolName, &lb),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func TestAccAzureRMLoadBalancerNatPool_update(t *testing.T) {
    71  	var lb network.LoadBalancer
    72  	ri := acctest.RandInt()
    73  	natPoolName := fmt.Sprintf("NatPool-%d", ri)
    74  	natPool2Name := fmt.Sprintf("NatPool-%d", acctest.RandInt())
    75  
    76  	resource.Test(t, resource.TestCase{
    77  		PreCheck:     func() { testAccPreCheck(t) },
    78  		Providers:    testAccProviders,
    79  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    80  		Steps: []resource.TestStep{
    81  			{
    82  				Config: testAccAzureRMLoadBalancerNatPool_multiplePools(ri, natPoolName, natPool2Name),
    83  				Check: resource.ComposeTestCheckFunc(
    84  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    85  					testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
    86  					testCheckAzureRMLoadBalancerNatPoolExists(natPool2Name, &lb),
    87  					resource.TestCheckResourceAttr("azurerm_lb_nat_pool.test2", "backend_port", "3390"),
    88  				),
    89  			},
    90  			{
    91  				Config: testAccAzureRMLoadBalancerNatPool_multiplePoolsUpdate(ri, natPoolName, natPool2Name),
    92  				Check: resource.ComposeTestCheckFunc(
    93  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    94  					testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
    95  					testCheckAzureRMLoadBalancerNatPoolExists(natPool2Name, &lb),
    96  					resource.TestCheckResourceAttr("azurerm_lb_nat_pool.test2", "backend_port", "3391"),
    97  				),
    98  			},
    99  		},
   100  	})
   101  }
   102  
   103  func TestAccAzureRMLoadBalancerNatPool_reapply(t *testing.T) {
   104  	var lb network.LoadBalancer
   105  	ri := acctest.RandInt()
   106  	natPoolName := fmt.Sprintf("NatPool-%d", ri)
   107  
   108  	deleteNatPoolState := func(s *terraform.State) error {
   109  		return s.Remove("azurerm_lb_nat_pool.test")
   110  	}
   111  
   112  	resource.Test(t, resource.TestCase{
   113  		PreCheck:     func() { testAccPreCheck(t) },
   114  		Providers:    testAccProviders,
   115  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
   116  		Steps: []resource.TestStep{
   117  			{
   118  				Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
   119  				Check: resource.ComposeTestCheckFunc(
   120  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   121  					testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
   122  					deleteNatPoolState,
   123  				),
   124  				ExpectNonEmptyPlan: true,
   125  			},
   126  			{
   127  				Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
   128  				Check: resource.ComposeTestCheckFunc(
   129  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   130  					testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
   131  				),
   132  			},
   133  		},
   134  	})
   135  }
   136  
   137  func TestAccAzureRMLoadBalancerNatPool_disappears(t *testing.T) {
   138  	var lb network.LoadBalancer
   139  	ri := acctest.RandInt()
   140  	natPoolName := fmt.Sprintf("NatPool-%d", ri)
   141  
   142  	resource.Test(t, resource.TestCase{
   143  		PreCheck:     func() { testAccPreCheck(t) },
   144  		Providers:    testAccProviders,
   145  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
   146  		Steps: []resource.TestStep{
   147  			{
   148  				Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
   149  				Check: resource.ComposeTestCheckFunc(
   150  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   151  					testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
   152  					testCheckAzureRMLoadBalancerNatPoolDisappears(natPoolName, &lb),
   153  				),
   154  				ExpectNonEmptyPlan: true,
   155  			},
   156  		},
   157  	})
   158  }
   159  
   160  func testCheckAzureRMLoadBalancerNatPoolExists(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   161  	return func(s *terraform.State) error {
   162  		_, _, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
   163  		if !exists {
   164  			return fmt.Errorf("A NAT Pool with name %q cannot be found.", natPoolName)
   165  		}
   166  
   167  		return nil
   168  	}
   169  }
   170  
   171  func testCheckAzureRMLoadBalancerNatPoolNotExists(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   172  	return func(s *terraform.State) error {
   173  		_, _, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
   174  		if exists {
   175  			return fmt.Errorf("A NAT Pool with name %q has been found.", natPoolName)
   176  		}
   177  
   178  		return nil
   179  	}
   180  }
   181  
   182  func testCheckAzureRMLoadBalancerNatPoolDisappears(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   183  	return func(s *terraform.State) error {
   184  		conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
   185  
   186  		_, i, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
   187  		if !exists {
   188  			return fmt.Errorf("A Nat Pool with name %q cannot be found.", natPoolName)
   189  		}
   190  
   191  		currentPools := *lb.LoadBalancerPropertiesFormat.InboundNatPools
   192  		pools := append(currentPools[:i], currentPools[i+1:]...)
   193  		lb.LoadBalancerPropertiesFormat.InboundNatPools = &pools
   194  
   195  		id, err := parseAzureResourceID(*lb.ID)
   196  		if err != nil {
   197  			return err
   198  		}
   199  
   200  		_, err = conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{}))
   201  		if err != nil {
   202  			return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err)
   203  		}
   204  
   205  		_, err = conn.Get(id.ResourceGroup, *lb.Name, "")
   206  		return err
   207  	}
   208  }
   209  
   210  func testAccAzureRMLoadBalancerNatPool_basic(rInt int, natPoolName string) string {
   211  	return fmt.Sprintf(`
   212  resource "azurerm_resource_group" "test" {
   213      name = "acctestrg-%d"
   214      location = "West US"
   215  }
   216  
   217  resource "azurerm_public_ip" "test" {
   218      name = "test-ip-%d"
   219      location = "West US"
   220      resource_group_name = "${azurerm_resource_group.test.name}"
   221      public_ip_address_allocation = "static"
   222  }
   223  
   224  resource "azurerm_lb" "test" {
   225      name = "arm-test-loadbalancer-%d"
   226      location = "West US"
   227      resource_group_name = "${azurerm_resource_group.test.name}"
   228  
   229      frontend_ip_configuration {
   230        name = "one-%d"
   231        public_ip_address_id = "${azurerm_public_ip.test.id}"
   232      }
   233  }
   234  
   235  resource "azurerm_lb_nat_pool" "test" {
   236    location = "West US"
   237    resource_group_name = "${azurerm_resource_group.test.name}"
   238    loadbalancer_id = "${azurerm_lb.test.id}"
   239    name = "%s"
   240    protocol = "Tcp"
   241    frontend_port_start = 80
   242    frontend_port_end = 81
   243    backend_port = 3389
   244    frontend_ip_configuration_name = "one-%d"
   245  }
   246  
   247  `, rInt, rInt, rInt, rInt, natPoolName, rInt)
   248  }
   249  
   250  func testAccAzureRMLoadBalancerNatPool_removal(rInt int) string {
   251  	return fmt.Sprintf(`
   252  resource "azurerm_resource_group" "test" {
   253      name = "acctestrg-%d"
   254      location = "West US"
   255  }
   256  
   257  resource "azurerm_public_ip" "test" {
   258      name = "test-ip-%d"
   259      location = "West US"
   260      resource_group_name = "${azurerm_resource_group.test.name}"
   261      public_ip_address_allocation = "static"
   262  }
   263  
   264  resource "azurerm_lb" "test" {
   265      name = "arm-test-loadbalancer-%d"
   266      location = "West US"
   267      resource_group_name = "${azurerm_resource_group.test.name}"
   268  
   269      frontend_ip_configuration {
   270        name = "one-%d"
   271        public_ip_address_id = "${azurerm_public_ip.test.id}"
   272      }
   273  }
   274  `, rInt, rInt, rInt, rInt)
   275  }
   276  
   277  func testAccAzureRMLoadBalancerNatPool_multiplePools(rInt int, natPoolName, natPool2Name string) string {
   278  	return fmt.Sprintf(`
   279  resource "azurerm_resource_group" "test" {
   280      name = "acctestrg-%d"
   281      location = "West US"
   282  }
   283  
   284  resource "azurerm_public_ip" "test" {
   285      name = "test-ip-%d"
   286      location = "West US"
   287      resource_group_name = "${azurerm_resource_group.test.name}"
   288      public_ip_address_allocation = "static"
   289  }
   290  
   291  resource "azurerm_lb" "test" {
   292      name = "arm-test-loadbalancer-%d"
   293      location = "West US"
   294      resource_group_name = "${azurerm_resource_group.test.name}"
   295  
   296      frontend_ip_configuration {
   297        name = "one-%d"
   298        public_ip_address_id = "${azurerm_public_ip.test.id}"
   299      }
   300  }
   301  
   302  resource "azurerm_lb_nat_pool" "test" {
   303    location = "West US"
   304    resource_group_name = "${azurerm_resource_group.test.name}"
   305    loadbalancer_id = "${azurerm_lb.test.id}"
   306    name = "%s"
   307    protocol = "Tcp"
   308    frontend_port_start = 80
   309    frontend_port_end = 81
   310    backend_port = 3389
   311    frontend_ip_configuration_name = "one-%d"
   312  }
   313  
   314  resource "azurerm_lb_nat_pool" "test2" {
   315    location = "West US"
   316    resource_group_name = "${azurerm_resource_group.test.name}"
   317    loadbalancer_id = "${azurerm_lb.test.id}"
   318    name = "%s"
   319    protocol = "Tcp"
   320    frontend_port_start = 82
   321    frontend_port_end = 83
   322    backend_port = 3390
   323    frontend_ip_configuration_name = "one-%d"
   324  }
   325  
   326  `, rInt, rInt, rInt, rInt, natPoolName, rInt, natPool2Name, rInt)
   327  }
   328  
   329  func testAccAzureRMLoadBalancerNatPool_multiplePoolsUpdate(rInt int, natPoolName, natPool2Name string) string {
   330  	return fmt.Sprintf(`
   331  resource "azurerm_resource_group" "test" {
   332      name = "acctestrg-%d"
   333      location = "West US"
   334  }
   335  
   336  resource "azurerm_public_ip" "test" {
   337      name = "test-ip-%d"
   338      location = "West US"
   339      resource_group_name = "${azurerm_resource_group.test.name}"
   340      public_ip_address_allocation = "static"
   341  }
   342  
   343  resource "azurerm_lb" "test" {
   344      name = "arm-test-loadbalancer-%d"
   345      location = "West US"
   346      resource_group_name = "${azurerm_resource_group.test.name}"
   347  
   348      frontend_ip_configuration {
   349        name = "one-%d"
   350        public_ip_address_id = "${azurerm_public_ip.test.id}"
   351      }
   352  }
   353  
   354  resource "azurerm_lb_nat_pool" "test" {
   355    location = "West US"
   356    resource_group_name = "${azurerm_resource_group.test.name}"
   357    loadbalancer_id = "${azurerm_lb.test.id}"
   358    name = "%s"
   359    protocol = "Tcp"
   360    frontend_port_start = 80
   361    frontend_port_end = 81
   362    backend_port = 3389
   363    frontend_ip_configuration_name = "one-%d"
   364  }
   365  
   366  resource "azurerm_lb_nat_pool" "test2" {
   367    location = "West US"
   368    resource_group_name = "${azurerm_resource_group.test.name}"
   369    loadbalancer_id = "${azurerm_lb.test.id}"
   370    name = "%s"
   371    protocol = "Tcp"
   372    frontend_port_start = 82
   373    frontend_port_end = 83
   374    backend_port = 3391
   375    frontend_ip_configuration_name = "one-%d"
   376  }
   377  
   378  `, rInt, rInt, rInt, rInt, natPoolName, rInt, natPool2Name, rInt)
   379  }