Skip to content

Commit

Permalink
New data source: azurerm_lb_rule (#8365)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvintang authored Sep 9, 2020
1 parent b099d80 commit 4bceaa1
Show file tree
Hide file tree
Showing 5 changed files with 381 additions and 0 deletions.
5 changes: 5 additions & 0 deletions azurerm/internal/services/network/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Client struct {
ExpressRoutePeeringsClient *network.ExpressRouteCircuitPeeringsClient
InterfacesClient *network.InterfacesClient
LoadBalancersClient *networkLegacy.LoadBalancersClient
LoadBalancerLoadBalancingRulesClient *networkLegacy.LoadBalancerLoadBalancingRulesClient
LocalNetworkGatewaysClient *network.LocalNetworkGatewaysClient
PointToSiteVpnGatewaysClient *network.P2sVpnGatewaysClient
ProfileClient *network.ProfilesClient
Expand Down Expand Up @@ -87,6 +88,9 @@ func NewClient(o *common.ClientOptions) *Client {
LoadBalancersClient := networkLegacy.NewLoadBalancersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&LoadBalancersClient.Client, o.ResourceManagerAuthorizer)

LoadBalancerLoadBalancingRulesClient := networkLegacy.NewLoadBalancerLoadBalancingRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&LoadBalancerLoadBalancingRulesClient.Client, o.ResourceManagerAuthorizer)

LocalNetworkGatewaysClient := network.NewLocalNetworkGatewaysClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&LocalNetworkGatewaysClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -187,6 +191,7 @@ func NewClient(o *common.ClientOptions) *Client {
ExpressRoutePeeringsClient: &ExpressRoutePeeringsClient,
InterfacesClient: &InterfacesClient,
LoadBalancersClient: &LoadBalancersClient,
LoadBalancerLoadBalancingRulesClient: &LoadBalancerLoadBalancingRulesClient,
LocalNetworkGatewaysClient: &LocalNetworkGatewaysClient,
PointToSiteVpnGatewaysClient: &pointToSiteVpnGatewaysClient,
ProfileClient: &ProfileClient,
Expand Down
177 changes: 177 additions & 0 deletions azurerm/internal/services/network/lb_rule_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package network

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network/parse"
networkValidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmLoadBalancerRule() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmLoadBalancerRuleRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: ValidateArmLoadBalancerRuleName,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"loadbalancer_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: networkValidate.LoadBalancerID,
},

"frontend_ip_configuration_name": {
Type: schema.TypeString,
Computed: true,
},

"protocol": {
Type: schema.TypeString,
Computed: true,
},

"frontend_port": {
Type: schema.TypeInt,
Computed: true,
},

"backend_port": {
Type: schema.TypeInt,
Computed: true,
},

"backend_address_pool_id": {
Type: schema.TypeString,
Computed: true,
},

"probe_id": {
Type: schema.TypeString,
Computed: true,
},

"enable_floating_ip": {
Type: schema.TypeBool,
Computed: true,
},

"enable_tcp_reset": {
Type: schema.TypeBool,
Computed: true,
},

"disable_outbound_snat": {
Type: schema.TypeBool,
Computed: true,
},

"idle_timeout_in_minutes": {
Type: schema.TypeInt,
Computed: true,
},

"load_distribution": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceArmLoadBalancerRuleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.LoadBalancersClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
loadBalancerID, err := parse.LoadBalancerID(d.Get("loadbalancer_id").(string))
if err != nil {
return err
}

loadBalancer, exists, err := retrieveLoadBalancerById(ctx, client, *loadBalancerID)
if err != nil {
return fmt.Errorf("retrieving Load Balancer by ID: %+v", err)
}
if !exists {
return fmt.Errorf("Load Balancer %q (Resource Group %q) was not found", loadBalancerID.Name, loadBalancerID.ResourceGroup)
}

lbRuleClient := meta.(*clients.Client).Network.LoadBalancerLoadBalancingRulesClient
ctx, cancel = timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

resp, err := lbRuleClient.Get(ctx, resourceGroup, *loadBalancer.Name, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Load Balancer Rule %q was not found in Load Balancer %q (Resource Group: %q)", name, *loadBalancer.Name, resourceGroup)
}

return fmt.Errorf("retrieving Load Balancer %s: %s", name, err)
}

d.SetId(*resp.ID)

if props := resp.LoadBalancingRulePropertiesFormat; props != nil {
frontendIPConfigurationName, err := parse.LoadBalancerFrontendIPConfigurationID(*props.FrontendIPConfiguration.ID)
if err != nil {
return err
}

d.Set("frontend_ip_configuration_name", frontendIPConfigurationName.Name)
d.Set("protocol", props.Protocol)
d.Set("frontend_port", props.FrontendPort)
d.Set("backend_port", props.BackendPort)

if props.BackendAddressPool != nil {
if err := d.Set("backend_address_pool_id", props.BackendAddressPool.ID); err != nil {
return fmt.Errorf("setting `backend_address_pool_id`: %+v", err)
}
}

if props.Probe != nil {
if err := d.Set("probe_id", props.Probe.ID); err != nil {
return fmt.Errorf("setting `probe_id`: %+v", err)
}
}

if err := d.Set("enable_floating_ip", props.EnableFloatingIP); err != nil {
return fmt.Errorf("setting `enable_floating_ip`: %+v", err)
}

if err := d.Set("enable_tcp_reset", props.EnableTCPReset); err != nil {
return fmt.Errorf("setting `enable_tcp_reset`: %+v", err)
}

if err := d.Set("disable_outbound_snat", props.DisableOutboundSnat); err != nil {
return fmt.Errorf("setting `disable_outbound_snat`: %+v", err)
}

if err := d.Set("idle_timeout_in_minutes", props.IdleTimeoutInMinutes); err != nil {
return fmt.Errorf("setting `idle_timeout_in_minutes`: %+v", err)
}

if err := d.Set("load_distribution", props.LoadDistribution); err != nil {
return fmt.Errorf("setting `load_distribution`: %+v", err)
}
}

