github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/loadbalancer.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/arm/network"
     9  	"github.com/hashicorp/errwrap"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  )
    12  
    13  func resourceGroupAndLBNameFromId(loadBalancerId string) (string, string, error) {
    14  	id, err := parseAzureResourceID(loadBalancerId)
    15  	if err != nil {
    16  		return "", "", err
    17  	}
    18  	name := id.Path["loadBalancers"]
    19  	resGroup := id.ResourceGroup
    20  
    21  	return resGroup, name, nil
    22  }
    23  
    24  func retrieveLoadBalancerById(loadBalancerId string, meta interface{}) (*network.LoadBalancer, bool, error) {
    25  	loadBalancerClient := meta.(*ArmClient).loadBalancerClient
    26  
    27  	resGroup, name, err := resourceGroupAndLBNameFromId(loadBalancerId)
    28  	if err != nil {
    29  		return nil, false, errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
    30  	}
    31  
    32  	resp, err := loadBalancerClient.Get(resGroup, name, "")
    33  	if err != nil {
    34  		if resp.StatusCode == http.StatusNotFound {
    35  			return nil, false, nil
    36  		}
    37  		return nil, false, fmt.Errorf("Error making Read request on Azure LoadBalancer %s: %s", name, err)
    38  	}
    39  
    40  	return &resp, true, nil
    41  }
    42  
    43  func findLoadBalancerBackEndAddressPoolByName(lb *network.LoadBalancer, name string) (*network.BackendAddressPool, int, bool) {
    44  	if lb == nil || lb.Properties == nil || lb.Properties.BackendAddressPools == nil {
    45  		return nil, -1, false
    46  	}
    47  
    48  	for i, apc := range *lb.Properties.BackendAddressPools {
    49  		if apc.Name != nil && *apc.Name == name {
    50  			return &apc, i, true
    51  		}
    52  	}
    53  
    54  	return nil, -1, false
    55  }
    56  
    57  func findLoadBalancerFrontEndIpConfigurationByName(lb *network.LoadBalancer, name string) (*network.FrontendIPConfiguration, int, bool) {
    58  	if lb == nil || lb.Properties == nil || lb.Properties.FrontendIPConfigurations == nil {
    59  		return nil, -1, false
    60  	}
    61  
    62  	for i, feip := range *lb.Properties.FrontendIPConfigurations {
    63  		if feip.Name != nil && *feip.Name == name {
    64  			return &feip, i, true
    65  		}
    66  	}
    67  
    68  	return nil, -1, false
    69  }
    70  
    71  func findLoadBalancerRuleByName(lb *network.LoadBalancer, name string) (*network.LoadBalancingRule, int, bool) {
    72  	if lb == nil || lb.Properties == nil || lb.Properties.LoadBalancingRules == nil {
    73  		return nil, -1, false
    74  	}
    75  
    76  	for i, lbr := range *lb.Properties.LoadBalancingRules {
    77  		if lbr.Name != nil && *lbr.Name == name {
    78  			return &lbr, i, true
    79  		}
    80  	}
    81  
    82  	return nil, -1, false
    83  }
    84  
    85  func findLoadBalancerNatRuleByName(lb *network.LoadBalancer, name string) (*network.InboundNatRule, int, bool) {
    86  	if lb == nil || lb.Properties == nil || lb.Properties.InboundNatRules == nil {
    87  		return nil, -1, false
    88  	}
    89  
    90  	for i, nr := range *lb.Properties.InboundNatRules {
    91  		if nr.Name != nil && *nr.Name == name {
    92  			return &nr, i, true
    93  		}
    94  	}
    95  
    96  	return nil, -1, false
    97  }
    98  
    99  func findLoadBalancerNatPoolByName(lb *network.LoadBalancer, name string) (*network.InboundNatPool, int, bool) {
   100  	if lb == nil || lb.Properties == nil || lb.Properties.InboundNatPools == nil {
   101  		return nil, -1, false
   102  	}
   103  
   104  	for i, np := range *lb.Properties.InboundNatPools {
   105  		if np.Name != nil && *np.Name == name {
   106  			return &np, i, true
   107  		}
   108  	}
   109  
   110  	return nil, -1, false
   111  }
   112  
   113  func findLoadBalancerProbeByName(lb *network.LoadBalancer, name string) (*network.Probe, int, bool) {
   114  	if lb == nil || lb.Properties == nil || lb.Properties.Probes == nil {
   115  		return nil, -1, false
   116  	}
   117  
   118  	for i, p := range *lb.Properties.Probes {
   119  		if p.Name != nil && *p.Name == name {
   120  			return &p, i, true
   121  		}
   122  	}
   123  
   124  	return nil, -1, false
   125  }
   126  
   127  func loadbalancerStateRefreshFunc(client *ArmClient, resourceGroupName string, loadbalancer string) resource.StateRefreshFunc {
   128  	return func() (interface{}, string, error) {
   129  		res, err := client.loadBalancerClient.Get(resourceGroupName, loadbalancer, "")
   130  		if err != nil {
   131  			return nil, "", fmt.Errorf("Error issuing read request in loadbalancerStateRefreshFunc to Azure ARM for LoadBalancer '%s' (RG: '%s'): %s", loadbalancer, resourceGroupName, err)
   132  		}
   133  
   134  		return res, *res.Properties.ProvisioningState, nil
   135  	}
   136  }
   137  
   138  func validateLoadBalancerPrivateIpAddressAllocation(v interface{}, k string) (ws []string, errors []error) {
   139  	value := strings.ToLower(v.(string))
   140  	if value != "static" && value != "dynamic" {
   141  		errors = append(errors, fmt.Errorf("LoadBalancer Allocations can only be Static or Dynamic"))
   142  	}
   143  	return
   144  }