2016-10-07 20:14:26 +02: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,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"location": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
StateFunc: azureRMNormalizeLocation,
|
|
|
|
},
|
|
|
|
|
|
|
|
"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
|
|
|
|
|
|
|
|
loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, exists = findLoadBalancerBackEndAddressPoolByName(loadBalancer, d.Get("name").(string))
|
|
|
|
if exists {
|
|
|
|
return fmt.Errorf("A BackEnd Address Pool with name %q already exists.", d.Get("name").(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
backendAddressPools := append(*loadBalancer.Properties.BackendAddressPools, expandAzureRmLoadBalancerBackendAddressPools(d))
|
|
|
|
loadBalancer.Properties.BackendAddressPools = &backendAddressPools
|
|
|
|
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 19:21:30 +02:00
|
|
|
var pool_id string
|
|
|
|
for _, element := range *(*read.Properties).BackendAddressPools {
|
|
|
|
if *element.Name == d.Get("name").(string) {
|
|
|
|
pool_id = *element.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if pool_id != "" {
|
|
|
|
d.SetId(pool_id)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Error can not find created loadbalacner backend address pool id %s", pool_id)
|
|
|
|
}
|
2016-10-07 20:14:26 +02: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 {
|
2016-10-17 19:21:30 +02:00
|
|
|
loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
|
2016-10-07 20:14:26 +02: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
|
|
|
|
}
|
|
|
|
|
|
|
|
configs := *loadBalancer.Properties.BackendAddressPools
|
|
|
|
for _, config := range configs {
|
|
|
|
if *config.Name == d.Get("name").(string) {
|
|
|
|
d.Set("name", config.Name)
|
|
|
|
|
|
|
|
if config.Properties.BackendIPConfigurations != nil {
|
|
|
|
backend_ip_configurations := make([]string, 0, len(*config.Properties.BackendIPConfigurations))
|
|
|
|
for _, backendConfig := range *config.Properties.BackendIPConfigurations {
|
|
|
|
backend_ip_configurations = append(backend_ip_configurations, *backendConfig.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("backend_ip_configurations", backend_ip_configurations)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Properties.LoadBalancingRules != nil {
|
|
|
|
load_balancing_rules := make([]string, 0, len(*config.Properties.LoadBalancingRules))
|
|
|
|
for _, rule := range *config.Properties.LoadBalancingRules {
|
|
|
|
load_balancing_rules = append(load_balancing_rules, *rule.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("backend_ip_configurations", load_balancing_rules)
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmLoadBalancerBackendAddressPoolDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
lbClient := client.loadBalancerClient
|
|
|
|
|
|
|
|
loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
oldBackEndPools := *loadBalancer.Properties.BackendAddressPools
|
|
|
|
newBackEndPools := append(oldBackEndPools[:index], oldBackEndPools[index+1:]...)
|
|
|
|
loadBalancer.Properties.BackendAddressPools = &newBackEndPools
|
|
|
|
|
|
|
|
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)),
|
|
|
|
}
|
|
|
|
}
|