-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New data source: azurerm_lb_rule (#8365)
- Loading branch information
Showing
5 changed files
with
381 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
azurerm/internal/services/network/lb_rule_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
azurerm/internal/services/network/tests/loadbalancer_rule_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.