github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/loadbalancer.go (about)

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