mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-07 14:44:11 -06:00
a085c8d71e
* provider/azurerm: Add AzureRM Loadbalancer resource Adds support for the elusive Azure LoadBalancer * [x] `azurerm_lb` * [x] `azurerm_lb_backend_address_pool` * [x] `azurerm_lb_rule` * [x] `azurerm_lb_nat_rule` * [x] `azurerm_lb_probe` * [x] `azurerm_lb_nat_pool` Test Results: ``` make testacc TEST=./builtin/providers/azurerm TESTARGS='-run=TestAccAzureRMLoadbalancer' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) TF_ACC=1 go test ./builtin/providers/azurerm -v -run=TestAccAzureRMLoadbalancer -timeout 120m === RUN TestAccAzureRMLoadbalancerBackEndAddressPool_basic --- PASS: TestAccAzureRMLoadbalancerBackEndAddressPool_basic (207.26s) === RUN TestAccAzureRMLoadbalancerBackEndAddressPool_removal --- PASS: TestAccAzureRMLoadbalancerBackEndAddressPool_removal (165.89s) === RUN TestAccAzureRMLoadbalancerNatRule_basic --- PASS: TestAccAzureRMLoadbalancerNatRule_basic (179.30s) === RUN TestAccAzureRMLoadbalancerNatRule_removal --- PASS: TestAccAzureRMLoadbalancerNatRule_removal (180.73s) === RUN TestAccAzureRMLoadbalancerRule_basic --- PASS: TestAccAzureRMLoadbalancerRule_basic (170.40s) === RUN TestAccAzureRMLoadbalancerRule_removal --- PASS: TestAccAzureRMLoadbalancerRule_removal (204.23s) === RUN TestAccAzureRMLoadbalancer_basic --- PASS: TestAccAzureRMLoadbalancer_basic (136.03s) === RUN TestAccAzureRMLoadbalancer_frontEndConfig --- PASS: TestAccAzureRMLoadbalancer_frontEndConfig (214.47s) === RUN TestAccAzureRMLoadbalancer_tags --- PASS: TestAccAzureRMLoadbalancer_tags (215.52s) === RUN TestAccAzureRMLoadbalancerProbe_basic --- PASS: TestAccAzureRMLoadbalancerProbe_basic (183.36s) === RUN TestAccAzureRMLoadbalancerProbe_removal --- PASS: TestAccAzureRMLoadbalancerProbe_removal (185.86s) === RUN TestAccAzureRMLoadbalancerNatPool_basic --- PASS: TestAccAzureRMLoadbalancerNatPool_basic (161.47s) === RUN TestAccAzureRMLoadbalancerNatPool_removal --- PASS: TestAccAzureRMLoadbalancerNatPool_removal (167.38s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 1673.852s ``` * provider/azurerm: Documentation for the ARM LB resources
294 lines
7.3 KiB
Go
294 lines
7.3 KiB
Go
package azurerm
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/network"
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestResourceAzureRMLoadBalancerPrivateIpAddressAllocation_validation(t *testing.T) {
|
|
cases := []struct {
|
|
Value string
|
|
ErrCount int
|
|
}{
|
|
{
|
|
Value: "Random",
|
|
ErrCount: 1,
|
|
},
|
|
{
|
|
Value: "Static",
|
|
ErrCount: 0,
|
|
},
|
|
{
|
|
Value: "Dynamic",
|
|
ErrCount: 0,
|
|
},
|
|
{
|
|
Value: "STATIC",
|
|
ErrCount: 0,
|
|
},
|
|
{
|
|
Value: "static",
|
|
ErrCount: 0,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
_, errors := validateLoadBalancerPrivateIpAddressAllocation(tc.Value, "azurerm_lb")
|
|
|
|
if len(errors) != tc.ErrCount {
|
|
t.Fatalf("Expected the Azure RM LoadBalancer private_ip_address_allocation to trigger a validation error")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAccAzureRMLoadBalancer_basic(t *testing.T) {
|
|
var lb network.LoadBalancer
|
|
ri := acctest.RandInt()
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccAzureRMLoadBalancer_basic(ri),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccAzureRMLoadBalancer_frontEndConfig(t *testing.T) {
|
|
var lb network.LoadBalancer
|
|
ri := acctest.RandInt()
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccAzureRMLoadBalancer_frontEndConfig(ri),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
resource.TestCheckResourceAttr(
|
|
"azurerm_lb.test", "frontend_ip_configuration.#", "2"),
|
|
),
|
|
},
|
|
{
|
|
Config: testAccAzureRMLoadBalancer_frontEndConfigRemoval(ri),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
resource.TestCheckResourceAttr(
|
|
"azurerm_lb.test", "frontend_ip_configuration.#", "1"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccAzureRMLoadBalancer_tags(t *testing.T) {
|
|
var lb network.LoadBalancer
|
|
ri := acctest.RandInt()
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccAzureRMLoadBalancer_basic(ri),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
resource.TestCheckResourceAttr(
|
|
"azurerm_lb.test", "tags.%", "2"),
|
|
resource.TestCheckResourceAttr(
|
|
"azurerm_lb.test", "tags.Environment", "production"),
|
|
resource.TestCheckResourceAttr(
|
|
"azurerm_lb.test", "tags.Purpose", "AcceptanceTests"),
|
|
),
|
|
},
|
|
{
|
|
Config: testAccAzureRMLoadBalancer_updatedTags(ri),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
resource.TestCheckResourceAttr(
|
|
"azurerm_lb.test", "tags.%", "1"),
|
|
resource.TestCheckResourceAttr(
|
|
"azurerm_lb.test", "tags.Purpose", "AcceptanceTests"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testCheckAzureRMLoadBalancerExists(name string, lb *network.LoadBalancer) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
rs, ok := s.RootModule().Resources[name]
|
|
if !ok {
|
|
return fmt.Errorf("Not found: %s", name)
|
|
}
|
|
|
|
loadbalancerName := rs.Primary.Attributes["name"]
|
|
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
if !hasResourceGroup {
|
|
return fmt.Errorf("Bad: no resource group found in state for loadbalancer: %s", loadbalancerName)
|
|
}
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
|
|
|
|
resp, err := conn.Get(resourceGroup, loadbalancerName, "")
|
|
if err != nil {
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
return fmt.Errorf("Bad: LoadBalancer %q (resource group: %q) does not exist", loadbalancerName, resourceGroup)
|
|
}
|
|
|
|
return fmt.Errorf("Bad: Get on loadBalancerClient: %s", err)
|
|
}
|
|
|
|
*lb = resp
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func testCheckAzureRMLoadBalancerDestroy(s *terraform.State) error {
|
|
conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
if rs.Type != "azurerm_lb" {
|
|
continue
|
|
}
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
resourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
resp, err := conn.Get(resourceGroup, name, "")
|
|
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusNotFound {
|
|
return fmt.Errorf("LoadBalancer still exists:\n%#v", resp.Properties)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func testAccAzureRMLoadBalancer_basic(rInt int) string {
|
|
return fmt.Sprintf(`
|
|
|
|
resource "azurerm_resource_group" "test" {
|
|
name = "acctestrg-%d"
|
|
location = "West US"
|
|
}
|
|
|
|
resource "azurerm_lb" "test" {
|
|
name = "arm-test-loadbalancer-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
tags {
|
|
Environment = "production"
|
|
Purpose = "AcceptanceTests"
|
|
}
|
|
|
|
}`, rInt, rInt)
|
|
}
|
|
|
|
func testAccAzureRMLoadBalancer_updatedTags(rInt int) string {
|
|
return fmt.Sprintf(`
|
|
|
|
resource "azurerm_resource_group" "test" {
|
|
name = "acctestrg-%d"
|
|
location = "West US"
|
|
}
|
|
|
|
resource "azurerm_lb" "test" {
|
|
name = "arm-test-loadbalancer-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
tags {
|
|
Purpose = "AcceptanceTests"
|
|
}
|
|
|
|
}`, rInt, rInt)
|
|
}
|
|
|
|
func testAccAzureRMLoadBalancer_frontEndConfig(rInt int) string {
|
|
return fmt.Sprintf(`
|
|
|
|
resource "azurerm_resource_group" "test" {
|
|
name = "acctestrg-%d"
|
|
location = "West US"
|
|
}
|
|
|
|
resource "azurerm_public_ip" "test" {
|
|
name = "test-ip-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
public_ip_address_allocation = "static"
|
|
}
|
|
|
|
resource "azurerm_public_ip" "test1" {
|
|
name = "another-test-ip-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
public_ip_address_allocation = "static"
|
|
}
|
|
|
|
resource "azurerm_lb" "test" {
|
|
name = "arm-test-loadbalancer-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
frontend_ip_configuration {
|
|
name = "one-%d"
|
|
public_ip_address_id = "${azurerm_public_ip.test.id}"
|
|
}
|
|
|
|
frontend_ip_configuration {
|
|
name = "two-%d"
|
|
public_ip_address_id = "${azurerm_public_ip.test1.id}"
|
|
}
|
|
}`, rInt, rInt, rInt, rInt, rInt, rInt)
|
|
}
|
|
|
|
func testAccAzureRMLoadBalancer_frontEndConfigRemoval(rInt int) string {
|
|
return fmt.Sprintf(`
|
|
|
|
resource "azurerm_resource_group" "test" {
|
|
name = "acctestrg-%d"
|
|
location = "West US"
|
|
}
|
|
|
|
resource "azurerm_public_ip" "test" {
|
|
name = "test-ip-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
public_ip_address_allocation = "static"
|
|
}
|
|
|
|
resource "azurerm_lb" "test" {
|
|
name = "arm-test-loadbalancer-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
frontend_ip_configuration {
|
|
name = "one-%d"
|
|
public_ip_address_id = "${azurerm_public_ip.test.id}"
|
|
}
|
|
}`, rInt, rInt, rInt, rInt)
|
|
}
|