2016-10-07 13:14:26 -05:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/network"
|
|
|
|
"github.com/hashicorp/errwrap"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/jen20/riviera/azure"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceArmLoadBalancerBackendAddressPool() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmLoadBalancerBackendAddressPoolCreate,
|
|
|
|
Read: resourceArmLoadBalancerBackendAddressPoolRead,
|
|
|
|
Delete: resourceArmLoadBalancerBackendAddressPoolDelete,
|
2017-02-01 11:17:51 -06:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: loadBalancerSubResourceStateImporter,
|
|
|
|
},
|
2016-10-07 13:14:26 -05:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-02-01 11:17:51 -06:00
|
|
|
"location": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
ForceNew: true,
|
|
|
|
Optional: true,
|
|
|
|
StateFunc: azureRMNormalizeLocation,
|
|
|
|
DiffSuppressFunc: azureRMSuppressLocationDiff,
|
|
|
|
Deprecated: "location is no longer used",
|
|
|
|
},
|
2016-10-07 13:14:26 -05:00
|
|
|
|
|
|
|
"resource_group_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"loadbalancer_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"backend_ip_configurations": {
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Computed: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
|
|
|
|
"load_balancing_rules": {
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Computed: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmLoadBalancerBackendAddressPoolCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
lbClient := client.loadBalancerClient
|
|
|
|
|
2016-10-19 07:51:47 -05:00
|
|
|
loadBalancerID := d.Get("loadbalancer_id").(string)
|
|
|
|
armMutexKV.Lock(loadBalancerID)
|
|
|
|
defer armMutexKV.Unlock(loadBalancerID)
|
|
|
|
|
|
|
|
loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta)
|
2016-10-07 13:14:26 -05:00
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
d.SetId("")
|
|
|
|
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-12 07:57:07 -06:00
|
|
|
backendAddressPools := append(*loadBalancer.LoadBalancerPropertiesFormat.BackendAddressPools, expandAzureRmLoadBalancerBackendAddressPools(d))
|
|
|
|
existingPool, existingPoolIndex, exists := findLoadBalancerBackEndAddressPoolByName(loadBalancer, d.Get("name").(string))
|
2016-10-07 13:14:26 -05:00
|
|
|
if exists {
|
2017-01-12 07:57:07 -06:00
|
|
|
if d.Get("name").(string) == *existingPool.Name {
|
|
|
|
// this pool is being updated/reapplied remove old copy from the slice
|
|
|
|
backendAddressPools = append(backendAddressPools[:existingPoolIndex], backendAddressPools[existingPoolIndex+1:]...)
|
|
|
|
}
|
2016-10-07 13:14:26 -05:00
|
|
|
}
|
|
|
|
|
2016-12-06 02:39:47 -06:00
|
|
|
loadBalancer.LoadBalancerPropertiesFormat.BackendAddressPools = &backendAddressPools
|
2016-10-07 13:14:26 -05:00
|
|
|
resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := lbClient.Get(resGroup, loadBalancerName, "")
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err)
|
|
|
|
}
|
|
|
|
if read.ID == nil {
|
|
|
|
return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
|
|
|
|
}
|
|
|
|
|
2016-10-17 12:21:30 -05:00
|
|
|
var pool_id string
|
2016-12-06 02:39:47 -06:00
|
|
|
for _, BackendAddressPool := range *(*read.LoadBalancerPropertiesFormat).BackendAddressPools {
|
2016-10-18 04:44:15 -05:00
|
|
|
if *BackendAddressPool.Name == d.Get("name").(string) {
|
|
|
|
pool_id = *BackendAddressPool.ID
|
2016-10-17 12:21:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if pool_id != "" {
|
|
|
|
d.SetId(pool_id)
|
|
|
|
} else {
|
2016-10-18 08:00:38 -05:00
|
|
|
return fmt.Errorf("Cannot find created LoadBalancer Backend Address Pool ID %q", pool_id)
|
2016-10-17 12:21:30 -05:00
|
|
|
}
|
2016-10-07 13:14:26 -05:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName)
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"Accepted", "Updating"},
|
|
|
|
Target: []string{"Succeeded"},
|
|
|
|
Refresh: loadbalancerStateRefreshFunc(client, resGroup, loadBalancerName),
|
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for LoadBalancer (%s) to become available: %s", loadBalancerName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceArmLoadBalancerBackendAddressPoolRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmLoadBalancerBackendAddressPoolRead(d *schema.ResourceData, meta interface{}) error {
|
2017-02-01 11:17:51 -06:00
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name := id.Path["backendAddressPools"]
|
|
|
|
|
2016-10-17 12:21:30 -05:00
|
|
|
loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
|
2016-10-07 13:14:26 -05:00
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
d.SetId("")
|
2017-02-01 11:17:51 -06:00
|
|
|
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", name)
|
2016-10-07 13:14:26 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-01 11:17:51 -06:00
|
|
|
config, _, exists := findLoadBalancerBackEndAddressPoolByName(loadBalancer, name)
|
2017-01-31 09:55:17 -06:00
|
|
|
if !exists {
|
|
|
|
d.SetId("")
|
2017-02-01 11:17:51 -06:00
|
|
|
log.Printf("[INFO] LoadBalancer Backend Address Pool %q not found. Removing from state", name)
|
2017-01-31 09:55:17 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", config.Name)
|
2017-02-01 11:17:51 -06:00
|
|
|
d.Set("resource_group_name", id.ResourceGroup)
|
2017-01-31 09:55:17 -06:00
|
|
|
|
2017-02-01 11:17:51 -06:00
|
|
|
var backend_ip_configurations []string
|
2017-01-31 09:55:17 -06:00
|
|
|
if config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations != nil {
|
|
|
|
for _, backendConfig := range *config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations {
|
|
|
|
backend_ip_configurations = append(backend_ip_configurations, *backendConfig.ID)
|
2016-10-07 13:14:26 -05:00
|
|
|
}
|
2017-01-31 09:55:17 -06:00
|
|
|
|
|
|
|
}
|
2017-02-01 11:17:51 -06:00
|
|
|
d.Set("backend_ip_configurations", backend_ip_configurations)
|
2017-01-31 09:55:17 -06:00
|
|
|
|
2017-02-01 11:17:51 -06:00
|
|
|
var load_balancing_rules []string
|
2017-01-31 09:55:17 -06:00
|
|
|
if config.BackendAddressPoolPropertiesFormat.LoadBalancingRules != nil {
|
|
|
|
for _, rule := range *config.BackendAddressPoolPropertiesFormat.LoadBalancingRules {
|
|
|
|
load_balancing_rules = append(load_balancing_rules, *rule.ID)
|
|
|
|
}
|
2016-10-07 13:14:26 -05:00
|
|
|
}
|
2017-02-01 11:17:51 -06:00
|
|
|
d.Set("load_balancing_rules", load_balancing_rules)
|
2016-10-07 13:14:26 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmLoadBalancerBackendAddressPoolDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
lbClient := client.loadBalancerClient
|
|
|
|
|
2016-10-19 07:51:47 -05:00
|
|
|
loadBalancerID := d.Get("loadbalancer_id").(string)
|
|
|
|
armMutexKV.Lock(loadBalancerID)
|
|
|
|
defer armMutexKV.Unlock(loadBalancerID)
|
|
|
|
|
|
|
|
loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta)
|
2016-10-07 13:14:26 -05:00
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, index, exists := findLoadBalancerBackEndAddressPoolByName(loadBalancer, d.Get("name").(string))
|
|
|
|
if !exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-06 02:39:47 -06:00
|
|
|
oldBackEndPools := *loadBalancer.LoadBalancerPropertiesFormat.BackendAddressPools
|
2016-10-07 13:14:26 -05:00
|
|
|
newBackEndPools := append(oldBackEndPools[:index], oldBackEndPools[index+1:]...)
|
2016-12-06 02:39:47 -06:00
|
|
|
loadBalancer.LoadBalancerPropertiesFormat.BackendAddressPools = &newBackEndPools
|
2016-10-07 13:14:26 -05:00
|
|
|
|
|
|
|
resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := lbClient.Get(resGroup, loadBalancerName, "")
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err)
|
|
|
|
}
|
|
|
|
if read.ID == nil {
|
|
|
|
return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandAzureRmLoadBalancerBackendAddressPools(d *schema.ResourceData) network.BackendAddressPool {
|
|
|
|
return network.BackendAddressPool{
|
|
|
|
Name: azure.String(d.Get("name").(string)),
|
|
|
|
}
|
|
|
|
}
|