Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions builtin/providers/azurerm/loadbalancer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package azurerm

import (
"fmt"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs goimports running on it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

"net/http"
"strings"

"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
)

func resourceGroupAndLBNameFromId(loadBalancerId string) (string, string, error) {
id, err := parseAzureResourceID(loadBalancerId)
if err != nil {
return "", "", err
}
name := id.Path["loadBalancers"]
resGroup := id.ResourceGroup

return resGroup, name, nil
}

func retrieveLoadBalancerById(loadBalancerId string, meta interface{}) (*network.LoadBalancer, bool, error) {
loadBalancerClient := meta.(*ArmClient).loadBalancerClient

resGroup, name, err := resourceGroupAndLBNameFromId(loadBalancerId)
if err != nil {
return nil, false, errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
}

resp, err := loadBalancerClient.Get(resGroup, name, "")
if err != nil {
if resp.StatusCode == http.StatusNotFound {
return nil, false, nil
}
return nil, false, fmt.Errorf("Error making Read request on Azure LoadBalancer %s: %s", name, err)
}

return &resp, true, nil
}

func findLoadBalancerBackEndAddressPoolByName(lb *network.LoadBalancer, name string) (*network.BackendAddressPool, int, bool) {
if lb == nil || lb.Properties == nil || lb.Properties.BackendAddressPools == nil {
return nil, -1, false
}

for i, apc := range *lb.Properties.BackendAddressPools {
if apc.Name != nil && *apc.Name == name {
return &apc, i, true
}
}

return nil, -1, false
}

func findLoadBalancerFrontEndIpConfigurationByName(lb *network.LoadBalancer, name string) (*network.FrontendIPConfiguration, int, bool) {
if lb == nil || lb.Properties == nil || lb.Properties.FrontendIPConfigurations == nil {
return nil, -1, false
}

for i, feip := range *lb.Properties.FrontendIPConfigurations {
if feip.Name != nil && *feip.Name == name {
return &feip, i, true
}
}

return nil, -1, false
}

func findLoadBalancerRuleByName(lb *network.LoadBalancer, name string) (*network.LoadBalancingRule, int, bool) {
if lb == nil || lb.Properties == nil || lb.Properties.LoadBalancingRules == nil {
return nil, -1, false
}

for i, lbr := range *lb.Properties.LoadBalancingRules {
if lbr.Name != nil && *lbr.Name == name {
return &lbr, i, true
}
}

return nil, -1, false
}

func findLoadBalancerNatRuleByName(lb *network.LoadBalancer, name string) (*network.InboundNatRule, int, bool) {
if lb == nil || lb.Properties == nil || lb.Properties.InboundNatRules == nil {
return nil, -1, false
}

for i, nr := range *lb.Properties.InboundNatRules {
if nr.Name != nil && *nr.Name == name {
return &nr, i, true
}
}

return nil, -1, false
}

func findLoadBalancerNatPoolByName(lb *network.LoadBalancer, name string) (*network.InboundNatPool, int, bool) {
if lb == nil || lb.Properties == nil || lb.Properties.InboundNatPools == nil {
return nil, -1, false
}

for i, np := range *lb.Properties.InboundNatPools {
if np.Name != nil && *np.Name == name {
return &np, i, true
}
}

return nil, -1, false
}

func findLoadBalancerProbeByName(lb *network.LoadBalancer, name string) (*network.Probe, int, bool) {
if lb == nil || lb.Properties == nil || lb.Properties.Probes == nil {
return nil, -1, false
}

for i, p := range *lb.Properties.Probes {
if p.Name != nil && *p.Name == name {
return &p, i, true
}
}

return nil, -1, false
}

func loadbalancerStateRefreshFunc(client *ArmClient, resourceGroupName string, loadbalancer string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.loadBalancerClient.Get(resourceGroupName, loadbalancer, "")
if err != nil {
return nil, "", fmt.Errorf("Error issuing read request in loadbalancerStateRefreshFunc to Azure ARM for LoadBalancer '%s' (RG: '%s'): %s", loadbalancer, resourceGroupName, err)
}

return res, *res.Properties.ProvisioningState, nil
}
}

func validateLoadBalancerPrivateIpAddressAllocation(v interface{}, k string) (ws []string, errors []error) {
value := strings.ToLower(v.(string))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unnecessary complex for something that could be:

value := strings.ToLower(v.(string))
if value == "static" || value == "dynamic" {
    // ok
} else {
    // not ok
}

Even if we keep the map lookup pattern, it should be map[string]struct{} rather than map[string]bool.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shameless plug #8103 :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like #8103 has been merged, we'll merge this as is (pending review) and then go back and fix it up to use the new bits.

if value != "static" && value != "dynamic" {
errors = append(errors, fmt.Errorf("LoadBalancer Allocations can only be Static or Dynamic"))
}
return
}
14 changes: 11 additions & 3 deletions builtin/providers/azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,17 @@ func Provider() terraform.ResourceProvider {

ResourcesMap: map[string]*schema.Resource{
// These resources use the Azure ARM SDK
"azurerm_availability_set": resourceArmAvailabilitySet(),
"azurerm_cdn_endpoint": resourceArmCdnEndpoint(),
"azurerm_cdn_profile": resourceArmCdnProfile(),
"azurerm_availability_set": resourceArmAvailabilitySet(),
"azurerm_cdn_endpoint": resourceArmCdnEndpoint(),
"azurerm_cdn_profile": resourceArmCdnProfile(),

"azurerm_lb": resourceArmLoadBalancer(),
"azurerm_lb_backend_address_pool": resourceArmLoadBalancerBackendAddressPool(),
"azurerm_lb_nat_rule": resourceArmLoadBalancerNatRule(),
"azurerm_lb_nat_pool": resourceArmLoadBalancerNatPool(),
"azurerm_lb_probe": resourceArmLoadBalancerProbe(),
"azurerm_lb_rule": resourceArmLoadBalancerRule(),

"azurerm_local_network_gateway": resourceArmLocalNetworkGateway(),
"azurerm_network_interface": resourceArmNetworkInterface(),
"azurerm_network_security_group": resourceArmNetworkSecurityGroup(),
Expand Down
Loading