github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 testCheckAzureRMLoadBalancerNatRuleExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc { 71 return func(s *terraform.State) error { 72 _, _, exists := findLoadBalancerNatRuleByName(lb, natRuleName) 73 if !exists { 74 return fmt.Errorf("A NAT Rule with name %q cannot be found.", natRuleName) 75 } 76 77 return nil 78 } 79 } 80 81 func testCheckAzureRMLoadBalancerNatRuleNotExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc { 82 return func(s *terraform.State) error { 83 _, _, exists := findLoadBalancerNatRuleByName(lb, natRuleName) 84 if exists { 85 return fmt.Errorf("A NAT Rule with name %q has been found.", natRuleName) 86 } 87 88 return nil 89 } 90 } 91 92 func testAccAzureRMLoadBalancerNatRule_basic(rInt int, natRuleName string) string { 93 return fmt.Sprintf(` 94 resource "azurerm_resource_group" "test" { 95 name = "acctestrg-%d" 96 location = "West US" 97 } 98 99 resource "azurerm_public_ip" "test" { 100 name = "test-ip-%d" 101 location = "West US" 102 resource_group_name = "${azurerm_resource_group.test.name}" 103 public_ip_address_allocation = "static" 104 } 105 106 resource "azurerm_lb" "test" { 107 name = "arm-test-loadbalancer-%d" 108 location = "West US" 109 resource_group_name = "${azurerm_resource_group.test.name}" 110 111 frontend_ip_configuration { 112 name = "one-%d" 113 public_ip_address_id = "${azurerm_public_ip.test.id}" 114 } 115 } 116 117 resource "azurerm_lb_nat_rule" "test" { 118 location = "West US" 119 resource_group_name = "${azurerm_resource_group.test.name}" 120 loadbalancer_id = "${azurerm_lb.test.id}" 121 name = "%s" 122 protocol = "Tcp" 123 frontend_port = 3389 124 backend_port = 3389 125 frontend_ip_configuration_name = "one-%d" 126 } 127 128 `, rInt, rInt, rInt, rInt, natRuleName, rInt) 129 } 130 131 func testAccAzureRMLoadBalancerNatRule_removal(rInt int) string { 132 return fmt.Sprintf(` 133 resource "azurerm_resource_group" "test" { 134 name = "acctestrg-%d" 135 location = "West US" 136 } 137 138 resource "azurerm_public_ip" "test" { 139 name = "test-ip-%d" 140 location = "West US" 141 resource_group_name = "${azurerm_resource_group.test.name}" 142 public_ip_address_allocation = "static" 143 } 144 145 resource "azurerm_lb" "test" { 146 name = "arm-test-loadbalancer-%d" 147 location = "West US" 148 resource_group_name = "${azurerm_resource_group.test.name}" 149 150 frontend_ip_configuration { 151 name = "one-%d" 152 public_ip_address_id = "${azurerm_public_ip.test.id}" 153 } 154 } 155 `, rInt, rInt, rInt, rInt) 156 }