return nil
}
1 change: 1 addition & 0 deletions azurerm/internal/services/network/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
"azurerm_firewall": dataSourceArmFirewall(),
"azurerm_lb": dataSourceArmLoadBalancer(),
"azurerm_lb_backend_address_pool": dataSourceArmLoadBalancerBackendAddressPool(),
"azurerm_lb_rule": dataSourceArmLoadBalancerRule(),
"azurerm_nat_gateway": dataSourceArmNatGateway(),
"azurerm_network_ddos_protection_plan": dataSourceNetworkDDoSProtectionPlan(),
"azurerm_network_interface": dataSourceArmNetworkInterface(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package tests

import (
"fmt"
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccAzureRMDataSourceLoadBalancerRule_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_lb_rule", "test")
lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMDataSourceLoadBalancerRule_basic(data, lbRuleName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(data.ResourceName, "id"),
resource.TestCheckResourceAttrSet(data.ResourceName, "frontend_ip_configuration_name"),
resource.TestCheckResourceAttrSet(data.ResourceName, "protocol"),
resource.TestCheckResourceAttrSet(data.ResourceName, "frontend_port"),
resource.TestCheckResourceAttrSet(data.ResourceName, "backend_port"),
),
},
},
})
}

func TestAccAzureRMDataSourceLoadBalancerRule_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_lb_rule", "test")
backendPoolName := fmt.Sprintf("LbPool-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
probeName := fmt.Sprintf("LbProbe-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMDataSourceLoadBalancerRule_complete(data, lbRuleName, backendPoolName, probeName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(data.ResourceName, "id"),
resource.TestCheckResourceAttrSet(data.ResourceName, "frontend_ip_configuration_name"),
resource.TestCheckResourceAttrSet(data.ResourceName, "protocol"),
resource.TestCheckResourceAttrSet(data.ResourceName, "frontend_port"),
resource.TestCheckResourceAttrSet(data.ResourceName, "backend_port"),
resource.TestCheckResourceAttrSet(data.ResourceName, "backend_address_pool_id"),
resource.TestCheckResourceAttrSet(data.ResourceName, "probe_id"),
resource.TestCheckResourceAttrSet(data.ResourceName, "enable_floating_ip"),
resource.TestCheckResourceAttrSet(data.ResourceName, "enable_tcp_reset"),
resource.TestCheckResourceAttrSet(data.ResourceName, "disable_outbound_snat"),
resource.TestCheckResourceAttrSet(data.ResourceName, "idle_timeout_in_minutes"),
resource.TestCheckResourceAttrSet(data.ResourceName, "load_distribution"),
),
},
},
})
}

func testAccAzureRMDataSourceLoadBalancerRule_basic(data acceptance.TestData, name string) string {
resource := testAccAzureRMLoadBalancerRule_basic(data, name, "Basic")
return fmt.Sprintf(`
%s
data "azurerm_lb_rule" "test" {
name = azurerm_lb_rule.test.name
resource_group_name = azurerm_lb_rule.test.resource_group_name
loadbalancer_id = azurerm_lb_rule.test.loadbalancer_id
}
`, resource)
}

func testAccAzureRMDataSourceLoadBalancerRule_complete(data acceptance.TestData, lbRuleName string, backendPoolName string, probeName string) string {
return fmt.Sprintf(`
%s
resource "azurerm_lb_backend_address_pool" "test" {
name = "%s"
resource_group_name = azurerm_resource_group.test.name
loadbalancer_id = azurerm_lb.test.id
}
resource "azurerm_lb_probe" "test" {
name = "%s"
resource_group_name = azurerm_resource_group.test.name
loadbalancer_id = azurerm_lb.test.id
protocol = "Tcp"
port = 443
}
resource "azurerm_lb_rule" "test" {
name = "%s"
resource_group_name = azurerm_resource_group.test.name
loadbalancer_id = azurerm_lb.test.id
protocol = "Tcp"
frontend_port = 3389
backend_port = 3389
disable_outbound_snat = true
enable_floating_ip = true
enable_tcp_reset = true
idle_timeout_in_minutes = 10
backend_address_pool_id = azurerm_lb_backend_address_pool.test.id
probe_id = azurerm_lb_probe.test.id
frontend_ip_configuration_name = azurerm_lb.test.frontend_ip_configuration.0.name
}
data "azurerm_lb_rule" "test" {
name = azurerm_lb_rule.test.name
resource_group_name = azurerm_lb_rule.test.resource_group_name
loadbalancer_id = azurerm_lb_rule.test.loadbalancer_id
}
`, testAccAzureRMLoadBalancerRule_template(data, "Standard"), backendPoolName, probeName, lbRuleName)
}
Loading

0 comments on commit 4bceaa1

Please sign in to comment.