github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/azurerm/resource_arm_loadbalancer_nat_rule_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 TestAccAzureRMLoadBalancerNatRule_basic(t *testing.T) {
    15  	var lb network.LoadBalancer
    16  	ri := acctest.RandInt()
    17  	natRuleName := fmt.Sprintf("NatRule-%d", ri)
    18  
    19  	subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
    20  	natRule_id := fmt.Sprintf(
    21  		"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/inboundNatRules/%s",
    22  		subscriptionID, ri, ri, natRuleName)
    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: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName),
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    33  					testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
    34  					resource.TestCheckResourceAttr(
    35  						"azurerm_lb_nat_rule.test", "id", natRule_id),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func TestAccAzureRMLoadBalancerNatRule_removal(t *testing.T) {
    43  	var lb network.LoadBalancer
    44  	ri := acctest.RandInt()
    45  	natRuleName := fmt.Sprintf("NatRule-%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: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName),
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    56  					testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
    57  				),
    58  			},
    59  			{
    60  				Config: testAccAzureRMLoadBalancerNatRule_removal(ri),
    61  				Check: resource.ComposeTestCheckFunc(
    62  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    63  					testCheckAzureRMLoadBalancerNatRuleNotExists(natRuleName, &lb),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func TestAccAzureRMLoadBalancerNatRule_update(t *testing.T) {
    71  	var lb network.LoadBalancer
    72  	ri := acctest.RandInt()
    73  	natRuleName := fmt.Sprintf("NatRule-%d", ri)
    74  	natRule2Name := fmt.Sprintf("NatRule-%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: testAccAzureRMLoadBalancerNatRule_multipleRules(ri, natRuleName, natRule2Name),
    83  				Check: resource.ComposeTestCheckFunc(
    84  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    85  					testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
    86  					testCheckAzureRMLoadBalancerNatRuleExists(natRule2Name, &lb),
    87  					resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "frontend_port", "3390"),
    88  					resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "backend_port", "3390"),
    89  				),
    90  			},
    91  			{
    92  				Config: testAccAzureRMLoadBalancerNatRule_multipleRulesUpdate(ri, natRuleName, natRule2Name),
    93  				Check: resource.ComposeTestCheckFunc(
    94  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    95  					testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
    96  					testCheckAzureRMLoadBalancerNatRuleExists(natRule2Name, &lb),
    97  					resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "frontend_port", "3391"),
    98  					resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "backend_port", "3391"),
    99  				),
   100  			},
   101  		},
   102  	})
   103  }
   104  
   105  func TestAccAzureRMLoadBalancerNatRule_reapply(t *testing.T) {
   106  	var lb network.LoadBalancer
   107  	ri := acctest.RandInt()
   108  	natRuleName := fmt.Sprintf("NatRule-%d", ri)
   109  
   110  	deleteNatRuleState := func(s *terraform.State) error {
   111  		return s.Remove("azurerm_lb_nat_rule.test")
   112  	}
   113  
   114  	resource.Test(t, resource.TestCase{
   115  		PreCheck:     func() { testAccPreCheck(t) },
   116  		Providers:    testAccProviders,
   117  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
   118  		Steps: []resource.TestStep{
   119  			{
   120  				Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName),
   121  				Check: resource.ComposeTestCheckFunc(
   122  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   123  					testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
   124  					deleteNatRuleState,
   125  				),
   126  				ExpectNonEmptyPlan: true,
   127  			},
   128  			{
   129  				Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName),
   130  				Check: resource.ComposeTestCheckFunc(
   131  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   132  					testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
   133  				),
   134  			},
   135  		},
   136  	})
   137  }
   138  
   139  func TestAccAzureRMLoadBalancerNatRule_disappears(t *testing.T) {
   140  	var lb network.LoadBalancer
   141  	ri := acctest.RandInt()
   142  	natRuleName := fmt.Sprintf("NatRule-%d", ri)
   143  
   144  	resource.Test(t, resource.TestCase{
   145  		PreCheck:     func() { testAccPreCheck(t) },
   146  		Providers:    testAccProviders,
   147  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
   148  		Steps: []resource.TestStep{
   149  			{
   150  				Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName),
   151  				Check: resource.ComposeTestCheckFunc(
   152  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   153  					testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
   154  					testCheckAzureRMLoadBalancerNatRuleDisappears(natRuleName, &lb),
   155  				),
   156  				ExpectNonEmptyPlan: true,
   157  			},
   158  		},
   159  	})
   160  }
   161  
   162  func testCheckAzureRMLoadBalancerNatRuleExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   163  	return func(s *terraform.State) error {
   164  		_, _, exists := findLoadBalancerNatRuleByName(lb, natRuleName)
   165  		if !exists {
   166  			return fmt.Errorf("A NAT Rule with name %q cannot be found.", natRuleName)
   167  		}
   168  
   169  		return nil
   170  	}
   171  }
   172  
   173  func testCheckAzureRMLoadBalancerNatRuleNotExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   174  	return func(s *terraform.State) error {
   175  		_, _, exists := findLoadBalancerNatRuleByName(lb, natRuleName)
   176  		if exists {
   177  			return fmt.Errorf("A NAT Rule with name %q has been found.", natRuleName)
   178  		}
   179  
   180  		return nil
   181  	}
   182  }
   183  
   184  func testCheckAzureRMLoadBalancerNatRuleDisappears(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   185  	return func(s *terraform.State) error {
   186  		conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
   187  
   188  		_, i, exists := findLoadBalancerNatRuleByName(lb, natRuleName)
   189  		if !exists {
   190  			return fmt.Errorf("A Nat Rule with name %q cannot be found.", natRuleName)
   191  		}
   192  
   193  		currentRules := *lb.LoadBalancerPropertiesFormat.InboundNatRules
   194  		rules := append(currentRules[:i], currentRules[i+1:]...)
   195  		lb.LoadBalancerPropertiesFormat.InboundNatRules = &rules
   196  
   197  		id, err := parseAzureResourceID(*lb.ID)
   198  		if err != nil {
   199  			return err
   200  		}
   201  
   202  		_, error := conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{}))
   203  		err = <-error
   204  		if err != nil {
   205  			return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err)
   206  		}
   207  
   208  		_, err = conn.Get(id.ResourceGroup, *lb.Name, "")
   209  		return err
   210  	}
   211  }
   212  
   213  func testAccAzureRMLoadBalancerNatRule_basic(rInt int, natRuleName string) string {
   214  	return fmt.Sprintf(`
   215  resource "azurerm_resource_group" "test" {
   216      name = "acctestrg-%d"
   217      location = "West US"
   218  }
   219  
   220  resource "azurerm_public_ip" "test" {
   221      name = "test-ip-%d"
   222      location = "West US"
   223      resource_group_name = "${azurerm_resource_group.test.name}"
   224      public_ip_address_allocation = "static"
   225  }
   226  
   227  resource "azurerm_lb" "test" {
   228      name = "arm-test-loadbalancer-%d"
   229      location = "West US"
   230      resource_group_name = "${azurerm_resource_group.test.name}"
   231  
   232      frontend_ip_configuration {
   233        name = "one-%d"
   234        public_ip_address_id = "${azurerm_public_ip.test.id}"
   235      }
   236  }
   237  
   238  resource "azurerm_lb_nat_rule" "test" {
   239    location = "West US"
   240    resource_group_name = "${azurerm_resource_group.test.name}"
   241    loadbalancer_id = "${azurerm_lb.test.id}"
   242    name = "%s"
   243    protocol = "Tcp"
   244    frontend_port = 3389
   245    backend_port = 3389
   246    frontend_ip_configuration_name = "one-%d"
   247  }
   248  
   249  `, rInt, rInt, rInt, rInt, natRuleName, rInt)
   250  }
   251  
   252  func testAccAzureRMLoadBalancerNatRule_removal(rInt int) string {
   253  	return fmt.Sprintf(`
   254  resource "azurerm_resource_group" "test" {
   255      name = "acctestrg-%d"
   256      location = "West US"
   257  }
   258  
   259  resource "azurerm_public_ip" "test" {
   260      name = "test-ip-%d"
   261      location = "West US"
   262      resource_group_name = "${azurerm_resource_group.test.name}"
   263      public_ip_address_allocation = "static"
   264  }
   265  
   266  resource "azurerm_lb" "test" {
   267      name = "arm-test-loadbalancer-%d"
   268      location = "West US"
   269      resource_group_name = "${azurerm_resource_group.test.name}"
   270  
   271      frontend_ip_configuration {
   272        name = "one-%d"
   273        public_ip_address_id = "${azurerm_public_ip.test.id}"
   274      }
   275  }
   276  `, rInt, rInt, rInt, rInt)
   277  }
   278  
   279  func testAccAzureRMLoadBalancerNatRule_multipleRules(rInt int, natRuleName, natRule2Name string) string {
   280  	return fmt.Sprintf(`
   281  resource "azurerm_resource_group" "test" {
   282      name = "acctestrg-%d"
   283      location = "West US"
   284  }
   285  
   286  resource "azurerm_public_ip" "test" {
   287      name = "test-ip-%d"
   288      location = "West US"
   289      resource_group_name = "${azurerm_resource_group.test.name}"
   290      public_ip_address_allocation = "static"
   291  }
   292  
   293  resource "azurerm_lb" "test" {
   294      name = "arm-test-loadbalancer-%d"
   295      location = "West US"
   296      resource_group_name = "${azurerm_resource_group.test.name}"
   297  
   298      frontend_ip_configuration {
   299        name = "one-%d"
   300        public_ip_address_id = "${azurerm_public_ip.test.id}"
   301      }
   302  }
   303  
   304  resource "azurerm_lb_nat_rule" "test" {
   305    location = "West US"
   306    resource_group_name = "${azurerm_resource_group.test.name}"
   307    loadbalancer_id = "${azurerm_lb.test.id}"
   308    name = "%s"
   309    protocol = "Tcp"
   310    frontend_port = 3389
   311    backend_port = 3389
   312    frontend_ip_configuration_name = "one-%d"
   313  }
   314  
   315  resource "azurerm_lb_nat_rule" "test2" {
   316    location = "West US"
   317    resource_group_name = "${azurerm_resource_group.test.name}"
   318    loadbalancer_id = "${azurerm_lb.test.id}"
   319    name = "%s"
   320    protocol = "Tcp"
   321    frontend_port = 3390
   322    backend_port = 3390
   323    frontend_ip_configuration_name = "one-%d"
   324  }
   325  
   326  `, rInt, rInt, rInt, rInt, natRuleName, rInt, natRule2Name, rInt)
   327  }
   328  
   329  func testAccAzureRMLoadBalancerNatRule_multipleRulesUpdate(rInt int, natRuleName, natRule2Name 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_rule" "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 = 3389
   361    backend_port = 3389
   362    frontend_ip_configuration_name = "one-%d"
   363  }
   364  
   365  resource "azurerm_lb_nat_rule" "test2" {
   366    location = "West US"
   367    resource_group_name = "${azurerm_resource_group.test.name}"
   368    loadbalancer_id = "${azurerm_lb.test.id}"
   369    name = "%s"
   370    protocol = "Tcp"
   371    frontend_port = 3391
   372    backend_port = 3391
   373    frontend_ip_configuration_name = "one-%d"
   374  }
   375  
   376  `, rInt, rInt, rInt, rInt, natRuleName, rInt, natRule2Name, rInt)
   377  }